gameux/tests: Add test of storing and reading game statistics.
[wine.git] / dlls / krnl386.exe16 / local.c
blob3eaa6ec4132bb06e8766560b11a7246ae1eba4be
1 /*
2 * 16-bit local heap functions
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Huw Davies
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Note:
25 * All local heap functions need the current DS as first parameter
26 * when called from the emulation library, so they take one more
27 * parameter than usual.
30 #include "config.h"
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include <stdlib.h>
35 #include <string.h>
36 #include "wine/winbase16.h"
37 #include "wownt32.h"
38 #include "winternl.h"
39 #include "kernel16_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(local);
44 typedef struct
46 /* Arena header */
47 WORD prev; /* Previous arena | arena type */
48 WORD next; /* Next arena */
49 /* Start of the memory block or free-list info */
50 WORD size; /* Size of the free block */
51 WORD free_prev; /* Previous free block */
52 WORD free_next; /* Next free block */
53 } LOCALARENA;
55 #define ARENA_HEADER_SIZE 4
56 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
58 /* Arena types (stored in 'prev' field of the arena) */
59 #define LOCAL_ARENA_FREE 0
60 #define LOCAL_ARENA_FIXED 1
62 /* LocalNotify() msgs */
64 #define LN_OUTOFMEM 0
65 #define LN_MOVE 1
66 #define LN_DISCARD 2
68 /* Layout of a handle entry table
70 * WORD count of entries
71 * LOCALHANDLEENTRY[count] entries
72 * WORD near ptr to next table
74 typedef struct
76 WORD addr; /* Address of the MOVEABLE block */
77 BYTE flags; /* Flags for this block */
78 BYTE lock; /* Lock count */
79 } LOCALHANDLEENTRY;
82 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
83 * in case something actually relies on this.
84 * Note that if the architecture does not allow unaligned accesses, we make
85 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
87 * An unused handle has lock = flags = 0xff. In windows addr is that of next
88 * free handle, at the moment in wine we set it to 0.
90 * A discarded block's handle has lock = addr = 0 and flags = 0x40
91 * (LMEM_DISCARDED >> 8)
94 #ifdef ALLOW_UNALIGNED_ACCESS
95 # define MOVEABLE_PREFIX sizeof(HLOCAL16)
96 #else
97 # define MOVEABLE_PREFIX sizeof(int)
98 #endif
101 #include "pshpack1.h"
103 typedef struct
105 WORD check; /* 00 Heap checking flag */
106 WORD freeze; /* 02 Heap frozen flag */
107 WORD items; /* 04 Count of items on the heap */
108 WORD first; /* 06 First item of the heap */
109 WORD pad1; /* 08 Always 0 */
110 WORD last; /* 0a Last item of the heap */
111 WORD pad2; /* 0c Always 0 */
112 BYTE ncompact; /* 0e Compactions counter */
113 BYTE dislevel; /* 0f Discard level */
114 DWORD distotal; /* 10 Total bytes discarded */
115 WORD htable; /* 14 Pointer to handle table */
116 WORD hfree; /* 16 Pointer to free handle table */
117 WORD hdelta; /* 18 Delta to expand the handle table */
118 WORD expand; /* 1a Pointer to expand function (unused) */
119 WORD pstat; /* 1c Pointer to status structure (unused) */
120 FARPROC16 notify; /* 1e Pointer to LocalNotify() function */
121 WORD lock; /* 22 Lock count for the heap */
122 WORD extra; /* 24 Extra bytes to allocate when expanding */
123 WORD minsize; /* 26 Minimum size of the heap */
124 WORD magic; /* 28 Magic number */
125 } LOCALHEAPINFO;
127 typedef struct
129 DWORD dwSize; /* 00 */
130 DWORD dwMemReserved; /* 04 */
131 DWORD dwMemCommitted; /* 08 */
132 DWORD dwTotalFree; /* 0C */
133 DWORD dwLargestFreeBlock; /* 10 */
134 DWORD dwcFreeHandles; /* 14 */
135 } LOCAL32INFO;
137 typedef struct
139 DWORD dwSize; /* 00 */
140 WORD hHandle; /* 04 */
141 DWORD dwAddress; /* 06 */
142 DWORD dwSizeBlock; /* 0A */
143 WORD wFlags; /* 0E */
144 WORD wType; /* 10 */
145 WORD hHeap; /* 12 */
146 WORD wHeapType; /* 14 */
147 DWORD dwNext; /* 16 */
148 DWORD dwNextAlt; /* 1A */
149 } LOCAL32ENTRY;
151 #include "poppack.h"
153 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
155 /* All local heap allocations are aligned on 4-byte boundaries */
156 #define LALIGN(word) (((word) + 3) & ~3)
158 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char *)(ptr)+(arena)))
159 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
160 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
161 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
163 /* determine whether the handle belongs to a fixed or a moveable block */
164 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
165 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
168 /* 32-bit heap definitions */
170 #define HTABLE_SIZE 0x10000
171 #define HTABLE_PAGESIZE 0x1000
172 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
174 #include "pshpack1.h"
175 typedef struct _LOCAL32HEADER
177 WORD freeListFirst[HTABLE_NPAGES];
178 WORD freeListSize[HTABLE_NPAGES];
179 WORD freeListLast[HTABLE_NPAGES];
181 DWORD selectorTableOffset;
182 WORD selectorTableSize;
183 WORD selectorDelta;
185 DWORD segment;
186 LPBYTE base;
188 DWORD limit;
189 DWORD flags;
191 DWORD magic;
192 HANDLE heap;
194 } LOCAL32HEADER;
195 #include "poppack.h"
197 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
200 static inline BOOL16 call_notify_func( FARPROC16 proc, WORD msg, HLOCAL16 handle, WORD arg )
202 DWORD ret;
203 WORD args[3];
205 if (!proc) return FALSE;
206 args[2] = msg;
207 args[1] = handle;
208 args[0] = arg;
209 WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
210 return LOWORD(ret);
214 /***********************************************************************
215 * LOCAL_GetHeap
217 * Return a pointer to the local heap, making sure it exists.
219 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
221 LOCALHEAPINFO *pInfo;
222 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
223 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
224 if (!ptr || !ptr->heap) return NULL;
225 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
227 WARN("Bad pointer\n");
228 return NULL;
230 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
231 if (pInfo->magic != LOCAL_HEAP_MAGIC)
233 WARN("Bad magic\n");
234 return NULL;
236 return pInfo;
240 /***********************************************************************
241 * LOCAL_MakeBlockFree
243 * Make a block free, inserting it in the free-list.
244 * 'block' is the handle of the block arena; 'baseptr' points to
245 * the beginning of the data segment containing the heap.
247 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
249 LOCALARENA *pArena, *pNext;
250 WORD next;
252 /* Mark the block as free */
254 pArena = ARENA_PTR( baseptr, block );
255 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
256 pArena->size = pArena->next - block;
258 /* Find the next free block (last block is always free) */
260 next = pArena->next;
261 for (;;)
263 pNext = ARENA_PTR( baseptr, next );
264 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
265 next = pNext->next;
268 TRACE("%04x, next %04x\n", block, next );
269 /* Insert the free block in the free-list */
271 pArena->free_prev = pNext->free_prev;
272 pArena->free_next = next;
273 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
274 pNext->free_prev = block;
278 /***********************************************************************
279 * LOCAL_RemoveFreeBlock
281 * Remove a block from the free-list.
282 * 'block' is the handle of the block arena; 'baseptr' points to
283 * the beginning of the data segment containing the heap.
285 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
287 /* Mark the block as fixed */
289 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
290 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
292 /* Remove it from the list */
294 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
295 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
299 /***********************************************************************
300 * LOCAL_AddBlock
302 * Insert a new block in the heap.
303 * 'new' is the handle of the new block arena; 'baseptr' points to
304 * the beginning of the data segment containing the heap; 'prev' is
305 * the block before the new one.
307 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
309 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
310 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
312 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
313 pNew->next = pPrev->next;
314 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
315 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
316 pPrev->next = new;
320 /***********************************************************************
321 * LOCAL_RemoveBlock
323 * Remove a block from the heap.
324 * 'block' is the handle of the block arena; 'baseptr' points to
325 * the beginning of the data segment containing the heap.
327 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
329 LOCALARENA *pArena, *pTmp;
331 /* Remove the block from the free-list */
333 TRACE("\n");
334 pArena = ARENA_PTR( baseptr, block );
335 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
336 LOCAL_RemoveFreeBlock( baseptr, block );
338 /* If the previous block is free, expand its size */
340 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
341 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
342 pTmp->size += pArena->next - block;
344 /* Remove the block from the linked list */
346 pTmp->next = pArena->next;
347 pTmp = ARENA_PTR( baseptr, pArena->next );
348 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
352 /***********************************************************************
353 * LOCAL_PrintHeap
355 static void LOCAL_PrintHeap( HANDLE16 ds )
357 char *ptr;
358 LOCALHEAPINFO *pInfo;
359 WORD arena;
361 /* FIXME - the test should be done when calling the function!
362 plus is not clear that we should print this info
363 only when TRACE_ON is on! */
364 if(!TRACE_ON(local)) return;
366 ptr = MapSL( MAKESEGPTR( ds, 0 ));
367 pInfo = LOCAL_GetHeap( ds );
369 if (!pInfo)
371 ERR( "Local Heap corrupted! ds=%04x\n", ds );
372 return;
374 TRACE( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
375 ds, pInfo->first, pInfo->last, pInfo->items );
377 arena = pInfo->first;
378 for (;;)
380 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
381 TRACE( " %04x: prev=%04x next=%04x type=%d\n", arena,
382 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
383 if (arena == pInfo->first)
385 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
386 pArena->size, pArena->free_prev, pArena->free_next );
388 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
390 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
391 pArena->size, pArena->free_prev, pArena->free_next );
392 if (pArena->next == arena) break; /* last one */
393 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
395 TRACE( "*** arena->free_next->free_prev != arena\n" );
396 break;
399 if (pArena->next == arena)
401 TRACE( "*** last block is not marked free\n" );
402 break;
404 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
406 TRACE( "*** arena->next->prev != arena (%04x, %04x)\n",
407 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
408 break;
410 arena = pArena->next;
415 /***********************************************************************
416 * LocalInit (KERNEL.4)
418 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
420 char *ptr;
421 WORD heapInfoArena, freeArena, lastArena;
422 LOCALHEAPINFO *pHeapInfo;
423 LOCALARENA *pArena, *pFirstArena, *pLastArena;
424 BOOL16 ret = FALSE;
426 /* The initial layout of the heap is: */
427 /* - first arena (FIXED) */
428 /* - heap info structure (FIXED) */
429 /* - large free block (FREE) */
430 /* - last arena (FREE) */
432 TRACE("%04x %04x-%04x\n", selector, start, end);
433 if (!selector) selector = CURRENT_DS;
435 if (TRACE_ON(local))
437 /* If TRACE_ON(local) is set, the global heap blocks are */
438 /* cleared before use, so we can test for double initialization. */
439 if (LOCAL_GetHeap(selector))
441 ERR("Heap %04x initialized twice.\n", selector);
442 LOCAL_PrintHeap(selector);
446 if (start == 0)
448 /* start == 0 means: put the local heap at the end of the segment */
450 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
451 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
452 if ( end > 0xfffe ) end = 0xfffe;
453 start -= end;
454 end += start;
456 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
458 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
459 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
460 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
461 + sizeof(LOCALHEAPINFO) );
462 lastArena = (end - sizeof(LOCALARENA)) & ~3;
464 /* Make sure there's enough space. */
466 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
468 /* Initialise the first arena */
470 pFirstArena = ARENA_PTR( ptr, start );
471 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
472 pFirstArena->next = heapInfoArena;
473 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
474 pFirstArena->free_prev = start; /* this one */
475 pFirstArena->free_next = freeArena;
477 /* Initialise the arena of the heap info structure */
479 pArena = ARENA_PTR( ptr, heapInfoArena );
480 pArena->prev = start | LOCAL_ARENA_FIXED;
481 pArena->next = freeArena;
483 /* Initialise the heap info structure */
485 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
486 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
487 pHeapInfo->items = 4;
488 pHeapInfo->first = start;
489 pHeapInfo->last = lastArena;
490 pHeapInfo->htable = 0;
491 pHeapInfo->hdelta = 0x20;
492 pHeapInfo->extra = 0x200;
493 pHeapInfo->minsize = lastArena - freeArena;
494 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
496 /* Initialise the large free block */
498 pArena = ARENA_PTR( ptr, freeArena );
499 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
500 pArena->next = lastArena;
501 pArena->size = lastArena - freeArena;
502 pArena->free_prev = start;
503 pArena->free_next = lastArena;
505 /* Initialise the last block */
507 pLastArena = ARENA_PTR( ptr, lastArena );
508 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
509 pLastArena->next = lastArena; /* this one */
510 pLastArena->size = LALIGN(sizeof(LOCALARENA));
511 pLastArena->free_prev = freeArena;
512 pLastArena->free_next = lastArena; /* this one */
514 /* Store the local heap address in the instance data */
516 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
517 LOCAL_PrintHeap( selector );
518 ret = TRUE;
520 done:
521 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
522 return ret;
526 /***********************************************************************
527 * LOCAL_GrowHeap
529 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
531 HANDLE16 hseg;
532 LONG oldsize;
533 LONG end;
534 LOCALHEAPINFO *pHeapInfo;
535 WORD freeArena, lastArena;
536 LOCALARENA *pArena, *pLastArena;
537 char *ptr;
539 hseg = GlobalHandle16( ds );
540 /* maybe mem allocated by Virtual*() ? */
541 if (!hseg) return FALSE;
543 oldsize = GlobalSize16( hseg );
544 /* if nothing can be gained, return */
545 if (oldsize > 0xfff0) return FALSE;
546 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
547 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
548 pHeapInfo = LOCAL_GetHeap( ds );
549 if (pHeapInfo == NULL) {
550 ERR("Heap not found\n" );
551 return FALSE;
553 end = GlobalSize16( hseg );
554 lastArena = (end - sizeof(LOCALARENA)) & ~3;
556 /* Update the HeapInfo */
557 pHeapInfo->items++;
558 freeArena = pHeapInfo->last;
559 pHeapInfo->last = lastArena;
560 pHeapInfo->minsize += end - oldsize;
562 /* grow the old last block */
563 pArena = ARENA_PTR( ptr, freeArena );
564 pArena->size = lastArena - freeArena;
565 pArena->next = lastArena;
566 pArena->free_next = lastArena;
568 /* Initialise the new last block */
570 pLastArena = ARENA_PTR( ptr, lastArena );
571 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
572 pLastArena->next = lastArena; /* this one */
573 pLastArena->size = LALIGN(sizeof(LOCALARENA));
574 pLastArena->free_prev = freeArena;
575 pLastArena->free_next = lastArena; /* this one */
577 /* If block before freeArena is also free then merge them */
578 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
580 LOCAL_RemoveBlock(ptr, freeArena);
581 pHeapInfo->items--;
584 TRACE("Heap expanded\n" );
585 LOCAL_PrintHeap( ds );
586 return TRUE;
590 /***********************************************************************
591 * LOCAL_FreeArena
593 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
595 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
596 LOCALHEAPINFO *pInfo;
597 LOCALARENA *pArena, *pPrev;
599 TRACE("%04x ds=%04x\n", arena, ds );
600 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
602 pArena = ARENA_PTR( ptr, arena );
603 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
605 /* shouldn't happen */
606 ERR("Trying to free block %04x twice!\n",
607 arena );
608 LOCAL_PrintHeap( ds );
609 return arena;
612 /* Check if we can merge with the previous block */
614 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
615 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
617 arena = pArena->prev & ~3;
618 pArena = pPrev;
619 LOCAL_RemoveBlock( ptr, pPrev->next );
620 pInfo->items--;
622 else /* Make a new free block */
624 LOCAL_MakeBlockFree( ptr, arena );
627 /* Check if we can merge with the next block */
629 if ((pArena->next == pArena->free_next) &&
630 (pArena->next != pInfo->last))
632 LOCAL_RemoveBlock( ptr, pArena->next );
633 pInfo->items--;
635 return 0;
639 /***********************************************************************
640 * LOCAL_ShrinkArena
642 * Shrink an arena by creating a free block at its end if possible.
643 * 'size' includes the arena header, and must be aligned.
645 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
647 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
648 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
650 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
652 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
653 if (!pInfo) return;
654 LOCAL_AddBlock( ptr, arena, arena + size );
655 pInfo->items++;
656 LOCAL_FreeArena( ds, arena + size );
661 /***********************************************************************
662 * LOCAL_GrowArenaDownward
664 * Grow an arena downward by using the previous arena (must be free).
666 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
668 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
669 LOCALHEAPINFO *pInfo;
670 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
671 WORD prevArena = pArena->prev & ~3;
672 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
673 WORD offset, size;
674 char *p;
676 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
677 offset = pPrevArena->size;
678 size = pArena->next - arena - ARENA_HEADER_SIZE;
679 LOCAL_RemoveFreeBlock( ptr, prevArena );
680 LOCAL_RemoveBlock( ptr, arena );
681 pInfo->items--;
682 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
683 while (offset < size)
685 memcpy( p, p + offset, offset );
686 p += offset;
687 size -= offset;
689 if (size) memcpy( p, p + offset, size );
690 LOCAL_ShrinkArena( ds, prevArena, newsize );
695 /***********************************************************************
696 * LOCAL_GrowArenaUpward
698 * Grow an arena upward by using the next arena (must be free and big
699 * enough). Newsize includes the arena header and must be aligned.
701 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
703 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
704 LOCALHEAPINFO *pInfo;
705 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
706 WORD nextArena = pArena->next;
708 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
709 LOCAL_RemoveBlock( ptr, nextArena );
710 pInfo->items--;
711 LOCAL_ShrinkArena( ds, arena, newsize );
715 /***********************************************************************
716 * LOCAL_GetFreeSpace
718 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
720 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
721 LOCALHEAPINFO *pInfo;
722 LOCALARENA *pArena;
723 WORD arena;
724 WORD freespace = 0;
726 if (!(pInfo = LOCAL_GetHeap( ds )))
728 ERR("Local heap not found\n" );
729 LOCAL_PrintHeap(ds);
730 return 0;
732 arena = pInfo->first;
733 pArena = ARENA_PTR( ptr, arena );
734 while (arena != pArena->free_next)
736 arena = pArena->free_next;
737 pArena = ARENA_PTR( ptr, arena );
738 if (pArena->size >= freespace) freespace = pArena->size;
740 /* FIXME doesn't yet calculate space that would become free if everything
741 were discarded when countdiscard == 1 */
742 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
743 else freespace -= ARENA_HEADER_SIZE;
744 return freespace;
748 /***********************************************************************
749 * LOCAL_Compact
751 static UINT16 LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
753 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
754 LOCALHEAPINFO *pInfo;
755 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
756 WORD arena, movearena, finalarena, table;
757 WORD count, movesize, size;
758 WORD freespace;
759 LOCALHANDLEENTRY *pEntry;
761 if (!(pInfo = LOCAL_GetHeap( ds )))
763 ERR("Local heap not found\n" );
764 LOCAL_PrintHeap(ds);
765 return 0;
767 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
768 ds, minfree, flags);
769 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
770 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
772 TRACE("Returning %04x.\n", freespace);
773 return freespace;
775 TRACE("Compacting heap %04x.\n", ds);
776 table = pInfo->htable;
777 while(table)
779 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
780 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
782 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
784 /* OK we can move this one if we want */
785 TRACE("handle %04x (block %04x) can be moved.\n",
786 (WORD)((char *)pEntry - ptr), pEntry->addr);
787 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
788 pMoveArena = ARENA_PTR(ptr, movearena);
789 movesize = pMoveArena->next - movearena;
790 arena = pInfo->first;
791 pArena = ARENA_PTR(ptr, arena);
792 size = 0xffff;
793 finalarena = 0;
794 /* Try to find the smallest arena that will do, */
795 /* which is below us in memory */
796 for(;;)
798 arena = pArena->free_next;
799 pArena = ARENA_PTR(ptr, arena);
800 if(arena >= movearena)
801 break;
802 if(arena == pArena->free_next)
803 break;
804 if((pArena->size >= movesize) && (pArena->size < size))
806 size = pArena->size;
807 finalarena = arena;
810 if (finalarena) /* Actually got somewhere to move */
812 TRACE("Moving it to %04x.\n", finalarena);
813 pFinalArena = ARENA_PTR(ptr, finalarena);
814 size = pFinalArena->size;
815 LOCAL_RemoveFreeBlock(ptr, finalarena);
816 LOCAL_ShrinkArena( ds, finalarena, movesize );
817 /* Copy the arena to it's new location */
818 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
819 (char *)pMoveArena + ARENA_HEADER_SIZE,
820 movesize - ARENA_HEADER_SIZE );
821 /* Free the old location */
822 LOCAL_FreeArena(ds, movearena);
823 call_notify_func(pInfo->notify, LN_MOVE,
824 (WORD)((char *)pEntry - ptr), pEntry->addr);
825 /* Update handle table entry */
826 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
828 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
829 == LOCAL_ARENA_FREE)
831 /* Previous arena is free (but < movesize) */
832 /* so we can 'slide' movearena down into it */
833 finalarena = pMoveArena->prev & ~3;
834 LOCAL_GrowArenaDownward( ds, movearena, movesize );
835 /* Update handle table entry */
836 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
840 table = *(WORD *)pEntry;
842 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
843 if(freespace >= minfree || (flags & LMEM_NODISCARD))
845 TRACE("Returning %04x.\n", freespace);
846 return freespace;
849 table = pInfo->htable;
850 while(table)
852 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
853 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
855 if(pEntry->addr && pEntry->lock == 0 &&
856 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
858 TRACE("Discarding handle %04x (block %04x).\n",
859 (char *)pEntry - ptr, pEntry->addr);
860 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
861 call_notify_func(pInfo->notify, LN_DISCARD, (char *)pEntry - ptr, pEntry->flags);
862 pEntry->addr = 0;
863 pEntry->flags = (LMEM_DISCARDED >> 8);
866 table = *(WORD *)pEntry;
868 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
872 /***********************************************************************
873 * LOCAL_FindFreeBlock
875 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
877 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
878 LOCALHEAPINFO *pInfo;
879 LOCALARENA *pArena;
880 WORD arena;
882 if (!(pInfo = LOCAL_GetHeap( ds )))
884 ERR("Local heap not found\n" );
885 LOCAL_PrintHeap(ds);
886 return 0;
889 arena = pInfo->first;
890 pArena = ARENA_PTR( ptr, arena );
891 for (;;) {
892 arena = pArena->free_next;
893 pArena = ARENA_PTR( ptr, arena );
894 if (arena == pArena->free_next) break;
895 if (pArena->size >= size) return arena;
897 TRACE("not enough space\n" );
898 LOCAL_PrintHeap(ds);
899 return 0;
903 /***********************************************************************
904 * get_heap_name
906 static const char *get_heap_name( WORD ds )
908 HINSTANCE16 inst = LoadLibrary16( "GDI" );
909 if (ds == GlobalHandleToSel16( inst ))
911 FreeLibrary16( inst );
912 return "GDI";
914 FreeLibrary16( inst );
915 inst = LoadLibrary16( "USER" );
916 if (ds == GlobalHandleToSel16( inst ))
918 FreeLibrary16( inst );
919 return "USER";
921 FreeLibrary16( inst );
922 return "local";
925 /***********************************************************************
926 * LOCAL_GetBlock
927 * The segment may get moved around in this function, so all callers
928 * should reset their pointer variables.
930 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
932 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
933 LOCALHEAPINFO *pInfo;
934 LOCALARENA *pArena;
935 WORD arena;
937 if (!(pInfo = LOCAL_GetHeap( ds )))
939 ERR("Local heap not found\n");
940 LOCAL_PrintHeap(ds);
941 return 0;
944 size += ARENA_HEADER_SIZE;
945 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
947 #if 0
948 notify_done:
949 #endif
950 /* Find a suitable free block */
951 arena = LOCAL_FindFreeBlock( ds, size );
952 if (arena == 0) {
953 /* no space: try to make some */
954 LOCAL_Compact( ds, size, flags );
955 arena = LOCAL_FindFreeBlock( ds, size );
957 if (arena == 0) {
958 /* still no space: try to grow the segment */
959 if (!(LOCAL_GrowHeap( ds )))
961 #if 0
962 /* FIXME: doesn't work correctly yet */
963 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds - 20, size)) /* FIXME: "size" correct ? (should indicate bytes needed) */
964 goto notify_done;
965 #endif
966 ERR( "not enough space in %s heap %04x for %d bytes\n",
967 get_heap_name(ds), ds, size );
968 return 0;
970 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
971 pInfo = LOCAL_GetHeap( ds );
972 arena = LOCAL_FindFreeBlock( ds, size );
974 if (arena == 0) {
975 ERR( "not enough space in %s heap %04x for %d bytes\n",
976 get_heap_name(ds), ds, size );
977 #if 0
978 /* FIXME: "size" correct ? (should indicate bytes needed) */
979 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds, size)) goto notify_done;
980 #endif
981 return 0;
984 /* Make a block out of the free arena */
985 pArena = ARENA_PTR( ptr, arena );
986 TRACE("size = %04x, arena %04x size %04x\n", size, arena, pArena->size );
987 LOCAL_RemoveFreeBlock( ptr, arena );
988 LOCAL_ShrinkArena( ds, arena, size );
990 if (flags & LMEM_ZEROINIT)
991 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
992 return arena + ARENA_HEADER_SIZE;
996 /***********************************************************************
997 * LOCAL_NewHTable
999 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
1001 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1002 LOCALHEAPINFO *pInfo;
1003 LOCALHANDLEENTRY *pEntry;
1004 HLOCAL16 handle;
1005 int i;
1007 TRACE("\n" );
1008 if (!(pInfo = LOCAL_GetHeap( ds )))
1010 ERR("Local heap not found\n");
1011 LOCAL_PrintHeap(ds);
1012 return FALSE;
1015 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
1016 + 2 * sizeof(WORD), LMEM_FIXED )))
1017 return FALSE;
1018 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
1019 ERR("ptr == NULL after GetBlock.\n");
1020 if (!(pInfo = LOCAL_GetHeap( ds )))
1021 ERR("pInfo == NULL after GetBlock.\n");
1023 /* Fill the entry table */
1025 *(WORD *)(ptr + handle) = pInfo->hdelta;
1026 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
1027 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
1028 pEntry->lock = pEntry->flags = 0xff;
1029 pEntry->addr = 0;
1031 *(WORD *)pEntry = pInfo->htable;
1032 pInfo->htable = handle;
1033 return TRUE;
1037 /***********************************************************************
1038 * LOCAL_GetNewHandleEntry
1040 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
1042 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1043 LOCALHEAPINFO *pInfo;
1044 LOCALHANDLEENTRY *pEntry = NULL;
1045 WORD table;
1047 if (!(pInfo = LOCAL_GetHeap( ds )))
1049 ERR("Local heap not found\n");
1050 LOCAL_PrintHeap(ds);
1051 return 0;
1054 /* Find a free slot in existing tables */
1056 table = pInfo->htable;
1057 while (table)
1059 WORD count = *(WORD *)(ptr + table);
1060 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1061 for (; count > 0; count--, pEntry++)
1062 if (pEntry->lock == 0xff) break;
1063 if (count) break;
1064 table = *(WORD *)pEntry;
1067 if (!table) /* We need to create a new table */
1069 if (!LOCAL_NewHTable( ds )) return 0;
1070 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1071 pInfo = LOCAL_GetHeap( ds );
1072 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1075 /* Now allocate this entry */
1077 pEntry->lock = 0;
1078 pEntry->flags = 0;
1079 TRACE("(%04x): %04x\n", ds, ((char *)pEntry - ptr) );
1080 return (HLOCAL16)((char *)pEntry - ptr);
1084 /***********************************************************************
1085 * LOCAL_FreeHandleEntry
1087 * Free a handle table entry.
1089 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1091 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1092 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1093 LOCALHEAPINFO *pInfo;
1094 WORD *pTable;
1095 WORD table, count, i;
1097 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1099 /* Find the table where this handle comes from */
1101 pTable = &pInfo->htable;
1102 while (*pTable)
1104 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1105 if ((handle >= *pTable + sizeof(WORD)) &&
1106 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1107 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1109 if (!*pTable)
1111 ERR("Invalid entry %04x\n", handle);
1112 LOCAL_PrintHeap( ds );
1113 return;
1116 /* Make the entry free */
1118 pEntry->addr = 0; /* just in case */
1119 pEntry->lock = 0xff;
1120 pEntry->flags = 0xff;
1121 /* Now check if all entries in this table are free */
1123 table = *pTable;
1124 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1125 count = *(WORD *)(ptr + table);
1126 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1128 /* Remove the table from the linked list and free it */
1130 TRACE("(%04x): freeing table %04x\n", ds, table);
1131 *pTable = *(WORD *)pEntry;
1132 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1136 /***********************************************************************
1137 * LocalFree (KERNEL.7)
1139 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1141 HANDLE16 ds = CURRENT_DS;
1142 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1144 TRACE("%04x ds=%04x\n", handle, ds );
1146 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1147 if (HANDLE_FIXED( handle ))
1149 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1150 else return handle; /* couldn't free it */
1152 else
1154 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1155 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1157 TRACE("real block at %04x\n", pEntry->addr );
1158 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1159 return handle; /* couldn't free it */
1161 LOCAL_FreeHandleEntry( ds, handle );
1162 return 0; /* OK */
1167 /***********************************************************************
1168 * LocalAlloc (KERNEL.5)
1170 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1172 HANDLE16 ds = CURRENT_DS;
1173 HLOCAL16 handle = 0;
1174 char *ptr;
1176 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1178 if(size > 0 && size <= 4) size = 5;
1179 if (flags & LMEM_MOVEABLE)
1181 LOCALHANDLEENTRY *plhe;
1182 HLOCAL16 hmem;
1184 if(size)
1186 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1187 goto exit;
1189 else /* We just need to allocate a discarded handle */
1190 hmem = 0;
1191 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1193 WARN("Couldn't get handle.\n");
1194 if(hmem)
1195 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1196 goto exit;
1198 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1199 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1200 plhe->lock = 0;
1201 if(hmem)
1203 plhe->addr = hmem + MOVEABLE_PREFIX;
1204 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1205 *(HLOCAL16 *)(ptr + hmem) = handle;
1207 else
1209 plhe->addr = 0;
1210 plhe->flags = LMEM_DISCARDED >> 8;
1213 else /* FIXED */
1215 if(size) handle = LOCAL_GetBlock( ds, size, flags );
1218 exit:
1219 CURRENT_STACK16->ecx = handle; /* must be returned in cx too */
1220 return handle;
1224 /***********************************************************************
1225 * LocalReAlloc (KERNEL.6)
1227 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1229 HANDLE16 ds = CURRENT_DS;
1230 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1231 LOCALHEAPINFO *pInfo;
1232 LOCALARENA *pArena, *pNext;
1233 LOCALHANDLEENTRY *pEntry = NULL;
1234 WORD arena, oldsize;
1235 HLOCAL16 hmem, blockhandle;
1236 LONG nextarena;
1238 if (!handle) return 0;
1239 if(HANDLE_MOVEABLE(handle) &&
1240 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1241 return 0;
1243 TRACE("%04x %d %04x ds=%04x\n", handle, size, flags, ds );
1244 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1246 if (HANDLE_FIXED( handle ))
1247 blockhandle = handle;
1248 else
1250 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1251 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1253 HLOCAL16 hl;
1254 if(pEntry->addr)
1255 WARN("Dicarded block has non-zero addr.\n");
1256 TRACE("ReAllocating discarded block\n");
1257 if(size <= 4) size = 5;
1258 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1259 return 0;
1260 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1261 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1262 pEntry->addr = hl + MOVEABLE_PREFIX;
1263 pEntry->flags = 0;
1264 pEntry->lock = 0;
1265 *(HLOCAL16 *)(ptr + hl) = handle;
1266 return handle;
1268 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1270 ERR("(%04x,%04x): invalid handle\n",
1271 ds, handle );
1272 return 0;
1274 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1275 ERR("Back ptr to handle is invalid\n");
1276 return 0;
1280 if (flags & LMEM_MODIFY)
1282 if (HANDLE_MOVEABLE(handle))
1284 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1285 pEntry->flags = (flags & 0x0f00) >> 8;
1286 TRACE("Changing flags to %x.\n", pEntry->flags);
1288 return handle;
1291 if (!size)
1293 if (flags & LMEM_MOVEABLE)
1295 if (HANDLE_FIXED(handle))
1297 TRACE("Freeing fixed block.\n");
1298 return LocalFree16( handle );
1300 else /* Moveable block */
1302 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1303 if (pEntry->lock == 0)
1305 /* discards moveable blocks */
1306 TRACE("Discarding block\n");
1307 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1308 pEntry->addr = 0;
1309 pEntry->flags = (LMEM_DISCARDED >> 8);
1310 return handle;
1313 return 0;
1315 else if(flags == 0)
1317 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1318 if (pEntry->lock == 0)
1320 /* Frees block */
1321 return LocalFree16( handle );
1324 return 0;
1327 arena = ARENA_HEADER( blockhandle );
1328 TRACE("arena is %04x\n", arena );
1329 pArena = ARENA_PTR( ptr, arena );
1331 if(size <= 4) size = 5;
1332 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1333 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1334 nextarena = LALIGN(blockhandle + size);
1336 /* Check for size reduction */
1338 if (nextarena <= pArena->next)
1340 TRACE("size reduction, making new free block\n");
1341 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1342 TRACE("returning %04x\n", handle );
1343 return handle;
1346 /* Check if the next block is free and large enough */
1348 pNext = ARENA_PTR( ptr, pArena->next );
1349 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1350 (nextarena <= pNext->next))
1352 TRACE("size increase, making new free block\n");
1353 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1354 if (flags & LMEM_ZEROINIT)
1356 char *oldend = (char *)pArena + ARENA_HEADER_SIZE + oldsize;
1357 char *newend = ptr + pArena->next;
1358 TRACE("Clearing memory from %p to %p (DS -> %p)\n", oldend, newend, ptr);
1359 memset(oldend, 0, newend - oldend);
1362 TRACE("returning %04x\n", handle );
1363 return handle;
1366 /* Now we have to allocate a new block, but not if (fixed block or locked
1367 block) and no LMEM_MOVEABLE */
1369 if (!(flags & LMEM_MOVEABLE))
1371 if (HANDLE_FIXED(handle))
1373 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1374 return 0;
1376 else
1378 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1380 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1381 return 0;
1386 hmem = LOCAL_GetBlock( ds, size, flags );
1387 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1388 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1389 { /* a compaction, which might in turn have */
1390 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1391 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1393 if (!hmem)
1395 /* Remove the block from the heap and try again */
1396 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1397 if (!buffer) return 0;
1398 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1399 LOCAL_FreeArena( ds, arena );
1400 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1402 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1404 ERR("Can't restore saved block\n" );
1405 HeapFree( GetProcessHeap(), 0, buffer );
1406 return 0;
1408 size = oldsize;
1410 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1411 memcpy( ptr + hmem, buffer, oldsize );
1412 HeapFree( GetProcessHeap(), 0, buffer );
1414 else
1416 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1417 LOCAL_FreeArena( ds, arena );
1419 if (HANDLE_MOVEABLE( handle ))
1421 TRACE("fixing handle\n");
1422 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1423 pEntry->addr = hmem + MOVEABLE_PREFIX;
1424 /* Back ptr should still be correct */
1425 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1426 ERR("back ptr is invalid.\n");
1427 hmem = handle;
1429 if (size == oldsize) hmem = 0; /* Realloc failed */
1430 TRACE("returning %04x\n", hmem );
1431 return hmem;
1435 /***********************************************************************
1436 * LOCAL_InternalLock
1438 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1440 HLOCAL16 old_handle = handle;
1442 if (HANDLE_MOVEABLE(handle))
1444 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1445 if (pEntry->flags == (LMEM_DISCARDED >> 8)) return 0;
1446 if (pEntry->lock < 0xfe) pEntry->lock++;
1447 handle = pEntry->addr;
1449 TRACE("%04x returning %04x\n", old_handle, handle );
1450 return handle;
1454 /***********************************************************************
1455 * LocalUnlock (KERNEL.9)
1457 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1459 HANDLE16 ds = CURRENT_DS;
1460 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1462 TRACE("%04x\n", handle );
1463 if (HANDLE_MOVEABLE(handle))
1465 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1466 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1467 /* For moveable block, return the new lock count */
1468 /* (see _Windows_Internals_ p. 197) */
1469 return --pEntry->lock;
1471 else return FALSE;
1475 /***********************************************************************
1476 * LocalSize (KERNEL.10)
1478 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1480 HANDLE16 ds = CURRENT_DS;
1481 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1482 LOCALARENA *pArena;
1484 TRACE("%04x ds=%04x\n", handle, ds );
1486 if (!handle) return 0;
1487 if (HANDLE_MOVEABLE( handle ))
1489 handle = *(WORD *)(ptr + handle);
1490 if (!handle) return 0;
1491 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1493 else
1494 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1496 return pArena->next - handle;
1500 /***********************************************************************
1501 * LocalFlags (KERNEL.12)
1503 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1505 HANDLE16 ds = CURRENT_DS;
1506 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1508 if (HANDLE_MOVEABLE(handle))
1510 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1511 TRACE("(%04x,%04x): returning %04x\n",
1512 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1513 return pEntry->lock | (pEntry->flags << 8);
1515 else
1517 TRACE("(%04x,%04x): returning 0\n",
1518 ds, handle );
1519 return 0;
1524 /***********************************************************************
1525 * LocalHeapSize (KERNEL.162)
1527 WORD WINAPI LocalHeapSize16(void)
1529 HANDLE16 ds = CURRENT_DS;
1530 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1531 return pInfo ? pInfo->last - pInfo->first : 0;
1535 /***********************************************************************
1536 * LocalCountFree (KERNEL.161)
1538 WORD WINAPI LocalCountFree16(void)
1540 HANDLE16 ds = CURRENT_DS;
1541 WORD arena, total;
1542 LOCALARENA *pArena;
1543 LOCALHEAPINFO *pInfo;
1544 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1546 if (!(pInfo = LOCAL_GetHeap( ds )))
1548 ERR("(%04x): Local heap not found\n", ds );
1549 LOCAL_PrintHeap( ds );
1550 return 0;
1553 total = 0;
1554 arena = pInfo->first;
1555 pArena = ARENA_PTR( ptr, arena );
1556 for (;;)
1558 arena = pArena->free_next;
1559 pArena = ARENA_PTR( ptr, arena );
1560 if (arena == pArena->free_next) break;
1561 total += pArena->size;
1563 TRACE("(%04x): returning %d\n", ds, total);
1564 return total;
1568 /***********************************************************************
1569 * LocalHandle (KERNEL.11)
1571 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1573 HANDLE16 ds = CURRENT_DS;
1574 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1575 LOCALHEAPINFO *pInfo;
1576 WORD table;
1578 if (!(pInfo = LOCAL_GetHeap( ds )))
1580 ERR("(%04x): Local heap not found\n", ds );
1581 LOCAL_PrintHeap( ds );
1582 return 0;
1585 /* Find the address in the entry tables */
1587 table = pInfo->htable;
1588 while (table)
1590 WORD count = *(WORD *)(ptr + table);
1591 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1592 for (; count > 0; count--, pEntry++)
1593 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1594 table = *(WORD *)pEntry;
1597 return (HLOCAL16)addr; /* Fixed block handle is addr */
1603 /***********************************************************************
1604 * LocalLock (KERNEL.8)
1606 * Note: only the offset part of the pointer is returned by the relay code.
1608 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1610 WORD ds = CURRENT_DS;
1611 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1612 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1616 /***********************************************************************
1617 * LocalCompact (KERNEL.13)
1619 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1621 TRACE("%04x\n", minfree );
1622 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1626 /***********************************************************************
1627 * LocalNotify (KERNEL.14)
1629 * Installs a callback function that is called for local memory events
1630 * Callback function prototype is
1631 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1632 * wMsg:
1633 * - LN_OUTOFMEM
1634 * NotifyFunc seems to be responsible for allocating some memory,
1635 * returns TRUE for success.
1636 * wArg = number of bytes needed additionally
1637 * - LN_MOVE
1638 * hMem = handle; wArg = old mem location
1639 * - LN_DISCARD
1640 * NotifyFunc seems to be strongly encouraged to return TRUE,
1641 * otherwise LogError() gets called.
1642 * hMem = handle; wArg = flags
1644 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1646 LOCALHEAPINFO *pInfo;
1647 FARPROC16 oldNotify;
1648 HANDLE16 ds = CURRENT_DS;
1650 if (!(pInfo = LOCAL_GetHeap( ds )))
1652 ERR("(%04x): Local heap not found\n", ds );
1653 LOCAL_PrintHeap( ds );
1654 return 0;
1656 TRACE("(%04x): %p\n", ds, func );
1657 FIXME("Half implemented\n");
1658 oldNotify = pInfo->notify;
1659 pInfo->notify = func;
1660 return oldNotify;
1664 /***********************************************************************
1665 * LocalShrink (KERNEL.121)
1667 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1669 TRACE("%04x %04x\n", handle, newsize );
1670 return 0;
1674 /***********************************************************************
1675 * GetHeapSpaces (KERNEL.138)
1677 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1679 NE_MODULE *pModule;
1680 WORD oldDS = CURRENT_DS;
1681 DWORD spaces;
1683 if (!(pModule = NE_GetPtr( module ))) return 0;
1684 CURRENT_DS = GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1)->hSeg);
1685 spaces = MAKELONG( LocalCountFree16(), LocalHeapSize16() );
1686 CURRENT_DS = oldDS;
1687 return spaces;
1691 /***********************************************************************
1692 * LocalHandleDelta (KERNEL.310)
1694 WORD WINAPI LocalHandleDelta16( WORD delta )
1696 LOCALHEAPINFO *pInfo;
1698 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1700 ERR("Local heap not found\n");
1701 LOCAL_PrintHeap( CURRENT_DS );
1702 return 0;
1704 if (delta) pInfo->hdelta = delta;
1705 TRACE("returning %04x\n", pInfo->hdelta);
1706 return pInfo->hdelta;
1710 /***********************************************************************
1711 * 32-bit local heap functions (Win95; undocumented)
1714 /***********************************************************************
1715 * K208 (KERNEL.208)
1717 HANDLE WINAPI Local32Init16( WORD segment, DWORD tableSize,
1718 DWORD heapSize, DWORD flags )
1720 DWORD totSize, segSize = 0;
1721 LPBYTE base;
1722 LOCAL32HEADER *header;
1723 HANDLE heap;
1724 WORD *selectorTable;
1725 WORD selectorEven, selectorOdd;
1726 int i, nrBlocks;
1728 /* Determine new heap size */
1730 if ( segment )
1732 if ( (segSize = GetSelectorLimit16( segment )) == 0 )
1733 return 0;
1734 else
1735 segSize++;
1738 if ( heapSize == (DWORD)-1 )
1739 heapSize = 1024*1024; /* FIXME */
1741 heapSize = (heapSize + 0xffff) & 0xffff0000;
1742 segSize = (segSize + 0x0fff) & 0xfffff000;
1743 totSize = segSize + HTABLE_SIZE + heapSize;
1746 /* Allocate memory and initialize heap */
1748 if ( !(base = VirtualAlloc( NULL, totSize, MEM_RESERVE, PAGE_READWRITE )) )
1749 return 0;
1751 if ( !VirtualAlloc( base, segSize + HTABLE_PAGESIZE,
1752 MEM_COMMIT, PAGE_READWRITE ) )
1754 VirtualFree( base, 0, MEM_RELEASE );
1755 return 0;
1758 if (!(heap = RtlCreateHeap( 0, base + segSize + HTABLE_SIZE, heapSize, 0x10000, NULL, NULL )))
1760 VirtualFree( base, 0, MEM_RELEASE );
1761 return 0;
1765 /* Set up header and handle table */
1767 header = (LOCAL32HEADER *)(base + segSize);
1768 header->base = base;
1769 header->limit = HTABLE_PAGESIZE-1;
1770 header->flags = 0;
1771 header->magic = LOCAL32_MAGIC;
1772 header->heap = heap;
1774 header->freeListFirst[0] = sizeof(LOCAL32HEADER);
1775 header->freeListLast[0] = HTABLE_PAGESIZE - 4;
1776 header->freeListSize[0] = (HTABLE_PAGESIZE - sizeof(LOCAL32HEADER)) / 4;
1778 for (i = header->freeListFirst[0]; i < header->freeListLast[0]; i += 4)
1779 *(DWORD *)((LPBYTE)header + i) = i+4;
1781 header->freeListFirst[1] = 0xffff;
1784 /* Set up selector table */
1786 nrBlocks = (totSize + 0x7fff) >> 15;
1787 selectorTable = HeapAlloc( header->heap, 0, nrBlocks * 2 );
1788 selectorEven = SELECTOR_AllocBlock( base, totSize, WINE_LDT_FLAGS_DATA );
1789 selectorOdd = SELECTOR_AllocBlock( base + 0x8000, totSize - 0x8000, WINE_LDT_FLAGS_DATA );
1790 if ( !selectorTable || !selectorEven || !selectorOdd )
1792 HeapFree( header->heap, 0, selectorTable );
1793 if ( selectorEven ) SELECTOR_FreeBlock( selectorEven );
1794 if ( selectorOdd ) SELECTOR_FreeBlock( selectorOdd );
1795 HeapDestroy( header->heap );
1796 VirtualFree( base, 0, MEM_RELEASE );
1797 return 0;
1800 header->selectorTableOffset = (LPBYTE)selectorTable - header->base;
1801 header->selectorTableSize = nrBlocks * 4; /* ??? Win95 does it this way! */
1802 header->selectorDelta = selectorEven - selectorOdd;
1803 header->segment = segment? segment : selectorEven;
1805 for (i = 0; i < nrBlocks; i++)
1806 selectorTable[i] = (i & 1)? selectorOdd + ((i >> 1) << __AHSHIFT)
1807 : selectorEven + ((i >> 1) << __AHSHIFT);
1809 /* Move old segment */
1811 if ( segment )
1813 /* FIXME: This is somewhat ugly and relies on implementation
1814 details about 16-bit global memory handles ... */
1816 LPBYTE oldBase = (LPBYTE)GetSelectorBase( segment );
1817 memcpy( base, oldBase, segSize );
1818 GLOBAL_MoveBlock( segment, base, totSize );
1819 HeapFree( GetProcessHeap(), 0, oldBase );
1822 return header;
1825 /***********************************************************************
1826 * Local32_SearchHandle
1828 static LPDWORD Local32_SearchHandle( LOCAL32HEADER *header, DWORD addr )
1830 LPDWORD handle;
1832 for ( handle = (LPDWORD)((LPBYTE)header + sizeof(LOCAL32HEADER));
1833 handle < (LPDWORD)((LPBYTE)header + header->limit);
1834 handle++)
1836 if (*handle == addr)
1837 return handle;
1840 return NULL;
1843 /***********************************************************************
1844 * Local32_ToHandle
1846 static VOID Local32_ToHandle( LOCAL32HEADER *header, INT16 type,
1847 DWORD addr, LPDWORD *handle, LPBYTE *ptr )
1849 *handle = NULL;
1850 *ptr = NULL;
1852 switch (type)
1854 case -2: /* 16:16 pointer, no handles */
1855 *ptr = MapSL( addr );
1856 *handle = (LPDWORD)*ptr;
1857 break;
1859 case -1: /* 32-bit offset, no handles */
1860 *ptr = header->base + addr;
1861 *handle = (LPDWORD)*ptr;
1862 break;
1864 case 0: /* handle */
1865 if ( addr >= sizeof(LOCAL32HEADER)
1866 && addr < header->limit && !(addr & 3)
1867 && *(LPDWORD)((LPBYTE)header + addr) >= HTABLE_SIZE )
1869 *handle = (LPDWORD)((LPBYTE)header + addr);
1870 *ptr = header->base + **handle;
1872 break;
1874 case 1: /* 16:16 pointer */
1875 *ptr = MapSL( addr );
1876 *handle = Local32_SearchHandle( header, *ptr - header->base );
1877 break;
1879 case 2: /* 32-bit offset */
1880 *ptr = header->base + addr;
1881 *handle = Local32_SearchHandle( header, *ptr - header->base );
1882 break;
1886 /***********************************************************************
1887 * Local32_FromHandle
1889 static VOID Local32_FromHandle( LOCAL32HEADER *header, INT16 type,
1890 DWORD *addr, LPDWORD handle, LPBYTE ptr )
1892 *addr = 0;
1893 switch (type)
1895 case -2: /* 16:16 pointer */
1896 case 1:
1898 WORD *selTable = (LPWORD)(header->base + header->selectorTableOffset);
1899 DWORD offset = ptr - header->base;
1900 *addr = MAKELONG( offset & 0x7fff, selTable[offset >> 15] );
1902 break;
1904 case -1: /* 32-bit offset */
1905 case 2:
1906 *addr = ptr - header->base;
1907 break;
1909 case 0: /* handle */
1910 *addr = (LPBYTE)handle - (LPBYTE)header;
1911 break;
1915 /***********************************************************************
1916 * K209 (KERNEL.209)
1918 DWORD WINAPI Local32Alloc16( HANDLE heap, DWORD size, INT16 type, DWORD flags )
1920 LOCAL32HEADER *header = heap;
1921 LPDWORD handle;
1922 LPBYTE ptr;
1923 DWORD addr;
1925 /* Allocate memory */
1926 ptr = HeapAlloc( header->heap,
1927 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0, size );
1928 if (!ptr) return 0;
1931 /* Allocate handle if requested */
1932 if (type >= 0)
1934 int page, i;
1936 /* Find first page of handle table with free slots */
1937 for (page = 0; page < HTABLE_NPAGES; page++)
1938 if (header->freeListFirst[page] != 0)
1939 break;
1940 if (page == HTABLE_NPAGES)
1942 WARN("Out of handles!\n" );
1943 HeapFree( header->heap, 0, ptr );
1944 return 0;
1947 /* If virgin page, initialize it */
1948 if (header->freeListFirst[page] == 0xffff)
1950 if ( !VirtualAlloc( (LPBYTE)header + (page << 12),
1951 0x1000, MEM_COMMIT, PAGE_READWRITE ) )
1953 WARN("Cannot grow handle table!\n" );
1954 HeapFree( header->heap, 0, ptr );
1955 return 0;
1958 header->limit += HTABLE_PAGESIZE;
1960 header->freeListFirst[page] = 0;
1961 header->freeListLast[page] = HTABLE_PAGESIZE - 4;
1962 header->freeListSize[page] = HTABLE_PAGESIZE / 4;
1964 for (i = 0; i < HTABLE_PAGESIZE; i += 4)
1965 *(DWORD *)((LPBYTE)header + i) = i+4;
1967 if (page < HTABLE_NPAGES-1)
1968 header->freeListFirst[page+1] = 0xffff;
1971 /* Allocate handle slot from page */
1972 handle = (LPDWORD)((LPBYTE)header + header->freeListFirst[page]);
1973 if (--header->freeListSize[page] == 0)
1974 header->freeListFirst[page] = header->freeListLast[page] = 0;
1975 else
1976 header->freeListFirst[page] = *handle;
1978 /* Store 32-bit offset in handle slot */
1979 *handle = ptr - header->base;
1981 else
1983 handle = (LPDWORD)ptr;
1984 header->flags |= 1;
1988 /* Convert handle to requested output type */
1989 Local32_FromHandle( header, type, &addr, handle, ptr );
1990 return addr;
1993 /***********************************************************************
1994 * K210 (KERNEL.210)
1996 DWORD WINAPI Local32ReAlloc16( HANDLE heap, DWORD addr, INT16 type,
1997 DWORD size, DWORD flags )
1999 LOCAL32HEADER *header = heap;
2000 LPDWORD handle;
2001 LPBYTE ptr;
2003 if (!addr)
2004 return Local32Alloc16( heap, size, type, flags );
2006 /* Retrieve handle and pointer */
2007 Local32_ToHandle( header, type, addr, &handle, &ptr );
2008 if (!handle) return FALSE;
2010 /* Reallocate memory block */
2011 ptr = HeapReAlloc( header->heap,
2012 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0,
2013 ptr, size );
2014 if (!ptr) return 0;
2016 /* Modify handle */
2017 if (type >= 0)
2018 *handle = ptr - header->base;
2019 else
2020 handle = (LPDWORD)ptr;
2022 /* Convert handle to requested output type */
2023 Local32_FromHandle( header, type, &addr, handle, ptr );
2024 return addr;
2027 /***********************************************************************
2028 * K211 (KERNEL.211)
2030 BOOL WINAPI Local32Free16( HANDLE heap, DWORD addr, INT16 type )
2032 LOCAL32HEADER *header = heap;
2033 LPDWORD handle;
2034 LPBYTE ptr;
2036 /* Retrieve handle and pointer */
2037 Local32_ToHandle( header, type, addr, &handle, &ptr );
2038 if (!handle) return FALSE;
2040 /* Free handle if necessary */
2041 if (type >= 0)
2043 int offset = (LPBYTE)handle - (LPBYTE)header;
2044 int page = offset >> 12;
2046 /* Return handle slot to page free list */
2047 if (header->freeListSize[page]++ == 0)
2048 header->freeListFirst[page] = header->freeListLast[page] = offset;
2049 else
2050 *(LPDWORD)((LPBYTE)header + header->freeListLast[page]) = offset,
2051 header->freeListLast[page] = offset;
2053 *handle = 0;
2055 /* Shrink handle table when possible */
2056 while (page > 0 && header->freeListSize[page] == HTABLE_PAGESIZE / 4)
2058 if ( VirtualFree( (LPBYTE)header +
2059 (header->limit & ~(HTABLE_PAGESIZE-1)),
2060 HTABLE_PAGESIZE, MEM_DECOMMIT ) )
2061 break;
2063 header->limit -= HTABLE_PAGESIZE;
2064 header->freeListFirst[page] = 0xffff;
2065 page--;
2069 /* Free memory */
2070 return HeapFree( header->heap, 0, ptr );
2073 /***********************************************************************
2074 * K213 (KERNEL.213)
2076 DWORD WINAPI Local32Translate16( HANDLE heap, DWORD addr, INT16 type1, INT16 type2 )
2078 LOCAL32HEADER *header = heap;
2079 LPDWORD handle;
2080 LPBYTE ptr;
2082 Local32_ToHandle( header, type1, addr, &handle, &ptr );
2083 if (!handle) return 0;
2085 Local32_FromHandle( header, type2, &addr, handle, ptr );
2086 return addr;
2089 /***********************************************************************
2090 * K214 (KERNEL.214)
2092 DWORD WINAPI Local32Size16( HANDLE heap, DWORD addr, INT16 type )
2094 LOCAL32HEADER *header = heap;
2095 LPDWORD handle;
2096 LPBYTE ptr;
2098 Local32_ToHandle( header, type, addr, &handle, &ptr );
2099 if (!handle) return 0;
2101 return HeapSize( header->heap, 0, ptr );
2104 /***********************************************************************
2105 * K215 (KERNEL.215)
2107 BOOL WINAPI Local32ValidHandle16( HANDLE heap, WORD addr )
2109 LOCAL32HEADER *header = heap;
2110 LPDWORD handle;
2111 LPBYTE ptr;
2113 Local32_ToHandle( header, 0, addr, &handle, &ptr );
2114 return handle != NULL;
2117 /***********************************************************************
2118 * K229 (KERNEL.229)
2120 WORD WINAPI Local32GetSegment16( HANDLE heap )
2122 LOCAL32HEADER *header = heap;
2123 return header->segment;
2126 /***********************************************************************
2127 * Local32_GetHeap
2129 static LOCAL32HEADER *Local32_GetHeap( HGLOBAL16 handle )
2131 WORD selector = GlobalHandleToSel16( handle );
2132 DWORD base = GetSelectorBase( selector );
2133 DWORD limit = GetSelectorLimit16( selector );
2135 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2136 it this way ... */
2138 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2139 return (LOCAL32HEADER *)base;
2141 base += 0x10000;
2142 limit -= 0x10000;
2144 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2145 return (LOCAL32HEADER *)base;
2147 return NULL;
2150 /***********************************************************************
2151 * Local32Info (KERNEL.444)
2153 BOOL16 WINAPI Local32Info16( LOCAL32INFO *pLocal32Info, HGLOBAL16 handle )
2155 PROCESS_HEAP_ENTRY entry;
2156 int i;
2158 LOCAL32HEADER *header = Local32_GetHeap( handle );
2159 if ( !header ) return FALSE;
2161 if ( !pLocal32Info || pLocal32Info->dwSize < sizeof(LOCAL32INFO) )
2162 return FALSE;
2164 pLocal32Info->dwMemReserved = 0;
2165 pLocal32Info->dwMemCommitted = 0;
2166 pLocal32Info->dwTotalFree = 0;
2167 pLocal32Info->dwLargestFreeBlock = 0;
2169 while (HeapWalk( header->heap, &entry ))
2171 if (entry.wFlags & PROCESS_HEAP_REGION)
2173 pLocal32Info->dwMemReserved += entry.u.Region.dwCommittedSize
2174 + entry.u.Region.dwUnCommittedSize;
2175 pLocal32Info->dwMemCommitted = entry.u.Region.dwCommittedSize;
2177 else if (!(entry.wFlags & PROCESS_HEAP_ENTRY_BUSY))
2179 DWORD size = entry.cbData + entry.cbOverhead;
2180 pLocal32Info->dwTotalFree += size;
2181 if (size > pLocal32Info->dwLargestFreeBlock) pLocal32Info->dwLargestFreeBlock = size;
2185 pLocal32Info->dwcFreeHandles = 0;
2186 for ( i = 0; i < HTABLE_NPAGES; i++ )
2188 if ( header->freeListFirst[i] == 0xffff ) break;
2189 pLocal32Info->dwcFreeHandles += header->freeListSize[i];
2191 pLocal32Info->dwcFreeHandles += (HTABLE_NPAGES - i) * HTABLE_PAGESIZE/4;
2193 return TRUE;
2196 /***********************************************************************
2197 * Local32First (KERNEL.445)
2199 BOOL16 WINAPI Local32First16( LOCAL32ENTRY *pLocal32Entry, HGLOBAL16 handle )
2201 FIXME("(%p, %04X): stub!\n", pLocal32Entry, handle );
2202 return FALSE;
2205 /***********************************************************************
2206 * Local32Next (KERNEL.446)
2208 BOOL16 WINAPI Local32Next16( LOCAL32ENTRY *pLocal32Entry )
2210 FIXME("(%p): stub!\n", pLocal32Entry );
2211 return FALSE;