Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / kernel / global16.c
blob251238c431e90323b2073d4af0ed20604c587a58
1 /*
2 * Global heap functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* 0xffff sometimes seems to mean: CURRENT_DS */
22 #include "config.h"
23 #include "wine/port.h"
25 #include <sys/types.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
35 #endif
36 #ifdef HAVE_SYS_SYSCTL_H
37 #include <sys/sysctl.h>
38 #endif
40 #include "wine/winbase16.h"
41 #include "ntstatus.h"
42 #include "toolhelp.h"
43 #include "kernel_private.h"
44 #include "kernel16_private.h"
45 #include "wine/debug.h"
46 #include "winerror.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(global);
50 /* Global arena block */
51 typedef struct
53 DWORD base; /* Base address (0 if discarded) */
54 DWORD size; /* Size in bytes (0 indicates a free block) */
55 HGLOBAL16 handle; /* Handle for this block */
56 HGLOBAL16 hOwner; /* Owner of this block */
57 BYTE lockCount; /* Count of GlobalFix() calls */
58 BYTE pageLockCount; /* Count of GlobalPageLock() calls */
59 BYTE flags; /* Allocation flags */
60 BYTE selCount; /* Number of selectors allocated for this block */
61 } GLOBALARENA;
63 /* Flags definitions */
64 #define GA_MOVEABLE 0x02 /* same as GMEM_MOVEABLE */
65 #define GA_DGROUP 0x04
66 #define GA_DISCARDABLE 0x08
67 #define GA_IPCSHARE 0x10 /* same as GMEM_DDESHARE */
68 #define GA_DOSMEM 0x20
70 /* Arena array (FIXME) */
71 static GLOBALARENA *pGlobalArena;
72 static int globalArenaSize;
74 #define GLOBAL_MAX_ALLOC_SIZE 0x00ff0000 /* Largest allocation is 16M - 64K */
76 #define VALID_HANDLE(handle) (((handle)>>__AHSHIFT)<globalArenaSize)
77 #define GET_ARENA_PTR(handle) (pGlobalArena + ((handle) >> __AHSHIFT))
79 static inline void* DOSMEM_AllocBlock(UINT size, UINT16* pseg)
81 if (!winedos.AllocDosBlock) load_winedos();
82 return winedos.AllocDosBlock ? winedos.AllocDosBlock(size, pseg) : NULL;
85 static inline BOOL DOSMEM_FreeBlock(void* ptr)
87 if (!winedos.FreeDosBlock) load_winedos();
88 return winedos.FreeDosBlock ? winedos.FreeDosBlock( ptr ) : FALSE;
91 static inline UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
93 if (!winedos.ResizeDosBlock) load_winedos();
94 return winedos.ResizeDosBlock ? winedos.ResizeDosBlock(ptr, size, TRUE) : 0;
97 /***********************************************************************
98 * GLOBAL_GetArena
100 * Return the arena for a given selector, growing the arena array if needed.
102 static GLOBALARENA *GLOBAL_GetArena( WORD sel, WORD selcount )
104 if (((sel >> __AHSHIFT) + selcount) > globalArenaSize)
106 int newsize = ((sel >> __AHSHIFT) + selcount + 0xff) & ~0xff;
107 GLOBALARENA *pNewArena = realloc( pGlobalArena,
108 newsize * sizeof(GLOBALARENA) );
109 if (!pNewArena) return 0;
110 pGlobalArena = pNewArena;
111 memset( pGlobalArena + globalArenaSize, 0,
112 (newsize - globalArenaSize) * sizeof(GLOBALARENA) );
113 globalArenaSize = newsize;
115 return pGlobalArena + (sel >> __AHSHIFT);
118 void debug_handles(void)
120 int printed=0;
121 int i;
122 for (i = globalArenaSize-1 ; i>=0 ; i--) {
123 if (pGlobalArena[i].size!=0 && (pGlobalArena[i].handle & 0x8000)){
124 printed=1;
125 DPRINTF("0x%08x, ",pGlobalArena[i].handle);
128 if (printed)
129 DPRINTF("\n");
133 /***********************************************************************
134 * GLOBAL_CreateBlock
136 * Create a global heap block for a fixed range of linear memory.
138 HGLOBAL16 GLOBAL_CreateBlock( WORD flags, const void *ptr, DWORD size,
139 HGLOBAL16 hOwner, unsigned char selflags )
141 WORD sel, selcount;
142 GLOBALARENA *pArena;
144 /* Allocate the selector(s) */
146 sel = SELECTOR_AllocBlock( ptr, size, selflags );
147 if (!sel) return 0;
148 selcount = (size + 0xffff) / 0x10000;
150 if (!(pArena = GLOBAL_GetArena( sel, selcount )))
152 SELECTOR_FreeBlock( sel );
153 return 0;
156 /* Fill the arena block */
158 pArena->base = (DWORD)ptr;
159 pArena->size = GetSelectorLimit16(sel) + 1;
160 pArena->handle = (flags & GMEM_MOVEABLE) ? sel - 1 : sel;
161 pArena->hOwner = hOwner;
162 pArena->lockCount = 0;
163 pArena->pageLockCount = 0;
164 pArena->flags = flags & GA_MOVEABLE;
165 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
166 if (flags & GMEM_DDESHARE) pArena->flags |= GA_IPCSHARE;
167 if (!(selflags & (WINE_LDT_FLAGS_CODE^WINE_LDT_FLAGS_DATA))) pArena->flags |= GA_DGROUP;
168 pArena->selCount = selcount;
169 if (selcount > 1) /* clear the next arena blocks */
170 memset( pArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
172 return pArena->handle;
176 /***********************************************************************
177 * GLOBAL_FreeBlock
179 * Free a block allocated by GLOBAL_CreateBlock, without touching
180 * the associated linear memory range.
182 BOOL16 GLOBAL_FreeBlock( HGLOBAL16 handle )
184 WORD sel;
185 GLOBALARENA *pArena;
187 if (!handle) return TRUE;
188 sel = GlobalHandleToSel16( handle );
189 if (!VALID_HANDLE(sel)) return FALSE;
190 pArena = GET_ARENA_PTR(sel);
191 SELECTOR_FreeBlock( sel );
192 memset( pArena, 0, sizeof(GLOBALARENA) );
193 return TRUE;
196 /***********************************************************************
197 * GLOBAL_MoveBlock
199 BOOL16 GLOBAL_MoveBlock( HGLOBAL16 handle, const void *ptr, DWORD size )
201 WORD sel;
202 GLOBALARENA *pArena;
204 if (!handle) return TRUE;
205 sel = GlobalHandleToSel16( handle );
206 if (!VALID_HANDLE(sel)) return FALSE;
207 pArena = GET_ARENA_PTR(sel);
208 if (pArena->selCount != 1)
209 return FALSE;
211 pArena->base = (DWORD)ptr;
212 pArena->size = size;
213 SELECTOR_ReallocBlock( sel, ptr, size );
214 return TRUE;
217 /***********************************************************************
218 * GLOBAL_Alloc
220 * Implementation of GlobalAlloc16()
222 HGLOBAL16 GLOBAL_Alloc( UINT16 flags, DWORD size, HGLOBAL16 hOwner, unsigned char selflags )
224 void *ptr;
225 HGLOBAL16 handle;
227 TRACE("%ld flags=%04x\n", size, flags );
229 /* If size is 0, create a discarded block */
231 if (size == 0) return GLOBAL_CreateBlock( flags, NULL, 1, hOwner, selflags );
233 /* Fixup the size */
235 if (size >= GLOBAL_MAX_ALLOC_SIZE - 0x1f) return 0;
236 size = (size + 0x1f) & ~0x1f;
238 /* Allocate the linear memory */
239 ptr = HeapAlloc( GetProcessHeap(), 0, size );
240 /* FIXME: free discardable blocks and try again? */
241 if (!ptr) return 0;
243 /* Allocate the selector(s) */
245 handle = GLOBAL_CreateBlock( flags, ptr, size, hOwner, selflags );
246 if (!handle)
248 HeapFree( GetProcessHeap(), 0, ptr );
249 return 0;
252 if (flags & GMEM_ZEROINIT) memset( ptr, 0, size );
253 return handle;
256 /***********************************************************************
257 * GlobalAlloc (KERNEL.15)
258 * GlobalAlloc16 (KERNEL32.24)
259 * RETURNS
260 * Handle: Success
261 * NULL: Failure
263 HGLOBAL16 WINAPI GlobalAlloc16(
264 UINT16 flags, /* [in] Object allocation attributes */
265 DWORD size /* [in] Number of bytes to allocate */
267 HANDLE16 owner = GetCurrentPDB16();
269 if (flags & GMEM_DDESHARE)
270 owner = GetExePtr(owner); /* Make it a module handle */
271 return GLOBAL_Alloc( flags, size, owner, WINE_LDT_FLAGS_DATA );
275 /***********************************************************************
276 * GlobalReAlloc (KERNEL.16)
277 * RETURNS
278 * Handle: Success
279 * NULL: Failure
281 HGLOBAL16 WINAPI GlobalReAlloc16(
282 HGLOBAL16 handle, /* [in] Handle of global memory object */
283 DWORD size, /* [in] New size of block */
284 UINT16 flags /* [in] How to reallocate object */
286 WORD selcount;
287 DWORD oldsize;
288 void *ptr, *newptr;
289 GLOBALARENA *pArena, *pNewArena;
290 WORD sel = GlobalHandleToSel16( handle );
292 TRACE("%04x %ld flags=%04x\n",
293 handle, size, flags );
294 if (!handle) return 0;
296 if (!VALID_HANDLE(handle))
298 WARN("Invalid handle 0x%04x!\n", handle);
299 return 0;
301 pArena = GET_ARENA_PTR( handle );
303 /* Discard the block if requested */
305 if ((size == 0) && (flags & GMEM_MOVEABLE) && !(flags & GMEM_MODIFY))
307 if (!(pArena->flags & GA_MOVEABLE) ||
308 !(pArena->flags & GA_DISCARDABLE) ||
309 (pArena->lockCount > 0) || (pArena->pageLockCount > 0)) return 0;
310 if (pArena->flags & GA_DOSMEM)
311 DOSMEM_FreeBlock( (void *)pArena->base );
312 else
313 HeapFree( GetProcessHeap(), 0, (void *)pArena->base );
314 pArena->base = 0;
316 /* Note: we rely on the fact that SELECTOR_ReallocBlock won't
317 * change the selector if we are shrinking the block.
318 * FIXME: shouldn't we keep selectors until the block is deleted?
320 SELECTOR_ReallocBlock( sel, 0, 1 );
321 return handle;
324 /* Fixup the size */
326 if (size > GLOBAL_MAX_ALLOC_SIZE - 0x20) return 0;
327 if (size == 0) size = 0x20;
328 else size = (size + 0x1f) & ~0x1f;
330 /* Change the flags */
332 if (flags & GMEM_MODIFY)
334 /* Change the flags, leaving GA_DGROUP alone */
335 pArena->flags = (pArena->flags & GA_DGROUP) | (flags & GA_MOVEABLE);
336 if (flags & GMEM_DISCARDABLE) pArena->flags |= GA_DISCARDABLE;
337 return handle;
340 /* Reallocate the linear memory */
342 ptr = (void *)pArena->base;
343 oldsize = pArena->size;
344 TRACE("oldbase %p oldsize %08lx newsize %08lx\n", ptr,oldsize,size);
345 if (ptr && (size == oldsize)) return handle; /* Nothing to do */
347 if (pArena->flags & GA_DOSMEM)
349 if (DOSMEM_ResizeBlock(ptr, size, TRUE) == size)
350 newptr = ptr;
351 else if(pArena->pageLockCount > 0)
352 newptr = 0;
353 else
355 newptr = DOSMEM_AllocBlock( size, 0 );
356 if (newptr)
358 memcpy( newptr, ptr, oldsize );
359 DOSMEM_FreeBlock( ptr );
363 else
366 * if more than one reader (e.g. some pointer has been
367 * given out by GetVDMPointer32W16),
368 * only try to realloc in place
371 if (ptr)
372 newptr = HeapReAlloc( GetProcessHeap(),
373 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
374 ptr, size );
375 else
376 newptr = HeapAlloc( GetProcessHeap(),
377 (pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0,
378 size );
382 if (!newptr)
384 FIXME("Realloc failed lock %d\n",pArena->pageLockCount);
385 if (pArena->pageLockCount <1)
387 if (pArena->flags & GA_DOSMEM)
388 DOSMEM_FreeBlock( (void *)pArena->base );
389 else
390 HeapFree( GetProcessHeap(), 0, ptr );
391 SELECTOR_FreeBlock( sel );
392 memset( pArena, 0, sizeof(GLOBALARENA) );
394 return 0;
396 ptr = newptr;
398 /* Reallocate the selector(s) */
400 sel = SELECTOR_ReallocBlock( sel, ptr, size );
401 if (!sel)
403 if (pArena->flags & GA_DOSMEM)
404 DOSMEM_FreeBlock( (void *)pArena->base );
405 else
406 HeapFree( GetProcessHeap(), 0, ptr );
407 memset( pArena, 0, sizeof(GLOBALARENA) );
408 return 0;
410 selcount = (size + 0xffff) / 0x10000;
412 if (!(pNewArena = GLOBAL_GetArena( sel, selcount )))
414 if (pArena->flags & GA_DOSMEM)
415 DOSMEM_FreeBlock( (void *)pArena->base );
416 else
417 HeapFree( GetProcessHeap(), 0, ptr );
418 SELECTOR_FreeBlock( sel );
419 return 0;
422 /* Fill the new arena block
423 As we may have used HEAP_REALLOC_IN_PLACE_ONLY, areas may overlap*/
425 if (pNewArena != pArena) memmove( pNewArena, pArena, sizeof(GLOBALARENA) );
426 pNewArena->base = (DWORD)ptr;
427 pNewArena->size = GetSelectorLimit16(sel) + 1;
428 pNewArena->selCount = selcount;
429 pNewArena->handle = (pNewArena->flags & GA_MOVEABLE) ? sel - 1 : sel;
431 if (selcount > 1) /* clear the next arena blocks */
432 memset( pNewArena + 1, 0, (selcount - 1) * sizeof(GLOBALARENA) );
434 if ((oldsize < size) && (flags & GMEM_ZEROINIT))
435 memset( (char *)ptr + oldsize, 0, size - oldsize );
436 return pNewArena->handle;
440 /***********************************************************************
441 * GlobalFree (KERNEL.17)
442 * GlobalFree16 (KERNEL32.31)
443 * RETURNS
444 * NULL: Success
445 * Handle: Failure
447 HGLOBAL16 WINAPI GlobalFree16(
448 HGLOBAL16 handle /* [in] Handle of global memory object */
450 void *ptr;
452 if (!VALID_HANDLE(handle))
454 WARN("Invalid handle 0x%04x passed to GlobalFree16!\n",handle);
455 return 0;
457 ptr = (void *)GET_ARENA_PTR(handle)->base;
459 TRACE("%04x\n", handle );
460 if (!GLOBAL_FreeBlock( handle )) return handle; /* failed */
461 HeapFree( GetProcessHeap(), 0, ptr );
462 return 0;
466 /**********************************************************************
467 * K32WOWGlobalLock16 (KERNEL32.60)
469 SEGPTR WINAPI K32WOWGlobalLock16( HGLOBAL16 handle )
471 WORD sel = GlobalHandleToSel16( handle );
472 TRACE("(%04x) -> %08lx\n", handle, MAKELONG( 0, sel ) );
474 if (handle)
476 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
478 if (!VALID_HANDLE(handle)) {
479 WARN("Invalid handle 0x%04x passed to WIN16_GlobalLock16!\n",handle);
480 sel = 0;
482 else if (!GET_ARENA_PTR(handle)->base)
483 sel = 0;
484 else
485 GET_ARENA_PTR(handle)->lockCount++;
488 return MAKESEGPTR( sel, 0 );
493 /***********************************************************************
494 * GlobalLock (KERNEL.18)
496 * This is the GlobalLock16() function used by 16-bit code.
498 SEGPTR WINAPI WIN16_GlobalLock16( HGLOBAL16 handle )
500 SEGPTR ret = K32WOWGlobalLock16( handle );
501 CURRENT_STACK16->ecx = SELECTOROF(ret); /* selector must be returned in CX as well */
502 return ret;
506 /***********************************************************************
507 * GlobalLock16 (KERNEL32.25)
509 * This is the GlobalLock16() function used by 32-bit code.
511 * RETURNS
512 * Pointer to first byte of memory block
513 * NULL: Failure
515 LPVOID WINAPI GlobalLock16(
516 HGLOBAL16 handle /* [in] Handle of global memory object */
518 if (!handle) return 0;
519 if (!VALID_HANDLE(handle))
520 return 0;
521 GET_ARENA_PTR(handle)->lockCount++;
522 return (LPVOID)GET_ARENA_PTR(handle)->base;
526 /***********************************************************************
527 * GlobalUnlock (KERNEL.19)
528 * GlobalUnlock16 (KERNEL32.26)
529 * NOTES
530 * Should the return values be cast to booleans?
532 * RETURNS
533 * TRUE: Object is still locked
534 * FALSE: Object is unlocked
536 BOOL16 WINAPI GlobalUnlock16(
537 HGLOBAL16 handle /* [in] Handle of global memory object */
539 GLOBALARENA *pArena = GET_ARENA_PTR(handle);
540 if (!VALID_HANDLE(handle)) {
541 WARN("Invalid handle 0x%04x passed to GlobalUnlock16!\n",handle);
542 return 0;
544 TRACE("%04x\n", handle );
545 if (pArena->lockCount) pArena->lockCount--;
546 return pArena->lockCount;
549 /***********************************************************************
550 * GlobalChangeLockCount (KERNEL.365)
552 * This is declared as a register function as it has to preserve
553 * *all* registers, even AX/DX !
556 void WINAPI GlobalChangeLockCount16( HGLOBAL16 handle, INT16 delta,
557 CONTEXT86 *context )
559 if ( delta == 1 )
560 GlobalLock16( handle );
561 else if ( delta == -1 )
562 GlobalUnlock16( handle );
563 else
564 ERR("(%04X, %d): strange delta value\n", handle, delta );
567 /***********************************************************************
568 * GlobalSize (KERNEL.20)
569 * GlobalSize16 (KERNEL32.32)
570 * RETURNS
571 * Size in bytes of object
572 * 0: Failure
574 DWORD WINAPI GlobalSize16(
575 HGLOBAL16 handle /* [in] Handle of global memory object */
577 TRACE("%04x\n", handle );
578 if (!handle) return 0;
579 if (!VALID_HANDLE(handle))
580 return 0;
581 return GET_ARENA_PTR(handle)->size;
585 /***********************************************************************
586 * GlobalHandle (KERNEL.21)
587 * NOTES
588 * Why is GlobalHandleToSel used here with the sel as input?
590 * RETURNS
591 * Handle: Success
592 * NULL: Failure
594 DWORD WINAPI GlobalHandle16(
595 WORD sel /* [in] Address of global memory block */
597 TRACE("%04x\n", sel );
598 if (!VALID_HANDLE(sel)) {
599 WARN("Invalid handle 0x%04x passed to GlobalHandle16!\n",sel);
600 return 0;
602 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
605 /***********************************************************************
606 * GlobalHandleNoRIP (KERNEL.159)
608 DWORD WINAPI GlobalHandleNoRIP16( WORD sel )
610 int i;
611 for (i = globalArenaSize-1 ; i>=0 ; i--) {
612 if (pGlobalArena[i].size!=0 && pGlobalArena[i].handle == sel)
613 return MAKELONG( GET_ARENA_PTR(sel)->handle, GlobalHandleToSel16(sel) );
615 return 0;
619 /***********************************************************************
620 * GlobalFlags (KERNEL.22)
622 * NOTES
623 * Should this return GMEM_INVALID_HANDLE instead of 0 on invalid
624 * handle?
626 * RETURNS
627 * Value specifying flags and lock count
628 * GMEM_INVALID_HANDLE: Invalid handle
630 UINT16 WINAPI GlobalFlags16(
631 HGLOBAL16 handle /* [in] Handle of global memory object */
633 GLOBALARENA *pArena;
635 TRACE("%04x\n", handle );
636 if (!VALID_HANDLE(handle)) {
637 WARN("Invalid handle 0x%04x passed to GlobalFlags16!\n",handle);
638 return 0;
640 pArena = GET_ARENA_PTR(handle);
641 return pArena->lockCount |
642 ((pArena->flags & GA_DISCARDABLE) ? GMEM_DISCARDABLE : 0) |
643 ((pArena->base == 0) ? GMEM_DISCARDED : 0);
647 /***********************************************************************
648 * LockSegment (KERNEL.23)
650 HGLOBAL16 WINAPI LockSegment16( HGLOBAL16 handle )
652 TRACE("%04x\n", handle );
653 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
654 if (!VALID_HANDLE(handle)) {
655 WARN("Invalid handle 0x%04x passed to LockSegment16!\n",handle);
656 return 0;
658 GET_ARENA_PTR(handle)->lockCount++;
659 return handle;
663 /***********************************************************************
664 * UnlockSegment (KERNEL.24)
666 void WINAPI UnlockSegment16( HGLOBAL16 handle )
668 TRACE("%04x\n", handle );
669 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
670 if (!VALID_HANDLE(handle)) {
671 WARN("Invalid handle 0x%04x passed to UnlockSegment16!\n",handle);
672 return;
674 GET_ARENA_PTR(handle)->lockCount--;
675 /* FIXME: this ought to return the lock count in CX (go figure...) */
679 /***********************************************************************
680 * GlobalCompact (KERNEL.25)
682 DWORD WINAPI GlobalCompact16( DWORD desired )
684 return GLOBAL_MAX_ALLOC_SIZE;
688 /***********************************************************************
689 * GlobalFreeAll (KERNEL.26)
691 void WINAPI GlobalFreeAll16( HGLOBAL16 owner )
693 int i;
694 GLOBALARENA *pArena;
696 pArena = pGlobalArena;
697 for (i = 0; i < globalArenaSize; i++, pArena++)
699 if ((pArena->size != 0) && (pArena->hOwner == owner))
700 GlobalFree16( pArena->handle );
705 /***********************************************************************
706 * GlobalWire (KERNEL.111)
707 * GlobalWire16 (KERNEL32.29)
709 SEGPTR WINAPI GlobalWire16( HGLOBAL16 handle )
711 return WIN16_GlobalLock16( handle );
715 /***********************************************************************
716 * GlobalUnWire (KERNEL.112)
717 * GlobalUnWire16 (KERNEL32.30)
719 BOOL16 WINAPI GlobalUnWire16( HGLOBAL16 handle )
721 return !GlobalUnlock16( handle );
725 /***********************************************************************
726 * SetSwapAreaSize (KERNEL.106)
728 LONG WINAPI SetSwapAreaSize16( WORD size )
730 FIXME("(%d) - stub!\n", size );
731 return MAKELONG( size, 0xffff );
735 /***********************************************************************
736 * GlobalLRUOldest (KERNEL.163)
738 HGLOBAL16 WINAPI GlobalLRUOldest16( HGLOBAL16 handle )
740 TRACE("%04x\n", handle );
741 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
742 return handle;
746 /***********************************************************************
747 * GlobalLRUNewest (KERNEL.164)
749 HGLOBAL16 WINAPI GlobalLRUNewest16( HGLOBAL16 handle )
751 TRACE("%04x\n", handle );
752 if (handle == (HGLOBAL16)-1) handle = CURRENT_DS;
753 return handle;
757 /***********************************************************************
758 * GetFreeSpace (KERNEL.169)
760 DWORD WINAPI GetFreeSpace16( UINT16 wFlags )
762 MEMORYSTATUS ms;
763 GlobalMemoryStatus( &ms );
764 return ms.dwAvailVirtual;
767 /***********************************************************************
768 * GlobalDOSAlloc (KERNEL.184)
769 * RETURNS
770 * Address (HW=Paragraph segment; LW=Selector)
772 DWORD WINAPI GlobalDOSAlloc16(
773 DWORD size /* [in] Number of bytes to be allocated */
775 UINT16 uParagraph;
776 LPVOID lpBlock = DOSMEM_AllocBlock( size, &uParagraph );
778 if( lpBlock )
780 HMODULE16 hModule = GetModuleHandle16("KERNEL");
781 WORD wSelector;
782 GLOBALARENA *pArena;
784 wSelector = GLOBAL_CreateBlock(GMEM_FIXED, lpBlock, size, hModule, WINE_LDT_FLAGS_DATA );
785 pArena = GET_ARENA_PTR(wSelector);
786 pArena->flags |= GA_DOSMEM;
787 return MAKELONG(wSelector,uParagraph);
789 return 0;
793 /***********************************************************************
794 * GlobalDOSFree (KERNEL.185)
795 * RETURNS
796 * NULL: Success
797 * sel: Failure
799 WORD WINAPI GlobalDOSFree16(
800 WORD sel /* [in] Selector */
802 DWORD block = GetSelectorBase(sel);
804 if( block && block < 0x100000 )
806 LPVOID lpBlock = DOSMEM_MapDosToLinear( block );
807 if( DOSMEM_FreeBlock( lpBlock ) )
808 GLOBAL_FreeBlock( sel );
809 sel = 0;
811 return sel;
815 /***********************************************************************
816 * GlobalPageLock (KERNEL.191)
817 * GlobalSmartPageLock(KERNEL.230)
819 WORD WINAPI GlobalPageLock16( HGLOBAL16 handle )
821 TRACE("%04x\n", handle );
822 if (!VALID_HANDLE(handle)) {
823 WARN("Invalid handle 0x%04x passed to GlobalPageLock!\n",handle);
824 return 0;
826 return ++(GET_ARENA_PTR(handle)->pageLockCount);
830 /***********************************************************************
831 * GlobalPageUnlock (KERNEL.192)
832 * GlobalSmartPageUnlock(KERNEL.231)
834 WORD WINAPI GlobalPageUnlock16( HGLOBAL16 handle )
836 TRACE("%04x\n", handle );
837 if (!VALID_HANDLE(handle)) {
838 WARN("Invalid handle 0x%04x passed to GlobalPageUnlock!\n",handle);
839 return 0;
841 return --(GET_ARENA_PTR(handle)->pageLockCount);
845 /***********************************************************************
846 * GlobalFix (KERNEL.197)
847 * GlobalFix16 (KERNEL32.27)
849 WORD WINAPI GlobalFix16( HGLOBAL16 handle )
851 TRACE("%04x\n", handle );
852 if (!VALID_HANDLE(handle)) {
853 WARN("Invalid handle 0x%04x passed to GlobalFix16!\n",handle);
854 return 0;
856 GET_ARENA_PTR(handle)->lockCount++;
858 return GlobalHandleToSel16(handle);
862 /***********************************************************************
863 * GlobalUnfix (KERNEL.198)
864 * GlobalUnfix16 (KERNEL32.28)
866 void WINAPI GlobalUnfix16( HGLOBAL16 handle )
868 TRACE("%04x\n", handle );
869 if (!VALID_HANDLE(handle)) {
870 WARN("Invalid handle 0x%04x passed to GlobalUnfix16!\n",handle);
871 return;
873 GET_ARENA_PTR(handle)->lockCount--;
877 /***********************************************************************
878 * FarSetOwner (KERNEL.403)
880 void WINAPI FarSetOwner16( HGLOBAL16 handle, HANDLE16 hOwner )
882 if (!VALID_HANDLE(handle)) {
883 WARN("Invalid handle 0x%04x passed to FarSetOwner!\n",handle);
884 return;
886 GET_ARENA_PTR(handle)->hOwner = hOwner;
890 /***********************************************************************
891 * FarGetOwner (KERNEL.404)
893 HANDLE16 WINAPI FarGetOwner16( HGLOBAL16 handle )
895 if (!VALID_HANDLE(handle)) {
896 WARN("Invalid handle 0x%04x passed to FarGetOwner!\n",handle);
897 return 0;
899 return GET_ARENA_PTR(handle)->hOwner;
903 /***********************************************************************
904 * GlobalHandleToSel (TOOLHELP.50)
906 WORD WINAPI GlobalHandleToSel16( HGLOBAL16 handle )
908 if (!handle) return 0;
909 if (!VALID_HANDLE(handle)) {
910 WARN("Invalid handle 0x%04x passed to GlobalHandleToSel!\n",handle);
911 return 0;
913 if (!(handle & 7))
915 WARN("Program attempted invalid selector conversion\n" );
916 return handle - 1;
918 return handle | 7;
922 /***********************************************************************
923 * GlobalFirst (TOOLHELP.51)
925 BOOL16 WINAPI GlobalFirst16( GLOBALENTRY *pGlobal, WORD wFlags )
927 if (wFlags == GLOBAL_LRU) return FALSE;
928 pGlobal->dwNext = 0;
929 return GlobalNext16( pGlobal, wFlags );
933 /***********************************************************************
934 * GlobalNext (TOOLHELP.52)
936 BOOL16 WINAPI GlobalNext16( GLOBALENTRY *pGlobal, WORD wFlags)
938 GLOBALARENA *pArena;
940 if (pGlobal->dwNext >= globalArenaSize) return FALSE;
941 pArena = pGlobalArena + pGlobal->dwNext;
942 if (wFlags == GLOBAL_FREE) /* only free blocks */
944 int i;
945 for (i = pGlobal->dwNext; i < globalArenaSize; i++, pArena++)
946 if (pArena->size == 0) break; /* block is free */
947 if (i >= globalArenaSize) return FALSE;
948 pGlobal->dwNext = i;
951 pGlobal->dwAddress = pArena->base;
952 pGlobal->dwBlockSize = pArena->size;
953 pGlobal->hBlock = pArena->handle;
954 pGlobal->wcLock = pArena->lockCount;
955 pGlobal->wcPageLock = pArena->pageLockCount;
956 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
957 pGlobal->wHeapPresent = FALSE;
958 pGlobal->hOwner = pArena->hOwner;
959 pGlobal->wType = GT_UNKNOWN;
960 pGlobal->wData = 0;
961 pGlobal->dwNext++;
962 return TRUE;
966 /***********************************************************************
967 * GlobalInfo (TOOLHELP.53)
969 BOOL16 WINAPI GlobalInfo16( GLOBALINFO *pInfo )
971 int i;
972 GLOBALARENA *pArena;
974 pInfo->wcItems = globalArenaSize;
975 pInfo->wcItemsFree = 0;
976 pInfo->wcItemsLRU = 0;
977 for (i = 0, pArena = pGlobalArena; i < globalArenaSize; i++, pArena++)
978 if (pArena->size == 0) pInfo->wcItemsFree++;
979 return TRUE;
983 /***********************************************************************
984 * GlobalEntryHandle (TOOLHELP.54)
986 BOOL16 WINAPI GlobalEntryHandle16( GLOBALENTRY *pGlobal, HGLOBAL16 hItem )
988 GLOBALARENA *pArena = GET_ARENA_PTR(hItem);
990 pGlobal->dwAddress = pArena->base;
991 pGlobal->dwBlockSize = pArena->size;
992 pGlobal->hBlock = pArena->handle;
993 pGlobal->wcLock = pArena->lockCount;
994 pGlobal->wcPageLock = pArena->pageLockCount;
995 pGlobal->wFlags = (GetCurrentPDB16() == pArena->hOwner);
996 pGlobal->wHeapPresent = FALSE;
997 pGlobal->hOwner = pArena->hOwner;
998 pGlobal->wType = GT_UNKNOWN;
999 pGlobal->wData = 0;
1000 pGlobal->dwNext++;
1001 return TRUE;
1005 /***********************************************************************
1006 * GlobalEntryModule (TOOLHELP.55)
1008 BOOL16 WINAPI GlobalEntryModule16( GLOBALENTRY *pGlobal, HMODULE16 hModule,
1009 WORD wSeg )
1011 FIXME("(%p, 0x%04x, 0x%04x), stub.\n", pGlobal, hModule, wSeg);
1012 return FALSE;
1016 /***********************************************************************
1017 * MemManInfo (TOOLHELP.72)
1019 BOOL16 WINAPI MemManInfo16( MEMMANINFO *info )
1021 MEMORYSTATUS status;
1024 * Not unsurprisingly although the documention says you
1025 * _must_ provide the size in the dwSize field, this function
1026 * (under Windows) always fills the structure and returns true.
1028 GlobalMemoryStatus( &status );
1029 info->wPageSize = getpagesize();
1030 info->dwLargestFreeBlock = status.dwAvailVirtual;
1031 info->dwMaxPagesAvailable = info->dwLargestFreeBlock / info->wPageSize;
1032 info->dwMaxPagesLockable = info->dwMaxPagesAvailable;
1033 info->dwTotalLinearSpace = status.dwTotalVirtual / info->wPageSize;
1034 info->dwTotalUnlockedPages = info->dwTotalLinearSpace;
1035 info->dwFreePages = info->dwMaxPagesAvailable;
1036 info->dwTotalPages = info->dwTotalLinearSpace;
1037 info->dwFreeLinearSpace = info->dwMaxPagesAvailable;
1038 info->dwSwapFilePages = status.dwTotalPageFile / info->wPageSize;
1039 return TRUE;
1042 /***********************************************************************
1043 * GetFreeMemInfo (KERNEL.316)
1045 DWORD WINAPI GetFreeMemInfo16(void)
1047 MEMMANINFO info;
1048 MemManInfo16( &info );
1049 return MAKELONG( info.dwTotalLinearSpace, info.dwMaxPagesAvailable );
1052 /***********************************************************************
1053 * A20Proc (KERNEL.165)
1054 * A20_Proc (SYSTEM.20)
1056 void WINAPI A20Proc16( WORD unused )
1058 /* this is also a NOP in Windows */
1061 /***********************************************************************
1062 * LimitEMSPages (KERNEL.156)
1064 DWORD WINAPI LimitEMSPages16( DWORD unused )
1066 return 0;