winegstreamer: Move the "flip" field to struct wg_parser_stream.
[wine/zf.git] / dlls / krnl386.exe16 / local.c
blob672f1c22a1965188addb114fbaa3aeb60981f4c0
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 #define NONAMELESSUNION
31 #include <stdlib.h>
32 #include <string.h>
33 #include "wine/winbase16.h"
34 #include "wownt32.h"
35 #include "winternl.h"
36 #include "kernel16_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(local);
41 typedef struct
43 /* Arena header */
44 WORD prev; /* Previous arena | arena type */
45 WORD next; /* Next arena */
46 /* Start of the memory block or free-list info */
47 WORD size; /* Size of the free block */
48 WORD free_prev; /* Previous free block */
49 WORD free_next; /* Next free block */
50 } LOCALARENA;
52 #define ARENA_HEADER_SIZE 4
53 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
55 /* Arena types (stored in 'prev' field of the arena) */
56 #define LOCAL_ARENA_FREE 0
57 #define LOCAL_ARENA_FIXED 1
59 /* LocalNotify() msgs */
61 #define LN_OUTOFMEM 0
62 #define LN_MOVE 1
63 #define LN_DISCARD 2
65 /* Layout of a handle entry table
67 * WORD count of entries
68 * LOCALHANDLEENTRY[count] entries
69 * WORD near ptr to next table
71 typedef struct
73 WORD addr; /* Address of the MOVEABLE block */
74 BYTE flags; /* Flags for this block */
75 BYTE lock; /* Lock count */
76 } LOCALHANDLEENTRY;
79 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
80 * in case something actually relies on this.
82 * An unused handle has lock = flags = 0xff. In windows addr is that of next
83 * free handle, at the moment in wine we set it to 0.
85 * A discarded block's handle has lock = addr = 0 and flags = 0x40
86 * (LMEM_DISCARDED >> 8)
89 #define MOVEABLE_PREFIX sizeof(HLOCAL16)
91 #include "pshpack1.h"
93 typedef struct
95 WORD check; /* 00 Heap checking flag */
96 WORD freeze; /* 02 Heap frozen flag */
97 WORD items; /* 04 Count of items on the heap */
98 WORD first; /* 06 First item of the heap */
99 WORD pad1; /* 08 Always 0 */
100 WORD last; /* 0a Last item of the heap */
101 WORD pad2; /* 0c Always 0 */
102 BYTE ncompact; /* 0e Compactions counter */
103 BYTE dislevel; /* 0f Discard level */
104 DWORD distotal; /* 10 Total bytes discarded */
105 WORD htable; /* 14 Pointer to handle table */
106 WORD hfree; /* 16 Pointer to free handle table */
107 WORD hdelta; /* 18 Delta to expand the handle table */
108 WORD expand; /* 1a Pointer to expand function (unused) */
109 WORD pstat; /* 1c Pointer to status structure (unused) */
110 FARPROC16 notify; /* 1e Pointer to LocalNotify() function */
111 WORD lock; /* 22 Lock count for the heap */
112 WORD extra; /* 24 Extra bytes to allocate when expanding */
113 WORD minsize; /* 26 Minimum size of the heap */
114 WORD magic; /* 28 Magic number */
115 } LOCALHEAPINFO;
117 typedef struct
119 DWORD dwSize; /* 00 */
120 DWORD dwMemReserved; /* 04 */
121 DWORD dwMemCommitted; /* 08 */
122 DWORD dwTotalFree; /* 0C */
123 DWORD dwLargestFreeBlock; /* 10 */
124 DWORD dwcFreeHandles; /* 14 */
125 } LOCAL32INFO;
127 typedef struct
129 DWORD dwSize; /* 00 */
130 WORD hHandle; /* 04 */
131 DWORD dwAddress; /* 06 */
132 DWORD dwSizeBlock; /* 0A */
133 WORD wFlags; /* 0E */
134 WORD wType; /* 10 */
135 WORD hHeap; /* 12 */
136 WORD wHeapType; /* 14 */
137 DWORD dwNext; /* 16 */
138 DWORD dwNextAlt; /* 1A */
139 } LOCAL32ENTRY;
141 #include "poppack.h"
143 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
145 /* All local heap allocations are aligned on 4-byte boundaries */
146 #define LALIGN(word) (((word) + 3) & ~3)
148 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char *)(ptr)+(arena)))
149 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
150 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
151 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
153 /* determine whether the handle belongs to a fixed or a moveable block */
154 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
155 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
158 /* 32-bit heap definitions */
160 #define HTABLE_SIZE 0x10000
161 #define HTABLE_PAGESIZE 0x1000
162 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
164 #include "pshpack1.h"
165 typedef struct _LOCAL32HEADER
167 WORD freeListFirst[HTABLE_NPAGES];
168 WORD freeListSize[HTABLE_NPAGES];
169 WORD freeListLast[HTABLE_NPAGES];
171 DWORD selectorTableOffset;
172 WORD selectorTableSize;
173 WORD selectorDelta;
175 DWORD segment;
176 LPBYTE base;
178 DWORD limit;
179 DWORD flags;
181 DWORD magic;
182 HANDLE heap;
184 } LOCAL32HEADER;
185 #include "poppack.h"
187 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
190 static inline BOOL16 call_notify_func( FARPROC16 proc, WORD msg, HLOCAL16 handle, WORD arg )
192 DWORD ret;
193 WORD args[3];
195 if (!proc) return FALSE;
196 args[2] = msg;
197 args[1] = handle;
198 args[0] = arg;
199 WOWCallback16Ex( (DWORD)proc, WCB16_PASCAL, sizeof(args), args, &ret );
200 return LOWORD(ret);
204 /***********************************************************************
205 * LOCAL_GetHeap
207 * Return a pointer to the local heap, making sure it exists.
209 static LOCALHEAPINFO *LOCAL_GetHeap( HANDLE16 ds )
211 LOCALHEAPINFO *pInfo;
212 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( ds, 0 ));
213 TRACE("Heap at %p, %04x\n", ptr, (ptr != NULL ? ptr->heap : 0xFFFF));
214 if (!ptr || !ptr->heap) return NULL;
215 if (IsBadReadPtr16( (SEGPTR)MAKELONG(ptr->heap,ds), sizeof(LOCALHEAPINFO)))
217 WARN("Bad pointer\n");
218 return NULL;
220 pInfo = (LOCALHEAPINFO*)((char*)ptr + ptr->heap);
221 if (pInfo->magic != LOCAL_HEAP_MAGIC)
223 WARN("Bad magic\n");
224 return NULL;
226 return pInfo;
230 /***********************************************************************
231 * LOCAL_MakeBlockFree
233 * Make a block free, inserting it in the free-list.
234 * 'block' is the handle of the block arena; 'baseptr' points to
235 * the beginning of the data segment containing the heap.
237 static void LOCAL_MakeBlockFree( char *baseptr, WORD block )
239 LOCALARENA *pArena, *pNext;
240 WORD next;
242 /* Mark the block as free */
244 pArena = ARENA_PTR( baseptr, block );
245 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FREE;
246 pArena->size = pArena->next - block;
248 /* Find the next free block (last block is always free) */
250 next = pArena->next;
251 for (;;)
253 pNext = ARENA_PTR( baseptr, next );
254 if ((pNext->prev & 3) == LOCAL_ARENA_FREE) break;
255 next = pNext->next;
258 TRACE("%04x, next %04x\n", block, next );
259 /* Insert the free block in the free-list */
261 pArena->free_prev = pNext->free_prev;
262 pArena->free_next = next;
263 ARENA_PTR(baseptr,pNext->free_prev)->free_next = block;
264 pNext->free_prev = block;
268 /***********************************************************************
269 * LOCAL_RemoveFreeBlock
271 * Remove a block from the free-list.
272 * 'block' is the handle of the block arena; 'baseptr' points to
273 * the beginning of the data segment containing the heap.
275 static void LOCAL_RemoveFreeBlock( char *baseptr, WORD block )
277 /* Mark the block as fixed */
279 LOCALARENA *pArena = ARENA_PTR( baseptr, block );
280 pArena->prev = (pArena->prev & ~3) | LOCAL_ARENA_FIXED;
282 /* Remove it from the list */
284 ARENA_PTR(baseptr,pArena->free_prev)->free_next = pArena->free_next;
285 ARENA_PTR(baseptr,pArena->free_next)->free_prev = pArena->free_prev;
289 /***********************************************************************
290 * LOCAL_AddBlock
292 * Insert a new block in the heap.
293 * 'new' is the handle of the new block arena; 'baseptr' points to
294 * the beginning of the data segment containing the heap; 'prev' is
295 * the block before the new one.
297 static void LOCAL_AddBlock( char *baseptr, WORD prev, WORD new )
299 LOCALARENA *pPrev = ARENA_PTR( baseptr, prev );
300 LOCALARENA *pNew = ARENA_PTR( baseptr, new );
302 pNew->prev = (prev & ~3) | LOCAL_ARENA_FIXED;
303 pNew->next = pPrev->next;
304 ARENA_PTR(baseptr,pPrev->next)->prev &= 3;
305 ARENA_PTR(baseptr,pPrev->next)->prev |= new;
306 pPrev->next = new;
310 /***********************************************************************
311 * LOCAL_RemoveBlock
313 * Remove a block from the heap.
314 * 'block' is the handle of the block arena; 'baseptr' points to
315 * the beginning of the data segment containing the heap.
317 static void LOCAL_RemoveBlock( char *baseptr, WORD block )
319 LOCALARENA *pArena, *pTmp;
321 /* Remove the block from the free-list */
323 TRACE("\n");
324 pArena = ARENA_PTR( baseptr, block );
325 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
326 LOCAL_RemoveFreeBlock( baseptr, block );
328 /* If the previous block is free, expand its size */
330 pTmp = ARENA_PTR( baseptr, pArena->prev & ~3 );
331 if ((pTmp->prev & 3) == LOCAL_ARENA_FREE)
332 pTmp->size += pArena->next - block;
334 /* Remove the block from the linked list */
336 pTmp->next = pArena->next;
337 pTmp = ARENA_PTR( baseptr, pArena->next );
338 pTmp->prev = (pTmp->prev & 3) | (pArena->prev & ~3);
342 /***********************************************************************
343 * LOCAL_PrintHeap
345 static void LOCAL_PrintHeap( HANDLE16 ds )
347 char *ptr;
348 LOCALHEAPINFO *pInfo;
349 WORD arena;
351 /* FIXME - the test should be done when calling the function!
352 plus is not clear that we should print this info
353 only when TRACE_ON is on! */
354 if(!TRACE_ON(local)) return;
356 ptr = MapSL( MAKESEGPTR( ds, 0 ));
357 pInfo = LOCAL_GetHeap( ds );
359 if (!pInfo)
361 ERR( "Local Heap corrupted! ds=%04x\n", ds );
362 return;
364 TRACE( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
365 ds, pInfo->first, pInfo->last, pInfo->items );
367 arena = pInfo->first;
368 for (;;)
370 LOCALARENA *pArena = ARENA_PTR(ptr,arena);
371 TRACE( " %04x: prev=%04x next=%04x type=%d\n", arena,
372 pArena->prev & ~3, pArena->next, pArena->prev & 3 );
373 if (arena == pInfo->first)
375 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
376 pArena->size, pArena->free_prev, pArena->free_next );
378 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
380 TRACE( " size=%d free_prev=%04x free_next=%04x\n",
381 pArena->size, pArena->free_prev, pArena->free_next );
382 if (pArena->next == arena) break; /* last one */
383 if (ARENA_PTR(ptr,pArena->free_next)->free_prev != arena)
385 TRACE( "*** arena->free_next->free_prev != arena\n" );
386 break;
389 if (pArena->next == arena)
391 TRACE( "*** last block is not marked free\n" );
392 break;
394 if ((ARENA_PTR(ptr,pArena->next)->prev & ~3) != arena)
396 TRACE( "*** arena->next->prev != arena (%04x, %04x)\n",
397 pArena->next, ARENA_PTR(ptr,pArena->next)->prev);
398 break;
400 arena = pArena->next;
405 /***********************************************************************
406 * LocalInit (KERNEL.4)
408 BOOL16 WINAPI LocalInit16( HANDLE16 selector, WORD start, WORD end )
410 char *ptr;
411 WORD heapInfoArena, freeArena, lastArena;
412 LOCALHEAPINFO *pHeapInfo;
413 LOCALARENA *pArena, *pFirstArena, *pLastArena;
414 BOOL16 ret = FALSE;
416 /* The initial layout of the heap is: */
417 /* - first arena (FIXED) */
418 /* - heap info structure (FIXED) */
419 /* - large free block (FREE) */
420 /* - last arena (FREE) */
422 TRACE("%04x %04x-%04x\n", selector, start, end);
423 if (!selector) selector = CURRENT_DS;
425 if (TRACE_ON(local))
427 /* If TRACE_ON(local) is set, the global heap blocks are */
428 /* cleared before use, so we can test for double initialization. */
429 if (LOCAL_GetHeap(selector))
431 ERR("Heap %04x initialized twice.\n", selector);
432 LOCAL_PrintHeap(selector);
436 if (start == 0)
438 /* start == 0 means: put the local heap at the end of the segment */
440 DWORD size = GlobalSize16( GlobalHandle16( selector ) );
441 start = (WORD)(size > 0xffff ? 0xffff : size) - 1;
442 if ( end > 0xfffe ) end = 0xfffe;
443 start -= end;
444 end += start;
446 ptr = MapSL( MAKESEGPTR( selector, 0 ) );
448 start = LALIGN( max( start, sizeof(INSTANCEDATA) ) );
449 heapInfoArena = LALIGN(start + sizeof(LOCALARENA) );
450 freeArena = LALIGN( heapInfoArena + ARENA_HEADER_SIZE
451 + sizeof(LOCALHEAPINFO) );
452 lastArena = (end - sizeof(LOCALARENA)) & ~3;
454 /* Make sure there's enough space. */
456 if (freeArena + sizeof(LOCALARENA) >= lastArena) goto done;
458 /* Initialise the first arena */
460 pFirstArena = ARENA_PTR( ptr, start );
461 pFirstArena->prev = start | LOCAL_ARENA_FIXED;
462 pFirstArena->next = heapInfoArena;
463 pFirstArena->size = LALIGN(sizeof(LOCALARENA));
464 pFirstArena->free_prev = start; /* this one */
465 pFirstArena->free_next = freeArena;
467 /* Initialise the arena of the heap info structure */
469 pArena = ARENA_PTR( ptr, heapInfoArena );
470 pArena->prev = start | LOCAL_ARENA_FIXED;
471 pArena->next = freeArena;
473 /* Initialise the heap info structure */
475 pHeapInfo = (LOCALHEAPINFO *) (ptr + heapInfoArena + ARENA_HEADER_SIZE );
476 memset( pHeapInfo, 0, sizeof(LOCALHEAPINFO) );
477 pHeapInfo->items = 4;
478 pHeapInfo->first = start;
479 pHeapInfo->last = lastArena;
480 pHeapInfo->htable = 0;
481 pHeapInfo->hdelta = 0x20;
482 pHeapInfo->extra = 0x200;
483 pHeapInfo->minsize = lastArena - freeArena;
484 pHeapInfo->magic = LOCAL_HEAP_MAGIC;
486 /* Initialise the large free block */
488 pArena = ARENA_PTR( ptr, freeArena );
489 pArena->prev = heapInfoArena | LOCAL_ARENA_FREE;
490 pArena->next = lastArena;
491 pArena->size = lastArena - freeArena;
492 pArena->free_prev = start;
493 pArena->free_next = lastArena;
495 /* Initialise the last block */
497 pLastArena = ARENA_PTR( ptr, lastArena );
498 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
499 pLastArena->next = lastArena; /* this one */
500 pLastArena->size = LALIGN(sizeof(LOCALARENA));
501 pLastArena->free_prev = freeArena;
502 pLastArena->free_next = lastArena; /* this one */
504 /* Store the local heap address in the instance data */
506 ((INSTANCEDATA *)ptr)->heap = heapInfoArena + ARENA_HEADER_SIZE;
507 LOCAL_PrintHeap( selector );
508 ret = TRUE;
510 done:
511 CURRENT_STACK16->ecx = ret; /* must be returned in cx too */
512 return ret;
516 /***********************************************************************
517 * LOCAL_GrowHeap
519 static BOOL16 LOCAL_GrowHeap( HANDLE16 ds )
521 HANDLE16 hseg;
522 LONG oldsize;
523 LONG end;
524 LOCALHEAPINFO *pHeapInfo;
525 WORD freeArena, lastArena;
526 LOCALARENA *pArena, *pLastArena;
527 char *ptr;
529 hseg = GlobalHandle16( ds );
530 /* maybe mem allocated by Virtual*() ? */
531 if (!hseg) return FALSE;
533 oldsize = GlobalSize16( hseg );
534 /* if nothing can be gained, return */
535 if (oldsize > 0xfff0) return FALSE;
536 hseg = GlobalReAlloc16( hseg, 0x10000, GMEM_FIXED );
537 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
538 pHeapInfo = LOCAL_GetHeap( ds );
539 if (pHeapInfo == NULL) {
540 ERR("Heap not found\n" );
541 return FALSE;
543 end = GlobalSize16( hseg );
544 lastArena = (end - sizeof(LOCALARENA)) & ~3;
546 /* Update the HeapInfo */
547 pHeapInfo->items++;
548 freeArena = pHeapInfo->last;
549 pHeapInfo->last = lastArena;
550 pHeapInfo->minsize += end - oldsize;
552 /* grow the old last block */
553 pArena = ARENA_PTR( ptr, freeArena );
554 pArena->size = lastArena - freeArena;
555 pArena->next = lastArena;
556 pArena->free_next = lastArena;
558 /* Initialise the new last block */
560 pLastArena = ARENA_PTR( ptr, lastArena );
561 pLastArena->prev = freeArena | LOCAL_ARENA_FREE;
562 pLastArena->next = lastArena; /* this one */
563 pLastArena->size = LALIGN(sizeof(LOCALARENA));
564 pLastArena->free_prev = freeArena;
565 pLastArena->free_next = lastArena; /* this one */
567 /* If block before freeArena is also free then merge them */
568 if((ARENA_PTR(ptr, (pArena->prev & ~3))->prev & 3) == LOCAL_ARENA_FREE)
570 LOCAL_RemoveBlock(ptr, freeArena);
571 pHeapInfo->items--;
574 TRACE("Heap expanded\n" );
575 LOCAL_PrintHeap( ds );
576 return TRUE;
580 /***********************************************************************
581 * LOCAL_FreeArena
583 static HLOCAL16 LOCAL_FreeArena( WORD ds, WORD arena )
585 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
586 LOCALHEAPINFO *pInfo;
587 LOCALARENA *pArena, *pPrev;
589 TRACE("%04x ds=%04x\n", arena, ds );
590 if (!(pInfo = LOCAL_GetHeap( ds ))) return arena;
592 pArena = ARENA_PTR( ptr, arena );
593 if ((pArena->prev & 3) == LOCAL_ARENA_FREE)
595 /* shouldn't happen */
596 ERR("Trying to free block %04x twice!\n",
597 arena );
598 LOCAL_PrintHeap( ds );
599 return arena;
602 /* Check if we can merge with the previous block */
604 pPrev = ARENA_PTR( ptr, pArena->prev & ~3 );
605 if ((pPrev->prev & 3) == LOCAL_ARENA_FREE)
607 arena = pArena->prev & ~3;
608 pArena = pPrev;
609 LOCAL_RemoveBlock( ptr, pPrev->next );
610 pInfo->items--;
612 else /* Make a new free block */
614 LOCAL_MakeBlockFree( ptr, arena );
617 /* Check if we can merge with the next block */
619 if ((pArena->next == pArena->free_next) &&
620 (pArena->next != pInfo->last))
622 LOCAL_RemoveBlock( ptr, pArena->next );
623 pInfo->items--;
625 return 0;
629 /***********************************************************************
630 * LOCAL_ShrinkArena
632 * Shrink an arena by creating a free block at its end if possible.
633 * 'size' includes the arena header, and must be aligned.
635 static void LOCAL_ShrinkArena( WORD ds, WORD arena, WORD size )
637 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
638 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
640 if (arena + size + LALIGN(sizeof(LOCALARENA)) < pArena->next)
642 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
643 if (!pInfo) return;
644 LOCAL_AddBlock( ptr, arena, arena + size );
645 pInfo->items++;
646 LOCAL_FreeArena( ds, arena + size );
651 /***********************************************************************
652 * LOCAL_GrowArenaDownward
654 * Grow an arena downward by using the previous arena (must be free).
656 static void LOCAL_GrowArenaDownward( WORD ds, WORD arena, WORD newsize )
658 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
659 LOCALHEAPINFO *pInfo;
660 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
661 WORD prevArena = pArena->prev & ~3;
662 LOCALARENA *pPrevArena = ARENA_PTR( ptr, prevArena );
663 WORD offset, size;
664 char *p;
666 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
667 offset = pPrevArena->size;
668 size = pArena->next - arena - ARENA_HEADER_SIZE;
669 LOCAL_RemoveFreeBlock( ptr, prevArena );
670 LOCAL_RemoveBlock( ptr, arena );
671 pInfo->items--;
672 p = (char *)pPrevArena + ARENA_HEADER_SIZE;
673 while (offset < size)
675 memcpy( p, p + offset, offset );
676 p += offset;
677 size -= offset;
679 if (size) memcpy( p, p + offset, size );
680 LOCAL_ShrinkArena( ds, prevArena, newsize );
685 /***********************************************************************
686 * LOCAL_GrowArenaUpward
688 * Grow an arena upward by using the next arena (must be free and big
689 * enough). Newsize includes the arena header and must be aligned.
691 static void LOCAL_GrowArenaUpward( WORD ds, WORD arena, WORD newsize )
693 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
694 LOCALHEAPINFO *pInfo;
695 LOCALARENA *pArena = ARENA_PTR( ptr, arena );
696 WORD nextArena = pArena->next;
698 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
699 LOCAL_RemoveBlock( ptr, nextArena );
700 pInfo->items--;
701 LOCAL_ShrinkArena( ds, arena, newsize );
705 /***********************************************************************
706 * LOCAL_GetFreeSpace
708 static WORD LOCAL_GetFreeSpace(WORD ds, WORD countdiscard)
710 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
711 LOCALHEAPINFO *pInfo;
712 LOCALARENA *pArena;
713 WORD arena;
714 WORD freespace = 0;
716 if (!(pInfo = LOCAL_GetHeap( ds )))
718 ERR("Local heap not found\n" );
719 LOCAL_PrintHeap(ds);
720 return 0;
722 arena = pInfo->first;
723 pArena = ARENA_PTR( ptr, arena );
724 while (arena != pArena->free_next)
726 arena = pArena->free_next;
727 pArena = ARENA_PTR( ptr, arena );
728 if (pArena->size >= freespace) freespace = pArena->size;
730 /* FIXME doesn't yet calculate space that would become free if everything
731 were discarded when countdiscard == 1 */
732 if (freespace < ARENA_HEADER_SIZE) freespace = 0;
733 else freespace -= ARENA_HEADER_SIZE;
734 return freespace;
738 /***********************************************************************
739 * LOCAL_Compact
741 static UINT16 LOCAL_Compact( HANDLE16 ds, UINT16 minfree, UINT16 flags )
743 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
744 LOCALHEAPINFO *pInfo;
745 LOCALARENA *pArena, *pMoveArena, *pFinalArena;
746 WORD arena, movearena, finalarena, table;
747 WORD count, movesize, size;
748 WORD freespace;
749 LOCALHANDLEENTRY *pEntry;
751 if (!(pInfo = LOCAL_GetHeap( ds )))
753 ERR("Local heap not found\n" );
754 LOCAL_PrintHeap(ds);
755 return 0;
757 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
758 ds, minfree, flags);
759 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
760 if(freespace >= minfree || (flags & LMEM_NOCOMPACT))
762 TRACE("Returning %04x.\n", freespace);
763 return freespace;
765 TRACE("Compacting heap %04x.\n", ds);
766 table = pInfo->htable;
767 while(table)
769 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
770 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
772 if((pEntry->lock == 0) && (pEntry->flags != (LMEM_DISCARDED >> 8)))
774 /* OK we can move this one if we want */
775 TRACE("handle %04x (block %04x) can be moved.\n",
776 (WORD)((char *)pEntry - ptr), pEntry->addr);
777 movearena = ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX);
778 pMoveArena = ARENA_PTR(ptr, movearena);
779 movesize = pMoveArena->next - movearena;
780 arena = pInfo->first;
781 pArena = ARENA_PTR(ptr, arena);
782 size = 0xffff;
783 finalarena = 0;
784 /* Try to find the smallest arena that will do, */
785 /* which is below us in memory */
786 for(;;)
788 arena = pArena->free_next;
789 pArena = ARENA_PTR(ptr, arena);
790 if(arena >= movearena)
791 break;
792 if(arena == pArena->free_next)
793 break;
794 if((pArena->size >= movesize) && (pArena->size < size))
796 size = pArena->size;
797 finalarena = arena;
800 if (finalarena) /* Actually got somewhere to move */
802 TRACE("Moving it to %04x.\n", finalarena);
803 pFinalArena = ARENA_PTR(ptr, finalarena);
804 size = pFinalArena->size;
805 LOCAL_RemoveFreeBlock(ptr, finalarena);
806 LOCAL_ShrinkArena( ds, finalarena, movesize );
807 /* Copy the arena to its new location */
808 memcpy((char *)pFinalArena + ARENA_HEADER_SIZE,
809 (char *)pMoveArena + ARENA_HEADER_SIZE,
810 movesize - ARENA_HEADER_SIZE );
811 /* Free the old location */
812 LOCAL_FreeArena(ds, movearena);
813 call_notify_func(pInfo->notify, LN_MOVE,
814 (WORD)((char *)pEntry - ptr), pEntry->addr);
815 /* Update handle table entry */
816 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
818 else if((ARENA_PTR(ptr, pMoveArena->prev & ~3)->prev & 3)
819 == LOCAL_ARENA_FREE)
821 /* Previous arena is free (but < movesize) */
822 /* so we can 'slide' movearena down into it */
823 finalarena = pMoveArena->prev & ~3;
824 LOCAL_GrowArenaDownward( ds, movearena, movesize );
825 /* Update handle table entry */
826 pEntry->addr = finalarena + ARENA_HEADER_SIZE + MOVEABLE_PREFIX;
830 table = *(WORD *)pEntry;
832 freespace = LOCAL_GetFreeSpace(ds, minfree ? 0 : 1);
833 if(freespace >= minfree || (flags & LMEM_NODISCARD))
835 TRACE("Returning %04x.\n", freespace);
836 return freespace;
839 table = pInfo->htable;
840 while(table)
842 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
843 for(count = *(WORD *)(ptr + table); count > 0; count--, pEntry++)
845 if(pEntry->addr && pEntry->lock == 0 &&
846 (pEntry->flags & (LMEM_DISCARDABLE >> 8)))
848 TRACE("Discarding handle %04x (block %04x).\n",
849 (char *)pEntry - ptr, pEntry->addr);
850 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
851 call_notify_func(pInfo->notify, LN_DISCARD, (char *)pEntry - ptr, pEntry->flags);
852 pEntry->addr = 0;
853 pEntry->flags = (LMEM_DISCARDED >> 8);
856 table = *(WORD *)pEntry;
858 return LOCAL_Compact(ds, 0xffff, LMEM_NODISCARD);
862 /***********************************************************************
863 * LOCAL_FindFreeBlock
865 static HLOCAL16 LOCAL_FindFreeBlock( HANDLE16 ds, WORD size )
867 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
868 LOCALHEAPINFO *pInfo;
869 LOCALARENA *pArena;
870 WORD arena;
872 if (!(pInfo = LOCAL_GetHeap( ds )))
874 ERR("Local heap not found\n" );
875 LOCAL_PrintHeap(ds);
876 return 0;
879 arena = pInfo->first;
880 pArena = ARENA_PTR( ptr, arena );
881 for (;;) {
882 arena = pArena->free_next;
883 pArena = ARENA_PTR( ptr, arena );
884 if (arena == pArena->free_next) break;
885 if (pArena->size >= size) return arena;
887 TRACE("not enough space\n" );
888 LOCAL_PrintHeap(ds);
889 return 0;
893 /***********************************************************************
894 * get_heap_name
896 static const char *get_heap_name( WORD ds )
898 HINSTANCE16 inst = LoadLibrary16( "GDI" );
899 if (ds == GlobalHandleToSel16( inst ))
901 FreeLibrary16( inst );
902 return "GDI";
904 FreeLibrary16( inst );
905 inst = LoadLibrary16( "USER" );
906 if (ds == GlobalHandleToSel16( inst ))
908 FreeLibrary16( inst );
909 return "USER";
911 FreeLibrary16( inst );
912 return "local";
915 /***********************************************************************
916 * LOCAL_GetBlock
917 * The segment may get moved around in this function, so all callers
918 * should reset their pointer variables.
920 static HLOCAL16 LOCAL_GetBlock( HANDLE16 ds, WORD size, WORD flags )
922 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
923 LOCALHEAPINFO *pInfo;
924 LOCALARENA *pArena;
925 WORD arena;
927 if (!(pInfo = LOCAL_GetHeap( ds )))
929 ERR("Local heap not found\n");
930 LOCAL_PrintHeap(ds);
931 return 0;
934 size += ARENA_HEADER_SIZE;
935 size = LALIGN( max( size, sizeof(LOCALARENA) ) );
937 #if 0
938 notify_done:
939 #endif
940 /* Find a suitable free block */
941 arena = LOCAL_FindFreeBlock( ds, size );
942 if (arena == 0) {
943 /* no space: try to make some */
944 LOCAL_Compact( ds, size, flags );
945 arena = LOCAL_FindFreeBlock( ds, size );
947 if (arena == 0) {
948 /* still no space: try to grow the segment */
949 if (!(LOCAL_GrowHeap( ds )))
951 #if 0
952 /* FIXME: doesn't work correctly yet */
953 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds - 20, size)) /* FIXME: "size" correct ? (should indicate bytes needed) */
954 goto notify_done;
955 #endif
956 ERR( "not enough space in %s heap %04x for %d bytes\n",
957 get_heap_name(ds), ds, size );
958 return 0;
960 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
961 pInfo = LOCAL_GetHeap( ds );
962 arena = LOCAL_FindFreeBlock( ds, size );
964 if (arena == 0) {
965 ERR( "not enough space in %s heap %04x for %d bytes\n",
966 get_heap_name(ds), ds, size );
967 #if 0
968 /* FIXME: "size" correct ? (should indicate bytes needed) */
969 if (call_notify_func(pInfo->notify, LN_OUTOFMEM, ds, size)) goto notify_done;
970 #endif
971 return 0;
974 /* Make a block out of the free arena */
975 pArena = ARENA_PTR( ptr, arena );
976 TRACE("size = %04x, arena %04x size %04x\n", size, arena, pArena->size );
977 LOCAL_RemoveFreeBlock( ptr, arena );
978 LOCAL_ShrinkArena( ds, arena, size );
980 if (flags & LMEM_ZEROINIT)
981 memset((char *)pArena + ARENA_HEADER_SIZE, 0, size-ARENA_HEADER_SIZE);
982 return arena + ARENA_HEADER_SIZE;
986 /***********************************************************************
987 * LOCAL_NewHTable
989 static BOOL16 LOCAL_NewHTable( HANDLE16 ds )
991 char *ptr;
992 LOCALHEAPINFO *pInfo;
993 LOCALHANDLEENTRY *pEntry;
994 HLOCAL16 handle;
995 int i;
997 TRACE("\n" );
998 if (!(pInfo = LOCAL_GetHeap( ds )))
1000 ERR("Local heap not found\n");
1001 LOCAL_PrintHeap(ds);
1002 return FALSE;
1005 if (!(handle = LOCAL_GetBlock( ds, pInfo->hdelta * sizeof(LOCALHANDLEENTRY)
1006 + 2 * sizeof(WORD), LMEM_FIXED )))
1007 return FALSE;
1008 if (!(ptr = MapSL( MAKESEGPTR( ds, 0 ) )))
1009 ERR("ptr == NULL after GetBlock.\n");
1010 if (!(pInfo = LOCAL_GetHeap( ds )))
1011 ERR("pInfo == NULL after GetBlock.\n");
1013 /* Fill the entry table */
1015 *(WORD *)(ptr + handle) = pInfo->hdelta;
1016 pEntry = (LOCALHANDLEENTRY *)(ptr + handle + sizeof(WORD));
1017 for (i = pInfo->hdelta; i > 0; i--, pEntry++) {
1018 pEntry->lock = pEntry->flags = 0xff;
1019 pEntry->addr = 0;
1021 *(WORD *)pEntry = pInfo->htable;
1022 pInfo->htable = handle;
1023 return TRUE;
1027 /***********************************************************************
1028 * LOCAL_GetNewHandleEntry
1030 static HLOCAL16 LOCAL_GetNewHandleEntry( HANDLE16 ds )
1032 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1033 LOCALHEAPINFO *pInfo;
1034 LOCALHANDLEENTRY *pEntry = NULL;
1035 WORD table;
1037 if (!(pInfo = LOCAL_GetHeap( ds )))
1039 ERR("Local heap not found\n");
1040 LOCAL_PrintHeap(ds);
1041 return 0;
1044 /* Find a free slot in existing tables */
1046 table = pInfo->htable;
1047 while (table)
1049 WORD count = *(WORD *)(ptr + table);
1050 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1051 for (; count > 0; count--, pEntry++)
1052 if (pEntry->lock == 0xff) break;
1053 if (count) break;
1054 table = *(WORD *)pEntry;
1057 if (!table) /* We need to create a new table */
1059 if (!LOCAL_NewHTable( ds )) return 0;
1060 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1061 pInfo = LOCAL_GetHeap( ds );
1062 pEntry = (LOCALHANDLEENTRY *)(ptr + pInfo->htable + sizeof(WORD));
1065 /* Now allocate this entry */
1067 pEntry->lock = 0;
1068 pEntry->flags = 0;
1069 TRACE("(%04x): %04x\n", ds, ((char *)pEntry - ptr) );
1070 return (HLOCAL16)((char *)pEntry - ptr);
1074 /***********************************************************************
1075 * LOCAL_FreeHandleEntry
1077 * Free a handle table entry.
1079 static void LOCAL_FreeHandleEntry( HANDLE16 ds, HLOCAL16 handle )
1081 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1082 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1083 LOCALHEAPINFO *pInfo;
1084 WORD *pTable;
1085 WORD table, count, i;
1087 if (!(pInfo = LOCAL_GetHeap( ds ))) return;
1089 /* Find the table where this handle comes from */
1091 pTable = &pInfo->htable;
1092 while (*pTable)
1094 WORD size = (*(WORD *)(ptr + *pTable)) * sizeof(LOCALHANDLEENTRY);
1095 if ((handle >= *pTable + sizeof(WORD)) &&
1096 (handle < *pTable + sizeof(WORD) + size)) break; /* Found it */
1097 pTable = (WORD *)(ptr + *pTable + sizeof(WORD) + size);
1099 if (!*pTable)
1101 ERR("Invalid entry %04x\n", handle);
1102 LOCAL_PrintHeap( ds );
1103 return;
1106 /* Make the entry free */
1108 pEntry->addr = 0; /* just in case */
1109 pEntry->lock = 0xff;
1110 pEntry->flags = 0xff;
1111 /* Now check if all entries in this table are free */
1113 table = *pTable;
1114 pEntry = (LOCALHANDLEENTRY *)(ptr + table + sizeof(WORD));
1115 count = *(WORD *)(ptr + table);
1116 for (i = count; i > 0; i--, pEntry++) if (pEntry->lock != 0xff) return;
1118 /* Remove the table from the linked list and free it */
1120 TRACE("(%04x): freeing table %04x\n", ds, table);
1121 *pTable = *(WORD *)pEntry;
1122 LOCAL_FreeArena( ds, ARENA_HEADER( table ) );
1126 /***********************************************************************
1127 * LocalFree (KERNEL.7)
1129 HLOCAL16 WINAPI LocalFree16( HLOCAL16 handle )
1131 HANDLE16 ds = CURRENT_DS;
1132 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1134 TRACE("%04x ds=%04x\n", handle, ds );
1136 if (!handle) { WARN("Handle is 0.\n" ); return 0; }
1137 if (HANDLE_FIXED( handle ))
1139 if (!LOCAL_FreeArena( ds, ARENA_HEADER( handle ) )) return 0; /* OK */
1140 else return handle; /* couldn't free it */
1142 else
1144 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1145 if (pEntry->flags != (LMEM_DISCARDED >> 8))
1147 TRACE("real block at %04x\n", pEntry->addr );
1148 if (LOCAL_FreeArena( ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX) ))
1149 return handle; /* couldn't free it */
1151 LOCAL_FreeHandleEntry( ds, handle );
1152 return 0; /* OK */
1157 /***********************************************************************
1158 * LocalAlloc (KERNEL.5)
1160 HLOCAL16 WINAPI LocalAlloc16( UINT16 flags, WORD size )
1162 HANDLE16 ds = CURRENT_DS;
1163 HLOCAL16 handle = 0;
1164 char *ptr;
1166 TRACE("%04x %d ds=%04x\n", flags, size, ds );
1168 if(size > 0 && size <= 4) size = 5;
1169 if (flags & LMEM_MOVEABLE)
1171 LOCALHANDLEENTRY *plhe;
1172 HLOCAL16 hmem;
1174 if(size)
1176 if (!(hmem = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags )))
1177 goto exit;
1179 else /* We just need to allocate a discarded handle */
1180 hmem = 0;
1181 if (!(handle = LOCAL_GetNewHandleEntry( ds )))
1183 WARN("Couldn't get handle.\n");
1184 if(hmem)
1185 LOCAL_FreeArena( ds, ARENA_HEADER(hmem) );
1186 goto exit;
1188 ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1189 plhe = (LOCALHANDLEENTRY *)(ptr + handle);
1190 plhe->lock = 0;
1191 if(hmem)
1193 plhe->addr = hmem + MOVEABLE_PREFIX;
1194 plhe->flags = (BYTE)((flags & 0x0f00) >> 8);
1195 *(HLOCAL16 *)(ptr + hmem) = handle;
1197 else
1199 plhe->addr = 0;
1200 plhe->flags = LMEM_DISCARDED >> 8;
1203 else /* FIXED */
1205 if(size) handle = LOCAL_GetBlock( ds, size, flags );
1208 exit:
1209 CURRENT_STACK16->ecx = handle; /* must be returned in cx too */
1210 return handle;
1214 /***********************************************************************
1215 * LocalReAlloc (KERNEL.6)
1217 HLOCAL16 WINAPI LocalReAlloc16( HLOCAL16 handle, WORD size, UINT16 flags )
1219 HANDLE16 ds = CURRENT_DS;
1220 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1221 LOCALHEAPINFO *pInfo;
1222 LOCALARENA *pArena, *pNext;
1223 LOCALHANDLEENTRY *pEntry = NULL;
1224 WORD arena, oldsize;
1225 HLOCAL16 hmem, blockhandle;
1226 LONG nextarena;
1228 if (!handle) return 0;
1229 if(HANDLE_MOVEABLE(handle) &&
1230 ((LOCALHANDLEENTRY *)(ptr + handle))->lock == 0xff) /* An unused handle */
1231 return 0;
1233 TRACE("%04x %d %04x ds=%04x\n", handle, size, flags, ds );
1234 if (!(pInfo = LOCAL_GetHeap( ds ))) return 0;
1236 if (HANDLE_FIXED( handle ))
1237 blockhandle = handle;
1238 else
1240 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1241 if(pEntry->flags == (LMEM_DISCARDED >> 8))
1243 HLOCAL16 hl;
1244 if(pEntry->addr)
1245 WARN("Dicarded block has non-zero addr.\n");
1246 TRACE("ReAllocating discarded block\n");
1247 if(size <= 4) size = 5;
1248 if (!(hl = LOCAL_GetBlock( ds, size + MOVEABLE_PREFIX, flags)))
1249 return 0;
1250 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1251 pEntry = (LOCALHANDLEENTRY *) (ptr + handle);
1252 pEntry->addr = hl + MOVEABLE_PREFIX;
1253 pEntry->flags = 0;
1254 pEntry->lock = 0;
1255 *(HLOCAL16 *)(ptr + hl) = handle;
1256 return handle;
1258 if (((blockhandle = pEntry->addr - MOVEABLE_PREFIX) & 3) != 0)
1260 ERR("(%04x,%04x): invalid handle\n",
1261 ds, handle );
1262 return 0;
1264 if (*(HLOCAL16 *)(ptr + blockhandle) != handle) {
1265 ERR("Back ptr to handle is invalid\n");
1266 return 0;
1270 if (flags & LMEM_MODIFY)
1272 if (HANDLE_MOVEABLE(handle))
1274 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1275 pEntry->flags = (flags & 0x0f00) >> 8;
1276 TRACE("Changing flags to %x.\n", pEntry->flags);
1278 return handle;
1281 if (!size)
1283 if (flags & LMEM_MOVEABLE)
1285 if (HANDLE_FIXED(handle))
1287 TRACE("Freeing fixed block.\n");
1288 return LocalFree16( handle );
1290 else /* Moveable block */
1292 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1293 if (pEntry->lock == 0)
1295 /* discards moveable blocks */
1296 TRACE("Discarding block\n");
1297 LOCAL_FreeArena(ds, ARENA_HEADER(pEntry->addr - MOVEABLE_PREFIX));
1298 pEntry->addr = 0;
1299 pEntry->flags = (LMEM_DISCARDED >> 8);
1300 return handle;
1303 return 0;
1305 else if(flags == 0)
1307 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1308 if (pEntry->lock == 0)
1310 /* Frees block */
1311 return LocalFree16( handle );
1314 return 0;
1317 arena = ARENA_HEADER( blockhandle );
1318 TRACE("arena is %04x\n", arena );
1319 pArena = ARENA_PTR( ptr, arena );
1321 if(size <= 4) size = 5;
1322 if(HANDLE_MOVEABLE(handle)) size += MOVEABLE_PREFIX;
1323 oldsize = pArena->next - arena - ARENA_HEADER_SIZE;
1324 nextarena = LALIGN(blockhandle + size);
1326 /* Check for size reduction */
1328 if (nextarena <= pArena->next)
1330 TRACE("size reduction, making new free block\n");
1331 LOCAL_ShrinkArena(ds, arena, nextarena - arena);
1332 TRACE("returning %04x\n", handle );
1333 return handle;
1336 /* Check if the next block is free and large enough */
1338 pNext = ARENA_PTR( ptr, pArena->next );
1339 if (((pNext->prev & 3) == LOCAL_ARENA_FREE) &&
1340 (nextarena <= pNext->next))
1342 TRACE("size increase, making new free block\n");
1343 LOCAL_GrowArenaUpward(ds, arena, nextarena - arena);
1344 if (flags & LMEM_ZEROINIT)
1346 char *oldend = (char *)pArena + ARENA_HEADER_SIZE + oldsize;
1347 char *newend = ptr + pArena->next;
1348 TRACE("Clearing memory from %p to %p (DS -> %p)\n", oldend, newend, ptr);
1349 memset(oldend, 0, newend - oldend);
1352 TRACE("returning %04x\n", handle );
1353 return handle;
1356 /* Now we have to allocate a new block, but not if (fixed block or locked
1357 block) and no LMEM_MOVEABLE */
1359 if (!(flags & LMEM_MOVEABLE))
1361 if (HANDLE_FIXED(handle))
1363 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1364 return 0;
1366 else
1368 if(((LOCALHANDLEENTRY *)(ptr + handle))->lock != 0)
1370 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1371 return 0;
1376 hmem = LOCAL_GetBlock( ds, size, flags );
1377 ptr = MapSL( MAKESEGPTR( ds, 0 )); /* Reload ptr */
1378 if(HANDLE_MOVEABLE(handle)) /* LOCAL_GetBlock might have triggered */
1379 { /* a compaction, which might in turn have */
1380 blockhandle = pEntry->addr - MOVEABLE_PREFIX; /* moved the very block we are resizing */
1381 arena = ARENA_HEADER( blockhandle ); /* thus, we reload arena, too */
1383 if (!hmem)
1385 /* Remove the block from the heap and try again */
1386 LPSTR buffer = HeapAlloc( GetProcessHeap(), 0, oldsize );
1387 if (!buffer) return 0;
1388 memcpy( buffer, ptr + arena + ARENA_HEADER_SIZE, oldsize );
1389 LOCAL_FreeArena( ds, arena );
1390 if (!(hmem = LOCAL_GetBlock( ds, size, flags )))
1392 if (!(hmem = LOCAL_GetBlock( ds, oldsize, flags )))
1394 ERR("Can't restore saved block\n" );
1395 HeapFree( GetProcessHeap(), 0, buffer );
1396 return 0;
1398 size = oldsize;
1400 ptr = MapSL( MAKESEGPTR( ds, 0 ) ); /* Reload ptr */
1401 memcpy( ptr + hmem, buffer, oldsize );
1402 HeapFree( GetProcessHeap(), 0, buffer );
1404 else
1406 memcpy( ptr + hmem, ptr + (arena + ARENA_HEADER_SIZE), oldsize );
1407 LOCAL_FreeArena( ds, arena );
1409 if (HANDLE_MOVEABLE( handle ))
1411 TRACE("fixing handle\n");
1412 pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1413 pEntry->addr = hmem + MOVEABLE_PREFIX;
1414 /* Back ptr should still be correct */
1415 if(*(HLOCAL16 *)(ptr + hmem) != handle)
1416 ERR("back ptr is invalid.\n");
1417 hmem = handle;
1419 if (size == oldsize) hmem = 0; /* Realloc failed */
1420 TRACE("returning %04x\n", hmem );
1421 return hmem;
1425 /***********************************************************************
1426 * LOCAL_InternalLock
1428 static HLOCAL16 LOCAL_InternalLock( LPSTR heap, HLOCAL16 handle )
1430 HLOCAL16 old_handle = handle;
1432 if (HANDLE_MOVEABLE(handle))
1434 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(heap + handle);
1435 if (pEntry->flags == (LMEM_DISCARDED >> 8)) return 0;
1436 if (pEntry->lock < 0xfe) pEntry->lock++;
1437 handle = pEntry->addr;
1439 TRACE("%04x returning %04x\n", old_handle, handle );
1440 return handle;
1444 /***********************************************************************
1445 * LocalUnlock (KERNEL.9)
1447 BOOL16 WINAPI LocalUnlock16( HLOCAL16 handle )
1449 HANDLE16 ds = CURRENT_DS;
1450 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1452 TRACE("%04x\n", handle );
1453 if (HANDLE_MOVEABLE(handle))
1455 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1456 if (!pEntry->lock || (pEntry->lock == 0xff)) return FALSE;
1457 /* For moveable block, return the new lock count */
1458 /* (see _Windows_Internals_ p. 197) */
1459 return --pEntry->lock;
1461 else return FALSE;
1465 /***********************************************************************
1466 * LocalSize (KERNEL.10)
1468 UINT16 WINAPI LocalSize16( HLOCAL16 handle )
1470 HANDLE16 ds = CURRENT_DS;
1471 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1472 LOCALARENA *pArena;
1474 TRACE("%04x ds=%04x\n", handle, ds );
1476 if (!handle) return 0;
1477 if (HANDLE_MOVEABLE( handle ))
1479 handle = *(WORD *)(ptr + handle);
1480 if (!handle) return 0;
1481 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle - MOVEABLE_PREFIX) );
1483 else
1484 pArena = ARENA_PTR( ptr, ARENA_HEADER(handle) );
1486 return pArena->next - handle;
1490 /***********************************************************************
1491 * LocalFlags (KERNEL.12)
1493 UINT16 WINAPI LocalFlags16( HLOCAL16 handle )
1495 HANDLE16 ds = CURRENT_DS;
1496 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1498 if (HANDLE_MOVEABLE(handle))
1500 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY *)(ptr + handle);
1501 TRACE("(%04x,%04x): returning %04x\n",
1502 ds, handle, pEntry->lock | (pEntry->flags << 8) );
1503 return pEntry->lock | (pEntry->flags << 8);
1505 else
1507 TRACE("(%04x,%04x): returning 0\n",
1508 ds, handle );
1509 return 0;
1514 /***********************************************************************
1515 * LocalHeapSize (KERNEL.162)
1517 WORD WINAPI LocalHeapSize16(void)
1519 HANDLE16 ds = CURRENT_DS;
1520 LOCALHEAPINFO *pInfo = LOCAL_GetHeap( ds );
1521 return pInfo ? pInfo->last - pInfo->first : 0;
1525 /***********************************************************************
1526 * LocalCountFree (KERNEL.161)
1528 WORD WINAPI LocalCountFree16(void)
1530 HANDLE16 ds = CURRENT_DS;
1531 WORD arena, total;
1532 LOCALARENA *pArena;
1533 LOCALHEAPINFO *pInfo;
1534 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1536 if (!(pInfo = LOCAL_GetHeap( ds )))
1538 ERR("(%04x): Local heap not found\n", ds );
1539 LOCAL_PrintHeap( ds );
1540 return 0;
1543 total = 0;
1544 arena = pInfo->first;
1545 pArena = ARENA_PTR( ptr, arena );
1546 for (;;)
1548 arena = pArena->free_next;
1549 pArena = ARENA_PTR( ptr, arena );
1550 if (arena == pArena->free_next) break;
1551 total += pArena->size;
1553 TRACE("(%04x): returning %d\n", ds, total);
1554 return total;
1558 /***********************************************************************
1559 * LocalHandle (KERNEL.11)
1561 HLOCAL16 WINAPI LocalHandle16( WORD addr )
1563 HANDLE16 ds = CURRENT_DS;
1564 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1565 LOCALHEAPINFO *pInfo;
1566 WORD table;
1568 if (!(pInfo = LOCAL_GetHeap( ds )))
1570 ERR("(%04x): Local heap not found\n", ds );
1571 LOCAL_PrintHeap( ds );
1572 return 0;
1575 /* Find the address in the entry tables */
1577 table = pInfo->htable;
1578 while (table)
1580 WORD count = *(WORD *)(ptr + table);
1581 LOCALHANDLEENTRY *pEntry = (LOCALHANDLEENTRY*)(ptr+table+sizeof(WORD));
1582 for (; count > 0; count--, pEntry++)
1583 if (pEntry->addr == addr) return (HLOCAL16)((char *)pEntry - ptr);
1584 table = *(WORD *)pEntry;
1587 return (HLOCAL16)addr; /* Fixed block handle is addr */
1593 /***********************************************************************
1594 * LocalLock (KERNEL.8)
1596 * Note: only the offset part of the pointer is returned by the relay code.
1598 SEGPTR WINAPI LocalLock16( HLOCAL16 handle )
1600 WORD ds = CURRENT_DS;
1601 char *ptr = MapSL( MAKESEGPTR( ds, 0 ) );
1602 return MAKESEGPTR( ds, LOCAL_InternalLock( ptr, handle ) );
1606 /***********************************************************************
1607 * LocalCompact (KERNEL.13)
1609 UINT16 WINAPI LocalCompact16( UINT16 minfree )
1611 TRACE("%04x\n", minfree );
1612 return LOCAL_Compact( CURRENT_DS, minfree, 0 );
1616 /***********************************************************************
1617 * LocalNotify (KERNEL.14)
1619 * Installs a callback function that is called for local memory events
1620 * Callback function prototype is
1621 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1622 * wMsg:
1623 * - LN_OUTOFMEM
1624 * NotifyFunc seems to be responsible for allocating some memory,
1625 * returns TRUE for success.
1626 * wArg = number of bytes needed additionally
1627 * - LN_MOVE
1628 * hMem = handle; wArg = old mem location
1629 * - LN_DISCARD
1630 * NotifyFunc seems to be strongly encouraged to return TRUE,
1631 * otherwise LogError() gets called.
1632 * hMem = handle; wArg = flags
1634 FARPROC16 WINAPI LocalNotify16( FARPROC16 func )
1636 LOCALHEAPINFO *pInfo;
1637 FARPROC16 oldNotify;
1638 HANDLE16 ds = CURRENT_DS;
1640 if (!(pInfo = LOCAL_GetHeap( ds )))
1642 ERR("(%04x): Local heap not found\n", ds );
1643 LOCAL_PrintHeap( ds );
1644 return 0;
1646 TRACE("(%04x): %p\n", ds, func );
1647 FIXME("Half implemented\n");
1648 oldNotify = pInfo->notify;
1649 pInfo->notify = func;
1650 return oldNotify;
1654 /***********************************************************************
1655 * LocalShrink (KERNEL.121)
1657 UINT16 WINAPI LocalShrink16( HGLOBAL16 handle, UINT16 newsize )
1659 TRACE("%04x %04x\n", handle, newsize );
1660 return 0;
1664 /***********************************************************************
1665 * GetHeapSpaces (KERNEL.138)
1667 DWORD WINAPI GetHeapSpaces16( HMODULE16 module )
1669 NE_MODULE *pModule;
1670 WORD oldDS = CURRENT_DS;
1671 DWORD spaces;
1673 if (!(pModule = NE_GetPtr( module ))) return 0;
1674 CURRENT_DS = GlobalHandleToSel16((NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1)->hSeg);
1675 spaces = MAKELONG( LocalCountFree16(), LocalHeapSize16() );
1676 CURRENT_DS = oldDS;
1677 return spaces;
1681 /***********************************************************************
1682 * LocalHandleDelta (KERNEL.310)
1684 WORD WINAPI LocalHandleDelta16( WORD delta )
1686 LOCALHEAPINFO *pInfo;
1688 if (!(pInfo = LOCAL_GetHeap( CURRENT_DS )))
1690 ERR("Local heap not found\n");
1691 LOCAL_PrintHeap( CURRENT_DS );
1692 return 0;
1694 if (delta) pInfo->hdelta = delta;
1695 TRACE("returning %04x\n", pInfo->hdelta);
1696 return pInfo->hdelta;
1700 /***********************************************************************
1701 * 32-bit local heap functions (Win95; undocumented)
1704 /***********************************************************************
1705 * K208 (KERNEL.208)
1707 HANDLE WINAPI Local32Init16( WORD segment, DWORD tableSize,
1708 DWORD heapSize, DWORD flags )
1710 DWORD totSize, segSize = 0;
1711 LPBYTE base;
1712 LOCAL32HEADER *header;
1713 HANDLE heap;
1714 WORD *selectorTable;
1715 WORD selectorEven, selectorOdd;
1716 int i, nrBlocks;
1718 /* Determine new heap size */
1720 if ( segment )
1722 if ( (segSize = GetSelectorLimit16( segment )) == 0 )
1723 return 0;
1724 else
1725 segSize++;
1728 if ( heapSize == (DWORD)-1 )
1729 heapSize = 1024*1024; /* FIXME */
1731 heapSize = (heapSize + 0xffff) & 0xffff0000;
1732 segSize = (segSize + 0x0fff) & 0xfffff000;
1733 totSize = segSize + HTABLE_SIZE + heapSize;
1736 /* Allocate memory and initialize heap */
1738 if ( !(base = VirtualAlloc( NULL, totSize, MEM_RESERVE, PAGE_READWRITE )) )
1739 return 0;
1741 if ( !VirtualAlloc( base, segSize + HTABLE_PAGESIZE,
1742 MEM_COMMIT, PAGE_READWRITE ) )
1744 VirtualFree( base, 0, MEM_RELEASE );
1745 return 0;
1748 if (!(heap = RtlCreateHeap( 0, base + segSize + HTABLE_SIZE, heapSize, 0x10000, NULL, NULL )))
1750 VirtualFree( base, 0, MEM_RELEASE );
1751 return 0;
1755 /* Set up header and handle table */
1757 header = (LOCAL32HEADER *)(base + segSize);
1758 header->base = base;
1759 header->limit = HTABLE_PAGESIZE-1;
1760 header->flags = 0;
1761 header->magic = LOCAL32_MAGIC;
1762 header->heap = heap;
1764 header->freeListFirst[0] = sizeof(LOCAL32HEADER);
1765 header->freeListLast[0] = HTABLE_PAGESIZE - 4;
1766 header->freeListSize[0] = (HTABLE_PAGESIZE - sizeof(LOCAL32HEADER)) / 4;
1768 for (i = header->freeListFirst[0]; i < header->freeListLast[0]; i += 4)
1769 *(DWORD *)((LPBYTE)header + i) = i+4;
1771 header->freeListFirst[1] = 0xffff;
1774 /* Set up selector table */
1776 nrBlocks = (totSize + 0x7fff) >> 15;
1777 selectorTable = HeapAlloc( header->heap, 0, nrBlocks * 2 );
1778 selectorEven = SELECTOR_AllocBlock( base, totSize, LDT_FLAGS_DATA );
1779 selectorOdd = SELECTOR_AllocBlock( base + 0x8000, totSize - 0x8000, LDT_FLAGS_DATA );
1780 if ( !selectorTable || !selectorEven || !selectorOdd )
1782 HeapFree( header->heap, 0, selectorTable );
1783 if ( selectorEven ) SELECTOR_FreeBlock( selectorEven );
1784 if ( selectorOdd ) SELECTOR_FreeBlock( selectorOdd );
1785 HeapDestroy( header->heap );
1786 VirtualFree( base, 0, MEM_RELEASE );
1787 return 0;
1790 header->selectorTableOffset = (LPBYTE)selectorTable - header->base;
1791 header->selectorTableSize = nrBlocks * 4; /* ??? Win95 does it this way! */
1792 header->selectorDelta = selectorEven - selectorOdd;
1793 header->segment = segment? segment : selectorEven;
1795 for (i = 0; i < nrBlocks; i++)
1796 selectorTable[i] = (i & 1)? selectorOdd + ((i >> 1) << __AHSHIFT)
1797 : selectorEven + ((i >> 1) << __AHSHIFT);
1799 /* Move old segment */
1801 if ( segment )
1803 /* FIXME: This is somewhat ugly and relies on implementation
1804 details about 16-bit global memory handles ... */
1806 LPBYTE oldBase = (LPBYTE)GetSelectorBase( segment );
1807 memcpy( base, oldBase, segSize );
1808 GLOBAL_MoveBlock( segment, base, totSize );
1809 HeapFree( GetProcessHeap(), 0, oldBase );
1812 return header;
1815 /***********************************************************************
1816 * Local32_SearchHandle
1818 static LPDWORD Local32_SearchHandle( LOCAL32HEADER *header, DWORD addr )
1820 LPDWORD handle;
1822 for ( handle = (LPDWORD)((LPBYTE)header + sizeof(LOCAL32HEADER));
1823 handle < (LPDWORD)((LPBYTE)header + header->limit);
1824 handle++)
1826 if (*handle == addr)
1827 return handle;
1830 return NULL;
1833 /***********************************************************************
1834 * Local32_ToHandle
1836 static VOID Local32_ToHandle( LOCAL32HEADER *header, INT16 type,
1837 DWORD addr, LPDWORD *handle, LPBYTE *ptr )
1839 *handle = NULL;
1840 *ptr = NULL;
1842 switch (type)
1844 case -2: /* 16:16 pointer, no handles */
1845 *ptr = MapSL( addr );
1846 *handle = (LPDWORD)*ptr;
1847 break;
1849 case -1: /* 32-bit offset, no handles */
1850 *ptr = header->base + addr;
1851 *handle = (LPDWORD)*ptr;
1852 break;
1854 case 0: /* handle */
1855 if ( addr >= sizeof(LOCAL32HEADER)
1856 && addr < header->limit && !(addr & 3)
1857 && *(LPDWORD)((LPBYTE)header + addr) >= HTABLE_SIZE )
1859 *handle = (LPDWORD)((LPBYTE)header + addr);
1860 *ptr = header->base + **handle;
1862 break;
1864 case 1: /* 16:16 pointer */
1865 *ptr = MapSL( addr );
1866 *handle = Local32_SearchHandle( header, *ptr - header->base );
1867 break;
1869 case 2: /* 32-bit offset */
1870 *ptr = header->base + addr;
1871 *handle = Local32_SearchHandle( header, *ptr - header->base );
1872 break;
1876 /***********************************************************************
1877 * Local32_FromHandle
1879 static VOID Local32_FromHandle( LOCAL32HEADER *header, INT16 type,
1880 DWORD *addr, LPDWORD handle, LPBYTE ptr )
1882 *addr = 0;
1883 switch (type)
1885 case -2: /* 16:16 pointer */
1886 case 1:
1888 WORD *selTable = (LPWORD)(header->base + header->selectorTableOffset);
1889 DWORD offset = ptr - header->base;
1890 *addr = MAKELONG( offset & 0x7fff, selTable[offset >> 15] );
1892 break;
1894 case -1: /* 32-bit offset */
1895 case 2:
1896 *addr = ptr - header->base;
1897 break;
1899 case 0: /* handle */
1900 *addr = (LPBYTE)handle - (LPBYTE)header;
1901 break;
1905 /***********************************************************************
1906 * K209 (KERNEL.209)
1908 DWORD WINAPI Local32Alloc16( HANDLE heap, DWORD size, INT16 type, DWORD flags )
1910 LOCAL32HEADER *header = heap;
1911 LPDWORD handle;
1912 LPBYTE ptr;
1913 DWORD addr;
1915 /* Allocate memory */
1916 ptr = HeapAlloc( header->heap,
1917 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0, size );
1918 if (!ptr) return 0;
1921 /* Allocate handle if requested */
1922 if (type >= 0)
1924 int page, i;
1926 /* Find first page of handle table with free slots */
1927 for (page = 0; page < HTABLE_NPAGES; page++)
1928 if (header->freeListFirst[page] != 0)
1929 break;
1930 if (page == HTABLE_NPAGES)
1932 WARN("Out of handles!\n" );
1933 HeapFree( header->heap, 0, ptr );
1934 return 0;
1937 /* If virgin page, initialize it */
1938 if (header->freeListFirst[page] == 0xffff)
1940 if ( !VirtualAlloc( (LPBYTE)header + (page << 12),
1941 0x1000, MEM_COMMIT, PAGE_READWRITE ) )
1943 WARN("Cannot grow handle table!\n" );
1944 HeapFree( header->heap, 0, ptr );
1945 return 0;
1948 header->limit += HTABLE_PAGESIZE;
1950 header->freeListFirst[page] = 0;
1951 header->freeListLast[page] = HTABLE_PAGESIZE - 4;
1952 header->freeListSize[page] = HTABLE_PAGESIZE / 4;
1954 for (i = 0; i < HTABLE_PAGESIZE; i += 4)
1955 *(DWORD *)((LPBYTE)header + i) = i+4;
1957 if (page < HTABLE_NPAGES-1)
1958 header->freeListFirst[page+1] = 0xffff;
1961 /* Allocate handle slot from page */
1962 handle = (LPDWORD)((LPBYTE)header + header->freeListFirst[page]);
1963 if (--header->freeListSize[page] == 0)
1964 header->freeListFirst[page] = header->freeListLast[page] = 0;
1965 else
1966 header->freeListFirst[page] = *handle;
1968 /* Store 32-bit offset in handle slot */
1969 *handle = ptr - header->base;
1971 else
1973 handle = (LPDWORD)ptr;
1974 header->flags |= 1;
1978 /* Convert handle to requested output type */
1979 Local32_FromHandle( header, type, &addr, handle, ptr );
1980 return addr;
1983 /***********************************************************************
1984 * K210 (KERNEL.210)
1986 DWORD WINAPI Local32ReAlloc16( HANDLE heap, DWORD addr, INT16 type,
1987 DWORD size, DWORD flags )
1989 LOCAL32HEADER *header = heap;
1990 LPDWORD handle;
1991 LPBYTE ptr;
1993 if (!addr)
1994 return Local32Alloc16( heap, size, type, flags );
1996 /* Retrieve handle and pointer */
1997 Local32_ToHandle( header, type, addr, &handle, &ptr );
1998 if (!handle) return FALSE;
2000 /* Reallocate memory block */
2001 ptr = HeapReAlloc( header->heap,
2002 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0,
2003 ptr, size );
2004 if (!ptr) return 0;
2006 /* Modify handle */
2007 if (type >= 0)
2008 *handle = ptr - header->base;
2009 else
2010 handle = (LPDWORD)ptr;
2012 /* Convert handle to requested output type */
2013 Local32_FromHandle( header, type, &addr, handle, ptr );
2014 return addr;
2017 /***********************************************************************
2018 * K211 (KERNEL.211)
2020 BOOL WINAPI Local32Free16( HANDLE heap, DWORD addr, INT16 type )
2022 LOCAL32HEADER *header = heap;
2023 LPDWORD handle;
2024 LPBYTE ptr;
2026 /* Retrieve handle and pointer */
2027 Local32_ToHandle( header, type, addr, &handle, &ptr );
2028 if (!handle) return FALSE;
2030 /* Free handle if necessary */
2031 if (type >= 0)
2033 int offset = (LPBYTE)handle - (LPBYTE)header;
2034 int page = offset >> 12;
2036 /* Return handle slot to page free list */
2037 if (header->freeListSize[page]++ == 0)
2038 header->freeListFirst[page] = header->freeListLast[page] = offset;
2039 else
2040 *(LPDWORD)((LPBYTE)header + header->freeListLast[page]) = offset,
2041 header->freeListLast[page] = offset;
2043 *handle = 0;
2045 /* Shrink handle table when possible */
2046 while (page > 0 && header->freeListSize[page] == HTABLE_PAGESIZE / 4)
2048 if ( VirtualFree( (LPBYTE)header +
2049 (header->limit & ~(HTABLE_PAGESIZE-1)),
2050 HTABLE_PAGESIZE, MEM_DECOMMIT ) )
2051 break;
2053 header->limit -= HTABLE_PAGESIZE;
2054 header->freeListFirst[page] = 0xffff;
2055 page--;
2059 /* Free memory */
2060 return HeapFree( header->heap, 0, ptr );
2063 /***********************************************************************
2064 * K213 (KERNEL.213)
2066 DWORD WINAPI Local32Translate16( HANDLE heap, DWORD addr, INT16 type1, INT16 type2 )
2068 LOCAL32HEADER *header = heap;
2069 LPDWORD handle;
2070 LPBYTE ptr;
2072 Local32_ToHandle( header, type1, addr, &handle, &ptr );
2073 if (!handle) return 0;
2075 Local32_FromHandle( header, type2, &addr, handle, ptr );
2076 return addr;
2079 /***********************************************************************
2080 * K214 (KERNEL.214)
2082 DWORD WINAPI Local32Size16( HANDLE heap, DWORD addr, INT16 type )
2084 LOCAL32HEADER *header = heap;
2085 LPDWORD handle;
2086 LPBYTE ptr;
2088 Local32_ToHandle( header, type, addr, &handle, &ptr );
2089 if (!handle) return 0;
2091 return HeapSize( header->heap, 0, ptr );
2094 /***********************************************************************
2095 * K215 (KERNEL.215)
2097 BOOL WINAPI Local32ValidHandle16( HANDLE heap, WORD addr )
2099 LOCAL32HEADER *header = heap;
2100 LPDWORD handle;
2101 LPBYTE ptr;
2103 Local32_ToHandle( header, 0, addr, &handle, &ptr );
2104 return handle != NULL;
2107 /***********************************************************************
2108 * K229 (KERNEL.229)
2110 WORD WINAPI Local32GetSegment16( HANDLE heap )
2112 LOCAL32HEADER *header = heap;
2113 return header->segment;
2116 /***********************************************************************
2117 * Local32_GetHeap
2119 static LOCAL32HEADER *Local32_GetHeap( HGLOBAL16 handle )
2121 WORD selector = GlobalHandleToSel16( handle );
2122 DWORD base = GetSelectorBase( selector );
2123 DWORD limit = GetSelectorLimit16( selector );
2125 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2126 it this way ... */
2128 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2129 return (LOCAL32HEADER *)base;
2131 base += 0x10000;
2132 limit -= 0x10000;
2134 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
2135 return (LOCAL32HEADER *)base;
2137 return NULL;
2140 /***********************************************************************
2141 * Local32Info (KERNEL.444)
2143 BOOL16 WINAPI Local32Info16( LOCAL32INFO *pLocal32Info, HGLOBAL16 handle )
2145 PROCESS_HEAP_ENTRY entry;
2146 int i;
2148 LOCAL32HEADER *header = Local32_GetHeap( handle );
2149 if ( !header ) return FALSE;
2151 if ( !pLocal32Info || pLocal32Info->dwSize < sizeof(LOCAL32INFO) )
2152 return FALSE;
2154 pLocal32Info->dwMemReserved = 0;
2155 pLocal32Info->dwMemCommitted = 0;
2156 pLocal32Info->dwTotalFree = 0;
2157 pLocal32Info->dwLargestFreeBlock = 0;
2159 while (HeapWalk( header->heap, &entry ))
2161 if (entry.wFlags & PROCESS_HEAP_REGION)
2163 pLocal32Info->dwMemReserved += entry.u.Region.dwCommittedSize
2164 + entry.u.Region.dwUnCommittedSize;
2165 pLocal32Info->dwMemCommitted = entry.u.Region.dwCommittedSize;
2167 else if (!(entry.wFlags & PROCESS_HEAP_ENTRY_BUSY))
2169 DWORD size = entry.cbData + entry.cbOverhead;
2170 pLocal32Info->dwTotalFree += size;
2171 if (size > pLocal32Info->dwLargestFreeBlock) pLocal32Info->dwLargestFreeBlock = size;
2175 pLocal32Info->dwcFreeHandles = 0;
2176 for ( i = 0; i < HTABLE_NPAGES; i++ )
2178 if ( header->freeListFirst[i] == 0xffff ) break;
2179 pLocal32Info->dwcFreeHandles += header->freeListSize[i];
2181 pLocal32Info->dwcFreeHandles += (HTABLE_NPAGES - i) * HTABLE_PAGESIZE/4;
2183 return TRUE;
2186 /***********************************************************************
2187 * Local32First (KERNEL.445)
2189 BOOL16 WINAPI Local32First16( LOCAL32ENTRY *pLocal32Entry, HGLOBAL16 handle )
2191 FIXME("(%p, %04X): stub!\n", pLocal32Entry, handle );
2192 return FALSE;
2195 /***********************************************************************
2196 * Local32Next (KERNEL.446)
2198 BOOL16 WINAPI Local32Next16( LOCAL32ENTRY *pLocal32Entry )
2200 FIXME("(%p): stub!\n", pLocal32Entry );
2201 return FALSE;