4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
13 #include "wine/winbase16.h"
14 #include "wine/unicode.h"
15 #include "selectors.h"
22 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(heap
);
27 /* Note: the heap data structures are based on what Pietrek describes in his
28 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
29 * the same, but could be easily adapted if it turns out some programs
33 typedef struct tagARENA_INUSE
35 DWORD size
; /* Block size; must be the first field */
36 WORD magic
; /* Magic number */
37 WORD threadId
; /* Allocating thread id */
38 void *callerEIP
; /* EIP of caller upon allocation */
41 typedef struct tagARENA_FREE
43 DWORD size
; /* Block size; must be the first field */
44 WORD magic
; /* Magic number */
45 WORD threadId
; /* Freeing thread id */
46 struct tagARENA_FREE
*next
; /* Next free arena */
47 struct tagARENA_FREE
*prev
; /* Prev free arena */
50 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
51 #define ARENA_FLAG_PREV_FREE 0x00000002
52 #define ARENA_SIZE_MASK 0xfffffffc
53 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
54 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
56 #define ARENA_INUSE_FILLER 0x55
57 #define ARENA_FREE_FILLER 0xaa
59 #define QUIET 1 /* Suppress messages */
60 #define NOISY 0 /* Report all errors */
62 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
64 /* Max size of the blocks on the free lists */
65 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
67 0x20, 0x80, 0x200, 0xffffffff
78 typedef struct tagSUBHEAP
80 DWORD size
; /* Size of the whole sub-heap */
81 DWORD commitSize
; /* Committed size of the sub-heap */
82 DWORD headerSize
; /* Size of the heap header */
83 struct tagSUBHEAP
*next
; /* Next sub-heap */
84 struct tagHEAP
*heap
; /* Main heap structure */
85 DWORD magic
; /* Magic number */
86 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
89 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
91 typedef struct tagHEAP
93 SUBHEAP subheap
; /* First sub-heap */
94 struct tagHEAP
*next
; /* Next heap for this process */
95 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
96 CRITICAL_SECTION critSection
; /* Critical section for serialization */
97 DWORD flags
; /* Heap flags */
98 DWORD magic
; /* Magic number */
101 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
103 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
104 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
105 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
107 static HEAP
*systemHeap
; /* globally shared heap */
108 static HEAP
*processHeap
; /* main process heap */
109 static HEAP
*segptrHeap
; /* main segptr heap */
110 static HEAP
*firstHeap
; /* head of secondary heaps list */
112 /* address where we try to map the system heap */
113 #define SYSTEM_HEAP_BASE ((void*)0x65430000)
115 static BOOL
HEAP_IsRealArena( HEAP
*heapPtr
, DWORD flags
, LPCVOID block
, BOOL quiet
);
118 #define GET_EIP() (__builtin_return_address(0))
119 #define SET_EIP(ptr) ((ARENA_INUSE*)(ptr) - 1)->callerEIP = GET_EIP()
122 #define SET_EIP(ptr) /* nothing */
123 #endif /* __GNUC__ */
125 /***********************************************************************
128 void HEAP_Dump( HEAP
*heap
)
134 DPRINTF( "Heap: %08lx\n", (DWORD
)heap
);
135 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
136 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
137 subheap
= &heap
->subheap
;
138 while (subheap
->next
)
140 DPRINTF( " -> %08lx", (DWORD
)subheap
->next
);
141 subheap
= subheap
->next
;
144 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
145 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
146 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
147 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
148 heap
->freeList
[i
].arena
.threadId
,
149 (DWORD
)heap
->freeList
[i
].arena
.prev
,
150 (DWORD
)heap
->freeList
[i
].arena
.next
);
152 subheap
= &heap
->subheap
;
155 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
156 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
157 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
159 DPRINTF( "\n Block Stat Size Id\n" );
160 ptr
= (char*)subheap
+ subheap
->headerSize
;
161 while (ptr
< (char *)subheap
+ subheap
->size
)
163 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
165 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
166 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
167 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
168 pArena
->threadId
, (DWORD
)pArena
->prev
,
169 (DWORD
)pArena
->next
);
170 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
171 arenaSize
+= sizeof(ARENA_FREE
);
172 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
174 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
176 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
177 DPRINTF( "%08lx Used %08lx %04x back=%08lx EIP=%p\n",
178 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
179 pArena
->threadId
, *((DWORD
*)pArena
- 1),
181 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
182 arenaSize
+= sizeof(ARENA_INUSE
);
183 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
187 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
188 DPRINTF( "%08lx used %08lx %04x EIP=%p\n",
189 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
190 pArena
->threadId
, pArena
->callerEIP
);
191 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
192 arenaSize
+= sizeof(ARENA_INUSE
);
193 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
196 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
197 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
198 arenaSize
, (arenaSize
* 100) / subheap
->size
);
199 subheap
= subheap
->next
;
204 /***********************************************************************
207 * Pointer to the heap
210 static HEAP
*HEAP_GetPtr(
211 HANDLE heap
/* [in] Handle to the heap */
213 HEAP
*heapPtr
= (HEAP
*)heap
;
214 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
216 ERR("Invalid heap %08x!\n", heap
);
217 SetLastError( ERROR_INVALID_HANDLE
);
220 if (TRACE_ON(heap
) && !HEAP_IsRealArena( heapPtr
, 0, NULL
, NOISY
))
222 HEAP_Dump( heapPtr
);
224 SetLastError( ERROR_INVALID_HANDLE
);
231 /***********************************************************************
232 * HEAP_InsertFreeBlock
234 * Insert a free block into the free list.
236 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
238 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
239 while (pEntry
->size
< pArena
->size
) pEntry
++;
240 pArena
->size
|= ARENA_FLAG_FREE
;
241 pArena
->next
= pEntry
->arena
.next
;
242 pArena
->next
->prev
= pArena
;
243 pArena
->prev
= &pEntry
->arena
;
244 pEntry
->arena
.next
= pArena
;
248 /***********************************************************************
250 * Find the sub-heap containing a given address.
256 static SUBHEAP
*HEAP_FindSubHeap(
257 HEAP
*heap
, /* [in] Heap pointer */
258 LPCVOID ptr
/* [in] Address */
260 SUBHEAP
*sub
= &heap
->subheap
;
263 if (((char *)ptr
>= (char *)sub
) &&
264 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
271 /***********************************************************************
274 * Make sure the heap storage is committed up to (not including) ptr.
276 static inline BOOL
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
278 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
279 size
= (size
+ COMMIT_MASK
) & ~COMMIT_MASK
;
280 if (size
> subheap
->size
) size
= subheap
->size
;
281 if (size
<= subheap
->commitSize
) return TRUE
;
282 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
283 size
- subheap
->commitSize
, MEM_COMMIT
,
284 PAGE_EXECUTE_READWRITE
))
286 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
287 size
- subheap
->commitSize
,
288 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
289 (DWORD
)subheap
->heap
);
292 subheap
->commitSize
= size
;
297 /***********************************************************************
300 * If possible, decommit the heap storage from (including) 'ptr'.
302 static inline BOOL
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
304 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
305 /* round to next block and add one full block */
306 size
= ((size
+ COMMIT_MASK
) & ~COMMIT_MASK
) + COMMIT_MASK
+ 1;
307 if (size
>= subheap
->commitSize
) return TRUE
;
308 if (!VirtualFree( (char *)subheap
+ size
,
309 subheap
->commitSize
- size
, MEM_DECOMMIT
))
311 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
312 subheap
->commitSize
- size
,
313 (DWORD
)((char *)subheap
+ size
),
314 (DWORD
)subheap
->heap
);
317 subheap
->commitSize
= size
;
322 /***********************************************************************
323 * HEAP_CreateFreeBlock
325 * Create a free block at a specified address. 'size' is the size of the
326 * whole block, including the new arena.
328 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
332 /* Create a free arena */
334 pFree
= (ARENA_FREE
*)ptr
;
335 pFree
->threadId
= GetCurrentTask();
336 pFree
->magic
= ARENA_FREE_MAGIC
;
338 /* If debugging, erase the freed block content */
342 char *pEnd
= (char *)ptr
+ size
;
343 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
344 pEnd
= (char *)subheap
+ subheap
->commitSize
;
345 if (pEnd
> (char *)(pFree
+ 1))
346 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
349 /* Check if next block is free also */
351 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
352 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
354 /* Remove the next arena from the free list */
355 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
356 pNext
->next
->prev
= pNext
->prev
;
357 pNext
->prev
->next
= pNext
->next
;
358 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
360 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
363 /* Set the next block PREV_FREE flag and pointer */
365 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
367 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
368 *pNext
|= ARENA_FLAG_PREV_FREE
;
369 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
372 /* Last, insert the new block into the free list */
374 pFree
->size
= size
- sizeof(*pFree
);
375 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
379 /***********************************************************************
380 * HEAP_MakeInUseBlockFree
382 * Turn an in-use block into a free block. Can also decommit the end of
383 * the heap, and possibly even free the sub-heap altogether.
385 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
388 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
390 /* Check if we can merge with previous block */
392 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
394 pFree
= *((ARENA_FREE
**)pArena
- 1);
395 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
396 /* Remove it from the free list */
397 pFree
->next
->prev
= pFree
->prev
;
398 pFree
->prev
->next
= pFree
->next
;
400 else pFree
= (ARENA_FREE
*)pArena
;
402 /* Create a free block */
404 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
405 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
406 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
407 return; /* Not the last block, so nothing more to do */
409 /* Free the whole sub-heap if it's empty and not the original one */
411 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
412 (subheap
!= &subheap
->heap
->subheap
))
414 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
415 /* Remove the free block from the list */
416 pFree
->next
->prev
= pFree
->prev
;
417 pFree
->prev
->next
= pFree
->next
;
418 /* Remove the subheap from the list */
419 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
420 if (pPrev
) pPrev
->next
= subheap
->next
;
421 /* Free the memory */
423 if (subheap
->selector
) FreeSelector16( subheap
->selector
);
424 VirtualFree( subheap
, 0, MEM_RELEASE
);
428 /* Decommit the end of the heap */
430 if (!(subheap
->heap
->flags
& HEAP_SHARED
)) HEAP_Decommit( subheap
, pFree
+ 1 );
434 /***********************************************************************
437 * Shrink an in-use block.
439 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
441 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
443 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
444 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
445 /* assign size plus previous arena flags */
446 pArena
->size
= size
| (pArena
->size
& ~ARENA_SIZE_MASK
);
450 /* Turn off PREV_FREE flag in next block */
451 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
452 if (pNext
< (char *)subheap
+ subheap
->size
)
453 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
457 /***********************************************************************
460 static BOOL
HEAP_InitSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
461 DWORD commitSize
, DWORD totalSize
)
463 SUBHEAP
*subheap
= (SUBHEAP
*)address
;
465 FREE_LIST_ENTRY
*pEntry
;
470 if (flags
& HEAP_SHARED
)
471 commitSize
= totalSize
; /* always commit everything in a shared heap */
472 if (!VirtualAlloc(address
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
474 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
475 commitSize
, (DWORD
)address
);
479 /* Allocate a selector if needed */
481 if (flags
& HEAP_WINE_SEGPTR
)
483 unsigned char selflags
= WINE_LDT_FLAGS_DATA
;
485 if (flags
& (HEAP_WINE_CODESEG
| HEAP_WINE_CODE16SEG
))
486 selflags
= WINE_LDT_FLAGS_CODE
;
487 if (flags
& HEAP_WINE_CODESEG
)
488 selflags
|= WINE_LDT_FLAGS_32BIT
;
489 selector
= SELECTOR_AllocBlock( address
, totalSize
, selflags
);
492 ERR("Could not allocate selector\n" );
497 /* Fill the sub-heap structure */
499 subheap
->heap
= heap
;
500 subheap
->selector
= selector
;
501 subheap
->size
= totalSize
;
502 subheap
->commitSize
= commitSize
;
503 subheap
->magic
= SUBHEAP_MAGIC
;
505 if ( subheap
!= (SUBHEAP
*)heap
)
507 /* If this is a secondary subheap, insert it into list */
509 subheap
->headerSize
= sizeof(SUBHEAP
);
510 subheap
->next
= heap
->subheap
.next
;
511 heap
->subheap
.next
= subheap
;
515 /* If this is a primary subheap, initialize main heap */
517 subheap
->headerSize
= sizeof(HEAP
);
518 subheap
->next
= NULL
;
521 heap
->magic
= HEAP_MAGIC
;
523 /* Build the free lists */
525 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
527 pEntry
->size
= HEAP_freeListSizes
[i
];
528 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
529 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
530 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
531 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
532 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
533 pEntry
->arena
.threadId
= 0;
534 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
537 /* Initialize critical section */
539 InitializeCriticalSection( &heap
->critSection
);
542 /* Create the first free block */
544 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
+ subheap
->headerSize
,
545 subheap
->size
- subheap
->headerSize
);
550 /***********************************************************************
553 * Create a sub-heap of the given size.
554 * If heap == NULL, creates a main heap.
556 static SUBHEAP
*HEAP_CreateSubHeap( HEAP
*heap
, DWORD flags
,
557 DWORD commitSize
, DWORD totalSize
)
561 /* Round-up sizes on a 64K boundary */
563 if (flags
& HEAP_WINE_SEGPTR
)
565 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
569 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
570 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
571 if (!commitSize
) commitSize
= 0x10000;
572 if (totalSize
< commitSize
) totalSize
= commitSize
;
575 /* Allocate the memory block */
577 if (!(address
= VirtualAlloc( NULL
, totalSize
,
578 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
580 WARN("Could not VirtualAlloc %08lx bytes\n",
585 /* Initialize subheap */
587 if (!HEAP_InitSubHeap( heap
? heap
: (HEAP
*)address
,
588 address
, flags
, commitSize
, totalSize
))
590 VirtualFree( address
, 0, MEM_RELEASE
);
594 return (SUBHEAP
*)address
;
598 /***********************************************************************
601 * Find a free block at least as large as the requested size, and make sure
602 * the requested size is committed.
604 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
605 SUBHEAP
**ppSubHeap
)
609 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
611 /* Find a suitable free list, and in it find a block large enough */
613 while (pEntry
->size
< size
) pEntry
++;
614 pArena
= pEntry
->arena
.next
;
615 while (pArena
!= &heap
->freeList
[0].arena
)
617 if (pArena
->size
> size
)
619 subheap
= HEAP_FindSubHeap( heap
, pArena
);
620 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
621 + size
+ HEAP_MIN_BLOCK_SIZE
))
623 *ppSubHeap
= subheap
;
627 pArena
= pArena
->next
;
630 /* If no block was found, attempt to grow the heap */
632 if (!(heap
->flags
& HEAP_GROWABLE
))
634 WARN("Not enough space in heap %08lx for %08lx bytes\n",
638 /* make sure that we have a big enough size *committed* to fit another
639 * last free arena in !
640 * So just one heap struct, one first free arena which will eventually
641 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
642 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
643 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_INUSE
) + HEAP_MIN_BLOCK_SIZE
;
644 if (!(subheap
= HEAP_CreateSubHeap( heap
, heap
->flags
, size
,
645 max( HEAP_DEF_SIZE
, size
) )))
648 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
649 (DWORD
)subheap
, size
, (DWORD
)heap
);
651 *ppSubHeap
= subheap
;
652 return (ARENA_FREE
*)(subheap
+ 1);
656 /***********************************************************************
657 * HEAP_IsValidArenaPtr
659 * Check that the pointer is inside the range possible for arenas.
661 static BOOL
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
664 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
665 if (!subheap
) return FALSE
;
666 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
667 if (subheap
!= &heap
->subheap
) return FALSE
;
668 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
669 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
674 /***********************************************************************
675 * HEAP_ValidateFreeArena
677 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
679 char *heapEnd
= (char *)subheap
+ subheap
->size
;
681 #if !defined(ALLOW_UNALIGNED_ACCESS)
682 /* Check for unaligned pointers */
683 if ( (long)pArena
% sizeof(void *) != 0 )
685 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
686 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
691 /* Check magic number */
692 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
694 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
695 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
698 /* Check size flags */
699 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
700 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
702 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
703 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
705 /* Check arena size */
706 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
708 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
709 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
712 /* Check that next pointer is valid */
713 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
715 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
716 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
719 /* Check that next arena is free */
720 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
721 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
723 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
724 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
727 /* Check that prev pointer is valid */
728 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
730 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
731 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
734 /* Check that prev arena is free */
735 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
736 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
738 /* this often means that the prev arena got overwritten
739 * by a memory write before that prev arena */
740 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
741 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
744 /* Check that next block has PREV_FREE flag */
745 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
747 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
748 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
750 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
751 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
754 /* Check next block back pointer */
755 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
756 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
758 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
759 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
760 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
768 /***********************************************************************
769 * HEAP_ValidateInUseArena
771 static BOOL
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, BOOL quiet
)
773 char *heapEnd
= (char *)subheap
+ subheap
->size
;
775 #if !defined(ALLOW_UNALIGNED_ACCESS)
776 /* Check for unaligned pointers */
777 if ( (long)pArena
% sizeof(void *) != 0 )
779 if ( quiet
== NOISY
)
781 ERR( "Heap %08lx: unaligned arena pointer %08lx\n",
782 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
783 if ( TRACE_ON(heap
) )
784 HEAP_Dump( subheap
->heap
);
786 else if ( WARN_ON(heap
) )
788 WARN( "Heap %08lx: unaligned arena pointer %08lx\n",
789 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
790 if ( TRACE_ON(heap
) )
791 HEAP_Dump( subheap
->heap
);
797 /* Check magic number */
798 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
800 if (quiet
== NOISY
) {
801 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
802 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
804 HEAP_Dump( subheap
->heap
);
805 } else if (WARN_ON(heap
)) {
806 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
807 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
809 HEAP_Dump( subheap
->heap
);
813 /* Check size flags */
814 if (pArena
->size
& ARENA_FLAG_FREE
)
816 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
817 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
819 /* Check arena size */
820 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
822 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
823 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
826 /* Check next arena PREV_FREE flag */
827 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
828 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
830 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
831 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
834 /* Check prev free arena */
835 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
837 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
838 /* Check prev pointer */
839 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
841 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
842 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
845 /* Check that prev arena is free */
846 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
847 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
849 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
850 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
853 /* Check that prev arena is really the previous block */
854 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
856 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
857 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
865 /***********************************************************************
868 * Transform a linear pointer into a SEGPTR. The pointer must have been
869 * allocated from a HEAP_WINE_SEGPTR heap.
871 SEGPTR
HEAP_GetSegptr( HANDLE heap
, DWORD flags
, LPCVOID ptr
)
873 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
877 /* Validate the parameters */
879 if (!heapPtr
) return 0;
880 flags
|= heapPtr
->flags
;
881 if (!(flags
& HEAP_WINE_SEGPTR
))
883 ERR("Heap %08x is not a SEGPTR heap\n",
887 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
889 /* Get the subheap */
891 if ((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
892 ret
= MAKESEGPTR(subheap
->selector
, (char *)ptr
- (char *)subheap
);
894 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
898 /***********************************************************************
902 * Maps linear pointer to segmented.
904 SEGPTR WINAPI
MapLS( LPCVOID ptr
)
909 if (!HIWORD(ptr
)) return (SEGPTR
)ptr
;
911 /* check if the pointer is inside the segptr heap */
912 EnterCriticalSection( &segptrHeap
->critSection
);
913 if ((subheap
= HEAP_FindSubHeap( segptrHeap
, ptr
)))
914 ret
= MAKESEGPTR( subheap
->selector
, (char *)ptr
- (char *)subheap
);
915 LeaveCriticalSection( &segptrHeap
->critSection
);
917 /* otherwise, allocate a brand-new selector */
920 WORD sel
= SELECTOR_AllocBlock( ptr
, 0x10000, WINE_LDT_FLAGS_DATA
);
921 ret
= MAKESEGPTR( sel
, 0 );
927 /***********************************************************************
928 * UnMapLS (KERNEL32.@)
929 * UnMapLS (KERNEL.359)
931 * Free mapped selector.
933 void WINAPI
UnMapLS( SEGPTR sptr
)
936 if (!SELECTOROF(sptr
)) return;
938 /* check if ptr is inside segptr heap */
939 EnterCriticalSection( &segptrHeap
->critSection
);
940 subheap
= HEAP_FindSubHeap( segptrHeap
, MapSL(sptr
) );
941 if ((subheap
) && (subheap
->selector
!= SELECTOROF(sptr
))) subheap
= NULL
;
942 LeaveCriticalSection( &segptrHeap
->critSection
);
943 /* if not inside heap, free the selector */
944 if (!subheap
) FreeSelector16( SELECTOROF(sptr
) );
947 /***********************************************************************
948 * HEAP_IsRealArena [Internal]
949 * Validates a block is a valid arena.
955 static BOOL
HEAP_IsRealArena( HEAP
*heapPtr
, /* [in] ptr to the heap */
956 DWORD flags
, /* [in] Bit flags that control access during operation */
957 LPCVOID block
, /* [in] Optional pointer to memory block to validate */
958 BOOL quiet
) /* [in] Flag - if true, HEAP_ValidateInUseArena
959 * does not complain */
964 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
966 ERR("Invalid heap %p!\n", heapPtr
);
970 flags
&= HEAP_NO_SERIALIZE
;
971 flags
|= heapPtr
->flags
;
972 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
973 if (!(flags
& HEAP_NO_SERIALIZE
))
974 EnterCriticalSection( &heapPtr
->critSection
);
978 /* Only check this single memory block */
980 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
981 ((char *)block
< (char *)subheap
+ subheap
->headerSize
982 + sizeof(ARENA_INUSE
)))
985 ERR("Heap %p: block %p is not inside heap\n", heapPtr
, block
);
986 else if (WARN_ON(heap
))
987 WARN("Heap %p: block %p is not inside heap\n", heapPtr
, block
);
990 ret
= HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1, quiet
);
992 if (!(flags
& HEAP_NO_SERIALIZE
))
993 LeaveCriticalSection( &heapPtr
->critSection
);
997 subheap
= &heapPtr
->subheap
;
998 while (subheap
&& ret
)
1000 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1001 while (ptr
< (char *)subheap
+ subheap
->size
)
1003 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1005 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
)) {
1009 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1013 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
, NOISY
)) {
1017 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1020 subheap
= subheap
->next
;
1023 if (!(flags
& HEAP_NO_SERIALIZE
))
1024 LeaveCriticalSection( &heapPtr
->critSection
);
1029 /***********************************************************************
1030 * HEAP_CreateSystemHeap
1032 * Create the system heap.
1034 static HANDLE
HEAP_CreateSystemHeap(void)
1038 HANDLE map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
1039 0, HEAP_DEF_SIZE
, "__SystemHeap" );
1041 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
1043 if (!(systemHeap
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
1045 /* pre-defined address not available, use any one */
1046 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE
);
1050 if (created
) /* newly created heap */
1052 HEAP_InitSubHeap( systemHeap
, systemHeap
, HEAP_SHARED
, 0, HEAP_DEF_SIZE
);
1053 MakeCriticalSectionGlobal( &systemHeap
->critSection
);
1057 /* wait for the heap to be initialized */
1058 while (!systemHeap
->critSection
.LockSemaphore
) Sleep(1);
1061 return (HANDLE
)systemHeap
;
1065 /***********************************************************************
1066 * HeapCreate (KERNEL32.@)
1068 * Handle of heap: Success
1071 HANDLE WINAPI
HeapCreate(
1072 DWORD flags
, /* [in] Heap allocation flag */
1073 DWORD initialSize
, /* [in] Initial heap size */
1074 DWORD maxSize
/* [in] Maximum heap size */
1078 if ( flags
& HEAP_SHARED
) {
1079 if (!systemHeap
) HEAP_CreateSystemHeap();
1080 else WARN( "Shared Heap requested, returning system heap.\n" );
1081 return (HANDLE
)systemHeap
;
1084 /* Allocate the heap block */
1088 maxSize
= HEAP_DEF_SIZE
;
1089 flags
|= HEAP_GROWABLE
;
1091 if (!(subheap
= HEAP_CreateSubHeap( NULL
, flags
, initialSize
, maxSize
)))
1093 SetLastError( ERROR_OUTOFMEMORY
);
1097 /* link it into the per-process heap list */
1100 HEAP
*heapPtr
= subheap
->heap
;
1101 EnterCriticalSection( &processHeap
->critSection
);
1102 heapPtr
->next
= firstHeap
;
1103 firstHeap
= heapPtr
;
1104 LeaveCriticalSection( &processHeap
->critSection
);
1106 else /* assume the first heap we create is the process main heap */
1109 processHeap
= subheap
->heap
;
1110 /* create the SEGPTR heap */
1111 if (!(segptr
= HEAP_CreateSubHeap( NULL
, flags
|HEAP_WINE_SEGPTR
|HEAP_GROWABLE
, 0, 0 )))
1113 SetLastError( ERROR_OUTOFMEMORY
);
1116 segptrHeap
= segptr
->heap
;
1118 return (HANDLE
)subheap
;
1121 /***********************************************************************
1122 * HeapDestroy (KERNEL32.@)
1127 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
1129 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1132 TRACE("%08x\n", heap
);
1133 if (!heapPtr
) return FALSE
;
1135 if (heapPtr
== systemHeap
)
1137 WARN( "attempt to destroy system heap, returning TRUE!\n" );
1140 if (heapPtr
== processHeap
) /* cannot delete the main process heap */
1142 SetLastError( ERROR_INVALID_PARAMETER
);
1145 else /* remove it from the per-process list */
1148 EnterCriticalSection( &processHeap
->critSection
);
1150 while (*pptr
&& *pptr
!= heapPtr
) pptr
= &(*pptr
)->next
;
1151 if (*pptr
) *pptr
= (*pptr
)->next
;
1152 LeaveCriticalSection( &processHeap
->critSection
);
1155 DeleteCriticalSection( &heapPtr
->critSection
);
1156 subheap
= &heapPtr
->subheap
;
1159 SUBHEAP
*next
= subheap
->next
;
1160 if (subheap
->selector
) FreeSelector16( subheap
->selector
);
1161 VirtualFree( subheap
, 0, MEM_RELEASE
);
1168 /***********************************************************************
1169 * HeapAlloc (KERNEL32.@)
1171 * Pointer to allocated memory block
1174 LPVOID WINAPI
HeapAlloc(
1175 HANDLE heap
, /* [in] Handle of private heap block */
1176 DWORD flags
, /* [in] Heap allocation control flags */
1177 DWORD size
/* [in] Number of bytes to allocate */
1180 ARENA_INUSE
*pInUse
;
1182 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1184 /* Validate the parameters */
1186 if ((flags
& HEAP_WINE_SEGPTR
) && size
< 0x10000) heapPtr
= segptrHeap
;
1187 if (!heapPtr
) return NULL
;
1188 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
1189 flags
|= heapPtr
->flags
;
1190 size
= (size
+ 3) & ~3;
1191 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1193 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1194 /* Locate a suitable free block */
1196 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
1198 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1199 heap
, flags
, size
);
1200 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1201 SetLastError( ERROR_COMMITMENT_LIMIT
);
1205 /* Remove the arena from the free list */
1207 pArena
->next
->prev
= pArena
->prev
;
1208 pArena
->prev
->next
= pArena
->next
;
1210 /* Build the in-use arena */
1212 pInUse
= (ARENA_INUSE
*)pArena
;
1214 /* in-use arena is smaller than free arena,
1215 * so we have to add the difference to the size */
1216 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1217 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1218 pInUse
->callerEIP
= GET_EIP();
1219 pInUse
->threadId
= GetCurrentTask();
1220 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1222 /* Shrink the block */
1224 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
1226 if (flags
& HEAP_ZERO_MEMORY
)
1227 memset( pInUse
+ 1, 0, pInUse
->size
& ARENA_SIZE_MASK
);
1228 else if (TRACE_ON(heap
))
1229 memset( pInUse
+ 1, ARENA_INUSE_FILLER
, pInUse
->size
& ARENA_SIZE_MASK
);
1231 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1233 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1234 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
1235 return (LPVOID
)(pInUse
+ 1);
1239 /***********************************************************************
1240 * HeapFree (KERNEL32.@)
1245 BOOL WINAPI
HeapFree(
1246 HANDLE heap
, /* [in] Handle of heap */
1247 DWORD flags
, /* [in] Heap freeing flags */
1248 LPVOID ptr
/* [in] Address of memory to free */
1250 ARENA_INUSE
*pInUse
;
1252 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1254 /* Validate the parameters */
1256 if (!ptr
) return TRUE
; /* freeing a NULL ptr isn't an error in Win2k */
1257 if (flags
& HEAP_WINE_SEGPTR
) heapPtr
= segptrHeap
;
1258 if (!heapPtr
) return FALSE
;
1260 flags
&= HEAP_NO_SERIALIZE
;
1261 flags
|= heapPtr
->flags
;
1262 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1263 if (!HEAP_IsRealArena( heapPtr
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1265 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1266 SetLastError( ERROR_INVALID_PARAMETER
);
1267 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1268 heap
, flags
, (DWORD
)ptr
);
1272 /* Turn the block into a free block */
1274 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1275 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
1276 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
1278 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1280 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1281 heap
, flags
, (DWORD
)ptr
);
1286 /***********************************************************************
1287 * HeapReAlloc (KERNEL32.@)
1289 * Pointer to reallocated memory block
1292 LPVOID WINAPI
HeapReAlloc(
1293 HANDLE heap
, /* [in] Handle of heap block */
1294 DWORD flags
, /* [in] Heap reallocation flags */
1295 LPVOID ptr
, /* [in] Address of memory to reallocate */
1296 DWORD size
/* [in] Number of bytes to reallocate */
1298 ARENA_INUSE
*pArena
;
1303 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
1304 if ((flags
& HEAP_WINE_SEGPTR
) && size
< 0x10000) heapPtr
= segptrHeap
;
1305 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1307 /* Validate the parameters */
1309 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1310 HEAP_REALLOC_IN_PLACE_ONLY
;
1311 flags
|= heapPtr
->flags
;
1312 size
= (size
+ 3) & ~3;
1313 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1315 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1316 if (!HEAP_IsRealArena( heapPtr
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1318 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1319 SetLastError( ERROR_INVALID_PARAMETER
);
1320 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1321 heap
, flags
, (DWORD
)ptr
, size
);
1325 /* Check if we need to grow the block */
1327 pArena
= (ARENA_INUSE
*)ptr
- 1;
1328 pArena
->threadId
= GetCurrentTask();
1329 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1330 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1333 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1334 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1335 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1336 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1338 /* The next block is free and large enough */
1339 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1340 pFree
->next
->prev
= pFree
->prev
;
1341 pFree
->prev
->next
= pFree
->next
;
1342 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1343 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1344 + size
+ HEAP_MIN_BLOCK_SIZE
))
1346 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1347 SetLastError( ERROR_OUTOFMEMORY
);
1350 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1352 else /* Do it the hard way */
1355 ARENA_INUSE
*pInUse
;
1356 SUBHEAP
*newsubheap
;
1358 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1359 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1361 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1362 SetLastError( ERROR_OUTOFMEMORY
);
1366 /* Build the in-use arena */
1368 pNew
->next
->prev
= pNew
->prev
;
1369 pNew
->prev
->next
= pNew
->next
;
1370 pInUse
= (ARENA_INUSE
*)pNew
;
1371 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1372 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1373 pInUse
->threadId
= GetCurrentTask();
1374 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1375 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1376 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1378 /* Free the previous block */
1380 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1381 subheap
= newsubheap
;
1385 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1387 /* Clear the extra bytes if needed */
1391 if (flags
& HEAP_ZERO_MEMORY
)
1392 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1393 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1394 else if (TRACE_ON(heap
))
1395 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1396 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1399 /* Return the new arena */
1401 pArena
->callerEIP
= GET_EIP();
1402 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1404 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1405 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1406 return (LPVOID
)(pArena
+ 1);
1410 /***********************************************************************
1411 * HeapCompact (KERNEL32.@)
1413 DWORD WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
1415 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1420 /***********************************************************************
1421 * HeapLock (KERNEL32.@)
1422 * Attempts to acquire the critical section object for a specified heap.
1428 BOOL WINAPI
HeapLock(
1429 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
1431 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1432 if (!heapPtr
) return FALSE
;
1433 EnterCriticalSection( &heapPtr
->critSection
);
1438 /***********************************************************************
1439 * HeapUnlock (KERNEL32.@)
1440 * Releases ownership of the critical section object.
1446 BOOL WINAPI
HeapUnlock(
1447 HANDLE heap
/* [in] Handle to the heap to unlock */
1449 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1450 if (!heapPtr
) return FALSE
;
1451 LeaveCriticalSection( &heapPtr
->critSection
);
1456 /***********************************************************************
1457 * HeapSize (KERNEL32.@)
1459 * Size in bytes of allocated memory
1460 * 0xffffffff: Failure
1462 DWORD WINAPI
HeapSize(
1463 HANDLE heap
, /* [in] Handle of heap */
1464 DWORD flags
, /* [in] Heap size control flags */
1465 LPVOID ptr
/* [in] Address of memory to return size for */
1468 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1470 if (flags
& HEAP_WINE_SEGPTR
) heapPtr
= segptrHeap
;
1471 if (!heapPtr
) return FALSE
;
1472 flags
&= HEAP_NO_SERIALIZE
;
1473 flags
|= heapPtr
->flags
;
1474 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1475 if (!HEAP_IsRealArena( heapPtr
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1477 SetLastError( ERROR_INVALID_PARAMETER
);
1482 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1483 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1485 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1487 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1488 heap
, flags
, (DWORD
)ptr
, ret
);
1493 /***********************************************************************
1494 * HeapValidate (KERNEL32.@)
1495 * Validates a specified heap.
1504 BOOL WINAPI
HeapValidate(
1505 HANDLE heap
, /* [in] Handle to the heap */
1506 DWORD flags
, /* [in] Bit flags that control access during operation */
1507 LPCVOID block
/* [in] Optional pointer to memory block to validate */
1509 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1510 if (flags
& HEAP_WINE_SEGPTR
) heapPtr
= segptrHeap
;
1511 if (!heapPtr
) return FALSE
;
1512 return HEAP_IsRealArena( heapPtr
, flags
, block
, QUIET
);
1516 /***********************************************************************
1517 * HeapWalk (KERNEL32.@)
1518 * Enumerates the memory blocks in a specified heap.
1519 * See HEAP_Dump() for info on heap structure.
1522 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
1523 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
1529 BOOL WINAPI
HeapWalk(
1530 HANDLE heap
, /* [in] Handle to heap to enumerate */
1531 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
1533 HEAP
*heapPtr
= HEAP_GetPtr(heap
);
1534 SUBHEAP
*sub
, *currentheap
= NULL
;
1537 int region_index
= 0;
1539 if (!heapPtr
|| !entry
)
1541 SetLastError(ERROR_INVALID_PARAMETER
);
1545 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1547 /* set ptr to the next arena to be examined */
1549 if (!entry
->lpData
) /* first call (init) ? */
1551 TRACE("begin walking of heap 0x%08x.\n", heap
);
1552 /*HEAP_Dump(heapPtr);*/
1553 currentheap
= &heapPtr
->subheap
;
1554 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1558 ptr
= entry
->lpData
;
1559 sub
= &heapPtr
->subheap
;
1562 if (((char *)ptr
>= (char *)sub
) &&
1563 ((char *)ptr
< (char *)sub
+ sub
->size
))
1571 if (currentheap
== NULL
)
1573 ERR("no matching subheap found, shouldn't happen !\n");
1574 SetLastError(ERROR_NO_MORE_ITEMS
);
1578 ptr
+= entry
->cbData
; /* point to next arena */
1579 if (ptr
> (char *)currentheap
+ currentheap
->size
- 1)
1580 { /* proceed with next subheap */
1581 if (!(currentheap
= currentheap
->next
))
1582 { /* successfully finished */
1583 TRACE("end reached.\n");
1584 SetLastError(ERROR_NO_MORE_ITEMS
);
1587 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1592 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1594 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
1596 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1598 entry
->lpData
= pArena
+ 1;
1599 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1600 entry
->cbOverhead
= sizeof(ARENA_FREE
);
1601 entry
->wFlags
= PROCESS_HEAP_UNCOMMITTED_RANGE
;
1605 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
1607 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1609 entry
->lpData
= pArena
+ 1;
1610 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1611 entry
->cbOverhead
= sizeof(ARENA_INUSE
);
1612 entry
->wFlags
= PROCESS_HEAP_ENTRY_BUSY
;
1613 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1614 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1617 entry
->iRegionIndex
= region_index
;
1619 /* first element of heap ? */
1620 if (ptr
== (char *)(currentheap
+ currentheap
->headerSize
))
1622 entry
->wFlags
|= PROCESS_HEAP_REGION
;
1623 entry
->u
.Region
.dwCommittedSize
= currentheap
->commitSize
;
1624 entry
->u
.Region
.dwUnCommittedSize
=
1625 currentheap
->size
- currentheap
->commitSize
;
1626 entry
->u
.Region
.lpFirstBlock
= /* first valid block */
1627 currentheap
+ currentheap
->headerSize
;
1628 entry
->u
.Region
.lpLastBlock
= /* first invalid block */
1629 currentheap
+ currentheap
->size
;
1634 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1640 /***********************************************************************
1641 * GetProcessHeap (KERNEL32.@)
1643 HANDLE WINAPI
GetProcessHeap(void)
1645 return (HANDLE
)processHeap
;
1649 /***********************************************************************
1650 * GetProcessHeaps (KERNEL32.@)
1652 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
1657 if (!processHeap
) return 0; /* should never happen */
1658 total
= 1; /* main heap */
1659 EnterCriticalSection( &processHeap
->critSection
);
1660 for (ptr
= firstHeap
; ptr
; ptr
= ptr
->next
) total
++;
1663 *heaps
++ = (HANDLE
)processHeap
;
1664 for (ptr
= firstHeap
; ptr
; ptr
= ptr
->next
) *heaps
++ = (HANDLE
)ptr
;
1666 LeaveCriticalSection( &processHeap
->critSection
);
1671 /***********************************************************************
1672 * 32-bit local heap functions (Win95; undocumented)
1675 #define HTABLE_SIZE 0x10000
1676 #define HTABLE_PAGESIZE 0x1000
1677 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
1679 #include "pshpack1.h"
1680 typedef struct _LOCAL32HEADER
1682 WORD freeListFirst
[HTABLE_NPAGES
];
1683 WORD freeListSize
[HTABLE_NPAGES
];
1684 WORD freeListLast
[HTABLE_NPAGES
];
1686 DWORD selectorTableOffset
;
1687 WORD selectorTableSize
;
1700 #include "poppack.h"
1702 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
1704 /***********************************************************************
1707 HANDLE WINAPI
Local32Init16( WORD segment
, DWORD tableSize
,
1708 DWORD heapSize
, DWORD flags
)
1710 DWORD totSize
, segSize
= 0;
1712 LOCAL32HEADER
*header
;
1714 WORD
*selectorTable
;
1715 WORD selectorEven
, selectorOdd
;
1718 /* Determine new heap size */
1722 if ( (segSize
= GetSelectorLimit16( segment
)) == 0 )
1728 if ( heapSize
== -1L )
1729 heapSize
= 1024L*1024L; /* 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
)) )
1741 if ( !VirtualAlloc( base
, segSize
+ HTABLE_PAGESIZE
,
1742 MEM_COMMIT
, PAGE_READWRITE
) )
1744 VirtualFree( base
, 0, MEM_RELEASE
);
1748 heap
= (HEAP
*)(base
+ segSize
+ HTABLE_SIZE
);
1749 if ( !HEAP_InitSubHeap( heap
, (LPVOID
)heap
, 0, 0x10000, heapSize
) )
1751 VirtualFree( base
, 0, MEM_RELEASE
);
1756 /* Set up header and handle table */
1758 header
= (LOCAL32HEADER
*)(base
+ segSize
);
1759 header
->base
= base
;
1760 header
->limit
= HTABLE_PAGESIZE
-1;
1762 header
->magic
= LOCAL32_MAGIC
;
1763 header
->heap
= (HANDLE
)heap
;
1765 header
->freeListFirst
[0] = sizeof(LOCAL32HEADER
);
1766 header
->freeListLast
[0] = HTABLE_PAGESIZE
- 4;
1767 header
->freeListSize
[0] = (HTABLE_PAGESIZE
- sizeof(LOCAL32HEADER
)) / 4;
1769 for (i
= header
->freeListFirst
[0]; i
< header
->freeListLast
[0]; i
+= 4)
1770 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1772 header
->freeListFirst
[1] = 0xffff;
1775 /* Set up selector table */
1777 nrBlocks
= (totSize
+ 0x7fff) >> 15;
1778 selectorTable
= (LPWORD
) HeapAlloc( header
->heap
, 0, nrBlocks
* 2 );
1779 selectorEven
= SELECTOR_AllocBlock( base
, totSize
, WINE_LDT_FLAGS_DATA
);
1780 selectorOdd
= SELECTOR_AllocBlock( base
+ 0x8000, totSize
- 0x8000, WINE_LDT_FLAGS_DATA
);
1781 if ( !selectorTable
|| !selectorEven
|| !selectorOdd
)
1783 if ( selectorTable
) HeapFree( header
->heap
, 0, selectorTable
);
1784 if ( selectorEven
) SELECTOR_FreeBlock( selectorEven
);
1785 if ( selectorOdd
) SELECTOR_FreeBlock( selectorOdd
);
1786 HeapDestroy( header
->heap
);
1787 VirtualFree( base
, 0, MEM_RELEASE
);
1791 header
->selectorTableOffset
= (LPBYTE
)selectorTable
- header
->base
;
1792 header
->selectorTableSize
= nrBlocks
* 4; /* ??? Win95 does it this way! */
1793 header
->selectorDelta
= selectorEven
- selectorOdd
;
1794 header
->segment
= segment
? segment
: selectorEven
;
1796 for (i
= 0; i
< nrBlocks
; i
++)
1797 selectorTable
[i
] = (i
& 1)? selectorOdd
+ ((i
>> 1) << __AHSHIFT
)
1798 : selectorEven
+ ((i
>> 1) << __AHSHIFT
);
1800 /* Move old segment */
1804 /* FIXME: This is somewhat ugly and relies on implementation
1805 details about 16-bit global memory handles ... */
1807 LPBYTE oldBase
= (LPBYTE
)GetSelectorBase( segment
);
1808 memcpy( base
, oldBase
, segSize
);
1809 GLOBAL_MoveBlock( segment
, base
, totSize
);
1810 HeapFree( GetProcessHeap(), 0, oldBase
);
1813 return (HANDLE
)header
;
1816 /***********************************************************************
1817 * Local32_SearchHandle
1819 static LPDWORD
Local32_SearchHandle( LOCAL32HEADER
*header
, DWORD addr
)
1823 for ( handle
= (LPDWORD
)((LPBYTE
)header
+ sizeof(LOCAL32HEADER
));
1824 handle
< (LPDWORD
)((LPBYTE
)header
+ header
->limit
);
1827 if (*handle
== addr
)
1834 /***********************************************************************
1837 static VOID
Local32_ToHandle( LOCAL32HEADER
*header
, INT16 type
,
1838 DWORD addr
, LPDWORD
*handle
, LPBYTE
*ptr
)
1845 case -2: /* 16:16 pointer, no handles */
1846 *ptr
= MapSL( addr
);
1847 *handle
= (LPDWORD
)*ptr
;
1850 case -1: /* 32-bit offset, no handles */
1851 *ptr
= header
->base
+ addr
;
1852 *handle
= (LPDWORD
)*ptr
;
1855 case 0: /* handle */
1856 if ( addr
>= sizeof(LOCAL32HEADER
)
1857 && addr
< header
->limit
&& !(addr
& 3)
1858 && *(LPDWORD
)((LPBYTE
)header
+ addr
) >= HTABLE_SIZE
)
1860 *handle
= (LPDWORD
)((LPBYTE
)header
+ addr
);
1861 *ptr
= header
->base
+ **handle
;
1865 case 1: /* 16:16 pointer */
1866 *ptr
= MapSL( addr
);
1867 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1870 case 2: /* 32-bit offset */
1871 *ptr
= header
->base
+ addr
;
1872 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1877 /***********************************************************************
1878 * Local32_FromHandle
1880 static VOID
Local32_FromHandle( LOCAL32HEADER
*header
, INT16 type
,
1881 DWORD
*addr
, LPDWORD handle
, LPBYTE ptr
)
1885 case -2: /* 16:16 pointer */
1888 WORD
*selTable
= (LPWORD
)(header
->base
+ header
->selectorTableOffset
);
1889 DWORD offset
= (LPBYTE
)ptr
- header
->base
;
1890 *addr
= MAKELONG( offset
& 0x7fff, selTable
[offset
>> 15] );
1894 case -1: /* 32-bit offset */
1896 *addr
= ptr
- header
->base
;
1899 case 0: /* handle */
1900 *addr
= (LPBYTE
)handle
- (LPBYTE
)header
;
1905 /***********************************************************************
1908 DWORD WINAPI
Local32Alloc16( HANDLE heap
, DWORD size
, INT16 type
, DWORD flags
)
1910 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1915 /* Allocate memory */
1916 ptr
= HeapAlloc( header
->heap
,
1917 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0, size
);
1921 /* Allocate handle if requested */
1926 /* Find first page of handle table with free slots */
1927 for (page
= 0; page
< HTABLE_NPAGES
; page
++)
1928 if (header
->freeListFirst
[page
] != 0)
1930 if (page
== HTABLE_NPAGES
)
1932 WARN("Out of handles!\n" );
1933 HeapFree( header
->heap
, 0, ptr
);
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
);
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;
1966 header
->freeListFirst
[page
] = *handle
;
1968 /* Store 32-bit offset in handle slot */
1969 *handle
= ptr
- header
->base
;
1973 handle
= (LPDWORD
)ptr
;
1978 /* Convert handle to requested output type */
1979 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
1983 /***********************************************************************
1986 DWORD WINAPI
Local32ReAlloc16( HANDLE heap
, DWORD addr
, INT16 type
,
1987 DWORD size
, DWORD flags
)
1989 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
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,
2008 *handle
= ptr
- header
->base
;
2010 handle
= (LPDWORD
)ptr
;
2012 /* Convert handle to requested output type */
2013 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
2017 /***********************************************************************
2020 BOOL WINAPI
Local32Free16( HANDLE heap
, DWORD addr
, INT16 type
)
2022 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2026 /* Retrieve handle and pointer */
2027 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2028 if (!handle
) return FALSE
;
2030 /* Free handle if necessary */
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
;
2040 *(LPDWORD
)((LPBYTE
)header
+ header
->freeListLast
[page
]) = offset
,
2041 header
->freeListLast
[page
] = offset
;
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
) )
2053 header
->limit
-= HTABLE_PAGESIZE
;
2054 header
->freeListFirst
[page
] = 0xffff;
2060 return HeapFree( header
->heap
, 0, ptr
);
2063 /***********************************************************************
2066 DWORD WINAPI
Local32Translate16( HANDLE heap
, DWORD addr
, INT16 type1
, INT16 type2
)
2068 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2072 Local32_ToHandle( header
, type1
, addr
, &handle
, &ptr
);
2073 if (!handle
) return 0;
2075 Local32_FromHandle( header
, type2
, &addr
, handle
, ptr
);
2079 /***********************************************************************
2082 DWORD WINAPI
Local32Size16( HANDLE heap
, DWORD addr
, INT16 type
)
2084 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2088 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2089 if (!handle
) return 0;
2091 return HeapSize( header
->heap
, 0, ptr
);
2094 /***********************************************************************
2097 BOOL WINAPI
Local32ValidHandle16( HANDLE heap
, WORD addr
)
2099 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2103 Local32_ToHandle( header
, 0, addr
, &handle
, &ptr
);
2104 return handle
!= NULL
;
2107 /***********************************************************************
2110 WORD WINAPI
Local32GetSegment16( HANDLE heap
)
2112 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2113 return header
->segment
;
2116 /***********************************************************************
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
2128 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2129 return (LOCAL32HEADER
*)base
;
2134 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2135 return (LOCAL32HEADER
*)base
;
2140 /***********************************************************************
2141 * Local32Info (KERNEL.444)
2142 * Local32Info (TOOLHELP.84)
2144 BOOL16 WINAPI
Local32Info16( LOCAL32INFO
*pLocal32Info
, HGLOBAL16 handle
)
2150 LOCAL32HEADER
*header
= Local32_GetHeap( handle
);
2151 if ( !header
) return FALSE
;
2153 if ( !pLocal32Info
|| pLocal32Info
->dwSize
< sizeof(LOCAL32INFO
) )
2156 heapPtr
= (SUBHEAP
*)HEAP_GetPtr( header
->heap
);
2157 pLocal32Info
->dwMemReserved
= heapPtr
->size
;
2158 pLocal32Info
->dwMemCommitted
= heapPtr
->commitSize
;
2159 pLocal32Info
->dwTotalFree
= 0L;
2160 pLocal32Info
->dwLargestFreeBlock
= 0L;
2162 /* Note: Local32 heaps always have only one subheap! */
2163 ptr
= (LPBYTE
)heapPtr
+ heapPtr
->headerSize
;
2164 while ( ptr
< (LPBYTE
)heapPtr
+ heapPtr
->size
)
2166 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
2168 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
2169 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
2170 ptr
+= sizeof(*pArena
) + size
;
2172 pLocal32Info
->dwTotalFree
+= size
;
2173 if ( size
> pLocal32Info
->dwLargestFreeBlock
)
2174 pLocal32Info
->dwLargestFreeBlock
= size
;
2178 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
2179 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
2180 ptr
+= sizeof(*pArena
) + size
;
2184 pLocal32Info
->dwcFreeHandles
= 0;
2185 for ( i
= 0; i
< HTABLE_NPAGES
; i
++ )
2187 if ( header
->freeListFirst
[i
] == 0xffff ) break;
2188 pLocal32Info
->dwcFreeHandles
+= header
->freeListSize
[i
];
2190 pLocal32Info
->dwcFreeHandles
+= (HTABLE_NPAGES
- i
) * HTABLE_PAGESIZE
/4;
2195 /***********************************************************************
2196 * Local32First (KERNEL.445)
2197 * Local32First (TOOLHELP.85)
2199 BOOL16 WINAPI
Local32First16( LOCAL32ENTRY
*pLocal32Entry
, HGLOBAL16 handle
)
2201 FIXME("(%p, %04X): stub!\n", pLocal32Entry
, handle
);
2205 /***********************************************************************
2206 * Local32Next (KERNEL.446)
2207 * Local32Next (TOOLHELP.86)
2209 BOOL16 WINAPI
Local32Next16( LOCAL32ENTRY
*pLocal32Entry
)
2211 FIXME("(%p): stub!\n", pLocal32Entry
);