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
+ size
,
270 subheap
->commitSize
- size
, MEM_DECOMMIT
))
272 fprintf( stderr
, "HEAP_Decommit: could not decommit %08lx bytes at %08lx for heap %08lx\n",
273 subheap
->commitSize
- size
,
274 (DWORD
)((char *)subheap
+ size
),
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
, 0, MEM_RELEASE
);
389 /* Decommit the end of the heap */
391 HEAP_Decommit( subheap
, pFree
+ 1 );
395 /***********************************************************************
398 * Shrink an in-use block.
400 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
402 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
404 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
405 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
406 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
410 /* Turn off PREV_FREE flag in next block */
411 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
412 if (pNext
< (char *)subheap
+ subheap
->size
)
413 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
418 /***********************************************************************
421 * Create a sub-heap of the given size.
423 static SUBHEAP
*HEAP_CreateSubHeap( DWORD flags
, DWORD commitSize
,
429 /* Round-up sizes on a 64K boundary */
431 if (flags
& HEAP_WINE_SEGPTR
)
433 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
437 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
438 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
439 if (!commitSize
) commitSize
= 0x10000;
440 if (totalSize
< commitSize
) totalSize
= commitSize
;
443 /* Allocate the memory block */
445 if (!(subheap
= VirtualAlloc( NULL
, totalSize
,
446 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
448 fprintf( stderr
, "HEAP_CreateSubHeap: could not VirtualAlloc %08lx bytes\n",
452 if (!VirtualAlloc(subheap
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
454 fprintf( stderr
, "HEAP_CreateSubHeap: could not commit %08lx bytes for sub-heap %08lx\n",
455 commitSize
, (DWORD
)subheap
);
456 VirtualFree( subheap
, 0, MEM_RELEASE
);
460 /* Allocate a selector if needed */
462 if (flags
& HEAP_WINE_SEGPTR
)
464 selector
= SELECTOR_AllocBlock( subheap
, totalSize
,
465 (flags
& HEAP_WINE_CODESEG
) ? SEGMENT_CODE
: SEGMENT_DATA
,
466 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
469 fprintf( stderr
, "HEAP_CreateSubHeap: could not allocate selector\n" );
470 VirtualFree( subheap
, 0, MEM_RELEASE
);
475 /* Fill the sub-heap structure */
477 subheap
->size
= totalSize
;
478 subheap
->commitSize
= commitSize
;
479 subheap
->headerSize
= sizeof(*subheap
);
480 subheap
->next
= NULL
;
481 subheap
->heap
= NULL
;
482 subheap
->magic
= SUBHEAP_MAGIC
;
483 subheap
->selector
= selector
;
488 /***********************************************************************
491 * Find a free block at least as large as the requested size, and make sure
492 * the requested size is committed.
494 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
495 SUBHEAP
**ppSubHeap
)
499 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
501 /* Find a suitable free list, and in it find a block large enough */
503 while (pEntry
->size
< size
) pEntry
++;
504 pArena
= pEntry
->arena
.next
;
505 while (pArena
!= &heap
->freeList
[0].arena
)
507 if (pArena
->size
> size
)
509 subheap
= HEAP_FindSubHeap( heap
, pArena
);
510 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
511 + size
+ HEAP_MIN_BLOCK_SIZE
))
513 *ppSubHeap
= subheap
;
517 pArena
= pArena
->next
;
520 /* If no block was found, attempt to grow the heap */
522 if (!(heap
->flags
& HEAP_GROWABLE
))
524 fprintf( stderr
, "HEAP_FindFreeBlock: Not enough space in heap %08lx for %08lx bytes\n",
528 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
529 if (!(subheap
= HEAP_CreateSubHeap( heap
->flags
, size
,
530 MAX( HEAP_DEF_SIZE
, size
) )))
533 /* Insert the new sub-heap in the list */
535 subheap
->heap
= heap
;
536 subheap
->next
= heap
->subheap
.next
;
537 heap
->subheap
.next
= subheap
;
538 size
= subheap
->size
;
539 dprintf_heap( stddeb
, "HEAP_FindFreeBlock: created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
540 (DWORD
)subheap
, size
, (DWORD
)heap
);
542 HEAP_CreateFreeBlock( subheap
, subheap
+ 1, size
- sizeof(*subheap
) );
543 *ppSubHeap
= subheap
;
544 return (ARENA_FREE
*)(subheap
+ 1);
548 /***********************************************************************
549 * HEAP_IsValidArenaPtr
551 * Check that the pointer is inside the range possible for arenas.
553 static BOOL32
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
556 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
557 if (!subheap
) return FALSE
;
558 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
559 if (subheap
!= &heap
->subheap
) return FALSE
;
560 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
561 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
566 /***********************************************************************
567 * HEAP_ValidateFreeArena
569 static BOOL32
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
571 char *heapEnd
= (char *)subheap
+ subheap
->size
;
573 /* Check magic number */
574 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
576 fprintf( stderr
, "Heap %08lx: invalid free arena magic for %08lx\n",
577 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
580 /* Check size flags */
581 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
582 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
584 fprintf( stderr
, "Heap %08lx: bad flags %lx for free arena %08lx\n",
585 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
587 /* Check arena size */
588 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
590 fprintf( stderr
, "Heap %08lx: bad size %08lx for free arena %08lx\n",
591 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
594 /* Check that next pointer is valid */
595 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
597 fprintf( stderr
, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
598 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
601 /* Check that next arena is free */
602 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
603 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
605 fprintf( stderr
, "Heap %08lx: next arena %08lx invalid for %08lx\n",
606 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
609 /* Check that prev pointer is valid */
610 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
612 fprintf( stderr
, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
613 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
616 /* Check that prev arena is free */
617 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
618 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
620 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for %08lx\n",
621 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
624 /* Check that next block has PREV_FREE flag */
625 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
627 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
628 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
630 fprintf( stderr
, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
631 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
634 /* Check next block back pointer */
635 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
636 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
638 fprintf( stderr
, "Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
639 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
640 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
648 /***********************************************************************
649 * HEAP_ValidateInUseArena
651 static BOOL32
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
653 char *heapEnd
= (char *)subheap
+ subheap
->size
;
655 /* Check magic number */
656 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
658 fprintf( stderr
, "Heap %08lx: invalid in-use arena magic for %08lx\n",
659 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
662 /* Check size flags */
663 if (pArena
->size
& ARENA_FLAG_FREE
)
665 fprintf( stderr
, "Heap %08lx: bad flags %lx for in-use arena %08lx\n",
666 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
668 /* Check arena size */
669 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
671 fprintf( stderr
, "Heap %08lx: bad size %08lx for in-use arena %08lx\n",
672 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
675 /* Check next arena PREV_FREE flag */
676 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
677 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
679 fprintf( stderr
, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
680 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
683 /* Check prev free arena */
684 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
686 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
687 /* Check prev pointer */
688 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
690 fprintf(stderr
, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
691 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
694 /* Check that prev arena is free */
695 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
696 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
698 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
699 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
702 /* Check that prev arena is really the previous block */
703 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
705 fprintf( stderr
, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
706 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
714 /***********************************************************************
717 * Check whether the pointer is to a block inside a given heap.
719 int HEAP_IsInsideHeap( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
721 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
725 /* Validate the parameters */
727 if (!heapPtr
) return 0;
728 flags
|= heapPtr
->flags
;
729 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
730 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
731 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
732 + sizeof(ARENA_INUSE
))));
733 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
738 /***********************************************************************
741 * Transform a linear pointer into a SEGPTR. The pointer must have been
742 * allocated from a HEAP_WINE_SEGPTR heap.
744 SEGPTR
HEAP_GetSegptr( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
746 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
750 /* Validate the parameters */
752 if (!heapPtr
) return 0;
753 flags
|= heapPtr
->flags
;
754 if (!(flags
& HEAP_WINE_SEGPTR
))
756 fprintf( stderr
, "HEAP_GetSegptr: heap %08x is not a SEGPTR heap\n",
760 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
762 /* Get the subheap */
764 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
766 fprintf( stderr
, "HEAP_GetSegptr: %p is not inside heap %08x\n",
768 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
772 /* Build the SEGPTR */
774 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
775 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
780 /***********************************************************************
781 * HeapCreate (KERNEL32.336)
783 HANDLE32 WINAPI
HeapCreate( DWORD flags
, DWORD initialSize
, DWORD maxSize
)
788 FREE_LIST_ENTRY
*pEntry
;
790 /* Allocate the heap block */
794 maxSize
= HEAP_DEF_SIZE
;
795 flags
|= HEAP_GROWABLE
;
797 if (!(subheap
= HEAP_CreateSubHeap( flags
, initialSize
, maxSize
)))
799 SetLastError( ERROR_OUTOFMEMORY
);
803 /* Fill the heap structure */
805 heap
= (HEAP
*)subheap
;
806 subheap
->heap
= heap
;
807 subheap
->headerSize
= sizeof(HEAP
);
810 heap
->magic
= HEAP_MAGIC
;
811 InitializeCriticalSection( &heap
->critSection
);
813 /* Build the free lists */
815 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
817 pEntry
->size
= HEAP_freeListSizes
[i
];
818 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
819 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
820 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
821 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
822 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
823 pEntry
->arena
.threadId
= 0;
824 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
827 /* Create the first free block */
829 HEAP_CreateFreeBlock( subheap
, heap
+ 1, subheap
->size
- sizeof(*heap
) );
834 return (HANDLE32
)heap
;
838 /***********************************************************************
839 * HeapDestroy (KERNEL32.337)
841 BOOL32 WINAPI
HeapDestroy( HANDLE32 heap
)
843 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
846 dprintf_heap( stddeb
, "HeapDestroy: %08x\n", heap
);
847 if (!heapPtr
) return FALSE
;
849 DeleteCriticalSection( &heapPtr
->critSection
);
850 subheap
= &heapPtr
->subheap
;
853 SUBHEAP
*next
= subheap
->next
;
854 if (subheap
->selector
) FreeSelector( subheap
->selector
);
855 VirtualFree( subheap
, 0, MEM_RELEASE
);
862 /***********************************************************************
863 * HeapAlloc (KERNEL32.334)
865 LPVOID WINAPI
HeapAlloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
870 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
872 /* Validate the parameters */
874 if (!heapPtr
) return NULL
;
875 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
876 flags
|= heapPtr
->flags
;
877 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
878 size
= (size
+ 3) & ~3;
879 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
881 /* Locate a suitable free block */
883 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
885 dprintf_heap( stddeb
, "HeapAlloc(%08x,%08lx,%08lx): returning NULL\n",
887 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
888 SetLastError( ERROR_COMMITMENT_LIMIT
);
892 /* Remove the arena from the free list */
894 pArena
->next
->prev
= pArena
->prev
;
895 pArena
->prev
->next
= pArena
->next
;
897 /* Build the in-use arena */
899 pInUse
= (ARENA_INUSE
*)pArena
;
900 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
901 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
902 pInUse
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
903 pInUse
->threadId
= GetCurrentTask();
904 pInUse
->magic
= ARENA_INUSE_MAGIC
;
906 /* Shrink the block */
908 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
910 if (flags
& HEAP_ZERO_MEMORY
) memset( pInUse
+ 1, 0, size
);
911 else if (debugging_heap
) memset( pInUse
+ 1, ARENA_INUSE_FILLER
, size
);
913 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
915 dprintf_heap( stddeb
, "HeapAlloc(%08x,%08lx,%08lx): returning %08lx\n",
916 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
917 return (LPVOID
)(pInUse
+ 1);
921 /***********************************************************************
922 * HeapFree (KERNEL32.338)
924 BOOL32 WINAPI
HeapFree( HANDLE32 heap
, DWORD flags
, LPVOID ptr
)
928 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
930 /* Validate the parameters */
932 if (!heapPtr
) return FALSE
;
933 flags
&= HEAP_NO_SERIALIZE
;
934 flags
|= heapPtr
->flags
;
935 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
936 if (!ptr
|| !HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
938 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
939 SetLastError( ERROR_INVALID_PARAMETER
);
940 dprintf_heap( stddeb
, "HeapFree(%08x,%08lx,%08lx): returning FALSE\n",
941 heap
, flags
, (DWORD
)ptr
);
945 /* Turn the block into a free block */
947 pInUse
= (ARENA_INUSE
*)ptr
- 1;
948 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
949 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
951 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
952 /* SetLastError( 0 ); */
954 dprintf_heap( stddeb
, "HeapFree(%08x,%08lx,%08lx): returning TRUE\n",
955 heap
, flags
, (DWORD
)ptr
);
960 /***********************************************************************
961 * HeapReAlloc (KERNEL32.340)
963 LPVOID WINAPI
HeapReAlloc( HANDLE32 heap
, DWORD flags
, LPVOID ptr
, DWORD size
)
970 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
971 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
973 /* Validate the parameters */
975 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
976 HEAP_REALLOC_IN_PLACE_ONLY
;
977 flags
|= heapPtr
->flags
;
978 size
= (size
+ 3) & ~3;
979 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
981 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
982 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
984 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
985 SetLastError( ERROR_INVALID_PARAMETER
);
986 dprintf_heap( stddeb
, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning NULL\n",
987 heap
, flags
, (DWORD
)ptr
, size
);
991 /* Check if we need to grow the block */
993 pArena
= (ARENA_INUSE
*)ptr
- 1;
994 pArena
->threadId
= GetCurrentTask();
995 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
996 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
999 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1000 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1001 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1002 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1004 /* The next block is free and large enough */
1005 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1006 pFree
->next
->prev
= pFree
->prev
;
1007 pFree
->prev
->next
= pFree
->next
;
1008 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1009 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1010 + size
+ HEAP_MIN_BLOCK_SIZE
))
1012 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1013 SetLastError( ERROR_OUTOFMEMORY
);
1016 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1018 else /* Do it the hard way */
1021 ARENA_INUSE
*pInUse
;
1022 SUBHEAP
*newsubheap
;
1024 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1025 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1027 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1028 SetLastError( ERROR_OUTOFMEMORY
);
1032 /* Build the in-use arena */
1034 pNew
->next
->prev
= pNew
->prev
;
1035 pNew
->prev
->next
= pNew
->next
;
1036 pInUse
= (ARENA_INUSE
*)pNew
;
1037 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1038 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1039 pInUse
->threadId
= GetCurrentTask();
1040 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1041 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1042 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1044 /* Free the previous block */
1046 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1047 subheap
= newsubheap
;
1051 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1053 /* Clear the extra bytes if needed */
1057 if (flags
& HEAP_ZERO_MEMORY
)
1058 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1059 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1060 else if (debugging_heap
)
1061 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1062 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1065 /* Return the new arena */
1067 pArena
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
1068 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1070 dprintf_heap( stddeb
, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1071 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1072 return (LPVOID
)(pArena
+ 1);
1076 /***********************************************************************
1077 * HeapCompact (KERNEL32.335)
1079 DWORD WINAPI
HeapCompact( HANDLE32 heap
, DWORD flags
)
1085 /***********************************************************************
1086 * HeapLock (KERNEL32.339)
1088 BOOL32 WINAPI
HeapLock( HANDLE32 heap
)
1090 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1092 if (!heapPtr
) return FALSE
;
1093 EnterCriticalSection( &heapPtr
->critSection
);
1098 /***********************************************************************
1099 * HeapUnlock (KERNEL32.342)
1101 BOOL32 WINAPI
HeapUnlock( HANDLE32 heap
)
1103 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1105 if (!heapPtr
) return FALSE
;
1106 LeaveCriticalSection( &heapPtr
->critSection
);
1111 /***********************************************************************
1112 * HeapSize (KERNEL32.341)
1114 DWORD WINAPI
HeapSize( HANDLE32 heap
, DWORD flags
, LPVOID ptr
)
1117 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1119 if (!heapPtr
) return FALSE
;
1120 flags
&= HEAP_NO_SERIALIZE
;
1121 flags
|= heapPtr
->flags
;
1122 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1123 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1125 SetLastError( ERROR_INVALID_PARAMETER
);
1130 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1131 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1133 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1135 dprintf_heap( stddeb
, "HeapSize(%08x,%08lx,%08lx): returning %08lx\n",
1136 heap
, flags
, (DWORD
)ptr
, ret
);
1141 /***********************************************************************
1142 * HeapValidate (KERNEL32.343)
1144 BOOL32 WINAPI
HeapValidate( HANDLE32 heap
, DWORD flags
, LPCVOID block
)
1147 HEAP
*heapPtr
= (HEAP
*)heap
;
1149 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
1151 fprintf( stderr
, "Invalid heap %08x!\n", heap
);
1157 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
1158 ((char *)block
< (char *)subheap
+ subheap
->headerSize
1159 + sizeof(ARENA_INUSE
)))
1161 fprintf( stderr
, "Heap %08lx: block %08lx is not inside heap\n",
1162 (DWORD
)heap
, (DWORD
)block
);
1165 return HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1 );
1168 subheap
= &heapPtr
->subheap
;
1171 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1172 while (ptr
< (char *)subheap
+ subheap
->size
)
1174 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1176 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
))
1178 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1182 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
))
1184 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1187 subheap
= subheap
->next
;
1193 /***********************************************************************
1194 * HeapWalk (KERNEL32.344)
1196 BOOL32 WINAPI
HeapWalk( HANDLE32 heap
, void *entry
)
1198 fprintf( stderr
, "HeapWalk(%08x): not implemented\n", heap
);
1203 /***********************************************************************
1206 * Same as HeapAlloc(), but die on failure.
1208 LPVOID
HEAP_xalloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
1210 LPVOID p
= HeapAlloc( heap
, flags
, size
);
1213 fprintf( stderr
, "Virtual memory exhausted.\n" );
1220 /***********************************************************************
1223 LPSTR
HEAP_strdupA( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1225 LPSTR p
= HEAP_xalloc( heap
, flags
, lstrlen32A(str
) + 1 );
1226 lstrcpy32A( p
, str
);
1231 /***********************************************************************
1234 LPWSTR
HEAP_strdupW( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1236 INT32 len
= lstrlen32W(str
) + 1;
1237 LPWSTR p
= HEAP_xalloc( heap
, flags
, len
* sizeof(WCHAR
) );
1238 lstrcpy32W( p
, str
);
1243 /***********************************************************************
1246 LPWSTR
HEAP_strdupAtoW( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1250 if (!str
) return NULL
;
1251 ret
= HEAP_xalloc( heap
, flags
, (lstrlen32A(str
)+1) * sizeof(WCHAR
) );
1252 lstrcpyAtoW( ret
, str
);
1257 /***********************************************************************
1260 LPSTR
HEAP_strdupWtoA( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1264 if (!str
) return NULL
;
1265 ret
= HEAP_xalloc( heap
, flags
, lstrlen32W(str
) + 1 );
1266 lstrcpyWtoA( ret
, str
);