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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
36 #include "wine/winbase16.h"
39 #include "kernel_private.h"
40 #include "kernel16_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(local
);
48 WORD prev
; /* Previous arena | arena type */
49 WORD next
; /* Next arena */
50 /* Start of the memory block or free-list info */
51 WORD size
; /* Size of the free block */
52 WORD free_prev
; /* Previous free block */
53 WORD free_next
; /* Next free block */
56 #define ARENA_HEADER_SIZE 4
57 #define ARENA_HEADER( handle) ((handle) - ARENA_HEADER_SIZE)
59 /* Arena types (stored in 'prev' field of the arena) */
60 #define LOCAL_ARENA_FREE 0
61 #define LOCAL_ARENA_FIXED 1
63 /* LocalNotify() msgs */
69 /* Layout of a handle entry table
71 * WORD count of entries
72 * LOCALHANDLEENTRY[count] entries
73 * WORD near ptr to next table
77 WORD addr
; /* Address of the MOVEABLE block */
78 BYTE flags
; /* Flags for this block */
79 BYTE lock
; /* Lock count */
83 * We make addr = 4n + 2 and set *((WORD *)addr - 1) = &addr like Windows does
84 * in case something actually relies on this.
85 * Note that if the architecture does not allow unaligned accesses, we make
86 * addr = 4n + 4 to avoid returning unaligned pointers from LocalAlloc etc.
88 * An unused handle has lock = flags = 0xff. In windows addr is that of next
89 * free handle, at the moment in wine we set it to 0.
91 * A discarded block's handle has lock = addr = 0 and flags = 0x40
92 * (LMEM_DISCARDED >> 8)
95 #ifdef ALLOW_UNALIGNED_ACCESS
96 # define MOVEABLE_PREFIX sizeof(HLOCAL16)
98 # define MOVEABLE_PREFIX sizeof(int)
102 #include "pshpack1.h"
106 WORD check
; /* 00 Heap checking flag */
107 WORD freeze
; /* 02 Heap frozen flag */
108 WORD items
; /* 04 Count of items on the heap */
109 WORD first
; /* 06 First item of the heap */
110 WORD pad1
; /* 08 Always 0 */
111 WORD last
; /* 0a Last item of the heap */
112 WORD pad2
; /* 0c Always 0 */
113 BYTE ncompact
; /* 0e Compactions counter */
114 BYTE dislevel
; /* 0f Discard level */
115 DWORD distotal
; /* 10 Total bytes discarded */
116 WORD htable
; /* 14 Pointer to handle table */
117 WORD hfree
; /* 16 Pointer to free handle table */
118 WORD hdelta
; /* 18 Delta to expand the handle table */
119 WORD expand
; /* 1a Pointer to expand function (unused) */
120 WORD pstat
; /* 1c Pointer to status structure (unused) */
121 FARPROC16 notify
; /* 1e Pointer to LocalNotify() function */
122 WORD lock
; /* 22 Lock count for the heap */
123 WORD extra
; /* 24 Extra bytes to allocate when expanding */
124 WORD minsize
; /* 26 Minimum size of the heap */
125 WORD magic
; /* 28 Magic number */
130 #define LOCAL_HEAP_MAGIC 0x484c /* 'LH' */
132 /* All local heap allocations are aligned on 4-byte boundaries */
133 #define LALIGN(word) (((word) + 3) & ~3)
135 #define ARENA_PTR(ptr,arena) ((LOCALARENA *)((char*)(ptr)+(arena)))
136 #define ARENA_PREV(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & ~3)
137 #define ARENA_NEXT(ptr,arena) (ARENA_PTR((ptr),(arena))->next)
138 #define ARENA_FLAGS(ptr,arena) (ARENA_PTR((ptr),(arena))->prev & 3)
140 /* determine whether the handle belongs to a fixed or a moveable block */
141 #define HANDLE_FIXED(handle) (((handle) & 3) == 0)
142 #define HANDLE_MOVEABLE(handle) (((handle) & 3) == 2)
145 /* 32-bit heap definitions */
147 #define HTABLE_SIZE 0x10000
148 #define HTABLE_PAGESIZE 0x1000
149 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
151 #include "pshpack1.h"
152 typedef struct _LOCAL32HEADER
154 WORD freeListFirst
[HTABLE_NPAGES
];
155 WORD freeListSize
[HTABLE_NPAGES
];
156 WORD freeListLast
[HTABLE_NPAGES
];
158 DWORD selectorTableOffset
;
159 WORD selectorTableSize
;
174 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
177 static inline BOOL16
call_notify_func( FARPROC16 proc
, WORD msg
, HLOCAL16 handle
, WORD arg
)
182 if (!proc
) return FALSE
;
186 WOWCallback16Ex( (DWORD
)proc
, WCB16_PASCAL
, sizeof(args
), args
, &ret
);
191 /***********************************************************************
194 * Return a pointer to the local heap, making sure it exists.
196 static LOCALHEAPINFO
*LOCAL_GetHeap( HANDLE16 ds
)
198 LOCALHEAPINFO
*pInfo
;
199 INSTANCEDATA
*ptr
= MapSL( MAKESEGPTR( ds
, 0 ));
200 TRACE("Heap at %p, %04x\n", ptr
, (ptr
!= NULL
? ptr
->heap
: 0xFFFF));
201 if (!ptr
|| !ptr
->heap
) return NULL
;
202 if (IsBadReadPtr16( (SEGPTR
)MAKELONG(ptr
->heap
,ds
), sizeof(LOCALHEAPINFO
)))
204 WARN("Bad pointer\n");
207 pInfo
= (LOCALHEAPINFO
*)((char*)ptr
+ ptr
->heap
);
208 if (pInfo
->magic
!= LOCAL_HEAP_MAGIC
)
217 /***********************************************************************
218 * LOCAL_MakeBlockFree
220 * Make a block free, inserting it in the free-list.
221 * 'block' is the handle of the block arena; 'baseptr' points to
222 * the beginning of the data segment containing the heap.
224 static void LOCAL_MakeBlockFree( char *baseptr
, WORD block
)
226 LOCALARENA
*pArena
, *pNext
;
229 /* Mark the block as free */
231 pArena
= ARENA_PTR( baseptr
, block
);
232 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FREE
;
233 pArena
->size
= pArena
->next
- block
;
235 /* Find the next free block (last block is always free) */
240 pNext
= ARENA_PTR( baseptr
, next
);
241 if ((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) break;
245 TRACE("%04x, next %04x\n", block
, next
);
246 /* Insert the free block in the free-list */
248 pArena
->free_prev
= pNext
->free_prev
;
249 pArena
->free_next
= next
;
250 ARENA_PTR(baseptr
,pNext
->free_prev
)->free_next
= block
;
251 pNext
->free_prev
= block
;
255 /***********************************************************************
256 * LOCAL_RemoveFreeBlock
258 * Remove a block from the free-list.
259 * 'block' is the handle of the block arena; 'baseptr' points to
260 * the beginning of the data segment containing the heap.
262 static void LOCAL_RemoveFreeBlock( char *baseptr
, WORD block
)
264 /* Mark the block as fixed */
266 LOCALARENA
*pArena
= ARENA_PTR( baseptr
, block
);
267 pArena
->prev
= (pArena
->prev
& ~3) | LOCAL_ARENA_FIXED
;
269 /* Remove it from the list */
271 ARENA_PTR(baseptr
,pArena
->free_prev
)->free_next
= pArena
->free_next
;
272 ARENA_PTR(baseptr
,pArena
->free_next
)->free_prev
= pArena
->free_prev
;
276 /***********************************************************************
279 * Insert a new block in the heap.
280 * 'new' is the handle of the new block arena; 'baseptr' points to
281 * the beginning of the data segment containing the heap; 'prev' is
282 * the block before the new one.
284 static void LOCAL_AddBlock( char *baseptr
, WORD prev
, WORD
new )
286 LOCALARENA
*pPrev
= ARENA_PTR( baseptr
, prev
);
287 LOCALARENA
*pNew
= ARENA_PTR( baseptr
, new );
289 pNew
->prev
= (prev
& ~3) | LOCAL_ARENA_FIXED
;
290 pNew
->next
= pPrev
->next
;
291 ARENA_PTR(baseptr
,pPrev
->next
)->prev
&= 3;
292 ARENA_PTR(baseptr
,pPrev
->next
)->prev
|= new;
297 /***********************************************************************
300 * Remove a block from the heap.
301 * 'block' is the handle of the block arena; 'baseptr' points to
302 * the beginning of the data segment containing the heap.
304 static void LOCAL_RemoveBlock( char *baseptr
, WORD block
)
306 LOCALARENA
*pArena
, *pTmp
;
308 /* Remove the block from the free-list */
311 pArena
= ARENA_PTR( baseptr
, block
);
312 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
313 LOCAL_RemoveFreeBlock( baseptr
, block
);
315 /* If the previous block is free, expand its size */
317 pTmp
= ARENA_PTR( baseptr
, pArena
->prev
& ~3 );
318 if ((pTmp
->prev
& 3) == LOCAL_ARENA_FREE
)
319 pTmp
->size
+= pArena
->next
- block
;
321 /* Remove the block from the linked list */
323 pTmp
->next
= pArena
->next
;
324 pTmp
= ARENA_PTR( baseptr
, pArena
->next
);
325 pTmp
->prev
= (pTmp
->prev
& 3) | (pArena
->prev
& ~3);
329 /***********************************************************************
332 static void LOCAL_PrintHeap( HANDLE16 ds
)
335 LOCALHEAPINFO
*pInfo
;
338 /* FIXME - the test should be done when calling the function!
339 plus is not clear that we should print this info
340 only when TRACE_ON is on! */
341 if(!TRACE_ON(local
)) return;
343 ptr
= MapSL( MAKESEGPTR( ds
, 0 ));
344 pInfo
= LOCAL_GetHeap( ds
);
348 DPRINTF( "Local Heap corrupted! ds=%04x\n", ds
);
351 DPRINTF( "Local Heap ds=%04x first=%04x last=%04x items=%d\n",
352 ds
, pInfo
->first
, pInfo
->last
, pInfo
->items
);
354 arena
= pInfo
->first
;
357 LOCALARENA
*pArena
= ARENA_PTR(ptr
,arena
);
358 DPRINTF( " %04x: prev=%04x next=%04x type=%d\n", arena
,
359 pArena
->prev
& ~3, pArena
->next
, pArena
->prev
& 3 );
360 if (arena
== pInfo
->first
)
362 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
363 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
365 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
367 DPRINTF( " size=%d free_prev=%04x free_next=%04x\n",
368 pArena
->size
, pArena
->free_prev
, pArena
->free_next
);
369 if (pArena
->next
== arena
) break; /* last one */
370 if (ARENA_PTR(ptr
,pArena
->free_next
)->free_prev
!= arena
)
372 DPRINTF( "*** arena->free_next->free_prev != arena\n" );
376 if (pArena
->next
== arena
)
378 DPRINTF( "*** last block is not marked free\n" );
381 if ((ARENA_PTR(ptr
,pArena
->next
)->prev
& ~3) != arena
)
383 DPRINTF( "*** arena->next->prev != arena (%04x, %04x)\n",
384 pArena
->next
, ARENA_PTR(ptr
,pArena
->next
)->prev
);
387 arena
= pArena
->next
;
392 /***********************************************************************
393 * LocalInit (KERNEL.4)
395 BOOL16 WINAPI
LocalInit16( HANDLE16 selector
, WORD start
, WORD end
)
398 WORD heapInfoArena
, freeArena
, lastArena
;
399 LOCALHEAPINFO
*pHeapInfo
;
400 LOCALARENA
*pArena
, *pFirstArena
, *pLastArena
;
403 /* The initial layout of the heap is: */
404 /* - first arena (FIXED) */
405 /* - heap info structure (FIXED) */
406 /* - large free block (FREE) */
407 /* - last arena (FREE) */
409 TRACE("%04x %04x-%04x\n", selector
, start
, end
);
410 if (!selector
) selector
= CURRENT_DS
;
414 /* If TRACE_ON(heap) is set, the global heap blocks are */
415 /* cleared before use, so we can test for double initialization. */
416 if (LOCAL_GetHeap(selector
))
418 ERR("Heap %04x initialized twice.\n", selector
);
419 LOCAL_PrintHeap(selector
);
425 /* start == 0 means: put the local heap at the end of the segment */
427 DWORD size
= GlobalSize16( GlobalHandle16( selector
) );
428 start
= (WORD
)(size
> 0xffff ? 0xffff : size
) - 1;
429 if ( end
> 0xfffe ) end
= 0xfffe;
433 ptr
= MapSL( MAKESEGPTR( selector
, 0 ) );
435 start
= LALIGN( max( start
, sizeof(INSTANCEDATA
) ) );
436 heapInfoArena
= LALIGN(start
+ sizeof(LOCALARENA
) );
437 freeArena
= LALIGN( heapInfoArena
+ ARENA_HEADER_SIZE
438 + sizeof(LOCALHEAPINFO
) );
439 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
441 /* Make sure there's enough space. */
443 if (freeArena
+ sizeof(LOCALARENA
) >= lastArena
) goto done
;
445 /* Initialise the first arena */
447 pFirstArena
= ARENA_PTR( ptr
, start
);
448 pFirstArena
->prev
= start
| LOCAL_ARENA_FIXED
;
449 pFirstArena
->next
= heapInfoArena
;
450 pFirstArena
->size
= LALIGN(sizeof(LOCALARENA
));
451 pFirstArena
->free_prev
= start
; /* this one */
452 pFirstArena
->free_next
= freeArena
;
454 /* Initialise the arena of the heap info structure */
456 pArena
= ARENA_PTR( ptr
, heapInfoArena
);
457 pArena
->prev
= start
| LOCAL_ARENA_FIXED
;
458 pArena
->next
= freeArena
;
460 /* Initialise the heap info structure */
462 pHeapInfo
= (LOCALHEAPINFO
*) (ptr
+ heapInfoArena
+ ARENA_HEADER_SIZE
);
463 memset( pHeapInfo
, 0, sizeof(LOCALHEAPINFO
) );
464 pHeapInfo
->items
= 4;
465 pHeapInfo
->first
= start
;
466 pHeapInfo
->last
= lastArena
;
467 pHeapInfo
->htable
= 0;
468 pHeapInfo
->hdelta
= 0x20;
469 pHeapInfo
->extra
= 0x200;
470 pHeapInfo
->minsize
= lastArena
- freeArena
;
471 pHeapInfo
->magic
= LOCAL_HEAP_MAGIC
;
473 /* Initialise the large free block */
475 pArena
= ARENA_PTR( ptr
, freeArena
);
476 pArena
->prev
= heapInfoArena
| LOCAL_ARENA_FREE
;
477 pArena
->next
= lastArena
;
478 pArena
->size
= lastArena
- freeArena
;
479 pArena
->free_prev
= start
;
480 pArena
->free_next
= lastArena
;
482 /* Initialise the last block */
484 pLastArena
= ARENA_PTR( ptr
, lastArena
);
485 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
486 pLastArena
->next
= lastArena
; /* this one */
487 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
488 pLastArena
->free_prev
= freeArena
;
489 pLastArena
->free_next
= lastArena
; /* this one */
491 /* Store the local heap address in the instance data */
493 ((INSTANCEDATA
*)ptr
)->heap
= heapInfoArena
+ ARENA_HEADER_SIZE
;
494 LOCAL_PrintHeap( selector
);
498 CURRENT_STACK16
->ecx
= ret
; /* must be returned in cx too */
503 /***********************************************************************
506 static BOOL16
LOCAL_GrowHeap( HANDLE16 ds
)
511 LOCALHEAPINFO
*pHeapInfo
;
512 WORD freeArena
, lastArena
;
513 LOCALARENA
*pArena
, *pLastArena
;
516 hseg
= GlobalHandle16( ds
);
517 /* maybe mem allocated by Virtual*() ? */
518 if (!hseg
) return FALSE
;
520 oldsize
= GlobalSize16( hseg
);
521 /* if nothing can be gained, return */
522 if (oldsize
> 0xfff0) return FALSE
;
523 hseg
= GlobalReAlloc16( hseg
, 0x10000, GMEM_FIXED
);
524 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
525 pHeapInfo
= LOCAL_GetHeap( ds
);
526 if (pHeapInfo
== NULL
) {
527 ERR("Heap not found\n" );
530 end
= GlobalSize16( hseg
);
531 lastArena
= (end
- sizeof(LOCALARENA
)) & ~3;
533 /* Update the HeapInfo */
535 freeArena
= pHeapInfo
->last
;
536 pHeapInfo
->last
= lastArena
;
537 pHeapInfo
->minsize
+= end
- oldsize
;
539 /* grow the old last block */
540 pArena
= ARENA_PTR( ptr
, freeArena
);
541 pArena
->size
= lastArena
- freeArena
;
542 pArena
->next
= lastArena
;
543 pArena
->free_next
= lastArena
;
545 /* Initialise the new last block */
547 pLastArena
= ARENA_PTR( ptr
, lastArena
);
548 pLastArena
->prev
= freeArena
| LOCAL_ARENA_FREE
;
549 pLastArena
->next
= lastArena
; /* this one */
550 pLastArena
->size
= LALIGN(sizeof(LOCALARENA
));
551 pLastArena
->free_prev
= freeArena
;
552 pLastArena
->free_next
= lastArena
; /* this one */
554 /* If block before freeArena is also free then merge them */
555 if((ARENA_PTR(ptr
, (pArena
->prev
& ~3))->prev
& 3) == LOCAL_ARENA_FREE
)
557 LOCAL_RemoveBlock(ptr
, freeArena
);
561 TRACE("Heap expanded\n" );
562 LOCAL_PrintHeap( ds
);
567 /***********************************************************************
570 static HLOCAL16
LOCAL_FreeArena( WORD ds
, WORD arena
)
572 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
573 LOCALHEAPINFO
*pInfo
;
574 LOCALARENA
*pArena
, *pPrev
, *pNext
;
576 TRACE("%04x ds=%04x\n", arena
, ds
);
577 if (!(pInfo
= LOCAL_GetHeap( ds
))) return arena
;
579 pArena
= ARENA_PTR( ptr
, arena
);
580 if ((pArena
->prev
& 3) == LOCAL_ARENA_FREE
)
582 /* shouldn't happen */
583 ERR("Trying to free block %04x twice!\n",
585 LOCAL_PrintHeap( ds
);
589 /* Check if we can merge with the previous block */
591 pPrev
= ARENA_PTR( ptr
, pArena
->prev
& ~3 );
592 pNext
= ARENA_PTR( ptr
, pArena
->next
);
593 if ((pPrev
->prev
& 3) == LOCAL_ARENA_FREE
)
595 arena
= pArena
->prev
& ~3;
597 LOCAL_RemoveBlock( ptr
, pPrev
->next
);
600 else /* Make a new free block */
602 LOCAL_MakeBlockFree( ptr
, arena
);
605 /* Check if we can merge with the next block */
607 if ((pArena
->next
== pArena
->free_next
) &&
608 (pArena
->next
!= pInfo
->last
))
610 LOCAL_RemoveBlock( ptr
, pArena
->next
);
617 /***********************************************************************
620 * Shrink an arena by creating a free block at its end if possible.
621 * 'size' includes the arena header, and must be aligned.
623 static void LOCAL_ShrinkArena( WORD ds
, WORD arena
, WORD size
)
625 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
626 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
628 if (arena
+ size
+ LALIGN(sizeof(LOCALARENA
)) < pArena
->next
)
630 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
632 LOCAL_AddBlock( ptr
, arena
, arena
+ size
);
634 LOCAL_FreeArena( ds
, arena
+ size
);
639 /***********************************************************************
640 * LOCAL_GrowArenaDownward
642 * Grow an arena downward by using the previous arena (must be free).
644 static void LOCAL_GrowArenaDownward( WORD ds
, WORD arena
, WORD newsize
)
646 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
647 LOCALHEAPINFO
*pInfo
;
648 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
649 WORD prevArena
= pArena
->prev
& ~3;
650 LOCALARENA
*pPrevArena
= ARENA_PTR( ptr
, prevArena
);
654 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
655 offset
= pPrevArena
->size
;
656 size
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
657 LOCAL_RemoveFreeBlock( ptr
, prevArena
);
658 LOCAL_RemoveBlock( ptr
, arena
);
660 p
= (char *)pPrevArena
+ ARENA_HEADER_SIZE
;
661 while (offset
< size
)
663 memcpy( p
, p
+ offset
, offset
);
667 if (size
) memcpy( p
, p
+ offset
, size
);
668 LOCAL_ShrinkArena( ds
, prevArena
, newsize
);
673 /***********************************************************************
674 * LOCAL_GrowArenaUpward
676 * Grow an arena upward by using the next arena (must be free and big
677 * enough). Newsize includes the arena header and must be aligned.
679 static void LOCAL_GrowArenaUpward( WORD ds
, WORD arena
, WORD newsize
)
681 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
682 LOCALHEAPINFO
*pInfo
;
683 LOCALARENA
*pArena
= ARENA_PTR( ptr
, arena
);
684 WORD nextArena
= pArena
->next
;
686 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
687 LOCAL_RemoveBlock( ptr
, nextArena
);
689 LOCAL_ShrinkArena( ds
, arena
, newsize
);
693 /***********************************************************************
696 static WORD
LOCAL_GetFreeSpace(WORD ds
, WORD countdiscard
)
698 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
699 LOCALHEAPINFO
*pInfo
;
704 if (!(pInfo
= LOCAL_GetHeap( ds
)))
706 ERR("Local heap not found\n" );
710 arena
= pInfo
->first
;
711 pArena
= ARENA_PTR( ptr
, arena
);
712 while (arena
!= pArena
->free_next
)
714 arena
= pArena
->free_next
;
715 pArena
= ARENA_PTR( ptr
, arena
);
716 if (pArena
->size
>= freespace
) freespace
= pArena
->size
;
718 /* FIXME doesn't yet calculate space that would become free if everything
719 were discarded when countdiscard == 1 */
720 if (freespace
< ARENA_HEADER_SIZE
) freespace
= 0;
721 else freespace
-= ARENA_HEADER_SIZE
;
726 /***********************************************************************
729 static UINT16
LOCAL_Compact( HANDLE16 ds
, UINT16 minfree
, UINT16 flags
)
731 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
732 LOCALHEAPINFO
*pInfo
;
733 LOCALARENA
*pArena
, *pMoveArena
, *pFinalArena
;
734 WORD arena
, movearena
, finalarena
, table
;
735 WORD count
, movesize
, size
;
737 LOCALHANDLEENTRY
*pEntry
;
739 if (!(pInfo
= LOCAL_GetHeap( ds
)))
741 ERR("Local heap not found\n" );
745 TRACE("ds = %04x, minfree = %04x, flags = %04x\n",
747 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
748 if(freespace
>= minfree
|| (flags
& LMEM_NOCOMPACT
))
750 TRACE("Returning %04x.\n", freespace
);
753 TRACE("Compacting heap %04x.\n", ds
);
754 table
= pInfo
->htable
;
757 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
758 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
760 if((pEntry
->lock
== 0) && (pEntry
->flags
!= (LMEM_DISCARDED
>> 8)))
762 /* OK we can move this one if we want */
763 TRACE("handle %04x (block %04x) can be moved.\n",
764 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
765 movearena
= ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
);
766 pMoveArena
= ARENA_PTR(ptr
, movearena
);
767 movesize
= pMoveArena
->next
- movearena
;
768 arena
= pInfo
->first
;
769 pArena
= ARENA_PTR(ptr
, arena
);
772 /* Try to find the smallest arena that will do, */
773 /* which is below us in memory */
776 arena
= pArena
->free_next
;
777 pArena
= ARENA_PTR(ptr
, arena
);
778 if(arena
>= movearena
)
780 if(arena
== pArena
->free_next
)
782 if((pArena
->size
>= movesize
) && (pArena
->size
< size
))
788 if (finalarena
) /* Actually got somewhere to move */
790 TRACE("Moving it to %04x.\n", finalarena
);
791 pFinalArena
= ARENA_PTR(ptr
, finalarena
);
792 size
= pFinalArena
->size
;
793 LOCAL_RemoveFreeBlock(ptr
, finalarena
);
794 LOCAL_ShrinkArena( ds
, finalarena
, movesize
);
795 /* Copy the arena to it's new location */
796 memcpy((char *)pFinalArena
+ ARENA_HEADER_SIZE
,
797 (char *)pMoveArena
+ ARENA_HEADER_SIZE
,
798 movesize
- ARENA_HEADER_SIZE
);
799 /* Free the old location */
800 LOCAL_FreeArena(ds
, movearena
);
801 call_notify_func(pInfo
->notify
, LN_MOVE
,
802 (WORD
)((char *)pEntry
- ptr
), pEntry
->addr
);
803 /* Update handle table entry */
804 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ MOVEABLE_PREFIX
;
806 else if((ARENA_PTR(ptr
, pMoveArena
->prev
& ~3)->prev
& 3)
809 /* Previous arena is free (but < movesize) */
810 /* so we can 'slide' movearena down into it */
811 finalarena
= pMoveArena
->prev
& ~3;
812 LOCAL_GrowArenaDownward( ds
, movearena
, movesize
);
813 /* Update handle table entry */
814 pEntry
->addr
= finalarena
+ ARENA_HEADER_SIZE
+ MOVEABLE_PREFIX
;
818 table
= *(WORD
*)pEntry
;
820 freespace
= LOCAL_GetFreeSpace(ds
, minfree
? 0 : 1);
821 if(freespace
>= minfree
|| (flags
& LMEM_NODISCARD
))
823 TRACE("Returning %04x.\n", freespace
);
827 table
= pInfo
->htable
;
830 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
831 for(count
= *(WORD
*)(ptr
+ table
); count
> 0; count
--, pEntry
++)
833 if(pEntry
->addr
&& pEntry
->lock
== 0 &&
834 (pEntry
->flags
& (LMEM_DISCARDABLE
>> 8)))
836 TRACE("Discarding handle %04x (block %04x).\n",
837 (char *)pEntry
- ptr
, pEntry
->addr
);
838 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
));
839 call_notify_func(pInfo
->notify
, LN_DISCARD
, (char *)pEntry
- ptr
, pEntry
->flags
);
841 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
844 table
= *(WORD
*)pEntry
;
846 return LOCAL_Compact(ds
, 0xffff, LMEM_NODISCARD
);
850 /***********************************************************************
851 * LOCAL_FindFreeBlock
853 static HLOCAL16
LOCAL_FindFreeBlock( HANDLE16 ds
, WORD size
)
855 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
856 LOCALHEAPINFO
*pInfo
;
860 if (!(pInfo
= LOCAL_GetHeap( ds
)))
862 ERR("Local heap not found\n" );
867 arena
= pInfo
->first
;
868 pArena
= ARENA_PTR( ptr
, arena
);
870 arena
= pArena
->free_next
;
871 pArena
= ARENA_PTR( ptr
, arena
);
872 if (arena
== pArena
->free_next
) break;
873 if (pArena
->size
>= size
) return arena
;
875 TRACE("not enough space\n" );
881 /***********************************************************************
884 static const char *get_heap_name( WORD ds
)
886 HINSTANCE16 inst
= LoadLibrary16( "GDI" );
887 if (ds
== GlobalHandleToSel16( inst
))
889 FreeLibrary16( inst
);
892 FreeLibrary16( inst
);
893 inst
= LoadLibrary16( "USER" );
894 if (ds
== GlobalHandleToSel16( inst
))
896 FreeLibrary16( inst
);
899 FreeLibrary16( inst
);
903 /***********************************************************************
905 * The segment may get moved around in this function, so all callers
906 * should reset their pointer variables.
908 static HLOCAL16
LOCAL_GetBlock( HANDLE16 ds
, WORD size
, WORD flags
)
910 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
911 LOCALHEAPINFO
*pInfo
;
915 if (!(pInfo
= LOCAL_GetHeap( ds
)))
917 ERR("Local heap not found\n");
922 size
+= ARENA_HEADER_SIZE
;
923 size
= LALIGN( max( size
, sizeof(LOCALARENA
) ) );
928 /* Find a suitable free block */
929 arena
= LOCAL_FindFreeBlock( ds
, size
);
931 /* no space: try to make some */
932 LOCAL_Compact( ds
, size
, flags
);
933 arena
= LOCAL_FindFreeBlock( ds
, size
);
936 /* still no space: try to grow the segment */
937 if (!(LOCAL_GrowHeap( ds
)))
940 /* FIXME: doesn't work correctly yet */
941 if (call_notify_func(pInfo
->notify
, LN_OUTOFMEM
, ds
- 20, size
)) /* FIXME: "size" correct ? (should indicate bytes needed) */
944 ERR( "not enough space in %s heap %04x for %d bytes\n",
945 get_heap_name(ds
), ds
, size
);
948 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
949 pInfo
= LOCAL_GetHeap( ds
);
950 arena
= LOCAL_FindFreeBlock( ds
, size
);
953 ERR( "not enough space in %s heap %04x for %d bytes\n",
954 get_heap_name(ds
), ds
, size
);
956 /* FIXME: "size" correct ? (should indicate bytes needed) */
957 if (call_notify_func(pInfo
->notify
, LN_OUTOFMEM
, ds
, size
)) goto notify_done
;
962 /* Make a block out of the free arena */
963 pArena
= ARENA_PTR( ptr
, arena
);
964 TRACE("size = %04x, arena %04x size %04x\n",
965 size
, arena
, pArena
->size
);
966 LOCAL_RemoveFreeBlock( ptr
, arena
);
967 LOCAL_ShrinkArena( ds
, arena
, size
);
969 if (flags
& LMEM_ZEROINIT
)
970 memset((char *)pArena
+ ARENA_HEADER_SIZE
, 0, size
-ARENA_HEADER_SIZE
);
971 return arena
+ ARENA_HEADER_SIZE
;
975 /***********************************************************************
978 static BOOL16
LOCAL_NewHTable( HANDLE16 ds
)
980 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
981 LOCALHEAPINFO
*pInfo
;
982 LOCALHANDLEENTRY
*pEntry
;
987 if (!(pInfo
= LOCAL_GetHeap( ds
)))
989 ERR("Local heap not found\n");
994 if (!(handle
= LOCAL_GetBlock( ds
, pInfo
->hdelta
* sizeof(LOCALHANDLEENTRY
)
995 + 2 * sizeof(WORD
), LMEM_FIXED
)))
997 if (!(ptr
= MapSL( MAKESEGPTR( ds
, 0 ) )))
998 ERR("ptr == NULL after GetBlock.\n");
999 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1000 ERR("pInfo == NULL after GetBlock.\n");
1002 /* Fill the entry table */
1004 *(WORD
*)(ptr
+ handle
) = pInfo
->hdelta
;
1005 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
+ sizeof(WORD
));
1006 for (i
= pInfo
->hdelta
; i
> 0; i
--, pEntry
++) {
1007 pEntry
->lock
= pEntry
->flags
= 0xff;
1010 *(WORD
*)pEntry
= pInfo
->htable
;
1011 pInfo
->htable
= handle
;
1016 /***********************************************************************
1017 * LOCAL_GetNewHandleEntry
1019 static HLOCAL16
LOCAL_GetNewHandleEntry( HANDLE16 ds
)
1021 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1022 LOCALHEAPINFO
*pInfo
;
1023 LOCALHANDLEENTRY
*pEntry
= NULL
;
1026 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1028 ERR("Local heap not found\n");
1029 LOCAL_PrintHeap(ds
);
1033 /* Find a free slot in existing tables */
1035 table
= pInfo
->htable
;
1038 WORD count
= *(WORD
*)(ptr
+ table
);
1039 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1040 for (; count
> 0; count
--, pEntry
++)
1041 if (pEntry
->lock
== 0xff) break;
1043 table
= *(WORD
*)pEntry
;
1046 if (!table
) /* We need to create a new table */
1048 if (!LOCAL_NewHTable( ds
)) return 0;
1049 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1050 pInfo
= LOCAL_GetHeap( ds
);
1051 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ pInfo
->htable
+ sizeof(WORD
));
1054 /* Now allocate this entry */
1058 TRACE("(%04x): %04x\n",
1059 ds
, ((char *)pEntry
- ptr
) );
1060 return (HLOCAL16
)((char *)pEntry
- ptr
);
1064 /***********************************************************************
1065 * LOCAL_FreeHandleEntry
1067 * Free a handle table entry.
1069 static void LOCAL_FreeHandleEntry( HANDLE16 ds
, HLOCAL16 handle
)
1071 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1072 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1073 LOCALHEAPINFO
*pInfo
;
1075 WORD table
, count
, i
;
1077 if (!(pInfo
= LOCAL_GetHeap( ds
))) return;
1079 /* Find the table where this handle comes from */
1081 pTable
= &pInfo
->htable
;
1084 WORD size
= (*(WORD
*)(ptr
+ *pTable
)) * sizeof(LOCALHANDLEENTRY
);
1085 if ((handle
>= *pTable
+ sizeof(WORD
)) &&
1086 (handle
< *pTable
+ sizeof(WORD
) + size
)) break; /* Found it */
1087 pTable
= (WORD
*)(ptr
+ *pTable
+ sizeof(WORD
) + size
);
1091 ERR("Invalid entry %04x\n", handle
);
1092 LOCAL_PrintHeap( ds
);
1096 /* Make the entry free */
1098 pEntry
->addr
= 0; /* just in case */
1099 pEntry
->lock
= 0xff;
1100 pEntry
->flags
= 0xff;
1101 /* Now check if all entries in this table are free */
1104 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ table
+ sizeof(WORD
));
1105 count
= *(WORD
*)(ptr
+ table
);
1106 for (i
= count
; i
> 0; i
--, pEntry
++) if (pEntry
->lock
!= 0xff) return;
1108 /* Remove the table from the linked list and free it */
1110 TRACE("(%04x): freeing table %04x\n",
1112 *pTable
= *(WORD
*)pEntry
;
1113 LOCAL_FreeArena( ds
, ARENA_HEADER( table
) );
1117 /***********************************************************************
1118 * LocalFree (KERNEL.7)
1120 HLOCAL16 WINAPI
LocalFree16( HLOCAL16 handle
)
1122 HANDLE16 ds
= CURRENT_DS
;
1123 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1125 TRACE("%04x ds=%04x\n", handle
, ds
);
1127 if (!handle
) { WARN("Handle is 0.\n" ); return 0; }
1128 if (HANDLE_FIXED( handle
))
1130 if (!LOCAL_FreeArena( ds
, ARENA_HEADER( handle
) )) return 0; /* OK */
1131 else return handle
; /* couldn't free it */
1135 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1136 if (pEntry
->flags
!= (LMEM_DISCARDED
>> 8))
1138 TRACE("real block at %04x\n",
1140 if (LOCAL_FreeArena( ds
, ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
) ))
1141 return handle
; /* couldn't free it */
1143 LOCAL_FreeHandleEntry( ds
, handle
);
1149 /***********************************************************************
1150 * LocalAlloc (KERNEL.5)
1152 HLOCAL16 WINAPI
LocalAlloc16( UINT16 flags
, WORD size
)
1154 HANDLE16 ds
= CURRENT_DS
;
1155 HLOCAL16 handle
= 0;
1158 TRACE("%04x %d ds=%04x\n", flags
, size
, ds
);
1160 if(size
> 0 && size
<= 4) size
= 5;
1161 if (flags
& LMEM_MOVEABLE
)
1163 LOCALHANDLEENTRY
*plhe
;
1168 if (!(hmem
= LOCAL_GetBlock( ds
, size
+ MOVEABLE_PREFIX
, flags
)))
1171 else /* We just need to allocate a discarded handle */
1173 if (!(handle
= LOCAL_GetNewHandleEntry( ds
)))
1175 WARN("Couldn't get handle.\n");
1177 LOCAL_FreeArena( ds
, ARENA_HEADER(hmem
) );
1180 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1181 plhe
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1185 plhe
->addr
= hmem
+ MOVEABLE_PREFIX
;
1186 plhe
->flags
= (BYTE
)((flags
& 0x0f00) >> 8);
1187 *(HLOCAL16
*)(ptr
+ hmem
) = handle
;
1192 plhe
->flags
= LMEM_DISCARDED
>> 8;
1197 if(size
) handle
= LOCAL_GetBlock( ds
, size
, flags
);
1201 CURRENT_STACK16
->ecx
= handle
; /* must be returned in cx too */
1206 /***********************************************************************
1207 * LocalReAlloc (KERNEL.6)
1209 HLOCAL16 WINAPI
LocalReAlloc16( HLOCAL16 handle
, WORD size
, UINT16 flags
)
1211 HANDLE16 ds
= CURRENT_DS
;
1212 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1213 LOCALHEAPINFO
*pInfo
;
1214 LOCALARENA
*pArena
, *pNext
;
1215 LOCALHANDLEENTRY
*pEntry
= NULL
;
1216 WORD arena
, oldsize
;
1217 HLOCAL16 hmem
, blockhandle
;
1220 if (!handle
) return 0;
1221 if(HANDLE_MOVEABLE(handle
) &&
1222 ((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
== 0xff) /* An unused handle */
1225 TRACE("%04x %d %04x ds=%04x\n",
1226 handle
, size
, flags
, ds
);
1227 if (!(pInfo
= LOCAL_GetHeap( ds
))) return 0;
1229 if (HANDLE_FIXED( handle
))
1230 blockhandle
= handle
;
1233 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1234 if(pEntry
->flags
== (LMEM_DISCARDED
>> 8))
1238 WARN("Dicarded block has non-zero addr.\n");
1239 TRACE("ReAllocating discarded block\n");
1240 if(size
<= 4) size
= 5;
1241 if (!(hl
= LOCAL_GetBlock( ds
, size
+ MOVEABLE_PREFIX
, flags
)))
1243 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) ); /* Reload ptr */
1244 pEntry
= (LOCALHANDLEENTRY
*) (ptr
+ handle
);
1245 pEntry
->addr
= hl
+ MOVEABLE_PREFIX
;
1248 *(HLOCAL16
*)(ptr
+ hl
) = handle
;
1251 if (((blockhandle
= pEntry
->addr
- MOVEABLE_PREFIX
) & 3) != 0)
1253 ERR("(%04x,%04x): invalid handle\n",
1257 if (*(HLOCAL16
*)(ptr
+ blockhandle
) != handle
) {
1258 ERR("Back ptr to handle is invalid\n");
1263 if (flags
& LMEM_MODIFY
)
1265 if (HANDLE_MOVEABLE(handle
))
1267 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1268 pEntry
->flags
= (flags
& 0x0f00) >> 8;
1269 TRACE("Changing flags to %x.\n", pEntry
->flags
);
1276 if (flags
& LMEM_MOVEABLE
)
1278 if (HANDLE_FIXED(handle
))
1280 TRACE("Freeing fixed block.\n");
1281 return LocalFree16( handle
);
1283 else /* Moveable block */
1285 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1286 if (pEntry
->lock
== 0)
1288 /* discards moveable blocks */
1289 TRACE("Discarding block\n");
1290 LOCAL_FreeArena(ds
, ARENA_HEADER(pEntry
->addr
- MOVEABLE_PREFIX
));
1292 pEntry
->flags
= (LMEM_DISCARDED
>> 8);
1300 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1301 if (pEntry
->lock
== 0)
1304 return LocalFree16( handle
);
1310 arena
= ARENA_HEADER( blockhandle
);
1311 TRACE("arena is %04x\n", arena
);
1312 pArena
= ARENA_PTR( ptr
, arena
);
1314 if(size
<= 4) size
= 5;
1315 if(HANDLE_MOVEABLE(handle
)) size
+= MOVEABLE_PREFIX
;
1316 oldsize
= pArena
->next
- arena
- ARENA_HEADER_SIZE
;
1317 nextarena
= LALIGN(blockhandle
+ size
);
1319 /* Check for size reduction */
1321 if (nextarena
<= pArena
->next
)
1323 TRACE("size reduction, making new free block\n");
1324 LOCAL_ShrinkArena(ds
, arena
, nextarena
- arena
);
1325 TRACE("returning %04x\n", handle
);
1329 /* Check if the next block is free and large enough */
1331 pNext
= ARENA_PTR( ptr
, pArena
->next
);
1332 if (((pNext
->prev
& 3) == LOCAL_ARENA_FREE
) &&
1333 (nextarena
<= pNext
->next
))
1335 TRACE("size increase, making new free block\n");
1336 LOCAL_GrowArenaUpward(ds
, arena
, nextarena
- arena
);
1337 TRACE("returning %04x\n", handle
);
1341 /* Now we have to allocate a new block, but not if (fixed block or locked
1342 block) and no LMEM_MOVEABLE */
1344 if (!(flags
& LMEM_MOVEABLE
))
1346 if (HANDLE_FIXED(handle
))
1348 ERR("Needed to move fixed block, but LMEM_MOVEABLE not specified.\n");
1353 if(((LOCALHANDLEENTRY
*)(ptr
+ handle
))->lock
!= 0)
1355 ERR("Needed to move locked block, but LMEM_MOVEABLE not specified.\n");
1361 hmem
= LOCAL_GetBlock( ds
, size
, flags
);
1362 ptr
= MapSL( MAKESEGPTR( ds
, 0 )); /* Reload ptr */
1363 if(HANDLE_MOVEABLE(handle
)) /* LOCAL_GetBlock might have triggered */
1364 { /* a compaction, which might in turn have */
1365 blockhandle
= pEntry
->addr
- MOVEABLE_PREFIX
; /* moved the very block we are resizing */
1366 arena
= ARENA_HEADER( blockhandle
); /* thus, we reload arena, too */
1370 /* Remove the block from the heap and try again */
1371 LPSTR buffer
= HeapAlloc( GetProcessHeap(), 0, oldsize
);
1372 if (!buffer
) return 0;
1373 memcpy( buffer
, ptr
+ arena
+ ARENA_HEADER_SIZE
, oldsize
);
1374 LOCAL_FreeArena( ds
, arena
);
1375 if (!(hmem
= LOCAL_GetBlock( ds
, size
, flags
)))
1377 if (!(hmem
= LOCAL_GetBlock( ds
, oldsize
, flags
)))
1379 ERR("Can't restore saved block\n" );
1380 HeapFree( GetProcessHeap(), 0, buffer
);
1385 ptr
= MapSL( MAKESEGPTR( ds
, 0 ) ); /* Reload ptr */
1386 memcpy( ptr
+ hmem
, buffer
, oldsize
);
1387 HeapFree( GetProcessHeap(), 0, buffer
);
1391 memcpy( ptr
+ hmem
, ptr
+ (arena
+ ARENA_HEADER_SIZE
), oldsize
);
1392 LOCAL_FreeArena( ds
, arena
);
1394 if (HANDLE_MOVEABLE( handle
))
1396 TRACE("fixing handle\n");
1397 pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1398 pEntry
->addr
= hmem
+ MOVEABLE_PREFIX
;
1399 /* Back ptr should still be correct */
1400 if(*(HLOCAL16
*)(ptr
+ hmem
) != handle
)
1401 ERR("back ptr is invalid.\n");
1404 if (size
== oldsize
) hmem
= 0; /* Realloc failed */
1405 TRACE("returning %04x\n", hmem
);
1410 /***********************************************************************
1411 * LOCAL_InternalLock
1413 static HLOCAL16
LOCAL_InternalLock( LPSTR heap
, HLOCAL16 handle
)
1415 HLOCAL16 old_handle
= handle
;
1417 if (HANDLE_MOVEABLE(handle
))
1419 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(heap
+ handle
);
1420 if (pEntry
->flags
== (LMEM_DISCARDED
>> 8)) return 0;
1421 if (pEntry
->lock
< 0xfe) pEntry
->lock
++;
1422 handle
= pEntry
->addr
;
1424 TRACE("%04x returning %04x\n",
1425 old_handle
, handle
);
1430 /***********************************************************************
1431 * LocalUnlock (KERNEL.9)
1433 BOOL16 WINAPI
LocalUnlock16( HLOCAL16 handle
)
1435 HANDLE16 ds
= CURRENT_DS
;
1436 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1438 TRACE("%04x\n", handle
);
1439 if (HANDLE_MOVEABLE(handle
))
1441 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1442 if (!pEntry
->lock
|| (pEntry
->lock
== 0xff)) return FALSE
;
1443 /* For moveable block, return the new lock count */
1444 /* (see _Windows_Internals_ p. 197) */
1445 return --pEntry
->lock
;
1451 /***********************************************************************
1452 * LocalSize (KERNEL.10)
1454 UINT16 WINAPI
LocalSize16( HLOCAL16 handle
)
1456 HANDLE16 ds
= CURRENT_DS
;
1457 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1460 TRACE("%04x ds=%04x\n", handle
, ds
);
1462 if (!handle
) return 0;
1463 if (HANDLE_MOVEABLE( handle
))
1465 handle
= *(WORD
*)(ptr
+ handle
);
1466 if (!handle
) return 0;
1467 pArena
= ARENA_PTR( ptr
, ARENA_HEADER(handle
- MOVEABLE_PREFIX
) );
1470 pArena
= ARENA_PTR( ptr
, ARENA_HEADER(handle
) );
1472 return pArena
->next
- handle
;
1476 /***********************************************************************
1477 * LocalFlags (KERNEL.12)
1479 UINT16 WINAPI
LocalFlags16( HLOCAL16 handle
)
1481 HANDLE16 ds
= CURRENT_DS
;
1482 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1484 if (HANDLE_MOVEABLE(handle
))
1486 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+ handle
);
1487 TRACE("(%04x,%04x): returning %04x\n",
1488 ds
, handle
, pEntry
->lock
| (pEntry
->flags
<< 8) );
1489 return pEntry
->lock
| (pEntry
->flags
<< 8);
1493 TRACE("(%04x,%04x): returning 0\n",
1500 /***********************************************************************
1501 * LocalHeapSize (KERNEL.162)
1503 WORD WINAPI
LocalHeapSize16(void)
1505 HANDLE16 ds
= CURRENT_DS
;
1506 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1507 return pInfo
? pInfo
->last
- pInfo
->first
: 0;
1511 /***********************************************************************
1512 * LocalCountFree (KERNEL.161)
1514 WORD WINAPI
LocalCountFree16(void)
1516 HANDLE16 ds
= CURRENT_DS
;
1519 LOCALHEAPINFO
*pInfo
;
1520 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1522 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1524 ERR("(%04x): Local heap not found\n", ds
);
1525 LOCAL_PrintHeap( ds
);
1530 arena
= pInfo
->first
;
1531 pArena
= ARENA_PTR( ptr
, arena
);
1534 arena
= pArena
->free_next
;
1535 pArena
= ARENA_PTR( ptr
, arena
);
1536 if (arena
== pArena
->free_next
) break;
1537 total
+= pArena
->size
;
1539 TRACE("(%04x): returning %d\n", ds
, total
);
1544 /***********************************************************************
1545 * LocalHandle (KERNEL.11)
1547 HLOCAL16 WINAPI
LocalHandle16( WORD addr
)
1549 HANDLE16 ds
= CURRENT_DS
;
1550 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1551 LOCALHEAPINFO
*pInfo
;
1554 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1556 ERR("(%04x): Local heap not found\n", ds
);
1557 LOCAL_PrintHeap( ds
);
1561 /* Find the address in the entry tables */
1563 table
= pInfo
->htable
;
1566 WORD count
= *(WORD
*)(ptr
+ table
);
1567 LOCALHANDLEENTRY
*pEntry
= (LOCALHANDLEENTRY
*)(ptr
+table
+sizeof(WORD
));
1568 for (; count
> 0; count
--, pEntry
++)
1569 if (pEntry
->addr
== addr
) return (HLOCAL16
)((char *)pEntry
- ptr
);
1570 table
= *(WORD
*)pEntry
;
1573 return (HLOCAL16
)addr
; /* Fixed block handle is addr */
1579 /***********************************************************************
1580 * LocalLock (KERNEL.8)
1582 * Note: only the offset part of the pointer is returned by the relay code.
1584 SEGPTR WINAPI
LocalLock16( HLOCAL16 handle
)
1586 WORD ds
= CURRENT_DS
;
1587 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1588 return MAKESEGPTR( ds
, LOCAL_InternalLock( ptr
, handle
) );
1592 /***********************************************************************
1593 * LocalCompact (KERNEL.13)
1595 UINT16 WINAPI
LocalCompact16( UINT16 minfree
)
1597 TRACE("%04x\n", minfree
);
1598 return LOCAL_Compact( CURRENT_DS
, minfree
, 0 );
1602 /***********************************************************************
1603 * LocalNotify (KERNEL.14)
1605 * Installs a callback function that is called for local memory events
1606 * Callback function prototype is
1607 * BOOL16 NotifyFunc(WORD wMsg, HLOCAL16 hMem, WORD wArg)
1610 * NotifyFunc seems to be responsible for allocating some memory,
1611 * returns TRUE for success.
1612 * wArg = number of bytes needed additionally
1614 * hMem = handle; wArg = old mem location
1616 * NotifyFunc seems to be strongly encouraged to return TRUE,
1617 * otherwise LogError() gets called.
1618 * hMem = handle; wArg = flags
1620 FARPROC16 WINAPI
LocalNotify16( FARPROC16 func
)
1622 LOCALHEAPINFO
*pInfo
;
1623 FARPROC16 oldNotify
;
1624 HANDLE16 ds
= CURRENT_DS
;
1626 if (!(pInfo
= LOCAL_GetHeap( ds
)))
1628 ERR("(%04x): Local heap not found\n", ds
);
1629 LOCAL_PrintHeap( ds
);
1632 TRACE("(%04x): %08lx\n", ds
, (DWORD
)func
);
1633 FIXME("Half implemented\n");
1634 oldNotify
= pInfo
->notify
;
1635 pInfo
->notify
= func
;
1640 /***********************************************************************
1641 * LocalShrink (KERNEL.121)
1643 UINT16 WINAPI
LocalShrink16( HGLOBAL16 handle
, UINT16 newsize
)
1645 TRACE("%04x %04x\n", handle
, newsize
);
1650 /***********************************************************************
1651 * GetHeapSpaces (KERNEL.138)
1653 DWORD WINAPI
GetHeapSpaces16( HMODULE16 module
)
1656 WORD oldDS
= CURRENT_DS
;
1659 if (!(pModule
= NE_GetPtr( module
))) return 0;
1660 CURRENT_DS
= GlobalHandleToSel16((NE_SEG_TABLE( pModule
) + pModule
->ne_autodata
- 1)->hSeg
);
1661 spaces
= MAKELONG( LocalCountFree16(), LocalHeapSize16() );
1667 /***********************************************************************
1668 * LocalHandleDelta (KERNEL.310)
1670 WORD WINAPI
LocalHandleDelta16( WORD delta
)
1672 LOCALHEAPINFO
*pInfo
;
1674 if (!(pInfo
= LOCAL_GetHeap( CURRENT_DS
)))
1676 ERR("Local heap not found\n");
1677 LOCAL_PrintHeap( CURRENT_DS
);
1680 if (delta
) pInfo
->hdelta
= delta
;
1681 TRACE("returning %04x\n", pInfo
->hdelta
);
1682 return pInfo
->hdelta
;
1686 /***********************************************************************
1687 * LocalInfo (TOOLHELP.56)
1689 BOOL16 WINAPI
LocalInfo16( LOCALINFO
*pLocalInfo
, HGLOBAL16 handle
)
1691 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap(SELECTOROF(K32WOWGlobalLock16(handle
)));
1692 if (!pInfo
) return FALSE
;
1693 pLocalInfo
->wcItems
= pInfo
->items
;
1698 /***********************************************************************
1699 * LocalFirst (TOOLHELP.57)
1701 BOOL16 WINAPI
LocalFirst16( LOCALENTRY
*pLocalEntry
, HGLOBAL16 handle
)
1703 WORD ds
= GlobalHandleToSel16( handle
);
1704 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1705 LOCALHEAPINFO
*pInfo
= LOCAL_GetHeap( ds
);
1706 if (!pInfo
) return FALSE
;
1708 pLocalEntry
->hHandle
= pInfo
->first
+ ARENA_HEADER_SIZE
;
1709 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1710 pLocalEntry
->wFlags
= LF_FIXED
;
1711 pLocalEntry
->wcLock
= 0;
1712 pLocalEntry
->wType
= LT_NORMAL
;
1713 pLocalEntry
->hHeap
= handle
;
1714 pLocalEntry
->wHeapType
= NORMAL_HEAP
;
1715 pLocalEntry
->wNext
= ARENA_PTR(ptr
,pInfo
->first
)->next
;
1716 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1721 /***********************************************************************
1722 * LocalNext (TOOLHELP.58)
1724 BOOL16 WINAPI
LocalNext16( LOCALENTRY
*pLocalEntry
)
1726 WORD ds
= GlobalHandleToSel16( pLocalEntry
->hHeap
);
1727 char *ptr
= MapSL( MAKESEGPTR( ds
, 0 ) );
1730 if (!LOCAL_GetHeap( ds
)) return FALSE
;
1731 if (!pLocalEntry
->wNext
) return FALSE
;
1732 pArena
= ARENA_PTR( ptr
, pLocalEntry
->wNext
);
1734 pLocalEntry
->hHandle
= pLocalEntry
->wNext
+ ARENA_HEADER_SIZE
;
1735 pLocalEntry
->wAddress
= pLocalEntry
->hHandle
;
1736 pLocalEntry
->wFlags
= (pArena
->prev
& 3) + 1;
1737 pLocalEntry
->wcLock
= 0;
1738 pLocalEntry
->wType
= LT_NORMAL
;
1739 if (pArena
->next
!= pLocalEntry
->wNext
) /* last one? */
1740 pLocalEntry
->wNext
= pArena
->next
;
1742 pLocalEntry
->wNext
= 0;
1743 pLocalEntry
->wSize
= pLocalEntry
->wNext
- pLocalEntry
->hHandle
;
1748 /***********************************************************************
1749 * 32-bit local heap functions (Win95; undocumented)
1752 /***********************************************************************
1755 HANDLE WINAPI
Local32Init16( WORD segment
, DWORD tableSize
,
1756 DWORD heapSize
, DWORD flags
)
1758 DWORD totSize
, segSize
= 0;
1760 LOCAL32HEADER
*header
;
1762 WORD
*selectorTable
;
1763 WORD selectorEven
, selectorOdd
;
1766 /* Determine new heap size */
1770 if ( (segSize
= GetSelectorLimit16( segment
)) == 0 )
1776 if ( heapSize
== -1L )
1777 heapSize
= 1024L*1024L; /* FIXME */
1779 heapSize
= (heapSize
+ 0xffff) & 0xffff0000;
1780 segSize
= (segSize
+ 0x0fff) & 0xfffff000;
1781 totSize
= segSize
+ HTABLE_SIZE
+ heapSize
;
1784 /* Allocate memory and initialize heap */
1786 if ( !(base
= VirtualAlloc( NULL
, totSize
, MEM_RESERVE
, PAGE_READWRITE
)) )
1789 if ( !VirtualAlloc( base
, segSize
+ HTABLE_PAGESIZE
,
1790 MEM_COMMIT
, PAGE_READWRITE
) )
1792 VirtualFree( base
, 0, MEM_RELEASE
);
1796 if (!(heap
= RtlCreateHeap( 0, base
+ segSize
+ HTABLE_SIZE
, heapSize
, 0x10000, NULL
, NULL
)))
1798 VirtualFree( base
, 0, MEM_RELEASE
);
1803 /* Set up header and handle table */
1805 header
= (LOCAL32HEADER
*)(base
+ segSize
);
1806 header
->base
= base
;
1807 header
->limit
= HTABLE_PAGESIZE
-1;
1809 header
->magic
= LOCAL32_MAGIC
;
1810 header
->heap
= heap
;
1812 header
->freeListFirst
[0] = sizeof(LOCAL32HEADER
);
1813 header
->freeListLast
[0] = HTABLE_PAGESIZE
- 4;
1814 header
->freeListSize
[0] = (HTABLE_PAGESIZE
- sizeof(LOCAL32HEADER
)) / 4;
1816 for (i
= header
->freeListFirst
[0]; i
< header
->freeListLast
[0]; i
+= 4)
1817 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1819 header
->freeListFirst
[1] = 0xffff;
1822 /* Set up selector table */
1824 nrBlocks
= (totSize
+ 0x7fff) >> 15;
1825 selectorTable
= HeapAlloc( header
->heap
, 0, nrBlocks
* 2 );
1826 selectorEven
= SELECTOR_AllocBlock( base
, totSize
, WINE_LDT_FLAGS_DATA
);
1827 selectorOdd
= SELECTOR_AllocBlock( base
+ 0x8000, totSize
- 0x8000, WINE_LDT_FLAGS_DATA
);
1828 if ( !selectorTable
|| !selectorEven
|| !selectorOdd
)
1830 HeapFree( header
->heap
, 0, selectorTable
);
1831 if ( selectorEven
) SELECTOR_FreeBlock( selectorEven
);
1832 if ( selectorOdd
) SELECTOR_FreeBlock( selectorOdd
);
1833 HeapDestroy( header
->heap
);
1834 VirtualFree( base
, 0, MEM_RELEASE
);
1838 header
->selectorTableOffset
= (LPBYTE
)selectorTable
- header
->base
;
1839 header
->selectorTableSize
= nrBlocks
* 4; /* ??? Win95 does it this way! */
1840 header
->selectorDelta
= selectorEven
- selectorOdd
;
1841 header
->segment
= segment
? segment
: selectorEven
;
1843 for (i
= 0; i
< nrBlocks
; i
++)
1844 selectorTable
[i
] = (i
& 1)? selectorOdd
+ ((i
>> 1) << __AHSHIFT
)
1845 : selectorEven
+ ((i
>> 1) << __AHSHIFT
);
1847 /* Move old segment */
1851 /* FIXME: This is somewhat ugly and relies on implementation
1852 details about 16-bit global memory handles ... */
1854 LPBYTE oldBase
= (LPBYTE
)GetSelectorBase( segment
);
1855 memcpy( base
, oldBase
, segSize
);
1856 GLOBAL_MoveBlock( segment
, base
, totSize
);
1857 HeapFree( GetProcessHeap(), 0, oldBase
);
1860 return (HANDLE
)header
;
1863 /***********************************************************************
1864 * Local32_SearchHandle
1866 static LPDWORD
Local32_SearchHandle( LOCAL32HEADER
*header
, DWORD addr
)
1870 for ( handle
= (LPDWORD
)((LPBYTE
)header
+ sizeof(LOCAL32HEADER
));
1871 handle
< (LPDWORD
)((LPBYTE
)header
+ header
->limit
);
1874 if (*handle
== addr
)
1881 /***********************************************************************
1884 static VOID
Local32_ToHandle( LOCAL32HEADER
*header
, INT16 type
,
1885 DWORD addr
, LPDWORD
*handle
, LPBYTE
*ptr
)
1892 case -2: /* 16:16 pointer, no handles */
1893 *ptr
= MapSL( addr
);
1894 *handle
= (LPDWORD
)*ptr
;
1897 case -1: /* 32-bit offset, no handles */
1898 *ptr
= header
->base
+ addr
;
1899 *handle
= (LPDWORD
)*ptr
;
1902 case 0: /* handle */
1903 if ( addr
>= sizeof(LOCAL32HEADER
)
1904 && addr
< header
->limit
&& !(addr
& 3)
1905 && *(LPDWORD
)((LPBYTE
)header
+ addr
) >= HTABLE_SIZE
)
1907 *handle
= (LPDWORD
)((LPBYTE
)header
+ addr
);
1908 *ptr
= header
->base
+ **handle
;
1912 case 1: /* 16:16 pointer */
1913 *ptr
= MapSL( addr
);
1914 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1917 case 2: /* 32-bit offset */
1918 *ptr
= header
->base
+ addr
;
1919 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1924 /***********************************************************************
1925 * Local32_FromHandle
1927 static VOID
Local32_FromHandle( LOCAL32HEADER
*header
, INT16 type
,
1928 DWORD
*addr
, LPDWORD handle
, LPBYTE ptr
)
1932 case -2: /* 16:16 pointer */
1935 WORD
*selTable
= (LPWORD
)(header
->base
+ header
->selectorTableOffset
);
1936 DWORD offset
= (LPBYTE
)ptr
- header
->base
;
1937 *addr
= MAKELONG( offset
& 0x7fff, selTable
[offset
>> 15] );
1941 case -1: /* 32-bit offset */
1943 *addr
= ptr
- header
->base
;
1946 case 0: /* handle */
1947 *addr
= (LPBYTE
)handle
- (LPBYTE
)header
;
1952 /***********************************************************************
1955 DWORD WINAPI
Local32Alloc16( HANDLE heap
, DWORD size
, INT16 type
, DWORD flags
)
1957 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1962 /* Allocate memory */
1963 ptr
= HeapAlloc( header
->heap
,
1964 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0, size
);
1968 /* Allocate handle if requested */
1973 /* Find first page of handle table with free slots */
1974 for (page
= 0; page
< HTABLE_NPAGES
; page
++)
1975 if (header
->freeListFirst
[page
] != 0)
1977 if (page
== HTABLE_NPAGES
)
1979 WARN("Out of handles!\n" );
1980 HeapFree( header
->heap
, 0, ptr
);
1984 /* If virgin page, initialize it */
1985 if (header
->freeListFirst
[page
] == 0xffff)
1987 if ( !VirtualAlloc( (LPBYTE
)header
+ (page
<< 12),
1988 0x1000, MEM_COMMIT
, PAGE_READWRITE
) )
1990 WARN("Cannot grow handle table!\n" );
1991 HeapFree( header
->heap
, 0, ptr
);
1995 header
->limit
+= HTABLE_PAGESIZE
;
1997 header
->freeListFirst
[page
] = 0;
1998 header
->freeListLast
[page
] = HTABLE_PAGESIZE
- 4;
1999 header
->freeListSize
[page
] = HTABLE_PAGESIZE
/ 4;
2001 for (i
= 0; i
< HTABLE_PAGESIZE
; i
+= 4)
2002 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
2004 if (page
< HTABLE_NPAGES
-1)
2005 header
->freeListFirst
[page
+1] = 0xffff;
2008 /* Allocate handle slot from page */
2009 handle
= (LPDWORD
)((LPBYTE
)header
+ header
->freeListFirst
[page
]);
2010 if (--header
->freeListSize
[page
] == 0)
2011 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = 0;
2013 header
->freeListFirst
[page
] = *handle
;
2015 /* Store 32-bit offset in handle slot */
2016 *handle
= ptr
- header
->base
;
2020 handle
= (LPDWORD
)ptr
;
2025 /* Convert handle to requested output type */
2026 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
2030 /***********************************************************************
2033 DWORD WINAPI
Local32ReAlloc16( HANDLE heap
, DWORD addr
, INT16 type
,
2034 DWORD size
, DWORD flags
)
2036 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2041 return Local32Alloc16( heap
, size
, type
, flags
);
2043 /* Retrieve handle and pointer */
2044 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2045 if (!handle
) return FALSE
;
2047 /* Reallocate memory block */
2048 ptr
= HeapReAlloc( header
->heap
,
2049 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0,
2055 *handle
= ptr
- header
->base
;
2057 handle
= (LPDWORD
)ptr
;
2059 /* Convert handle to requested output type */
2060 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
2064 /***********************************************************************
2067 BOOL WINAPI
Local32Free16( HANDLE heap
, DWORD addr
, INT16 type
)
2069 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2073 /* Retrieve handle and pointer */
2074 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2075 if (!handle
) return FALSE
;
2077 /* Free handle if necessary */
2080 int offset
= (LPBYTE
)handle
- (LPBYTE
)header
;
2081 int page
= offset
>> 12;
2083 /* Return handle slot to page free list */
2084 if (header
->freeListSize
[page
]++ == 0)
2085 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = offset
;
2087 *(LPDWORD
)((LPBYTE
)header
+ header
->freeListLast
[page
]) = offset
,
2088 header
->freeListLast
[page
] = offset
;
2092 /* Shrink handle table when possible */
2093 while (page
> 0 && header
->freeListSize
[page
] == HTABLE_PAGESIZE
/ 4)
2095 if ( VirtualFree( (LPBYTE
)header
+
2096 (header
->limit
& ~(HTABLE_PAGESIZE
-1)),
2097 HTABLE_PAGESIZE
, MEM_DECOMMIT
) )
2100 header
->limit
-= HTABLE_PAGESIZE
;
2101 header
->freeListFirst
[page
] = 0xffff;
2107 return HeapFree( header
->heap
, 0, ptr
);
2110 /***********************************************************************
2113 DWORD WINAPI
Local32Translate16( HANDLE heap
, DWORD addr
, INT16 type1
, INT16 type2
)
2115 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2119 Local32_ToHandle( header
, type1
, addr
, &handle
, &ptr
);
2120 if (!handle
) return 0;
2122 Local32_FromHandle( header
, type2
, &addr
, handle
, ptr
);
2126 /***********************************************************************
2129 DWORD WINAPI
Local32Size16( HANDLE heap
, DWORD addr
, INT16 type
)
2131 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2135 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2136 if (!handle
) return 0;
2138 return HeapSize( header
->heap
, 0, ptr
);
2141 /***********************************************************************
2144 BOOL WINAPI
Local32ValidHandle16( HANDLE heap
, WORD addr
)
2146 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2150 Local32_ToHandle( header
, 0, addr
, &handle
, &ptr
);
2151 return handle
!= NULL
;
2154 /***********************************************************************
2157 WORD WINAPI
Local32GetSegment16( HANDLE heap
)
2159 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2160 return header
->segment
;
2163 /***********************************************************************
2166 static LOCAL32HEADER
*Local32_GetHeap( HGLOBAL16 handle
)
2168 WORD selector
= GlobalHandleToSel16( handle
);
2169 DWORD base
= GetSelectorBase( selector
);
2170 DWORD limit
= GetSelectorLimit16( selector
);
2172 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2175 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2176 return (LOCAL32HEADER
*)base
;
2181 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2182 return (LOCAL32HEADER
*)base
;
2187 /***********************************************************************
2188 * Local32Info (KERNEL.444)
2189 * Local32Info (TOOLHELP.84)
2191 BOOL16 WINAPI
Local32Info16( LOCAL32INFO
*pLocal32Info
, HGLOBAL16 handle
)
2193 PROCESS_HEAP_ENTRY entry
;
2196 LOCAL32HEADER
*header
= Local32_GetHeap( handle
);
2197 if ( !header
) return FALSE
;
2199 if ( !pLocal32Info
|| pLocal32Info
->dwSize
< sizeof(LOCAL32INFO
) )
2202 pLocal32Info
->dwMemReserved
= 0;
2203 pLocal32Info
->dwMemCommitted
= 0;
2204 pLocal32Info
->dwTotalFree
= 0;
2205 pLocal32Info
->dwLargestFreeBlock
= 0;
2207 while (HeapWalk( header
->heap
, &entry
))
2209 if (entry
.wFlags
& PROCESS_HEAP_REGION
)
2211 pLocal32Info
->dwMemReserved
+= entry
.u
.Region
.dwCommittedSize
2212 + entry
.u
.Region
.dwUnCommittedSize
;
2213 pLocal32Info
->dwMemCommitted
= entry
.u
.Region
.dwCommittedSize
;
2215 else if (!(entry
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
))
2217 DWORD size
= entry
.cbData
+ entry
.cbOverhead
;
2218 pLocal32Info
->dwTotalFree
+= size
;
2219 if (size
> pLocal32Info
->dwLargestFreeBlock
) pLocal32Info
->dwLargestFreeBlock
= size
;
2223 pLocal32Info
->dwcFreeHandles
= 0;
2224 for ( i
= 0; i
< HTABLE_NPAGES
; i
++ )
2226 if ( header
->freeListFirst
[i
] == 0xffff ) break;
2227 pLocal32Info
->dwcFreeHandles
+= header
->freeListSize
[i
];
2229 pLocal32Info
->dwcFreeHandles
+= (HTABLE_NPAGES
- i
) * HTABLE_PAGESIZE
/4;
2234 /***********************************************************************
2235 * Local32First (KERNEL.445)
2236 * Local32First (TOOLHELP.85)
2238 BOOL16 WINAPI
Local32First16( LOCAL32ENTRY
*pLocal32Entry
, HGLOBAL16 handle
)
2240 FIXME("(%p, %04X): stub!\n", pLocal32Entry
, handle
);
2244 /***********************************************************************
2245 * Local32Next (KERNEL.446)
2246 * Local32Next (TOOLHELP.86)
2248 BOOL16 WINAPI
Local32Next16( LOCAL32ENTRY
*pLocal32Entry
)
2250 FIXME("(%p): stub!\n", pLocal32Entry
);