4 * Copyright 1996 Alexandre Julliard
12 #include "kernel32.h" /* for CRITICAL_SECTION */
13 #include "selectors.h"
20 /* Note: the heap data structures are based on what Pietrek describes in his
21 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
22 * the same, but could be easily adapted if it turns out some programs
26 typedef struct tagARENA_INUSE
28 DWORD size
; /* Block size; must be the first field */
29 WORD threadId
; /* Allocating thread id */
30 WORD magic
; /* Magic number */
31 DWORD callerEIP
; /* EIP of caller upon allocation */
34 typedef struct tagARENA_FREE
36 DWORD size
; /* Block size; must be the first field */
37 WORD threadId
; /* Freeing thread id */
38 WORD magic
; /* Magic number */
39 struct tagARENA_FREE
*next
; /* Next free arena */
40 struct tagARENA_FREE
*prev
; /* Prev free arena */
43 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
44 #define ARENA_FLAG_PREV_FREE 0x00000002
45 #define ARENA_SIZE_MASK 0xfffffffc
46 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
47 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
49 #define ARENA_INUSE_FILLER 0x55
50 #define ARENA_FREE_FILLER 0xaa
52 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
54 /* Max size of the blocks on the free lists */
55 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
57 0x20, 0x80, 0x200, 0xffffffff
68 typedef struct tagSUBHEAP
70 DWORD size
; /* Size of the whole sub-heap */
71 DWORD commitSize
; /* Committed size of the sub-heap */
72 DWORD headerSize
; /* Size of the heap header */
73 struct tagSUBHEAP
*next
; /* Next sub-heap */
74 struct tagHEAP
*heap
; /* Main heap structure */
75 DWORD magic
; /* Magic number */
76 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
79 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
81 typedef struct tagHEAP
83 SUBHEAP subheap
; /* First sub-heap */
84 struct tagHEAP
*next
; /* Next heap for this process */
85 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
86 CRITICAL_SECTION critSection
; /* Critical section for serialization */
87 DWORD flags
; /* Heap flags */
88 DWORD magic
; /* Magic number */
91 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
93 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
94 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
97 /***********************************************************************
100 void HEAP_Dump( HEAP
*heap
)
106 printf( "Heap: %08lx\n", (DWORD
)heap
);
107 printf( "Next: %08lx Sub-heaps: %08lx",
108 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
109 subheap
= &heap
->subheap
;
110 while (subheap
->next
)
112 printf( " -> %08lx", (DWORD
)subheap
->next
);
113 subheap
= subheap
->next
;
116 printf( "\nFree lists:\n Block Stat Size Id\n" );
117 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
118 printf( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
119 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
120 heap
->freeList
[i
].arena
.threadId
,
121 (DWORD
)heap
->freeList
[i
].arena
.prev
,
122 (DWORD
)heap
->freeList
[i
].arena
.next
);
124 subheap
= &heap
->subheap
;
127 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
128 printf( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
129 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
131 printf( "\n Block Stat Size Id\n" );
132 ptr
= (char*)subheap
+ subheap
->headerSize
;
133 while (ptr
< (char *)subheap
+ subheap
->size
)
135 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
137 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
138 printf( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
139 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
140 pArena
->threadId
, (DWORD
)pArena
->prev
,
141 (DWORD
)pArena
->next
);
142 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
143 arenaSize
+= sizeof(ARENA_FREE
);
144 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
146 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
148 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
149 printf( "%08lx Used %08lx %04x back=%08lx EIP=%08lx\n",
150 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
151 pArena
->threadId
, *((DWORD
*)pArena
- 1),
153 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
154 arenaSize
+= sizeof(ARENA_INUSE
);
155 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
159 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
160 printf( "%08lx used %08lx %04x EIP=%08lx\n",
161 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
162 pArena
->threadId
, pArena
->callerEIP
);
163 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
164 arenaSize
+= sizeof(ARENA_INUSE
);
165 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
168 printf( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
169 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
170 arenaSize
, (arenaSize
* 100) / subheap
->size
);
171 subheap
= subheap
->next
;
176 /***********************************************************************
179 static HEAP
*HEAP_GetPtr( HANDLE32 heap
)
181 HEAP
*heapPtr
= (HEAP
*)heap
;
182 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
184 fprintf( stderr
, "Invalid heap %08x!\n", heap
);
185 SetLastError( ERROR_INVALID_HANDLE
);
188 if (debugging_heap
&& !HeapValidate( heap
, 0, NULL
))
190 HEAP_Dump( heapPtr
);
191 DEBUG_EnterDebugger();
192 SetLastError( ERROR_INVALID_HANDLE
);
199 /***********************************************************************
200 * HEAP_InsertFreeBlock
202 * Insert a free block into the free list.
204 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
206 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
207 while (pEntry
->size
< pArena
->size
) pEntry
++;
208 pArena
->size
|= ARENA_FLAG_FREE
;
209 pArena
->next
= pEntry
->arena
.next
;
210 pArena
->next
->prev
= pArena
;
211 pArena
->prev
= &pEntry
->arena
;
212 pEntry
->arena
.next
= pArena
;
216 /***********************************************************************
219 * Find the sub-heap containing a given address.
221 static SUBHEAP
*HEAP_FindSubHeap( HEAP
*heap
, LPCVOID ptr
)
223 SUBHEAP
*sub
= &heap
->subheap
;
226 if (((char *)ptr
>= (char *)sub
) &&
227 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
234 /***********************************************************************
237 * Make sure the heap storage is committed up to (not including) ptr.
239 static BOOL
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
241 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
242 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
243 if (size
> subheap
->size
) size
= subheap
->size
;
244 if (size
<= subheap
->commitSize
) return TRUE
;
245 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
246 size
- subheap
->commitSize
, MEM_COMMIT
,
247 PAGE_EXECUTE_READWRITE
))
249 fprintf( stderr
, "HEAP_Commit: could not commit %08lx bytes at %08lx for heap %08lx\n",
250 size
- subheap
->commitSize
,
251 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
252 (DWORD
)subheap
->heap
);
255 subheap
->commitSize
= size
;
260 /***********************************************************************
263 * If possible, decommit the heap storage from (including) 'ptr'.
265 static BOOL
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
267 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
268 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
269 if (size
>= subheap
->commitSize
) return TRUE
;
270 if (!VirtualFree( (char *)subheap
+ subheap
->commitSize
,
271 size
- subheap
->commitSize
, MEM_DECOMMIT
))
273 fprintf( stderr
, "HEAP_Decommit: could not decommit %08lx bytes at %08lx for heap %08lx\n",
274 size
- subheap
->commitSize
,
275 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
276 (DWORD
)subheap
->heap
);
279 subheap
->commitSize
= size
;
284 /***********************************************************************
285 * HEAP_CreateFreeBlock
287 * Create a free block at a specified address. 'size' is the size of the
288 * whole block, including the new arena.
290 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
294 /* Create a free arena */
296 pFree
= (ARENA_FREE
*)ptr
;
297 pFree
->threadId
= GetCurrentTask();
298 pFree
->magic
= ARENA_FREE_MAGIC
;
300 /* If debugging, erase the freed block content */
304 char *pEnd
= (char *)ptr
+ size
;
305 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
306 pEnd
= (char *)subheap
+ subheap
->commitSize
;
307 if (pEnd
> (char *)(pFree
+ 1))
308 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
311 /* Check if next block is free also */
313 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
314 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
316 /* Remove the next arena from the free list */
317 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
318 pNext
->next
->prev
= pNext
->prev
;
319 pNext
->prev
->next
= pNext
->next
;
320 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
322 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
325 /* Set the next block PREV_FREE flag and pointer */
327 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
329 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
330 *pNext
|= ARENA_FLAG_PREV_FREE
;
331 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
334 /* Last, insert the new block into the free list */
336 pFree
->size
= size
- sizeof(*pFree
);
337 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
341 /***********************************************************************
342 * HEAP_MakeInUseBlockFree
344 * Turn an in-use block into a free block. Can also decommit the end of
345 * the heap, and possibly even free the sub-heap altogether.
347 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
350 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
352 /* Check if we can merge with previous block */
354 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
356 pFree
= *((ARENA_FREE
**)pArena
- 1);
357 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
358 /* Remove it from the free list */
359 pFree
->next
->prev
= pFree
->prev
;
360 pFree
->prev
->next
= pFree
->next
;
362 else pFree
= (ARENA_FREE
*)pArena
;
364 /* Create a free block */
366 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
367 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
368 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
369 return; /* Not the last block, so nothing more to do */
371 /* Free the whole sub-heap if it's empty and not the original one */
373 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
374 (subheap
!= &subheap
->heap
->subheap
))
376 /* FIXME: free the sub-heap here */
380 /* Decommit the end of the heap */
382 HEAP_Decommit( subheap
, pFree
+ 1 );
386 /***********************************************************************
389 * Shrink an in-use block.
391 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
393 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
395 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
396 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
397 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
401 /* Turn off PREV_FREE flag in next block */
402 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
403 if (pNext
< (char *)subheap
+ subheap
->size
)
404 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
409 /***********************************************************************
412 * Create a sub-heap of the given size.
414 static SUBHEAP
*HEAP_CreateSubHeap( DWORD flags
, DWORD commitSize
,
420 /* Round-up sizes on a 64K boundary */
422 if (flags
& HEAP_WINE_SEGPTR
)
424 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
428 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
429 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
430 if (!commitSize
) commitSize
= 0x10000;
431 if (totalSize
< commitSize
) totalSize
= commitSize
;
434 /* Allocate the memory block */
436 if (!(subheap
= VirtualAlloc( NULL
, totalSize
,
437 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
439 fprintf( stderr
, "HEAP_CreateSubHeap: could not VirtualAlloc %08lx bytes\n",
443 if (!VirtualAlloc(subheap
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
445 fprintf( stderr
, "HEAP_CreateSubHeap: could not commit %08lx bytes for sub-heap %08lx\n",
446 commitSize
, (DWORD
)subheap
);
447 VirtualFree( subheap
, 0, MEM_RELEASE
);
451 /* Allocate a selector if needed */
453 if (flags
& HEAP_WINE_SEGPTR
)
455 selector
= SELECTOR_AllocBlock( subheap
, totalSize
,
456 (flags
& HEAP_WINE_CODESEG
) ? SEGMENT_CODE
: SEGMENT_DATA
,
457 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
460 fprintf( stderr
, "HEAP_CreateSubHeap: could not allocate selector\n" );
461 VirtualFree( subheap
, 0, MEM_RELEASE
);
466 /* Fill the sub-heap structure */
468 subheap
->size
= totalSize
;
469 subheap
->commitSize
= commitSize
;
470 subheap
->headerSize
= sizeof(*subheap
);
471 subheap
->next
= NULL
;
472 subheap
->heap
= NULL
;
473 subheap
->magic
= SUBHEAP_MAGIC
;
474 subheap
->selector
= selector
;
479 /***********************************************************************
482 * Find a free block at least as large as the requested size, and make sure
483 * the requested size is committed.
485 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
486 SUBHEAP
**ppSubHeap
)
490 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
492 /* Find a suitable free list, and in it find a block large enough */
494 while (pEntry
->size
< size
) pEntry
++;
495 pArena
= pEntry
->arena
.next
;
496 while (pArena
!= &heap
->freeList
[0].arena
)
498 if (pArena
->size
> size
)
500 subheap
= HEAP_FindSubHeap( heap
, pArena
);
501 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
502 + size
+ HEAP_MIN_BLOCK_SIZE
))
504 *ppSubHeap
= subheap
;
508 pArena
= pArena
->next
;
511 /* If no block was found, attempt to grow the heap */
513 if (!(heap
->flags
& HEAP_GROWABLE
))
515 fprintf( stderr
, "HEAP_FindFreeBlock: Not enough space in heap %08lx for %08lx bytes\n",
519 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
520 if (!(subheap
= HEAP_CreateSubHeap( heap
->flags
, size
,
521 MAX( HEAP_DEF_SIZE
, size
) )))
524 /* Insert the new sub-heap in the list */
526 subheap
->heap
= heap
;
527 subheap
->next
= heap
->subheap
.next
;
528 heap
->subheap
.next
= subheap
;
529 size
= subheap
->size
;
530 dprintf_heap( stddeb
, "HEAP_FindFreeBlock: created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
531 (DWORD
)subheap
, size
, (DWORD
)heap
);
533 HEAP_CreateFreeBlock( subheap
, subheap
+ 1, size
- sizeof(*subheap
) );
534 *ppSubHeap
= subheap
;
535 return (ARENA_FREE
*)(subheap
+ 1);
539 /***********************************************************************
540 * HEAP_IsValidArenaPtr
542 * Check that the pointer is inside the range possible for arenas.
544 static BOOL
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
547 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
548 if (!subheap
) return FALSE
;
549 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
550 if (subheap
!= &heap
->subheap
) return FALSE
;
551 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
552 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
557 /***********************************************************************
558 * HEAP_ValidateFreeArena
560 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
562 char *heapEnd
= (char *)subheap
+ subheap
->size
;
564 /* Check magic number */
565 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
567 fprintf( stderr
, "Heap %08lx: invalid free arena magic for %08lx\n",
568 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
571 /* Check size flags */
572 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
573 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
575 fprintf( stderr
, "Heap %08lx: bad flags %lx for free arena %08lx\n",
576 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
578 /* Check arena size */
579 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
581 fprintf( stderr
, "Heap %08lx: bad size %08lx for free arena %08lx\n",
582 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
585 /* Check that next pointer is valid */
586 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
588 fprintf( stderr
, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
589 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
592 /* Check that next arena is free */
593 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
594 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
596 fprintf( stderr
, "Heap %08lx: next arena %08lx invalid for %08lx\n",
597 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
600 /* Check that prev pointer is valid */
601 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
603 fprintf( stderr
, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
604 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
607 /* Check that prev arena is free */
608 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
609 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
611 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for %08lx\n",
612 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
615 /* Check that next block has PREV_FREE flag */
616 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
618 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
619 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
621 fprintf( stderr
, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
622 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
625 /* Check next block back pointer */
626 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
627 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
629 fprintf( stderr
, "Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
630 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
631 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
639 /***********************************************************************
640 * HEAP_ValidateInUseArena
642 static BOOL
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
644 char *heapEnd
= (char *)subheap
+ subheap
->size
;
646 /* Check magic number */
647 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
649 fprintf( stderr
, "Heap %08lx: invalid in-use arena magic for %08lx\n",
650 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
653 /* Check size flags */
654 if (pArena
->size
& ARENA_FLAG_FREE
)
656 fprintf( stderr
, "Heap %08lx: bad flags %lx for in-use arena %08lx\n",
657 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
659 /* Check arena size */
660 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
662 fprintf( stderr
, "Heap %08lx: bad size %08lx for in-use arena %08lx\n",
663 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
666 /* Check next arena PREV_FREE flag */
667 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
668 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
670 fprintf( stderr
, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
671 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
674 /* Check prev free arena */
675 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
677 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
678 /* Check prev pointer */
679 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
681 fprintf(stderr
, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
682 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
685 /* Check that prev arena is free */
686 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
687 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
689 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
690 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
693 /* Check that prev arena is really the previous block */
694 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
696 fprintf( stderr
, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
697 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
705 /***********************************************************************
708 * Check whether the pointer is to a block inside a given heap.
710 int HEAP_IsInsideHeap( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
712 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
716 /* Validate the parameters */
718 if (!heapPtr
) return 0;
719 flags
|= heapPtr
->flags
;
720 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
721 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
722 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
723 + sizeof(ARENA_INUSE
))));
724 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
729 /***********************************************************************
732 * Transform a linear pointer into a SEGPTR. The pointer must have been
733 * allocated from a HEAP_WINE_SEGPTR heap.
735 SEGPTR
HEAP_GetSegptr( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
737 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
741 /* Validate the parameters */
743 if (!heapPtr
) return 0;
744 flags
|= heapPtr
->flags
;
745 if (!(flags
& HEAP_WINE_SEGPTR
))
747 fprintf( stderr
, "HEAP_GetSegptr: heap %08x is not a SEGPTR heap\n",
751 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
753 /* Get the subheap */
755 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
757 fprintf( stderr
, "HEAP_GetSegptr: %p is not inside heap %08x\n",
759 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
763 /* Build the SEGPTR */
765 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
766 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
771 /***********************************************************************
772 * HeapCreate (KERNEL32.336)
774 HANDLE32
HeapCreate( DWORD flags
, DWORD initialSize
, DWORD maxSize
)
779 FREE_LIST_ENTRY
*pEntry
;
781 /* Allocate the heap block */
785 maxSize
= HEAP_DEF_SIZE
;
786 flags
|= HEAP_GROWABLE
;
788 if (!(subheap
= HEAP_CreateSubHeap( flags
, initialSize
, maxSize
)))
790 SetLastError( ERROR_OUTOFMEMORY
);
794 /* Fill the heap structure */
796 heap
= (HEAP
*)subheap
;
797 subheap
->heap
= heap
;
798 subheap
->headerSize
= sizeof(HEAP
);
801 heap
->magic
= HEAP_MAGIC
;
802 InitializeCriticalSection( &heap
->critSection
);
804 /* Build the free lists */
806 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
808 pEntry
->size
= HEAP_freeListSizes
[i
];
809 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
810 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
811 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
812 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
813 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
814 pEntry
->arena
.threadId
= 0;
815 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
818 /* Create the first free block */
820 HEAP_CreateFreeBlock( subheap
, heap
+ 1, subheap
->size
- sizeof(*heap
) );
825 return (HANDLE32
)heap
;
829 /***********************************************************************
830 * HeapDestroy (KERNEL32.337)
832 BOOL
HeapDestroy( HANDLE32 heap
)
834 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
837 dprintf_heap( stddeb
, "HeapDestroy: %08x\n", heap
);
838 if (!heapPtr
) return FALSE
;
840 DeleteCriticalSection( &heapPtr
->critSection
);
841 subheap
= &heapPtr
->subheap
;
844 SUBHEAP
*next
= subheap
->next
;
845 VirtualFree( subheap
, subheap
->commitSize
, MEM_DECOMMIT
);
846 VirtualFree( subheap
, 0, MEM_RELEASE
);
853 /***********************************************************************
854 * HeapAlloc (KERNEL32.334)
856 LPVOID
HeapAlloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
861 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
863 /* Validate the parameters */
865 if (!heapPtr
) return NULL
;
866 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
867 flags
|= heapPtr
->flags
;
868 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
869 size
= (size
+ 3) & ~3;
870 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
872 /* Locate a suitable free block */
874 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
876 dprintf_heap( stddeb
, "HeapAlloc(%08x,%08lx,%08lx): returning NULL\n",
878 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
879 SetLastError( ERROR_OUTOFMEMORY
);
883 /* Remove the arena from the free list */
885 pArena
->next
->prev
= pArena
->prev
;
886 pArena
->prev
->next
= pArena
->next
;
888 /* Build the in-use arena */
890 pInUse
= (ARENA_INUSE
*)pArena
;
891 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
892 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
893 pInUse
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
894 pInUse
->threadId
= GetCurrentTask();
895 pInUse
->magic
= ARENA_INUSE_MAGIC
;
897 /* Shrink the block */
899 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
901 if (flags
& HEAP_ZERO_MEMORY
) memset( pInUse
+ 1, 0, size
);
902 else if (debugging_heap
) memset( pInUse
+ 1, ARENA_INUSE_FILLER
, size
);
904 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
907 dprintf_heap( stddeb
, "HeapAlloc(%08x,%08lx,%08lx): returning %08lx\n",
908 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
909 return (LPVOID
)(pInUse
+ 1);
913 /***********************************************************************
914 * HeapFree (KERNEL32.338)
916 BOOL
HeapFree( HANDLE32 heap
, DWORD flags
, LPVOID ptr
)
920 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
922 /* Validate the parameters */
924 if (!heapPtr
) return FALSE
;
925 flags
&= HEAP_NO_SERIALIZE
;
926 flags
|= heapPtr
->flags
;
927 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
928 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
930 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
931 SetLastError( ERROR_INVALID_PARAMETER
);
932 dprintf_heap( stddeb
, "HeapFree(%08x,%08lx,%08lx): returning FALSE\n",
933 heap
, flags
, (DWORD
)ptr
);
937 /* Turn the block into a free block */
939 pInUse
= (ARENA_INUSE
*)ptr
- 1;
940 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
941 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
943 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
946 dprintf_heap( stddeb
, "HeapFree(%08x,%08lx,%08lx): returning TRUE\n",
947 heap
, flags
, (DWORD
)ptr
);
952 /***********************************************************************
953 * HeapReAlloc (KERNEL32.340)
955 LPVOID
HeapReAlloc( HANDLE32 heap
, DWORD flags
, LPVOID ptr
, DWORD size
)
962 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
963 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
965 /* Validate the parameters */
967 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
968 HEAP_REALLOC_IN_PLACE_ONLY
;
969 flags
|= heapPtr
->flags
;
970 size
= (size
+ 3) & ~3;
971 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
973 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
974 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
976 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
977 SetLastError( ERROR_INVALID_PARAMETER
);
978 dprintf_heap( stddeb
, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning NULL\n",
979 heap
, flags
, (DWORD
)ptr
, size
);
983 /* Check if we need to grow the block */
985 pArena
= (ARENA_INUSE
*)ptr
- 1;
986 pArena
->threadId
= GetCurrentTask();
987 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
988 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
991 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
992 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
993 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
994 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
996 /* The next block is free and large enough */
997 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
998 pFree
->next
->prev
= pFree
->prev
;
999 pFree
->prev
->next
= pFree
->next
;
1000 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1001 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1002 + size
+ HEAP_MIN_BLOCK_SIZE
))
1004 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1005 SetLastError( ERROR_OUTOFMEMORY
);
1009 else /* Do it the hard way */
1012 ARENA_INUSE
*pInUse
;
1013 SUBHEAP
*newsubheap
;
1015 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1016 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1018 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1019 SetLastError( ERROR_OUTOFMEMORY
);
1023 /* Build the in-use arena */
1025 pNew
->next
->prev
= pNew
->prev
;
1026 pNew
->prev
->next
= pNew
->next
;
1027 pInUse
= (ARENA_INUSE
*)pNew
;
1028 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1029 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1030 pInUse
->threadId
= GetCurrentTask();
1031 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1032 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1034 /* Free the previous block */
1036 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1037 subheap
= newsubheap
;
1043 /* Shrink the block */
1045 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1047 /* Clear the extra bytes if needed */
1051 if (flags
& HEAP_ZERO_MEMORY
)
1052 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1053 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1054 else if (debugging_heap
)
1055 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1056 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1059 /* Return the new arena */
1061 pArena
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
1062 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1064 dprintf_heap( stddeb
, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1065 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1066 return (LPVOID
)(pArena
+ 1);
1070 /***********************************************************************
1071 * HeapCompact (KERNEL32.335)
1073 DWORD
HeapCompact( HANDLE32 heap
, DWORD flags
)
1079 /***********************************************************************
1080 * HeapLock (KERNEL32.339)
1082 BOOL
HeapLock( HANDLE32 heap
)
1084 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1086 if (!heapPtr
) return FALSE
;
1087 EnterCriticalSection( &heapPtr
->critSection
);
1092 /***********************************************************************
1093 * HeapUnlock (KERNEL32.342)
1095 BOOL
HeapUnlock( HANDLE32 heap
)
1097 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1099 if (!heapPtr
) return FALSE
;
1100 LeaveCriticalSection( &heapPtr
->critSection
);
1105 /***********************************************************************
1106 * HeapSize (KERNEL32.341)
1108 DWORD
HeapSize( HANDLE32 heap
, DWORD flags
, LPVOID ptr
)
1111 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1113 if (!heapPtr
) return FALSE
;
1114 flags
&= HEAP_NO_SERIALIZE
;
1115 flags
|= heapPtr
->flags
;
1116 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1117 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1119 SetLastError( ERROR_INVALID_PARAMETER
);
1124 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1125 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1127 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1129 dprintf_heap( stddeb
, "HeapSize(%08x,%08lx,%08lx): returning %08lx\n",
1130 heap
, flags
, (DWORD
)ptr
, ret
);
1135 /***********************************************************************
1136 * HeapValidate (KERNEL32.343)
1138 BOOL
HeapValidate( HANDLE32 heap
, DWORD flags
, LPVOID block
)
1141 HEAP
*heapPtr
= (HEAP
*)heap
;
1143 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
1145 fprintf( stderr
, "Invalid heap %08x!\n", heap
);
1151 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
1152 ((char *)block
< (char *)subheap
+ subheap
->headerSize
1153 + sizeof(ARENA_INUSE
)))
1155 fprintf( stderr
, "Heap %08lx: block %08lx is not inside heap\n",
1156 (DWORD
)heap
, (DWORD
)block
);
1159 return HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1 );
1162 subheap
= &heapPtr
->subheap
;
1165 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1166 while (ptr
< (char *)subheap
+ subheap
->size
)
1168 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1170 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
))
1172 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1176 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
))
1178 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1181 subheap
= subheap
->next
;
1187 /***********************************************************************
1188 * HeapWalk (KERNEL32.344)
1190 BOOL
HeapWalk( HANDLE32 heap
, void *entry
)
1192 fprintf( stderr
, "HeapWalk(%08x): not implemented\n", heap
);
1197 /***********************************************************************
1200 LPSTR
HEAP_strdupA( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1202 INT32 len
= lstrlen(str
) + 1;
1203 LPSTR p
= HeapAlloc( heap
, flags
, len
);
1204 if (p
) strcpy( p
, str
);