include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / krnl386.exe16 / local.c
blob1f75d8ef00f5e3aa1da3e6620666b1a8fd219f3d
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 <stdlib.h>
31 #include <string.h>
32 #include "wine/winbase16.h"
33 #include "wownt32.h"
34 #include "winternl.h"
35 #include "kernel16_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(local);
40 typedef struct
42 /* Arena header */
43 WORD prev; /* Previous arena | arena type */
44 WORD next; /* Next arena */
45 /* Start of the memory block or free-list info */
46 WORD size; /* Size of the free block */
47 WORD free_prev; /* Previous free block */
48 WORD free_next; /* Next free block */
49 } LOCALARENA;
51 #define ARENA_HEADER_SIZE 4
52 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
54 /* Arena types (stored in 'prev' field of the arena) */
55 #define LOCAL_ARENA_FREE 0
56 #define LOCAL_ARENA_FIXED 1
58 /* LocalNotify() msgs */
60 #define LN_OUTOFMEM 0
61 #define LN_MOVE 1
62 #define LN_DISCARD 2
64 /* Layout of a handle entry table
66 * WORD count of entries
67 * LOCALHANDLEENTRY[count] entries
68 * WORD near ptr to next table
70 typedef struct
72 WORD addr; /* Address of the MOVEABLE block */
73 BYTE flags; /* Flags for this block */
74 BYTE lock; /* Lock count */
75 } LOCALHANDLEENTRY;
78 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
79 * in case something actually relies on this.
81 * An unused handle has lock = flags = 0xff. In windows addr is that of next
82 * free handle, at the moment in wine we set it to 0.
84 * A discarded block's handle has lock = addr = 0 and flags = 0x40
85 * (LMEM_DISCARDED >> 8)
88 #define MOVEABLE_PREFIX sizeof(HLOCAL16)
90 #include "pshpack1.h"
92 typedef struct
94 WORD check; /* 00 Heap checking flag */
95 WORD freeze; /* 02 Heap frozen flag */
96 WORD items; /* 04 Count of items on the heap */
97 WORD first; /* 06 First item of the heap */
98 WORD pad1; /* 08 Always 0 */
99 WORD last; /* 0a Last item of the heap */
100 WORD pad2; /* 0c Always 0 */
101 BYTE ncompact; /* 0e Compactions counter */
102 BYTE dislevel; /* 0f Discard level */
103 DWORD distotal; /* 10 Total bytes discarded */
104 WORD htable; /* 14 Pointer to handle table */
105 WORD hfree; /* 16 Pointer to free handle table */
106 WORD hdelta; /* 18 Delta to expand the handle table */
107 WORD expand; /* 1a Pointer to expand function (unused) */
108 WORD pstat; /* 1c Pointer to status structure (unused) */
109 FARPROC16 notify; /* 1e Pointer to LocalNotify() function */
110 WORD lock; /* 22 Lock count for the heap */
111 WORD extra; /* 24 Extra bytes to allocate when expanding */
112 WORD minsize; /* 26 Minimum size of the heap */
113 WORD magic; /* 28 Magic number */
114 } LOCALHEAPINFO;
116 typedef struct
118 DWORD dwSize; /* 00 */
119 DWORD dwMemReserved; /* 04 */
120 DWORD dwMemCommitted; /* 08 */
121 DWORD dwTotalFree; /* 0C */
122 DWORD dwLargestFreeBlock; /* 10 */
123 DWORD dwcFreeHandles; /* 14 */
124 } LOCAL32INFO;
126 typedef struct
128 DWORD dwSize; /* 00 */
129 WORD hHandle; /* 04 */
130 DWORD dwAddress; /* 06 */
131 DWORD dwSizeBlock; /* 0A */
132 WORD wFlags; /* 0E */
133 WORD wType; /* 10 */
134 WORD hHeap; /* 12 */
135 WORD wHeapType; /* 14 */
136 DWORD dwNext; /* 16 */
137 DWORD dwNextAlt; /* 1A */
138 } LOCAL32ENTRY;
140 #include "poppack.h"
142 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
144 /* All local heap allocations are aligned on 4-byte boundaries */
145 #define LALIGN(word) (((word) + 3) & ~3)
147 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char *)(ptr)+(arena)))
148 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
149 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
150 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
152 /* determine whether the handle belongs to a fixed or a moveable block */
153 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
154 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
157 /* 32-bit heap definitions */
159 #define HTABLE_SIZE 0x10000
160 #define HTABLE_PAGESIZE 0x1000
161 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
163 #include "pshpack1.h"
164 typedef struct _LOCAL32HEADER
166 WORD freeListFirst[HTABLE_NPAGES];
167 WORD freeListSize[HTABLE_NPAGES];
168 WORD freeListLast[HTABLE_NPAGES];
170 DWORD selectorTableOffset;
171 WORD selectorTableSize;
172 WORD selectorDelta;
174 DWORD segment;
175 LPBYTE base;
177 DWORD limit;
178 DWORD flags;
180 DWORD magic;
181 HANDLE heap;
183 } LOCAL32HEADER;
184 #include "poppack.h"
186 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
189 static inline BOOL16 call_notify_func( FARPROC16 proc, WORD msg, HLOCAL16 handle, WORD arg )
191 DWORD ret;
192 WORD args[3];
194 if (!proc) return FALSE;
195 args[2] = msg;
196 args[1] = handle;
197 args[0] = arg;
198 WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
199 return LOWORD(ret);
203 /***********************************************************************
204 * LOCAL_GetHeap
206 * Return a pointer to the local heap, making sure it exists.
208 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
210 LOCALHEAPINFO *pInfo;
211 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
212 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
213 if (!ptr || !ptr->heap) return NULL;
214 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
216 WARN("Bad pointer\n");
217 return NULL;
219 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
220 if (pInfo->magic != LOCAL_HEAP_MAGIC)
222 WARN("Bad magic\n");
223 return NULL;
225 return pInfo;
229 /***********************************************************************
230 * LOCAL_MakeBlockFree
232 * Make a block free, inserting it in the free-list.
233 * 'block' is the handle of the block arena; 'baseptr' points to
234 * the beginning of the data segment containing the heap.
236 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
238 LOCALARENA *pArena, *pNext;
239 WORD next;
241 /* Mark the block as free */
243 pArena = ARENA_PTR( baseptr, block );
244 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
245 pArena->size = pArena->next - block;
247 /* Find the next free block (last block is always free) */
249 next = pArena->next;
250 for (;;)
252 pNext = ARENA_PTR( baseptr, next );
253 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
254 next = pNext->next;
257 TRACE("%04x, next %04x\n", block, next );
258 /* Insert the free block in the free-list */
260 pArena->free_prev = pNext->free_prev;
261 pArena->free_next = next;
262 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
263 pNext->free_prev = block;
267 /***********************************************************************
268 * LOCAL_RemoveFreeBlock
270 * Remove a block from the free-list.
271 * 'block' is the handle of the block arena; 'baseptr' points to
272 * the beginning of the data segment containing the heap.
274 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
276 /* Mark the block as fixed */
278 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
279 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
281 /* Remove it from the list */
283 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
284 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
288 /***********************************************************************
289 * LOCAL_AddBlock
291 * Insert a new block in the heap.
292 * 'new' is the handle of the new block arena; 'baseptr' points to
293 * the beginning of the data segment containing the heap; 'prev' is
294 * the block before the new one.
296 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
298 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
299 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
301 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
302 pNew->next = pPrev->next;
303 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
304 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
305 pPrev->next = new;
309 /***********************************************************************
310 * LOCAL_RemoveBlock
312 * Remove a block from the heap.
313 * 'block' is the handle of the block arena; 'baseptr' points to
314 * the beginning of the data segment containing the heap.
316 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
318 LOCALARENA *pArena, *pTmp;
320 /* Remove the block from the free-list */
322 TRACE("\n");
323 pArena = ARENA_PTR( baseptr, block );
324 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
325 LOCAL_RemoveFreeBlock( baseptr, block );
327 /* If the previous block is free, expand its size */
329 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
330 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
331 pTmp->size += pArena->next - block;
333 /* Remove the block from the linked list */
335 pTmp->next = pArena->next;
336 pTmp = ARENA_PTR( baseptr, pArena->next );
337 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
341 /***********************************************************************
342 * LOCAL_PrintHeap
344 static void LOCAL_PrintHeap( HANDLE16 ds )
346 char *ptr;
347 LOCALHEAPINFO *pInfo;
348 WORD arena;
350 /* FIXME - the test should be done when calling the function!
351 plus is not clear that we should print this info
352 only when TRACE_ON is on! */
353 if(!TRACE_ON(local)) return;
355 ptr = MapSL( MAKESEGPTR( ds, 0 ));
356 pInfo = LOCAL_GetHeap( ds );
358 if (!pInfo)
360 ERR( "Local Heap corrupted! ds=%04x\n", ds );
361 return;
363 TRACE( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
364 ds, pInfo->first, pInfo->last, pInfo->items );
366 arena = pInfo->first;
367 for (;;)
369 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
370 TRACE( " %04x: prev=%04x next=%04x type=%d\n", arena,
371 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
372 if (arena == pInfo->first)
374 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
375 pArena->size, pArena->free_prev, pArena->free_next );
377 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
379 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
380 pArena->size, pArena->free_prev, pArena->free_next );
381 if (pArena->next == arena) break; /* last one */
382 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
384 TRACE( "*** arena->free_next->free_prev != arena\n" );
385 break;
388 if (pArena->next == arena)
390 TRACE( "*** last block is not marked free\n" );
391 break;
393 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
395 TRACE( "*** arena->next->prev != arena (%04x, %04x)\n",
396 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
397 break;
399 arena = pArena->next;
404 /***********************************************************************
405 * LocalInit (KERNEL.4)
407 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
409 char *ptr;
410 WORD heapInfoArena, freeArena, lastArena;
411 LOCALHEAPINFO *pHeapInfo;
412 LOCALARENA *pArena, *pFirstArena, *pLastArena;
413 BOOL16 ret = FALSE;
415 /* The initial layout of the heap is: */
416 /* - first arena (FIXED) */
417 /* - heap info structure (FIXED) */
418 /* - large free block (FREE) */
419 /* - last arena (FREE) */
421 TRACE("%04x %04x-%04x\n", selector, start, end);
422 if (!selector) selector = CURRENT_DS;
424 if (TRACE_ON(local))
426 /* If TRACE_ON(local) is set, the global heap blocks are */
427 /* cleared before use, so we can test for double initialization. */
428 if (LOCAL_GetHeap(selector))
430 ERR("Heap %04x initialized twice.\n", selector);
431 LOCAL_PrintHeap(selector);
435 if (start == 0)
437 /* start == 0 means: put the local heap at the end of the segment */
439 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
440 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
441 if ( end > 0xfffe ) end = 0xfffe;
442 start -= end;
443 end += start;
445 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
447 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
448 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
449 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
450 + sizeof(LOCALHEAPINFO) );
451 lastArena = (end - sizeof(LOCALARENA)) & ~3;
453 /* Make sure there's enough space. */
455 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
457 /* Initialise the first arena */
459 pFirstArena = ARENA_PTR( ptr, start );
460 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
461 pFirstArena->next = heapInfoArena;
462 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
463 pFirstArena->free_prev = start; /* this one */
464 pFirstArena->free_next = freeArena;
466 /* Initialise the arena of the heap info structure */
468 pArena = ARENA_PTR( ptr, heapInfoArena );
469 pArena->prev = start | LOCAL_ARENA_FIXED;
470 pArena->next = freeArena;
472 /* Initialise the heap info structure */
474 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
475 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
476 pHeapInfo->items = 4;
477 pHeapInfo->first = start;
478 pHeapInfo->last = lastArena;
479 pHeapInfo->htable = 0;
480 pHeapInfo->hdelta = 0x20;
481 pHeapInfo->extra = 0x200;
482 pHeapInfo->minsize = lastArena - freeArena;
483 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
485 /* Initialise the large free block */
487 pArena = ARENA_PTR( ptr, freeArena );
488 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
489 pArena->next = lastArena;
490 pArena->size = lastArena - freeArena;
491 pArena->free_prev = start;
492 pArena->free_next = lastArena;
494 /* Initialise the last block */
496 pLastArena = ARENA_PTR( ptr, lastArena );
497 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
498 pLastArena->next = lastArena; /* this one */
499 pLastArena->size = LALIGN(sizeof(LOCALARENA));
500 pLastArena->free_prev = freeArena;
501 pLastArena->free_next = lastArena; /* this one */
503 /* Store the local heap address in the instance data */
505 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
506 LOCAL_PrintHeap( selector );
507 ret = TRUE;
509 done:
510 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
511 return ret;
515 /***********************************************************************
516 * LOCAL_GrowHeap
518 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
520 HANDLE16 hseg;
521 LONG oldsize;
522 LONG end;
523 LOCALHEAPINFO *pHeapInfo;
524 WORD freeArena, lastArena;
525 LOCALARENA *pArena, *pLastArena;
526 char *ptr;
528 hseg = GlobalHandle16( ds );
529 /* maybe mem allocated by Virtual*() ? */
530 if (!hseg) return FALSE;
532 oldsize = GlobalSize16( hseg );
533 /* if nothing can be gained, return */
534 if (oldsize > 0xfff0) return FALSE;
535 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
536 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
537 pHeapInfo = LOCAL_GetHeap( ds );
538 if (pHeapInfo == NULL) {
539 ERR("Heap not found\n" );
540 return FALSE;
542 end = GlobalSize16( hseg );
543 lastArena = (end - sizeof(LOCALARENA)) & ~3;
545 /* Update the HeapInfo */
546 pHeapInfo->items++;
547 freeArena = pHeapInfo->last;
548 pHeapInfo->last = lastArena;
549 pHeapInfo->minsize += end - oldsize;
551 /* grow the old last block */
552 pArena = ARENA_PTR( ptr, freeArena );
553 pArena->size = lastArena - freeArena;
554 pArena->next = lastArena;
555 pArena->free_next = lastArena;
557 /* Initialise the new last block */
559 pLastArena = ARENA_PTR( ptr, lastArena );
560 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
561 pLastArena->next = lastArena; /* this one */
562 pLastArena->size = LALIGN(sizeof(LOCALARENA));
563 pLastArena->free_prev = freeArena;
564 pLastArena->free_next = lastArena; /* this one */
566 /* If block before freeArena is also free then merge them */
567 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
569 LOCAL_RemoveBlock(ptr, freeArena);
570 pHeapInfo->items--;
573 TRACE("Heap expanded\n" );
574 LOCAL_PrintHeap( ds );
575 return TRUE;
579 /***********************************************************************
580 * LOCAL_FreeArena
582 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
584 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
585 LOCALHEAPINFO *pInfo;
586 LOCALARENA *pArena, *pPrev;
588 TRACE("%04x ds=%04x\n", arena, ds );
589 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
591 pArena = ARENA_PTR( ptr, arena );
592 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
594 /* shouldn't happen */
595 ERR("Trying to free block %04x twice!\n",
596 arena );
597 LOCAL_PrintHeap( ds );
598 return arena;
601 /* Check if we can merge with the previous block */
603 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
604 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
606 arena = pArena->prev & ~3;
607 pArena = pPrev;
608 LOCAL_RemoveBlock( ptr, pPrev->next );
609 pInfo->items--;
611 else /* Make a new free block */
613 LOCAL_MakeBlockFree( ptr, arena );
616 /* Check if we can merge with the next block */
618 if ((pArena->next == pArena->free_next) &&
619 (pArena->next != pInfo->last))
621 LOCAL_RemoveBlock( ptr, pArena->next );
622 pInfo->items--;
624 return 0;
628 /***********************************************************************
629 * LOCAL_ShrinkArena
631 * Shrink an arena by creating a free block at its end if possible.
632 * 'size' includes the arena header, and must be aligned.
634 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
636 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
637 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
639 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
641 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
642 if (!pInfo) return;
643 LOCAL_AddBlock( ptr, arena, arena + size );
644 pInfo->items++;
645 LOCAL_FreeArena( ds, arena + size );
650 /***********************************************************************
651 * LOCAL_GrowArenaDownward
653 * Grow an arena downward by using the previous arena (must be free).
655 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
657 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
658 LOCALHEAPINFO *pInfo;
659 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
660 WORD prevArena = pArena->prev & ~3;
661 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
662 WORD offset, size;
663 char *p;
665 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
666 offset = pPrevArena->size;
667 size = pArena->next - arena - ARENA_HEADER_SIZE;
668 LOCAL_RemoveFreeBlock( ptr, prevArena );
669 LOCAL_RemoveBlock( ptr, arena );
670 pInfo->items--;
671 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
672 while (offset < size)
674 memcpy( p, p + offset, offset );
675 p += offset;
676 size -= offset;
678 if (size) memcpy( p, p + offset, size );
679 LOCAL_ShrinkArena( ds, prevArena, newsize );
684 /***********************************************************************
685 * LOCAL_GrowArenaUpward
687 * Grow an arena upward by using the next arena (must be free and big
688 * enough). Newsize includes the arena header and must be aligned.
690 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
692 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
693 LOCALHEAPINFO *pInfo;
694 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
695 WORD nextArena = pArena->next;
697 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
698 LOCAL_RemoveBlock( ptr, nextArena );
699 pInfo->items--;
700 LOCAL_ShrinkArena( ds, arena, newsize );
704 /***********************************************************************
705 * LOCAL_GetFreeSpace
707 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
709 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
710 LOCALHEAPINFO *pInfo;
711 LOCALARENA *pArena;
712 WORD arena;
713 WORD freespace = 0;
715 if (!(pInfo = LOCAL_GetHeap( ds )))
717 ERR("Local heap not found\n" );
718 LOCAL_PrintHeap(ds);
719 return 0;
721 arena = pInfo->first;
722 pArena = ARENA_PTR( ptr, arena );
723 while (arena != pArena->free_next)
725 arena = pArena->free_next;
726 pArena = ARENA_PTR( ptr, arena );
727 if (pArena->size >= freespace) freespace = pArena->size;
729 /* FIXME doesn't yet calculate space that would become free if everything
730 were discarded when countdiscard == 1 */
731 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
732 else freespace -= ARENA_HEADER_SIZE;
733 return freespace;
737 /***********************************************************************
738 * LOCAL_Compact
740 static UINT16 LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
742 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
743 LOCALHEAPINFO *pInfo;
744 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
745 WORD arena, movearena, finalarena, table;
746 WORD count, movesize, size;
747 WORD freespace;
748 LOCALHANDLEENTRY *pEntry;
750 if (!(pInfo = LOCAL_GetHeap( ds )))
752 ERR("Local heap not found\n" );
753 LOCAL_PrintHeap(ds);
754 return 0;
756 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
757 ds, minfree, flags);
758 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
759 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
761 TRACE("Returning %04x.\n", freespace);
762 return freespace;
764 TRACE("Compacting heap %04x.\n", ds);
765 table = pInfo->htable;
766 while(table)
768 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
769 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
771 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
773 /* OK we can move this one if we want */
774 TRACE("handle %04x (block %04x) can be moved.\n",
775 (WORD)((char *)pEntry - ptr), pEntry->addr);
776 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
777 pMoveArena = ARENA_PTR(ptr, movearena);
778 movesize = pMoveArena->next - movearena;
779 arena = pInfo->first;
780 pArena = ARENA_PTR(ptr, arena);
781 size = 0xffff;
782 finalarena = 0;
783 /* Try to find the smallest arena that will do, */
784 /* which is below us in memory */
785 for(;;)
787 arena = pArena->free_next;
788 pArena = ARENA_PTR(ptr, arena);
789 if(arena >= movearena)
790 break;
791 if(arena == pArena->free_next)
792 break;
793 if((pArena->size >= movesize) && (pArena->size < size))
795 size = pArena->size;
796 finalarena = arena;
799 if (finalarena) /* Actually got somewhere to move */
801 TRACE("Moving it to %04x.\n", finalarena);
802 pFinalArena = ARENA_PTR(ptr, finalarena);
803 size = pFinalArena->size;
804 LOCAL_RemoveFreeBlock(ptr, finalarena);
805 LOCAL_ShrinkArena( ds, finalarena, movesize );
806 /* Copy the arena to its new location */
807 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
808 (char *)pMoveArena + ARENA_HEADER_SIZE,
809 movesize - ARENA_HEADER_SIZE );
810 /* Free the old location */
811 LOCAL_FreeArena(ds, movearena);
812 call_notify_func(pInfo->notify, LN_MOVE,
813 (WORD)((char *)pEntry - ptr), pEntry->addr);
814 /* Update handle table entry */
815 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
817 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
818 == LOCAL_ARENA_FREE)
820 /* Previous arena is free (but < movesize) */
821 /* so we can 'slide' movearena down into it */
822 finalarena = pMoveArena->prev & ~3;
823 LOCAL_GrowArenaDownward( ds, movearena, movesize );
824 /* Update handle table entry */
825 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
829 table = *(WORD *)pEntry;
831 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
832 if(freespace >= minfree || (flags & LMEM_NODISCARD))
834 TRACE("Returning %04x.\n", freespace);
835 return freespace;
838 table = pInfo->htable;
839 while(table)
841 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
842 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
844 if(pEntry->addr && pEntry->lock == 0 &&
845 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
847 TRACE("Discarding handle %04x (block %04x).\n",
848 (char *)pEntry - ptr, pEntry->addr);
849 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
850 call_notify_func(pInfo->notify, LN_DISCARD, (char *)pEntry - ptr, pEntry->flags);
851 pEntry->addr = 0;
852 pEntry->flags = (LMEM_DISCARDED >> 8);
855 table = *(WORD *)pEntry;
857 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
861 /***********************************************************************
862 * LOCAL_FindFreeBlock
864 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
866 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
867 LOCALHEAPINFO *pInfo;
868 LOCALARENA *pArena;
869 WORD arena;
871 if (!(pInfo = LOCAL_GetHeap( ds )))
873 ERR("Local heap not found\n" );
874 LOCAL_PrintHeap(ds);
875 return 0;
878 arena = pInfo->first;
879 pArena = ARENA_PTR( ptr, arena );
880 for (;;) {
881 arena = pArena->free_next;
882 pArena = ARENA_PTR( ptr, arena );
883 if (arena == pArena->free_next) break;
884 if (pArena->size >= size) return arena;
886 TRACE("not enough space\n" );
887 LOCAL_PrintHeap(ds);
888 return 0;
892 /***********************************************************************
893 * get_heap_name
895 static const char *get_heap_name( WORD ds )
897 HINSTANCE16 inst = LoadLibrary16( "GDI" );
898 if (ds == GlobalHandleToSel16( inst ))
900 FreeLibrary16( inst );
901 return "GDI";
903 FreeLibrary16( inst );
904 inst = LoadLibrary16( "USER" );
905 if (ds == GlobalHandleToSel16( inst ))
907 FreeLibrary16( inst );
908 return "USER";
910 FreeLibrary16( inst );
911 return "local";
914 /***********************************************************************
915 * LOCAL_GetBlock
916 * The segment may get moved around in this function, so all callers
917 * should reset their pointer variables.
919 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
921 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
922 LOCALHEAPINFO *pInfo;
923 LOCALARENA *pArena;
924 WORD arena;
926 if (!(pInfo = LOCAL_GetHeap( ds )))
928 ERR("Local heap not found\n");
929 LOCAL_PrintHeap(ds);
930 return 0;
933 size += ARENA_HEADER_SIZE;
934 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
936 #if 0
937 notify_done:
938 #endif
939 /* Find a suitable free block */
940 arena = LOCAL_FindFreeBlock( ds, size );
941 if (arena == 0) {
942 /* no space: try to make some */
943 LOCAL_Compact( ds, size, flags );
944 arena = LOCAL_FindFreeBlock( ds, size );
946 if (arena == 0) {
947 /* still no space: try to grow the segment */
948 if (!(LOCAL_GrowHeap( ds )))
950 #if 0
951 /* FIXME: doesn't work correctly yet */
952 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds - 20, size)) /* FIXME: "size" correct ? (should indicate bytes needed) */
953 goto notify_done;
954 #endif
955 ERR( "not enough space in %s heap %04x for %d bytes\n",
956 get_heap_name(ds), ds, size );
957 return 0;
959 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
960 pInfo = LOCAL_GetHeap( ds );
961 arena = LOCAL_FindFreeBlock( ds, size );
963 if (arena == 0) {
964 ERR( "not enough space in %s heap %04x for %d bytes\n",
965 get_heap_name(ds), ds, size );
966 #if 0
967 /* FIXME: "size" correct ? (should indicate bytes needed) */
968 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds, size)) goto notify_done;
969 #endif
970 return 0;
973 /* Make a block out of the free arena */
974 pArena = ARENA_PTR( ptr, arena );
975 TRACE("size = %04x, arena %04x size %04x\n", size, arena, pArena->size );
976 LOCAL_RemoveFreeBlock( ptr, arena );
977 LOCAL_ShrinkArena( ds, arena, size );
979 if (flags & LMEM_ZEROINIT)
980 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
981 return arena + ARENA_HEADER_SIZE;
985 /***********************************************************************
986 * LOCAL_NewHTable
988 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
990 char *ptr;
991 LOCALHEAPINFO *pInfo;
992 LOCALHANDLEENTRY *pEntry;
993 HLOCAL16 handle;
994 int i;
996 TRACE("\n" );
997 if (!(pInfo = LOCAL_GetHeap( ds )))
999 ERR("Local heap not found\n");
1000 LOCAL_PrintHeap(ds);
1001 return FALSE;
1004 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
1005 + 2 * sizeof(WORD), LMEM_FIXED )))
1006 return FALSE;
1007 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
1008 ERR("ptr == NULL after GetBlock.\n");
1009 if (!(pInfo = LOCAL_GetHeap( ds )))
1010 ERR("pInfo == NULL after GetBlock.\n");
1012 /* Fill the entry table */
1014 *(WORD *)(ptr + handle) = pInfo->hdelta;
1015 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
1016 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
1017 pEntry->lock = pEntry->flags = 0xff;
1018 pEntry->addr = 0;
1020 *(WORD *)pEntry = pInfo->htable;
1021 pInfo->htable = handle;
1022 return TRUE;
1026 /***********************************************************************
1027 * LOCAL_GetNewHandleEntry
1029 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
1031 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1032 LOCALHEAPINFO *pInfo;
1033 LOCALHANDLEENTRY *pEntry = NULL;
1034 WORD table;
1036 if (!(pInfo = LOCAL_GetHeap( ds )))
1038 ERR("Local heap not found\n");
1039 LOCAL_PrintHeap(ds);
1040 return 0;
1043 /* Find a free slot in existing tables */
1045 table = pInfo->htable;
1046 while (table)
1048 WORD count = *(WORD *)(ptr + table);
1049 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1050 for (; count > 0; count--, pEntry++)
1051 if (pEntry->lock == 0xff) break;
1052 if (count) break;
1053 table = *(WORD *)pEntry;
1056 if (!table) /* We need to create a new table */
1058 if (!LOCAL_NewHTable( ds )) return 0;
1059 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1060 pInfo = LOCAL_GetHeap( ds );
1061 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1064 /* Now allocate this entry */
1066 pEntry->lock = 0;
1067 pEntry->flags = 0;
1068 TRACE("(%04x): %04x\n", ds, ((char *)pEntry - ptr) );
1069 return (HLOCAL16)((char *)pEntry - ptr);
1073 /***********************************************************************
1074 * LOCAL_FreeHandleEntry
1076 * Free a handle table entry.
1078 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1080 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1081 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1082 LOCALHEAPINFO *pInfo;
1083 WORD *pTable;
1084 WORD table, count, i;
1086 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1088 /* Find the table where this handle comes from */
1090 pTable = &pInfo->htable;
1091 while (*pTable)
1093 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1094 if ((handle >= *pTable + sizeof(WORD)) &&
1095 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1096 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1098 if (!*pTable)
1100 ERR("Invalid entry %04x\n", handle);
1101 LOCAL_PrintHeap( ds );
1102 return;
1105 /* Make the entry free */
1107 pEntry->addr = 0; /* just in case */
1108 pEntry->lock = 0xff;
1109 pEntry->flags = 0xff;
1110 /* Now check if all entries in this table are free */
1112 table = *pTable;
1113 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1114 count = *(WORD *)(ptr + table);
1115 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1117 /* Remove the table from the linked list and free it */
1119 TRACE("(%04x): freeing table %04x\n", ds, table);
1120 *pTable = *(WORD *)pEntry;
1121 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1125 /***********************************************************************
1126 * LocalFree (KERNEL.7)
1128 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1130 HANDLE16 ds = CURRENT_DS;
1131 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1133 TRACE("%04x ds=%04x\n", handle, ds );
1135 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1136 if (HANDLE_FIXED( handle ))
1138 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1139 else return handle; /* couldn't free it */
1141 else
1143 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1144 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1146 TRACE("real block at %04x\n", pEntry->addr );
1147 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1148 return handle; /* couldn't free it */
1150 LOCAL_FreeHandleEntry( ds, handle );
1151 return 0; /* OK */
1156 /***********************************************************************
1157 * LocalAlloc (KERNEL.5)
1159 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1161 HANDLE16 ds = CURRENT_DS;
1162 HLOCAL16 handle = 0;
1163 char *ptr;
1165 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1167 if(size > 0 && size <= 4) size = 5;
1168 if (flags & LMEM_MOVEABLE)
1170 LOCALHANDLEENTRY *plhe;
1171 HLOCAL16 hmem;
1173 if(size)
1175 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1176 goto exit;
1178 else /* We just need to allocate a discarded handle */
1179 hmem = 0;
1180 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1182 WARN("Couldn't get handle.\n");
1183 if(hmem)
1184 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1185 goto exit;
1187 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1188 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1189 plhe->lock = 0;
1190 if(hmem)
1192 plhe->addr = hmem + MOVEABLE_PREFIX;
1193 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1194 *(HLOCAL16 *)(ptr + hmem) = handle;
1196 else
1198 plhe->addr = 0;
1199 plhe->flags = LMEM_DISCARDED >> 8;
1202 else /* FIXED */
1204 if(size) handle = LOCAL_GetBlock( ds, size, flags );
1207 exit:
1208 CURRENT_STACK16->ecx = handle; /* must be returned in cx too */
1209 return handle;
1213 /***********************************************************************
1214 * LocalReAlloc (KERNEL.6)
1216 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1218 HANDLE16 ds = CURRENT_DS;
1219 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1220 LOCALHEAPINFO *pInfo;
1221 LOCALARENA *pArena, *pNext;
1222 LOCALHANDLEENTRY *pEntry = NULL;
1223 WORD arena, oldsize;
1224 HLOCAL16 hmem, blockhandle;
1225 LONG nextarena;
1227 if (!handle) return 0;
1228 if(HANDLE_MOVEABLE(handle) &&
1229 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1230 return 0;
1232 TRACE("%04x %d %04x ds=%04x\n", handle, size, flags, ds );
1233 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1235 if (HANDLE_FIXED( handle ))
1236 blockhandle = handle;
1237 else
1239 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1240 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1242 HLOCAL16 hl;
1243 if(pEntry->addr)
1244 WARN("Dicarded block has non-zero addr.\n");
1245 TRACE("ReAllocating discarded block\n");
1246 if(size <= 4) size = 5;
1247 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1248 return 0;
1249 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1250 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1251 pEntry->addr = hl + MOVEABLE_PREFIX;
1252 pEntry->flags = 0;
1253 pEntry->lock = 0;
1254 *(HLOCAL16 *)(ptr + hl) = handle;
1255 return handle;
1257 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1259 ERR("(%04x,%04x): invalid handle\n",
1260 ds, handle );
1261 return 0;
1263 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1264 ERR("Back ptr to handle is invalid\n");
1265 return 0;
1269 if (flags & LMEM_MODIFY)
1271 if (HANDLE_MOVEABLE(handle))
1273 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1274 pEntry->flags = (flags & 0x0f00) >> 8;
1275 TRACE("Changing flags to %x.\n", pEntry->flags);
1277 return handle;
1280 if (!size)
1282 if (flags & LMEM_MOVEABLE)
1284 if (HANDLE_FIXED(handle))
1286 TRACE("Freeing fixed block.\n");
1287 return LocalFree16( handle );
1289 else /* Moveable block */
1291 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1292 if (pEntry->lock == 0)
1294 /* discards moveable blocks */
1295 TRACE("Discarding block\n");
1296 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1297 pEntry->addr = 0;
1298 pEntry->flags = (LMEM_DISCARDED >> 8);
1299 return handle;
1302 return 0;
1304 else if(flags == 0)
1306 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1307 if (pEntry->lock == 0)
1309 /* Frees block */
1310 return LocalFree16( handle );
1313 return 0;
1316 arena = ARENA_HEADER( blockhandle );
1317 TRACE("arena is %04x\n", arena );
1318 pArena = ARENA_PTR( ptr, arena );
1320 if(size <= 4) size = 5;
1321 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1322 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1323 nextarena = LALIGN(blockhandle + size);
1325 /* Check for size reduction */
1327 if (nextarena <= pArena->next)
1329 TRACE("size reduction, making new free block\n");
1330 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1331 TRACE("returning %04x\n", handle );
1332 return handle;
1335 /* Check if the next block is free and large enough */
1337 pNext = ARENA_PTR( ptr, pArena->next );
1338 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1339 (nextarena <= pNext->next))
1341 TRACE("size increase, making new free block\n");
1342 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1343 if (flags & LMEM_ZEROINIT)
1345 char *oldend = (char *)pArena + ARENA_HEADER_SIZE + oldsize;
1346 char *newend = ptr + pArena->next;
1347 TRACE("Clearing memory from %p to %p (DS -> %p)\n", oldend, newend, ptr);
1348 memset(oldend, 0, newend - oldend);
1351 TRACE("returning %04x\n", handle );
1352 return handle;
1355 /* Now we have to allocate a new block, but not if (fixed block or locked
1356 block) and no LMEM_MOVEABLE */
1358 if (!(flags & LMEM_MOVEABLE))
1360 if (HANDLE_FIXED(handle))
1362 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1363 return 0;
1365 else
1367 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1369 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1370 return 0;
1375 hmem = LOCAL_GetBlock( ds, size, flags );
1376 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1377 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1378 { /* a compaction, which might in turn have */
1379 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1380 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1382 if (!hmem)
1384 /* Remove the block from the heap and try again */
1385 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1386 if (!buffer) return 0;
1387 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1388 LOCAL_FreeArena( ds, arena );
1389 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1391 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1393 ERR("Can't restore saved block\n" );
1394 HeapFree( GetProcessHeap(), 0, buffer );
1395 return 0;
1397 size = oldsize;
1399 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1400 memcpy( ptr + hmem, buffer, oldsize );
1401 HeapFree( GetProcessHeap(), 0, buffer );
1403 else
1405 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1406 LOCAL_FreeArena( ds, arena );
1408 if (HANDLE_MOVEABLE( handle ))
1410 TRACE("fixing handle\n");
1411 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1412 pEntry->addr = hmem + MOVEABLE_PREFIX;
1413 /* Back ptr should still be correct */
1414 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1415 ERR("back ptr is invalid.\n");
1416 hmem = handle;
1418 if (size == oldsize) hmem = 0; /* Realloc failed */
1419 TRACE("returning %04x\n", hmem );
1420 return hmem;
1424 /***********************************************************************
1425 * LOCAL_InternalLock
1427 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1429 HLOCAL16 old_handle = handle;
1431 if (HANDLE_MOVEABLE(handle))
1433 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1434 if (pEntry->flags == (LMEM_DISCARDED >> 8)) return 0;
1435 if (pEntry->lock < 0xfe) pEntry->lock++;
1436 handle = pEntry->addr;
1438 TRACE("%04x returning %04x\n", old_handle, handle );
1439 return handle;
1443 /***********************************************************************
1444 * LocalUnlock (KERNEL.9)
1446 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1448 HANDLE16 ds = CURRENT_DS;
1449 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1451 TRACE("%04x\n", handle );
1452 if (HANDLE_MOVEABLE(handle))
1454 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1455 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1456 /* For moveable block, return the new lock count */
1457 /* (see _Windows_Internals_ p. 197) */
1458 return --pEntry->lock;
1460 else return FALSE;
1464 /***********************************************************************
1465 * LocalSize (KERNEL.10)
1467 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1469 HANDLE16 ds = CURRENT_DS;
1470 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1471 LOCALARENA *pArena;
1473 TRACE("%04x ds=%04x\n", handle, ds );
1475 if (!handle) return 0;
1476 if (HANDLE_MOVEABLE( handle ))
1478 handle = *(WORD *)(ptr + handle);
1479 if (!handle) return 0;
1480 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1482 else
1483 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1485 return pArena->next - handle;
1489 /***********************************************************************
1490 * LocalFlags (KERNEL.12)
1492 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1494 HANDLE16 ds = CURRENT_DS;
1495 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1497 if (HANDLE_MOVEABLE(handle))
1499 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1500 TRACE("(%04x,%04x): returning %04x\n",
1501 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1502 return pEntry->lock | (pEntry->flags << 8);
1504 else
1506 TRACE("(%04x,%04x): returning 0\n",
1507 ds, handle );
1508 return 0;
1513 /***********************************************************************
1514 * LocalHeapSize (KERNEL.162)
1516 WORD WINAPI LocalHeapSize16(void)
1518 HANDLE16 ds = CURRENT_DS;
1519 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1520 return pInfo ? pInfo->last - pInfo->first : 0;
1524 /***********************************************************************
1525 * LocalCountFree (KERNEL.161)
1527 WORD WINAPI LocalCountFree16(void)
1529 HANDLE16 ds = CURRENT_DS;
1530 WORD arena, total;
1531 LOCALARENA *pArena;
1532 LOCALHEAPINFO *pInfo;
1533 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1535 if (!(pInfo = LOCAL_GetHeap( ds )))
1537 ERR("(%04x): Local heap not found\n", ds );
1538 LOCAL_PrintHeap( ds );
1539 return 0;
1542 total = 0;
1543 arena = pInfo->first;
1544 pArena = ARENA_PTR( ptr, arena );
1545 for (;;)
1547 arena = pArena->free_next;
1548 pArena = ARENA_PTR( ptr, arena );
1549 if (arena == pArena->free_next) break;
1550 total += pArena->size;
1552 TRACE("(%04x): returning %d\n", ds, total);
1553 return total;
1557 /***********************************************************************
1558 * LocalHandle (KERNEL.11)
1560 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1562 HANDLE16 ds = CURRENT_DS;
1563 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1564 LOCALHEAPINFO *pInfo;
1565 WORD table;
1567 if (!(pInfo = LOCAL_GetHeap( ds )))
1569 ERR("(%04x): Local heap not found\n", ds );
1570 LOCAL_PrintHeap( ds );
1571 return 0;
1574 /* Find the address in the entry tables */
1576 table = pInfo->htable;
1577 while (table)
1579 WORD count = *(WORD *)(ptr + table);
1580 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1581 for (; count > 0; count--, pEntry++)
1582 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1583 table = *(WORD *)pEntry;
1586 return (HLOCAL16)addr; /* Fixed block handle is addr */
1592 /***********************************************************************
1593 * LocalLock (KERNEL.8)
1595 * Note: only the offset part of the pointer is returned by the relay code.
1597 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1599 WORD ds = CURRENT_DS;
1600 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1601 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1605 /***********************************************************************
1606 * LocalCompact (KERNEL.13)
1608 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1610 TRACE("%04x\n", minfree );
1611 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1615 /***********************************************************************
1616 * LocalNotify (KERNEL.14)
1618 * Installs a callback function that is called for local memory events
1619 * Callback function prototype is
1620 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1621 * wMsg:
1622 * - LN_OUTOFMEM
1623 * NotifyFunc seems to be responsible for allocating some memory,
1624 * returns TRUE for success.
1625 * wArg = number of bytes needed additionally
1626 * - LN_MOVE
1627 * hMem = handle; wArg = old mem location
1628 * - LN_DISCARD
1629 * NotifyFunc seems to be strongly encouraged to return TRUE,
1630 * otherwise LogError() gets called.
1631 * hMem = handle; wArg = flags
1633 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1635 LOCALHEAPINFO *pInfo;
1636 FARPROC16 oldNotify;
1637 HANDLE16 ds = CURRENT_DS;
1639 if (!(pInfo = LOCAL_GetHeap( ds )))
1641 ERR("(%04x): Local heap not found\n", ds );
1642 LOCAL_PrintHeap( ds );
1643 return 0;
1645 TRACE("(%04x): %p\n", ds, func );
1646 FIXME("Half implemented\n");
1647 oldNotify = pInfo->notify;
1648 pInfo->notify = func;
1649 return oldNotify;
1653 /***********************************************************************
1654 * LocalShrink (KERNEL.121)
1656 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1658 TRACE("%04x %04x\n", handle, newsize );
1659 return 0;
1663 /***********************************************************************
1664 * GetHeapSpaces (KERNEL.138)
1666 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1668 NE_MODULE *pModule;
1669 WORD oldDS = CURRENT_DS;
1670 DWORD spaces;
1672 if (!(pModule = NE_GetPtr( module ))) return 0;
1673 CURRENT_DS = GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1)->hSeg);
1674 spaces = MAKELONG( LocalCountFree16(), LocalHeapSize16() );
1675 CURRENT_DS = oldDS;
1676 return spaces;
1680 /***********************************************************************
1681 * LocalHandleDelta (KERNEL.310)
1683 WORD WINAPI LocalHandleDelta16( WORD delta )
1685 LOCALHEAPINFO *pInfo;
1687 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1689 ERR("Local heap not found\n");
1690 LOCAL_PrintHeap( CURRENT_DS );
1691 return 0;
1693 if (delta) pInfo->hdelta = delta;
1694 TRACE("returning %04x\n", pInfo->hdelta);
1695 return pInfo->hdelta;
1699 /***********************************************************************
1700 * 32-bit local heap functions (Win95; undocumented)
1703 /***********************************************************************
1704 * K208 (KERNEL.208)
1706 HANDLE WINAPI Local32Init16( WORD segment, DWORD tableSize,
1707 DWORD heapSize, DWORD flags )
1709 DWORD totSize, segSize = 0;
1710 LPBYTE base;
1711 LOCAL32HEADER *header;
1712 HANDLE heap;
1713 WORD *selectorTable;
1714 WORD selectorEven, selectorOdd;
1715 int i, nrBlocks;
1717 /* Determine new heap size */
1719 if ( segment )
1721 if ( (segSize = GetSelectorLimit16( segment )) == 0 )
1722 return 0;
1723 else
1724 segSize++;
1727 if ( heapSize == (DWORD)-1 )
1728 heapSize = 1024*1024; /* FIXME */
1730 heapSize = (heapSize + 0xffff) & 0xffff0000;
1731 segSize = (segSize + 0x0fff) & 0xfffff000;
1732 totSize = segSize + HTABLE_SIZE + heapSize;
1735 /* Allocate memory and initialize heap */
1737 if ( !(base = VirtualAlloc( NULL, totSize, MEM_RESERVE, PAGE_READWRITE )) )
1738 return 0;
1740 if ( !VirtualAlloc( base, segSize + HTABLE_PAGESIZE,
1741 MEM_COMMIT, PAGE_READWRITE ) )
1743 VirtualFree( base, 0, MEM_RELEASE );
1744 return 0;
1747 if (!(heap = RtlCreateHeap( 0, base + segSize + HTABLE_SIZE, heapSize, 0x10000, NULL, NULL )))
1749 VirtualFree( base, 0, MEM_RELEASE );
1750 return 0;
1754 /* Set up header and handle table */
1756 header = (LOCAL32HEADER *)(base + segSize);
1757 header->base = base;
1758 header->limit = HTABLE_PAGESIZE-1;
1759 header->flags = 0;
1760 header->magic = LOCAL32_MAGIC;
1761 header->heap = heap;
1763 header->freeListFirst[0] = sizeof(LOCAL32HEADER);
1764 header->freeListLast[0] = HTABLE_PAGESIZE - 4;
1765 header->freeListSize[0] = (HTABLE_PAGESIZE - sizeof(LOCAL32HEADER)) / 4;
1767 for (i = header->freeListFirst[0]; i < header->freeListLast[0]; i += 4)
1768 *(DWORD *)((LPBYTE)header + i) = i+4;
1770 header->freeListFirst[1] = 0xffff;
1773 /* Set up selector table */
1775 nrBlocks = (totSize + 0x7fff) >> 15;
1776 selectorTable = HeapAlloc( header->heap, 0, nrBlocks * 2 );
1777 selectorEven = SELECTOR_AllocBlock( base, totSize, LDT_FLAGS_DATA );
1778 selectorOdd = SELECTOR_AllocBlock( base + 0x8000, totSize - 0x8000, LDT_FLAGS_DATA );
1779 if ( !selectorTable || !selectorEven || !selectorOdd )
1781 HeapFree( header->heap, 0, selectorTable );
1782 if ( selectorEven ) SELECTOR_FreeBlock( selectorEven );
1783 if ( selectorOdd ) SELECTOR_FreeBlock( selectorOdd );
1784 HeapDestroy( header->heap );
1785 VirtualFree( base, 0, MEM_RELEASE );
1786 return 0;
1789 header->selectorTableOffset = (LPBYTE)selectorTable - header->base;
1790 header->selectorTableSize = nrBlocks * 4; /* ??? Win95 does it this way! */
1791 header->selectorDelta = selectorEven - selectorOdd;
1792 header->segment = segment? segment : selectorEven;
1794 for (i = 0; i < nrBlocks; i++)
1795 selectorTable[i] = (i & 1)? selectorOdd + ((i >> 1) << __AHSHIFT)
1796 : selectorEven + ((i >> 1) << __AHSHIFT);
1798 /* Move old segment */
1800 if ( segment )
1802 /* FIXME: This is somewhat ugly and relies on implementation
1803 details about 16-bit global memory handles ... */
1805 LPBYTE oldBase = (LPBYTE)GetSelectorBase( segment );
1806 memcpy( base, oldBase, segSize );
1807 GLOBAL_MoveBlock( segment, base, totSize );
1808 HeapFree( GetProcessHeap(), 0, oldBase );
1811 return header;
1814 /***********************************************************************
1815 * Local32_SearchHandle
1817 static LPDWORD Local32_SearchHandle( LOCAL32HEADER *header, DWORD addr )
1819 LPDWORD handle;
1821 for ( handle = (LPDWORD)((LPBYTE)header + sizeof(LOCAL32HEADER));
1822 handle < (LPDWORD)((LPBYTE)header + header->limit);
1823 handle++)
1825 if (*handle == addr)
1826 return handle;
1829 return NULL;
1832 /***********************************************************************
1833 * Local32_ToHandle
1835 static VOID Local32_ToHandle( LOCAL32HEADER *header, INT16 type,
1836 DWORD addr, LPDWORD *handle, LPBYTE *ptr )
1838 *handle = NULL;
1839 *ptr = NULL;
1841 switch (type)
1843 case -2: /* 16:16 pointer, no handles */
1844 *ptr = MapSL( addr );
1845 *handle = (LPDWORD)*ptr;
1846 break;
1848 case -1: /* 32-bit offset, no handles */
1849 *ptr = header->base + addr;
1850 *handle = (LPDWORD)*ptr;
1851 break;
1853 case 0: /* handle */
1854 if ( addr >= sizeof(LOCAL32HEADER)
1855 && addr < header->limit && !(addr & 3)
1856 && *(LPDWORD)((LPBYTE)header + addr) >= HTABLE_SIZE )
1858 *handle = (LPDWORD)((LPBYTE)header + addr);
1859 *ptr = header->base + **handle;
1861 break;
1863 case 1: /* 16:16 pointer */
1864 *ptr = MapSL( addr );
1865 *handle = Local32_SearchHandle( header, *ptr - header->base );
1866 break;
1868 case 2: /* 32-bit offset */
1869 *ptr = header->base + addr;
1870 *handle = Local32_SearchHandle( header, *ptr - header->base );
1871 break;
1875 /***********************************************************************
1876 * Local32_FromHandle
1878 static VOID Local32_FromHandle( LOCAL32HEADER *header, INT16 type,
1879 DWORD *addr, LPDWORD handle, LPBYTE ptr )
1881 *addr = 0;
1882 switch (type)
1884 case -2: /* 16:16 pointer */
1885 case 1:
1887 WORD *selTable = (LPWORD)(header->base + header->selectorTableOffset);
1888 DWORD offset = ptr - header->base;
1889 *addr = MAKELONG( offset & 0x7fff, selTable[offset >> 15] );
1891 break;
1893 case -1: /* 32-bit offset */
1894 case 2:
1895 *addr = ptr - header->base;
1896 break;
1898 case 0: /* handle */
1899 *addr = (LPBYTE)handle - (LPBYTE)header;
1900 break;
1904 /***********************************************************************
1905 * K209 (KERNEL.209)
1907 DWORD WINAPI Local32Alloc16( HANDLE heap, DWORD size, INT16 type, DWORD flags )
1909 LOCAL32HEADER *header = heap;
1910 LPDWORD handle;
1911 LPBYTE ptr;
1912 DWORD addr;
1914 /* Allocate memory */
1915 ptr = HeapAlloc( header->heap,
1916 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0, size );
1917 if (!ptr) return 0;
1920 /* Allocate handle if requested */
1921 if (type >= 0)
1923 int page, i;
1925 /* Find first page of handle table with free slots */
1926 for (page = 0; page < HTABLE_NPAGES; page++)
1927 if (header->freeListFirst[page] != 0)
1928 break;
1929 if (page == HTABLE_NPAGES)
1931 WARN("Out of handles!\n" );
1932 HeapFree( header->heap, 0, ptr );
1933 return 0;
1936 /* If virgin page, initialize it */
1937 if (header->freeListFirst[page] == 0xffff)
1939 if ( !VirtualAlloc( (LPBYTE)header + (page << 12),
1940 0x1000, MEM_COMMIT, PAGE_READWRITE ) )
1942 WARN("Cannot grow handle table!\n" );
1943 HeapFree( header->heap, 0, ptr );
1944 return 0;
1947 header->limit += HTABLE_PAGESIZE;
1949 header->freeListFirst[page] = 0;
1950 header->freeListLast[page] = HTABLE_PAGESIZE - 4;
1951 header->freeListSize[page] = HTABLE_PAGESIZE / 4;
1953 for (i = 0; i < HTABLE_PAGESIZE; i += 4)
1954 *(DWORD *)((LPBYTE)header + i) = i+4;
1956 if (page < HTABLE_NPAGES-1)
1957 header->freeListFirst[page+1] = 0xffff;
1960 /* Allocate handle slot from page */
1961 handle = (LPDWORD)((LPBYTE)header + header->freeListFirst[page]);
1962 if (--header->freeListSize[page] == 0)
1963 header->freeListFirst[page] = header->freeListLast[page] = 0;
1964 else
1965 header->freeListFirst[page] = *handle;
1967 /* Store 32-bit offset in handle slot */
1968 *handle = ptr - header->base;
1970 else
1972 handle = (LPDWORD)ptr;
1973 header->flags |= 1;
1977 /* Convert handle to requested output type */
1978 Local32_FromHandle( header, type, &addr, handle, ptr );
1979 return addr;
1982 /***********************************************************************
1983 * K210 (KERNEL.210)
1985 DWORD WINAPI Local32ReAlloc16( HANDLE heap, DWORD addr, INT16 type,
1986 DWORD size, DWORD flags )
1988 LOCAL32HEADER *header = heap;
1989 LPDWORD handle;
1990 LPBYTE ptr;
1992 if (!addr)
1993 return Local32Alloc16( heap, size, type, flags );
1995 /* Retrieve handle and pointer */
1996 Local32_ToHandle( header, type, addr, &handle, &ptr );
1997 if (!handle) return FALSE;
1999 /* Reallocate memory block */
2000 ptr = HeapReAlloc( header->heap,
2001 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0,
2002 ptr, size );
2003 if (!ptr) return 0;
2005 /* Modify handle */
2006 if (type >= 0)
2007 *handle = ptr - header->base;
2008 else
2009 handle = (LPDWORD)ptr;
2011 /* Convert handle to requested output type */
2012 Local32_FromHandle( header, type, &addr, handle, ptr );
2013 return addr;
2016 /***********************************************************************
2017 * K211 (KERNEL.211)
2019 BOOL WINAPI Local32Free16( HANDLE heap, DWORD addr, INT16 type )
2021 LOCAL32HEADER *header = heap;
2022 LPDWORD handle;
2023 LPBYTE ptr;
2025 /* Retrieve handle and pointer */
2026 Local32_ToHandle( header, type, addr, &handle, &ptr );
2027 if (!handle) return FALSE;
2029 /* Free handle if necessary */
2030 if (type >= 0)
2032 int offset = (LPBYTE)handle - (LPBYTE)header;
2033 int page = offset >> 12;
2035 /* Return handle slot to page free list */
2036 if (header->freeListSize[page]++ == 0)
2037 header->freeListFirst[page] = header->freeListLast[page] = offset;
2038 else
2039 *(LPDWORD)((LPBYTE)header + header->freeListLast[page]) = offset,
2040 header->freeListLast[page] = offset;
2042 *handle = 0;
2044 /* Shrink handle table when possible */
2045 while (page > 0 && header->freeListSize[page] == HTABLE_PAGESIZE / 4)
2047 if ( VirtualFree( (LPBYTE)header +
2048 (header->limit & ~(HTABLE_PAGESIZE-1)),
2049 HTABLE_PAGESIZE, MEM_DECOMMIT ) )
2050 break;
2052 header->limit -= HTABLE_PAGESIZE;
2053 header->freeListFirst[page] = 0xffff;
2054 page--;
2058 /* Free memory */
2059 return HeapFree( header->heap, 0, ptr );
2062 /***********************************************************************
2063 * K213 (KERNEL.213)
2065 DWORD WINAPI Local32Translate16( HANDLE heap, DWORD addr, INT16 type1, INT16 type2 )
2067 LOCAL32HEADER *header = heap;
2068 LPDWORD handle;
2069 LPBYTE ptr;
2071 Local32_ToHandle( header, type1, addr, &handle, &ptr );
2072 if (!handle) return 0;
2074 Local32_FromHandle( header, type2, &addr, handle, ptr );
2075 return addr;
2078 /***********************************************************************
2079 * K214 (KERNEL.214)
2081 DWORD WINAPI Local32Size16( HANDLE heap, DWORD addr, INT16 type )
2083 LOCAL32HEADER *header = heap;
2084 LPDWORD handle;
2085 LPBYTE ptr;
2087 Local32_ToHandle( header, type, addr, &handle, &ptr );
2088 if (!handle) return 0;
2090 return HeapSize( header->heap, 0, ptr );
2093 /***********************************************************************
2094 * K215 (KERNEL.215)
2096 BOOL WINAPI Local32ValidHandle16( HANDLE heap, WORD addr )
2098 LOCAL32HEADER *header = heap;
2099 LPDWORD handle;
2100 LPBYTE ptr;
2102 Local32_ToHandle( header, 0, addr, &handle, &ptr );
2103 return handle != NULL;
2106 /***********************************************************************
2107 * K229 (KERNEL.229)
2109 WORD WINAPI Local32GetSegment16( HANDLE heap )
2111 LOCAL32HEADER *header = heap;
2112 return header->segment;
2115 /***********************************************************************
2116 * Local32_GetHeap
2118 static LOCAL32HEADER *Local32_GetHeap( HGLOBAL16 handle )
2120 WORD selector = GlobalHandleToSel16( handle );
2121 DWORD base = GetSelectorBase( selector );
2122 DWORD limit = GetSelectorLimit16( selector );
2124 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2125 it this way ... */
2127 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2128 return (LOCAL32HEADER *)base;
2130 base += 0x10000;
2131 limit -= 0x10000;
2133 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2134 return (LOCAL32HEADER *)base;
2136 return NULL;
2139 /***********************************************************************
2140 * Local32Info (KERNEL.444)
2142 BOOL16 WINAPI Local32Info16( LOCAL32INFO *pLocal32Info, HGLOBAL16 handle )
2144 PROCESS_HEAP_ENTRY entry;
2145 int i;
2147 LOCAL32HEADER *header = Local32_GetHeap( handle );
2148 if ( !header ) return FALSE;
2150 if ( !pLocal32Info || pLocal32Info->dwSize < sizeof(LOCAL32INFO) )
2151 return FALSE;
2153 pLocal32Info->dwMemReserved = 0;
2154 pLocal32Info->dwMemCommitted = 0;
2155 pLocal32Info->dwTotalFree = 0;
2156 pLocal32Info->dwLargestFreeBlock = 0;
2158 while (HeapWalk( header->heap, &entry ))
2160 if (entry.wFlags & PROCESS_HEAP_REGION)
2162 pLocal32Info->dwMemReserved += entry.Region.dwCommittedSize
2163 + entry.Region.dwUnCommittedSize;
2164 pLocal32Info->dwMemCommitted = entry.Region.dwCommittedSize;
2166 else if (!(entry.wFlags & PROCESS_HEAP_ENTRY_BUSY))
2168 DWORD size = entry.cbData + entry.cbOverhead;
2169 pLocal32Info->dwTotalFree += size;
2170 if (size > pLocal32Info->dwLargestFreeBlock) pLocal32Info->dwLargestFreeBlock = size;
2174 pLocal32Info->dwcFreeHandles = 0;
2175 for ( i = 0; i < HTABLE_NPAGES; i++ )
2177 if ( header->freeListFirst[i] == 0xffff ) break;
2178 pLocal32Info->dwcFreeHandles += header->freeListSize[i];
2180 pLocal32Info->dwcFreeHandles += (HTABLE_NPAGES - i) * HTABLE_PAGESIZE/4;
2182 return TRUE;
2185 /***********************************************************************
2186 * Local32First (KERNEL.445)
2188 BOOL16 WINAPI Local32First16( LOCAL32ENTRY *pLocal32Entry, HGLOBAL16 handle )
2190 FIXME("(%p, %04X): stub!\n", pLocal32Entry, handle );
2191 return FALSE;
2194 /***********************************************************************
2195 * Local32Next (KERNEL.446)
2197 BOOL16 WINAPI Local32Next16( LOCAL32ENTRY *pLocal32Entry )
2199 FIXME("(%p): stub!\n", pLocal32Entry );
2200 return FALSE;