4 * Copyright 1996 Alexandre Julliard
12 #include "selectors.h"
19 /* Note: the heap data structures are based on what Pietrek describes in his
20 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
21 * the same, but could be easily adapted if it turns out some programs
25 typedef struct tagARENA_INUSE
27 DWORD size
; /* Block size; must be the first field */
28 WORD threadId
; /* Allocating thread id */
29 WORD magic
; /* Magic number */
30 DWORD callerEIP
; /* EIP of caller upon allocation */
33 typedef struct tagARENA_FREE
35 DWORD size
; /* Block size; must be the first field */
36 WORD threadId
; /* Freeing thread id */
37 WORD magic
; /* Magic number */
38 struct tagARENA_FREE
*next
; /* Next free arena */
39 struct tagARENA_FREE
*prev
; /* Prev free arena */
42 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
43 #define ARENA_FLAG_PREV_FREE 0x00000002
44 #define ARENA_SIZE_MASK 0xfffffffc
45 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
46 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
48 #define ARENA_INUSE_FILLER 0x55
49 #define ARENA_FREE_FILLER 0xaa
51 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
53 /* Max size of the blocks on the free lists */
54 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
56 0x20, 0x80, 0x200, 0xffffffff
67 typedef struct tagSUBHEAP
69 DWORD size
; /* Size of the whole sub-heap */
70 DWORD commitSize
; /* Committed size of the sub-heap */
71 DWORD headerSize
; /* Size of the heap header */
72 struct tagSUBHEAP
*next
; /* Next sub-heap */
73 struct tagHEAP
*heap
; /* Main heap structure */
74 DWORD magic
; /* Magic number */
75 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
78 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
80 typedef struct tagHEAP
82 SUBHEAP subheap
; /* First sub-heap */
83 struct tagHEAP
*next
; /* Next heap for this process */
84 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
85 CRITICAL_SECTION critSection
; /* Critical section for serialization */
86 DWORD flags
; /* Heap flags */
87 DWORD magic
; /* Magic number */
90 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
92 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
93 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
96 /***********************************************************************
99 void HEAP_Dump( HEAP
*heap
)
105 printf( "Heap: %08lx\n", (DWORD
)heap
);
106 printf( "Next: %08lx Sub-heaps: %08lx",
107 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
108 subheap
= &heap
->subheap
;
109 while (subheap
->next
)
111 printf( " -> %08lx", (DWORD
)subheap
->next
);
112 subheap
= subheap
->next
;
115 printf( "\nFree lists:\n Block Stat Size Id\n" );
116 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
117 printf( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
118 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
119 heap
->freeList
[i
].arena
.threadId
,
120 (DWORD
)heap
->freeList
[i
].arena
.prev
,
121 (DWORD
)heap
->freeList
[i
].arena
.next
);
123 subheap
= &heap
->subheap
;
126 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
127 printf( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
128 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
130 printf( "\n Block Stat Size Id\n" );
131 ptr
= (char*)subheap
+ subheap
->headerSize
;
132 while (ptr
< (char *)subheap
+ subheap
->size
)
134 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
136 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
137 printf( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
138 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
139 pArena
->threadId
, (DWORD
)pArena
->prev
,
140 (DWORD
)pArena
->next
);
141 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
142 arenaSize
+= sizeof(ARENA_FREE
);
143 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
145 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
147 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
148 printf( "%08lx Used %08lx %04x back=%08lx EIP=%08lx\n",
149 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
150 pArena
->threadId
, *((DWORD
*)pArena
- 1),
152 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
153 arenaSize
+= sizeof(ARENA_INUSE
);
154 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
158 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
159 printf( "%08lx used %08lx %04x EIP=%08lx\n",
160 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
161 pArena
->threadId
, pArena
->callerEIP
);
162 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
163 arenaSize
+= sizeof(ARENA_INUSE
);
164 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
167 printf( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
168 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
169 arenaSize
, (arenaSize
* 100) / subheap
->size
);
170 subheap
= subheap
->next
;
175 /***********************************************************************
178 static HEAP
*HEAP_GetPtr( HANDLE32 heap
)
180 HEAP
*heapPtr
= (HEAP
*)heap
;
181 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
183 fprintf( stderr
, "Invalid heap %08x!\n", heap
);
184 SetLastError( ERROR_INVALID_HANDLE
);
187 if (debugging_heap
&& !HeapValidate( heap
, 0, NULL
))
189 HEAP_Dump( heapPtr
);
191 SetLastError( ERROR_INVALID_HANDLE
);
198 /***********************************************************************
199 * HEAP_InsertFreeBlock
201 * Insert a free block into the free list.
203 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
205 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
206 while (pEntry
->size
< pArena
->size
) pEntry
++;
207 pArena
->size
|= ARENA_FLAG_FREE
;
208 pArena
->next
= pEntry
->arena
.next
;
209 pArena
->next
->prev
= pArena
;
210 pArena
->prev
= &pEntry
->arena
;
211 pEntry
->arena
.next
= pArena
;
215 /***********************************************************************
218 * Find the sub-heap containing a given address.
220 static SUBHEAP
*HEAP_FindSubHeap( HEAP
*heap
, LPCVOID ptr
)
222 SUBHEAP
*sub
= &heap
->subheap
;
225 if (((char *)ptr
>= (char *)sub
) &&
226 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
233 /***********************************************************************
236 * Make sure the heap storage is committed up to (not including) ptr.
238 static BOOL32
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
240 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
241 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
242 if (size
> subheap
->size
) size
= subheap
->size
;
243 if (size
<= subheap
->commitSize
) return TRUE
;
244 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
245 size
- subheap
->commitSize
, MEM_COMMIT
,
246 PAGE_EXECUTE_READWRITE
))
248 fprintf( stderr
, "HEAP_Commit: could not commit %08lx bytes at %08lx for heap %08lx\n",
249 size
- subheap
->commitSize
,
250 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
251 (DWORD
)subheap
->heap
);
254 subheap
->commitSize
= size
;
259 /***********************************************************************
262 * If possible, decommit the heap storage from (including) 'ptr'.
264 static BOOL32
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
266 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
267 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
268 if (size
>= subheap
->commitSize
) return TRUE
;
269 if (!VirtualFree( (char *)subheap
+ subheap
->commitSize
,
270 size
- subheap
->commitSize
, MEM_DECOMMIT
))
272 fprintf( stderr
, "HEAP_Decommit: could not decommit %08lx bytes at %08lx for heap %08lx\n",
273 size
- subheap
->commitSize
,
274 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
275 (DWORD
)subheap
->heap
);
278 subheap
->commitSize
= size
;
283 /***********************************************************************
284 * HEAP_CreateFreeBlock
286 * Create a free block at a specified address. 'size' is the size of the
287 * whole block, including the new arena.
289 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
293 /* Create a free arena */
295 pFree
= (ARENA_FREE
*)ptr
;
296 pFree
->threadId
= GetCurrentTask();
297 pFree
->magic
= ARENA_FREE_MAGIC
;
299 /* If debugging, erase the freed block content */
303 char *pEnd
= (char *)ptr
+ size
;
304 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
305 pEnd
= (char *)subheap
+ subheap
->commitSize
;
306 if (pEnd
> (char *)(pFree
+ 1))
307 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
310 /* Check if next block is free also */
312 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
313 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
315 /* Remove the next arena from the free list */
316 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
317 pNext
->next
->prev
= pNext
->prev
;
318 pNext
->prev
->next
= pNext
->next
;
319 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
321 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
324 /* Set the next block PREV_FREE flag and pointer */
326 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
328 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
329 *pNext
|= ARENA_FLAG_PREV_FREE
;
330 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
333 /* Last, insert the new block into the free list */
335 pFree
->size
= size
- sizeof(*pFree
);
336 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
340 /***********************************************************************
341 * HEAP_MakeInUseBlockFree
343 * Turn an in-use block into a free block. Can also decommit the end of
344 * the heap, and possibly even free the sub-heap altogether.
346 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
349 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
351 /* Check if we can merge with previous block */
353 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
355 pFree
= *((ARENA_FREE
**)pArena
- 1);
356 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
357 /* Remove it from the free list */
358 pFree
->next
->prev
= pFree
->prev
;
359 pFree
->prev
->next
= pFree
->next
;
361 else pFree
= (ARENA_FREE
*)pArena
;
363 /* Create a free block */
365 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
366 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
367 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
368 return; /* Not the last block, so nothing more to do */
370 /* Free the whole sub-heap if it's empty and not the original one */
372 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
373 (subheap
!= &subheap
->heap
->subheap
))
375 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
376 /* Remove the free block from the list */
377 pFree
->next
->prev
= pFree
->prev
;
378 pFree
->prev
->next
= pFree
->next
;
379 /* Remove the subheap from the list */
380 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
381 if (pPrev
) pPrev
->next
= subheap
->next
;
382 /* Free the memory */
384 if (subheap
->selector
) FreeSelector( subheap
->selector
);
385 VirtualFree( subheap
, subheap
->size
, MEM_DECOMMIT
);
386 VirtualFree( subheap
, 0, MEM_RELEASE
);
390 /* Decommit the end of the heap */
392 HEAP_Decommit( subheap
, pFree
+ 1 );
396 /***********************************************************************
399 * Shrink an in-use block.
401 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
403 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
405 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
406 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
407 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
411 /* Turn off PREV_FREE flag in next block */
412 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
413 if (pNext
< (char *)subheap
+ subheap
->size
)
414 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
419 /***********************************************************************
422 * Create a sub-heap of the given size.
424 static SUBHEAP
*HEAP_CreateSubHeap( DWORD flags
, DWORD commitSize
,
430 /* Round-up sizes on a 64K boundary */
432 if (flags
& HEAP_WINE_SEGPTR
)
434 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
438 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
439 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
440 if (!commitSize
) commitSize
= 0x10000;
441 if (totalSize
< commitSize
) totalSize
= commitSize
;
444 /* Allocate the memory block */
446 if (!(subheap
= VirtualAlloc( NULL
, totalSize
,
447 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
449 fprintf( stderr
, "HEAP_CreateSubHeap: could not VirtualAlloc %08lx bytes\n",
453 if (!VirtualAlloc(subheap
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
455 fprintf( stderr
, "HEAP_CreateSubHeap: could not commit %08lx bytes for sub-heap %08lx\n",
456 commitSize
, (DWORD
)subheap
);
457 VirtualFree( subheap
, 0, MEM_RELEASE
);
461 /* Allocate a selector if needed */
463 if (flags
& HEAP_WINE_SEGPTR
)
465 selector
= SELECTOR_AllocBlock( subheap
, totalSize
,
466 (flags
& HEAP_WINE_CODESEG
) ? SEGMENT_CODE
: SEGMENT_DATA
,
467 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
470 fprintf( stderr
, "HEAP_CreateSubHeap: could not allocate selector\n" );
471 VirtualFree( subheap
, 0, MEM_RELEASE
);
476 /* Fill the sub-heap structure */
478 subheap
->size
= totalSize
;
479 subheap
->commitSize
= commitSize
;
480 subheap
->headerSize
= sizeof(*subheap
);
481 subheap
->next
= NULL
;
482 subheap
->heap
= NULL
;
483 subheap
->magic
= SUBHEAP_MAGIC
;
484 subheap
->selector
= selector
;
489 /***********************************************************************
492 * Find a free block at least as large as the requested size, and make sure
493 * the requested size is committed.
495 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
496 SUBHEAP
**ppSubHeap
)
500 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
502 /* Find a suitable free list, and in it find a block large enough */
504 while (pEntry
->size
< size
) pEntry
++;
505 pArena
= pEntry
->arena
.next
;
506 while (pArena
!= &heap
->freeList
[0].arena
)
508 if (pArena
->size
> size
)
510 subheap
= HEAP_FindSubHeap( heap
, pArena
);
511 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
512 + size
+ HEAP_MIN_BLOCK_SIZE
))
514 *ppSubHeap
= subheap
;
518 pArena
= pArena
->next
;
521 /* If no block was found, attempt to grow the heap */
523 if (!(heap
->flags
& HEAP_GROWABLE
))
525 fprintf( stderr
, "HEAP_FindFreeBlock: Not enough space in heap %08lx for %08lx bytes\n",
529 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
530 if (!(subheap
= HEAP_CreateSubHeap( heap
->flags
, size
,
531 MAX( HEAP_DEF_SIZE
, size
) )))
534 /* Insert the new sub-heap in the list */
536 subheap
->heap
= heap
;
537 subheap
->next
= heap
->subheap
.next
;
538 heap
->subheap
.next
= subheap
;
539 size
= subheap
->size
;
540 dprintf_heap( stddeb
, "HEAP_FindFreeBlock: created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
541 (DWORD
)subheap
, size
, (DWORD
)heap
);
543 HEAP_CreateFreeBlock( subheap
, subheap
+ 1, size
- sizeof(*subheap
) );
544 *ppSubHeap
= subheap
;
545 return (ARENA_FREE
*)(subheap
+ 1);
549 /***********************************************************************
550 * HEAP_IsValidArenaPtr
552 * Check that the pointer is inside the range possible for arenas.
554 static BOOL32
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
557 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
558 if (!subheap
) return FALSE
;
559 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
560 if (subheap
!= &heap
->subheap
) return FALSE
;
561 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
562 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
567 /***********************************************************************
568 * HEAP_ValidateFreeArena
570 static BOOL32
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
572 char *heapEnd
= (char *)subheap
+ subheap
->size
;
574 /* Check magic number */
575 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
577 fprintf( stderr
, "Heap %08lx: invalid free arena magic for %08lx\n",
578 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
581 /* Check size flags */
582 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
583 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
585 fprintf( stderr
, "Heap %08lx: bad flags %lx for free arena %08lx\n",
586 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
588 /* Check arena size */
589 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
591 fprintf( stderr
, "Heap %08lx: bad size %08lx for free arena %08lx\n",
592 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
595 /* Check that next pointer is valid */
596 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
598 fprintf( stderr
, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
599 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
602 /* Check that next arena is free */
603 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
604 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
606 fprintf( stderr
, "Heap %08lx: next arena %08lx invalid for %08lx\n",
607 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
610 /* Check that prev pointer is valid */
611 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
613 fprintf( stderr
, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
614 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
617 /* Check that prev arena is free */
618 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
619 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
621 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for %08lx\n",
622 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
625 /* Check that next block has PREV_FREE flag */
626 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
628 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
629 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
631 fprintf( stderr
, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
632 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
635 /* Check next block back pointer */
636 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
637 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
639 fprintf( stderr
, "Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
640 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
641 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
649 /***********************************************************************
650 * HEAP_ValidateInUseArena
652 static BOOL32
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
654 char *heapEnd
= (char *)subheap
+ subheap
->size
;
656 /* Check magic number */
657 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
659 fprintf( stderr
, "Heap %08lx: invalid in-use arena magic for %08lx\n",
660 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
663 /* Check size flags */
664 if (pArena
->size
& ARENA_FLAG_FREE
)
666 fprintf( stderr
, "Heap %08lx: bad flags %lx for in-use arena %08lx\n",
667 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
669 /* Check arena size */
670 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
672 fprintf( stderr
, "Heap %08lx: bad size %08lx for in-use arena %08lx\n",
673 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
676 /* Check next arena PREV_FREE flag */
677 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
678 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
680 fprintf( stderr
, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
681 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
684 /* Check prev free arena */
685 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
687 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
688 /* Check prev pointer */
689 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
691 fprintf(stderr
, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
692 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
695 /* Check that prev arena is free */
696 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
697 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
699 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
700 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
703 /* Check that prev arena is really the previous block */
704 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
706 fprintf( stderr
, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
707 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
715 /***********************************************************************
718 * Check whether the pointer is to a block inside a given heap.
720 int HEAP_IsInsideHeap( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
722 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
726 /* Validate the parameters */
728 if (!heapPtr
) return 0;
729 flags
|= heapPtr
->flags
;
730 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
731 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
732 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
733 + sizeof(ARENA_INUSE
))));
734 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
739 /***********************************************************************
742 * Transform a linear pointer into a SEGPTR. The pointer must have been
743 * allocated from a HEAP_WINE_SEGPTR heap.
745 SEGPTR
HEAP_GetSegptr( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
747 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
751 /* Validate the parameters */
753 if (!heapPtr
) return 0;
754 flags
|= heapPtr
->flags
;
755 if (!(flags
& HEAP_WINE_SEGPTR
))
757 fprintf( stderr
, "HEAP_GetSegptr: heap %08x is not a SEGPTR heap\n",
761 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
763 /* Get the subheap */
765 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
767 fprintf( stderr
, "HEAP_GetSegptr: %p is not inside heap %08x\n",
769 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
773 /* Build the SEGPTR */
775 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
776 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
781 /***********************************************************************
782 * HeapCreate (KERNEL32.336)
784 HANDLE32
HeapCreate( DWORD flags
, DWORD initialSize
, DWORD maxSize
)
789 FREE_LIST_ENTRY
*pEntry
;
791 /* Allocate the heap block */
795 maxSize
= HEAP_DEF_SIZE
;
796 flags
|= HEAP_GROWABLE
;
798 if (!(subheap
= HEAP_CreateSubHeap( flags
, initialSize
, maxSize
)))
800 SetLastError( ERROR_OUTOFMEMORY
);
804 /* Fill the heap structure */
806 heap
= (HEAP
*)subheap
;
807 subheap
->heap
= heap
;
808 subheap
->headerSize
= sizeof(HEAP
);
811 heap
->magic
= HEAP_MAGIC
;
812 InitializeCriticalSection( &heap
->critSection
);
814 /* Build the free lists */
816 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
818 pEntry
->size
= HEAP_freeListSizes
[i
];
819 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
820 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
821 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
822 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
823 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
824 pEntry
->arena
.threadId
= 0;
825 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
828 /* Create the first free block */
830 HEAP_CreateFreeBlock( subheap
, heap
+ 1, subheap
->size
- sizeof(*heap
) );
835 return (HANDLE32
)heap
;
839 /***********************************************************************
840 * HeapDestroy (KERNEL32.337)
842 BOOL32
HeapDestroy( HANDLE32 heap
)
844 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
847 dprintf_heap( stddeb
, "HeapDestroy: %08x\n", heap
);
848 if (!heapPtr
) return FALSE
;
850 DeleteCriticalSection( &heapPtr
->critSection
);
851 subheap
= &heapPtr
->subheap
;
854 SUBHEAP
*next
= subheap
->next
;
855 if (subheap
->selector
) FreeSelector( subheap
->selector
);
856 VirtualFree( subheap
, subheap
->commitSize
, MEM_DECOMMIT
);
857 VirtualFree( subheap
, 0, MEM_RELEASE
);
864 /***********************************************************************
865 * HeapAlloc (KERNEL32.334)
867 LPVOID
HeapAlloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
872 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
874 /* Validate the parameters */
876 if (!heapPtr
) return NULL
;
877 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
878 flags
|= heapPtr
->flags
;
879 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
880 size
= (size
+ 3) & ~3;
881 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
883 /* Locate a suitable free block */
885 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
887 dprintf_heap( stddeb
, "HeapAlloc(%08x,%08lx,%08lx): returning NULL\n",
889 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
890 SetLastError( ERROR_OUTOFMEMORY
);
894 /* Remove the arena from the free list */
896 pArena
->next
->prev
= pArena
->prev
;
897 pArena
->prev
->next
= pArena
->next
;
899 /* Build the in-use arena */
901 pInUse
= (ARENA_INUSE
*)pArena
;
902 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
903 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
904 pInUse
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
905 pInUse
->threadId
= GetCurrentTask();
906 pInUse
->magic
= ARENA_INUSE_MAGIC
;
908 /* Shrink the block */
910 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
912 if (flags
& HEAP_ZERO_MEMORY
) memset( pInUse
+ 1, 0, size
);
913 else if (debugging_heap
) memset( pInUse
+ 1, ARENA_INUSE_FILLER
, size
);
915 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
918 dprintf_heap( stddeb
, "HeapAlloc(%08x,%08lx,%08lx): returning %08lx\n",
919 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
920 return (LPVOID
)(pInUse
+ 1);
924 /***********************************************************************
925 * HeapFree (KERNEL32.338)
927 BOOL32
HeapFree( HANDLE32 heap
, DWORD flags
, LPVOID ptr
)
931 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
933 /* Validate the parameters */
935 if (!heapPtr
) return FALSE
;
936 flags
&= HEAP_NO_SERIALIZE
;
937 flags
|= heapPtr
->flags
;
938 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
939 if (!ptr
|| !HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
941 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
942 SetLastError( ERROR_INVALID_PARAMETER
);
943 dprintf_heap( stddeb
, "HeapFree(%08x,%08lx,%08lx): returning FALSE\n",
944 heap
, flags
, (DWORD
)ptr
);
948 /* Turn the block into a free block */
950 pInUse
= (ARENA_INUSE
*)ptr
- 1;
951 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
952 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
954 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
955 /* SetLastError( 0 ); */
957 dprintf_heap( stddeb
, "HeapFree(%08x,%08lx,%08lx): returning TRUE\n",
958 heap
, flags
, (DWORD
)ptr
);
963 /***********************************************************************
964 * HeapReAlloc (KERNEL32.340)
966 LPVOID
HeapReAlloc( HANDLE32 heap
, DWORD flags
, LPVOID ptr
, DWORD size
)
973 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
974 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
976 /* Validate the parameters */
978 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
979 HEAP_REALLOC_IN_PLACE_ONLY
;
980 flags
|= heapPtr
->flags
;
981 size
= (size
+ 3) & ~3;
982 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
984 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
985 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
987 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
988 SetLastError( ERROR_INVALID_PARAMETER
);
989 dprintf_heap( stddeb
, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning NULL\n",
990 heap
, flags
, (DWORD
)ptr
, size
);
994 /* Check if we need to grow the block */
996 pArena
= (ARENA_INUSE
*)ptr
- 1;
997 pArena
->threadId
= GetCurrentTask();
998 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
999 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1002 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1003 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1004 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1005 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1007 /* The next block is free and large enough */
1008 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1009 pFree
->next
->prev
= pFree
->prev
;
1010 pFree
->prev
->next
= pFree
->next
;
1011 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1012 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1013 + size
+ HEAP_MIN_BLOCK_SIZE
))
1015 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1016 SetLastError( ERROR_OUTOFMEMORY
);
1019 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1021 else /* Do it the hard way */
1024 ARENA_INUSE
*pInUse
;
1025 SUBHEAP
*newsubheap
;
1027 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1028 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1030 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1031 SetLastError( ERROR_OUTOFMEMORY
);
1035 /* Build the in-use arena */
1037 pNew
->next
->prev
= pNew
->prev
;
1038 pNew
->prev
->next
= pNew
->next
;
1039 pInUse
= (ARENA_INUSE
*)pNew
;
1040 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1041 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1042 pInUse
->threadId
= GetCurrentTask();
1043 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1044 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1045 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1047 /* Free the previous block */
1049 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1050 subheap
= newsubheap
;
1054 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1056 /* Clear the extra bytes if needed */
1060 if (flags
& HEAP_ZERO_MEMORY
)
1061 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1062 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1063 else if (debugging_heap
)
1064 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1065 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1068 /* Return the new arena */
1070 pArena
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
1071 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1073 dprintf_heap( stddeb
, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1074 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1075 return (LPVOID
)(pArena
+ 1);
1079 /***********************************************************************
1080 * HeapCompact (KERNEL32.335)
1082 DWORD
HeapCompact( HANDLE32 heap
, DWORD flags
)
1088 /***********************************************************************
1089 * HeapLock (KERNEL32.339)
1091 BOOL32
HeapLock( HANDLE32 heap
)
1093 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1095 if (!heapPtr
) return FALSE
;
1096 EnterCriticalSection( &heapPtr
->critSection
);
1101 /***********************************************************************
1102 * HeapUnlock (KERNEL32.342)
1104 BOOL32
HeapUnlock( HANDLE32 heap
)
1106 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1108 if (!heapPtr
) return FALSE
;
1109 LeaveCriticalSection( &heapPtr
->critSection
);
1114 /***********************************************************************
1115 * HeapSize (KERNEL32.341)
1117 DWORD
HeapSize( HANDLE32 heap
, DWORD flags
, LPVOID ptr
)
1120 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1122 if (!heapPtr
) return FALSE
;
1123 flags
&= HEAP_NO_SERIALIZE
;
1124 flags
|= heapPtr
->flags
;
1125 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1126 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1128 SetLastError( ERROR_INVALID_PARAMETER
);
1133 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1134 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1136 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1138 dprintf_heap( stddeb
, "HeapSize(%08x,%08lx,%08lx): returning %08lx\n",
1139 heap
, flags
, (DWORD
)ptr
, ret
);
1144 /***********************************************************************
1145 * HeapValidate (KERNEL32.343)
1147 BOOL32
HeapValidate( HANDLE32 heap
, DWORD flags
, LPVOID block
)
1150 HEAP
*heapPtr
= (HEAP
*)heap
;
1152 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
1154 fprintf( stderr
, "Invalid heap %08x!\n", heap
);
1160 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
1161 ((char *)block
< (char *)subheap
+ subheap
->headerSize
1162 + sizeof(ARENA_INUSE
)))
1164 fprintf( stderr
, "Heap %08lx: block %08lx is not inside heap\n",
1165 (DWORD
)heap
, (DWORD
)block
);
1168 return HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1 );
1171 subheap
= &heapPtr
->subheap
;
1174 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1175 while (ptr
< (char *)subheap
+ subheap
->size
)
1177 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1179 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
))
1181 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1185 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
))
1187 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1190 subheap
= subheap
->next
;
1196 /***********************************************************************
1197 * HeapWalk (KERNEL32.344)
1199 BOOL32
HeapWalk( HANDLE32 heap
, void *entry
)
1201 fprintf( stderr
, "HeapWalk(%08x): not implemented\n", heap
);
1206 /***********************************************************************
1209 * Same as HeapAlloc(), but die on failure.
1211 LPVOID
HEAP_xalloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
1213 LPVOID p
= HeapAlloc( heap
, flags
, size
);
1216 fprintf( stderr
, "Virtual memory exhausted.\n" );
1223 /***********************************************************************
1226 LPSTR
HEAP_strdupA( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1228 LPSTR p
= HEAP_xalloc( heap
, flags
, lstrlen32A(str
) + 1 );
1229 lstrcpy32A( p
, str
);
1234 /***********************************************************************
1237 LPWSTR
HEAP_strdupW( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1239 INT32 len
= lstrlen32W(str
) + 1;
1240 LPWSTR p
= HEAP_xalloc( heap
, flags
, len
* sizeof(WCHAR
) );
1241 lstrcpy32W( p
, str
);
1246 /***********************************************************************
1249 LPWSTR
HEAP_strdupAtoW( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1253 if (!str
) return NULL
;
1254 ret
= HEAP_xalloc( heap
, flags
, (lstrlen32A(str
)+1) * sizeof(WCHAR
) );
1255 lstrcpyAtoW( ret
, str
);
1260 /***********************************************************************
1263 LPSTR
HEAP_strdupWtoA( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1267 if (!str
) return NULL
;
1268 ret
= HEAP_xalloc( heap
, flags
, lstrlen32W(str
) + 1 );
1269 lstrcpyWtoA( ret
, str
);