4 * Copyright 1996 Alexandre Julliard
7 #define NO_TRANSITION_TYPES /* This file is Win32-clean */
13 #include "kernel32.h" /* for CRITICAL_SECTION */
14 #include "selectors.h"
21 /* Note: the heap data structures are based on what Pietrek describes in his
22 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
23 * the same, but could be easily adapted if it turns out some programs
27 typedef struct tagARENA_INUSE
29 DWORD size; /* Block size; must be the first field */
30 WORD threadId; /* Allocating thread id */
31 WORD magic; /* Magic number */
32 DWORD callerEIP; /* EIP of caller upon allocation */
35 typedef struct tagARENA_FREE
37 DWORD size; /* Block size; must be the first field */
38 WORD threadId; /* Freeing thread id */
39 WORD magic; /* Magic number */
40 struct tagARENA_FREE *next; /* Next free arena */
41 struct tagARENA_FREE *prev; /* Prev free arena */
44 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
45 #define ARENA_FLAG_PREV_FREE 0x00000002
46 #define ARENA_SIZE_MASK 0xfffffffc
47 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
48 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
50 #define ARENA_INUSE_FILLER 0x55
51 #define ARENA_FREE_FILLER 0xaa
53 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
55 /* Max size of the blocks on the free lists */
56 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
58 0x20, 0x80, 0x200, 0xffffffff
69 typedef struct tagSUBHEAP
71 DWORD size; /* Size of the whole sub-heap */
72 DWORD commitSize; /* Committed size of the sub-heap */
73 DWORD headerSize; /* Size of the heap header */
74 struct tagSUBHEAP *next; /* Next sub-heap */
75 struct tagHEAP *heap; /* Main heap structure */
76 DWORD magic; /* Magic number */
77 WORD selector; /* Selector for HEAP_WINE_SEGPTR heaps */
80 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
82 typedef struct tagHEAP
84 SUBHEAP subheap; /* First sub-heap */
85 struct tagHEAP *next; /* Next heap for this process */
86 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
87 CRITICAL_SECTION critSection; /* Critical section for serialization */
88 DWORD flags; /* Heap flags */
89 DWORD magic; /* Magic number */
92 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
94 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
95 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
98 /***********************************************************************
101 void HEAP_Dump( HEAP *heap )
107 printf( "Heap: %08lx\n", (DWORD)heap );
108 printf( "Next: %08lx Sub-heaps: %08lx",
109 (DWORD)heap->next, (DWORD)&heap->subheap );
110 subheap = &heap->subheap;
111 while (subheap->next)
113 printf( " -> %08lx", (DWORD)subheap->next );
114 subheap = subheap->next;
117 printf( "\nFree lists:\n Block Stat Size Id\n" );
118 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
119 printf( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
120 (DWORD)&heap->freeList[i].arena, heap->freeList[i].arena.size,
121 heap->freeList[i].arena.threadId,
122 (DWORD)heap->freeList[i].arena.prev,
123 (DWORD)heap->freeList[i].arena.next );
125 subheap = &heap->subheap;
128 DWORD freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
129 printf( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
130 (DWORD)subheap, subheap->size, subheap->commitSize );
132 printf( "\n Block Stat Size Id\n" );
133 ptr = (char*)subheap + subheap->headerSize;
134 while (ptr < (char *)subheap + subheap->size)
136 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
138 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
139 printf( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
140 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
141 pArena->threadId, (DWORD)pArena->prev,
142 (DWORD)pArena->next);
143 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
144 arenaSize += sizeof(ARENA_FREE);
145 freeSize += pArena->size & ARENA_SIZE_MASK;
147 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
149 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
150 printf( "%08lx Used %08lx %04x back=%08lx EIP=%08lx\n",
151 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
152 pArena->threadId, *((DWORD *)pArena - 1),
154 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
155 arenaSize += sizeof(ARENA_INUSE);
156 usedSize += pArena->size & ARENA_SIZE_MASK;
160 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
161 printf( "%08lx used %08lx %04x EIP=%08lx\n",
162 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
163 pArena->threadId, pArena->callerEIP );
164 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
165 arenaSize += sizeof(ARENA_INUSE);
166 usedSize += pArena->size & ARENA_SIZE_MASK;
169 printf( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
170 subheap->size, subheap->commitSize, freeSize, usedSize,
171 arenaSize, (arenaSize * 100) / subheap->size );
172 subheap = subheap->next;
177 /***********************************************************************
180 static HEAP *HEAP_GetPtr( HANDLE32 heap )
182 HEAP *heapPtr = (HEAP *)heap;
183 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
185 fprintf( stderr, "Invalid heap %08x!\n", heap );
186 SetLastError( ERROR_INVALID_HANDLE );
189 if (debugging_heap && !HeapValidate( heap, 0, NULL ))
191 HEAP_Dump( heapPtr );
192 DEBUG_EnterDebugger();
193 SetLastError( ERROR_INVALID_HANDLE );
200 /***********************************************************************
201 * HEAP_InsertFreeBlock
203 * Insert a free block into the free list.
205 static void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena )
207 FREE_LIST_ENTRY *pEntry = heap->freeList;
208 while (pEntry->size < pArena->size) pEntry++;
209 pArena->size |= ARENA_FLAG_FREE;
210 pArena->next = pEntry->arena.next;
211 pArena->next->prev = pArena;
212 pArena->prev = &pEntry->arena;
213 pEntry->arena.next = pArena;
217 /***********************************************************************
220 * Find the sub-heap containing a given address.
222 static SUBHEAP *HEAP_FindSubHeap( HEAP *heap, LPCVOID ptr )
224 SUBHEAP *sub = &heap->subheap;
227 if (((char *)ptr >= (char *)sub) &&
228 ((char *)ptr < (char *)sub + sub->size)) return sub;
235 /***********************************************************************
238 * Make sure the heap storage is committed up to (not including) ptr.
240 static BOOL32 HEAP_Commit( SUBHEAP *subheap, void *ptr )
242 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
243 size = (size + 0xfff) & 0xfffff000; /* Align size on a page boundary */
244 if (size > subheap->size) size = subheap->size;
245 if (size <= subheap->commitSize) return TRUE;
246 if (!VirtualAlloc( (char *)subheap + subheap->commitSize,
247 size - subheap->commitSize, MEM_COMMIT,
248 PAGE_EXECUTE_READWRITE))
250 fprintf( stderr, "HEAP_Commit: could not commit %08lx bytes at %08lx for heap %08lx\n",
251 size - subheap->commitSize,
252 (DWORD)((char *)subheap + subheap->commitSize),
253 (DWORD)subheap->heap );
256 subheap->commitSize = size;
261 /***********************************************************************
264 * If possible, decommit the heap storage from (including) 'ptr'.
266 static BOOL32 HEAP_Decommit( SUBHEAP *subheap, void *ptr )
268 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
269 size = (size + 0xfff) & 0xfffff000; /* Align size on a page boundary */
270 if (size >= subheap->commitSize) return TRUE;
271 if (!VirtualFree( (char *)subheap + subheap->commitSize,
272 size - subheap->commitSize, MEM_DECOMMIT ))
274 fprintf( stderr, "HEAP_Decommit: could not decommit %08lx bytes at %08lx for heap %08lx\n",
275 size - subheap->commitSize,
276 (DWORD)((char *)subheap + subheap->commitSize),
277 (DWORD)subheap->heap );
280 subheap->commitSize = size;
285 /***********************************************************************
286 * HEAP_CreateFreeBlock
288 * Create a free block at a specified address. 'size' is the size of the
289 * whole block, including the new arena.
291 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
295 /* Create a free arena */
297 pFree = (ARENA_FREE *)ptr;
298 pFree->threadId = GetCurrentTask();
299 pFree->magic = ARENA_FREE_MAGIC;
301 /* If debugging, erase the freed block content */
305 char *pEnd = (char *)ptr + size;
306 if (pEnd > (char *)subheap + subheap->commitSize)
307 pEnd = (char *)subheap + subheap->commitSize;
308 if (pEnd > (char *)(pFree + 1))
309 memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
312 /* Check if next block is free also */
314 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
315 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
317 /* Remove the next arena from the free list */
318 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
319 pNext->next->prev = pNext->prev;
320 pNext->prev->next = pNext->next;
321 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
323 memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
326 /* Set the next block PREV_FREE flag and pointer */
328 if ((char *)ptr + size < (char *)subheap + subheap->size)
330 DWORD *pNext = (DWORD *)((char *)ptr + size);
331 *pNext |= ARENA_FLAG_PREV_FREE;
332 *(ARENA_FREE **)(pNext - 1) = pFree;
335 /* Last, insert the new block into the free list */
337 pFree->size = size - sizeof(*pFree);
338 HEAP_InsertFreeBlock( subheap->heap, pFree );
342 /***********************************************************************
343 * HEAP_MakeInUseBlockFree
345 * Turn an in-use block into a free block. Can also decommit the end of
346 * the heap, and possibly even free the sub-heap altogether.
348 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
351 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
353 /* Check if we can merge with previous block */
355 if (pArena->size & ARENA_FLAG_PREV_FREE)
357 pFree = *((ARENA_FREE **)pArena - 1);
358 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
359 /* Remove it from the free list */
360 pFree->next->prev = pFree->prev;
361 pFree->prev->next = pFree->next;
363 else pFree = (ARENA_FREE *)pArena;
365 /* Create a free block */
367 HEAP_CreateFreeBlock( subheap, pFree, size );
368 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
369 if ((char *)pFree + size < (char *)subheap + subheap->size)
370 return; /* Not the last block, so nothing more to do */
372 /* Free the whole sub-heap if it's empty and not the original one */
374 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
375 (subheap != &subheap->heap->subheap))
377 SUBHEAP *pPrev = &subheap->heap->subheap;
378 /* Remove the free block from the list */
379 pFree->next->prev = pFree->prev;
380 pFree->prev->next = pFree->next;
381 /* Remove the subheap from the list */
382 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
383 if (pPrev) pPrev->next = subheap->next;
384 /* Free the memory */
386 if (subheap->selector) FreeSelector( subheap->selector );
387 VirtualFree( subheap, subheap->size, MEM_DECOMMIT );
388 VirtualFree( subheap, 0, MEM_RELEASE );
392 /* Decommit the end of the heap */
394 HEAP_Decommit( subheap, pFree + 1 );
398 /***********************************************************************
401 * Shrink an in-use block.
403 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
405 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
407 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
408 (pArena->size & ARENA_SIZE_MASK) - size );
409 pArena->size = (pArena->size & ~ARENA_SIZE_MASK) | size;
413 /* Turn off PREV_FREE flag in next block */
414 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
415 if (pNext < (char *)subheap + subheap->size)
416 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
421 /***********************************************************************
424 * Create a sub-heap of the given size.
426 static SUBHEAP *HEAP_CreateSubHeap( DWORD flags, DWORD commitSize,
432 /* Round-up sizes on a 64K boundary */
434 if (flags & HEAP_WINE_SEGPTR)
436 totalSize = commitSize = 0x10000; /* Only 64K at a time for SEGPTRs */
440 totalSize = (totalSize + 0xffff) & 0xffff0000;
441 commitSize = (commitSize + 0xffff) & 0xffff0000;
442 if (!commitSize) commitSize = 0x10000;
443 if (totalSize < commitSize) totalSize = commitSize;
446 /* Allocate the memory block */
448 if (!(subheap = VirtualAlloc( NULL, totalSize,
449 MEM_RESERVE, PAGE_EXECUTE_READWRITE )))
451 fprintf( stderr, "HEAP_CreateSubHeap: could not VirtualAlloc %08lx bytes\n",
455 if (!VirtualAlloc(subheap, commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
457 fprintf( stderr, "HEAP_CreateSubHeap: could not commit %08lx bytes for sub-heap %08lx\n",
458 commitSize, (DWORD)subheap );
459 VirtualFree( subheap, 0, MEM_RELEASE );
463 /* Allocate a selector if needed */
465 if (flags & HEAP_WINE_SEGPTR)
467 selector = SELECTOR_AllocBlock( subheap, totalSize,
468 (flags & HEAP_WINE_CODESEG) ? SEGMENT_CODE : SEGMENT_DATA,
469 (flags & HEAP_WINE_CODESEG) != 0, FALSE );
472 fprintf( stderr, "HEAP_CreateSubHeap: could not allocate selector\n" );
473 VirtualFree( subheap, 0, MEM_RELEASE );
478 /* Fill the sub-heap structure */
480 subheap->size = totalSize;
481 subheap->commitSize = commitSize;
482 subheap->headerSize = sizeof(*subheap);
483 subheap->next = NULL;
484 subheap->heap = NULL;
485 subheap->magic = SUBHEAP_MAGIC;
486 subheap->selector = selector;
491 /***********************************************************************
494 * Find a free block at least as large as the requested size, and make sure
495 * the requested size is committed.
497 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
498 SUBHEAP **ppSubHeap )
502 FREE_LIST_ENTRY *pEntry = heap->freeList;
504 /* Find a suitable free list, and in it find a block large enough */
506 while (pEntry->size < size) pEntry++;
507 pArena = pEntry->arena.next;
508 while (pArena != &heap->freeList[0].arena)
510 if (pArena->size > size)
512 subheap = HEAP_FindSubHeap( heap, pArena );
513 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
514 + size + HEAP_MIN_BLOCK_SIZE))
516 *ppSubHeap = subheap;
520 pArena = pArena->next;
523 /* If no block was found, attempt to grow the heap */
525 if (!(heap->flags & HEAP_GROWABLE))
527 fprintf( stderr, "HEAP_FindFreeBlock: Not enough space in heap %08lx for %08lx bytes\n",
531 size += sizeof(SUBHEAP) + sizeof(ARENA_FREE);
532 if (!(subheap = HEAP_CreateSubHeap( heap->flags, size,
533 MAX( HEAP_DEF_SIZE, size ) )))
536 /* Insert the new sub-heap in the list */
538 subheap->heap = heap;
539 subheap->next = heap->subheap.next;
540 heap->subheap.next = subheap;
541 size = subheap->size;
542 dprintf_heap( stddeb, "HEAP_FindFreeBlock: created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
543 (DWORD)subheap, size, (DWORD)heap );
545 HEAP_CreateFreeBlock( subheap, subheap + 1, size - sizeof(*subheap) );
546 *ppSubHeap = subheap;
547 return (ARENA_FREE *)(subheap + 1);
551 /***********************************************************************
552 * HEAP_IsValidArenaPtr
554 * Check that the pointer is inside the range possible for arenas.
556 static BOOL32 HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
559 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
560 if (!subheap) return FALSE;
561 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
562 if (subheap != &heap->subheap) return FALSE;
563 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
564 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
569 /***********************************************************************
570 * HEAP_ValidateFreeArena
572 static BOOL32 HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
574 char *heapEnd = (char *)subheap + subheap->size;
576 /* Check magic number */
577 if (pArena->magic != ARENA_FREE_MAGIC)
579 fprintf( stderr, "Heap %08lx: invalid free arena magic for %08lx\n",
580 (DWORD)subheap->heap, (DWORD)pArena );
583 /* Check size flags */
584 if (!(pArena->size & ARENA_FLAG_FREE) ||
585 (pArena->size & ARENA_FLAG_PREV_FREE))
587 fprintf( stderr, "Heap %08lx: bad flags %lx for free arena %08lx\n",
588 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
590 /* Check arena size */
591 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
593 fprintf( stderr, "Heap %08lx: bad size %08lx for free arena %08lx\n",
594 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
597 /* Check that next pointer is valid */
598 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
600 fprintf( stderr, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
601 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
604 /* Check that next arena is free */
605 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
606 (pArena->next->magic != ARENA_FREE_MAGIC))
608 fprintf( stderr, "Heap %08lx: next arena %08lx invalid for %08lx\n",
609 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
612 /* Check that prev pointer is valid */
613 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
615 fprintf( stderr, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
616 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
619 /* Check that prev arena is free */
620 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
621 (pArena->prev->magic != ARENA_FREE_MAGIC))
623 fprintf( stderr, "Heap %08lx: prev arena %08lx invalid for %08lx\n",
624 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
627 /* Check that next block has PREV_FREE flag */
628 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
630 if (!(*(DWORD *)((char *)(pArena + 1) +
631 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
633 fprintf( stderr, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
634 (DWORD)subheap->heap, (DWORD)pArena );
637 /* Check next block back pointer */
638 if (*((ARENA_FREE **)((char *)(pArena + 1) +
639 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
641 fprintf( stderr, "Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
642 (DWORD)subheap->heap, (DWORD)pArena,
643 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
651 /***********************************************************************
652 * HEAP_ValidateInUseArena
654 static BOOL32 HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena )
656 char *heapEnd = (char *)subheap + subheap->size;
658 /* Check magic number */
659 if (pArena->magic != ARENA_INUSE_MAGIC)
661 fprintf( stderr, "Heap %08lx: invalid in-use arena magic for %08lx\n",
662 (DWORD)subheap->heap, (DWORD)pArena );
665 /* Check size flags */
666 if (pArena->size & ARENA_FLAG_FREE)
668 fprintf( stderr, "Heap %08lx: bad flags %lx for in-use arena %08lx\n",
669 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
671 /* Check arena size */
672 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
674 fprintf( stderr, "Heap %08lx: bad size %08lx for in-use arena %08lx\n",
675 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
678 /* Check next arena PREV_FREE flag */
679 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
680 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
682 fprintf( stderr, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
683 (DWORD)subheap->heap, (DWORD)pArena );
686 /* Check prev free arena */
687 if (pArena->size & ARENA_FLAG_PREV_FREE)
689 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
690 /* Check prev pointer */
691 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
693 fprintf(stderr, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
694 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
697 /* Check that prev arena is free */
698 if (!(pPrev->size & ARENA_FLAG_FREE) ||
699 (pPrev->magic != ARENA_FREE_MAGIC))
701 fprintf( stderr, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
702 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
705 /* Check that prev arena is really the previous block */
706 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
708 fprintf( stderr, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
709 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
717 /***********************************************************************
720 * Check whether the pointer is to a block inside a given heap.
722 int HEAP_IsInsideHeap( HANDLE32 heap, DWORD flags, LPCVOID ptr )
724 HEAP *heapPtr = HEAP_GetPtr( heap );
728 /* Validate the parameters */
730 if (!heapPtr) return 0;
731 flags |= heapPtr->flags;
732 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
733 ret = (((subheap = HEAP_FindSubHeap( heapPtr, ptr )) != NULL) &&
734 (((char *)ptr >= (char *)subheap + subheap->headerSize
735 + sizeof(ARENA_INUSE))));
736 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
741 /***********************************************************************
744 * Transform a linear pointer into a SEGPTR. The pointer must have been
745 * allocated from a HEAP_WINE_SEGPTR heap.
747 SEGPTR HEAP_GetSegptr( HANDLE32 heap, DWORD flags, LPCVOID ptr )
749 HEAP *heapPtr = HEAP_GetPtr( heap );
753 /* Validate the parameters */
755 if (!heapPtr) return 0;
756 flags |= heapPtr->flags;
757 if (!(flags & HEAP_WINE_SEGPTR))
759 fprintf( stderr, "HEAP_GetSegptr: heap %08x is not a SEGPTR heap\n",
763 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
765 /* Get the subheap */
767 if (!(subheap = HEAP_FindSubHeap( heapPtr, ptr )))
769 fprintf( stderr, "HEAP_GetSegptr: %p is not inside heap %08x\n",
771 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
775 /* Build the SEGPTR */
777 ret = PTR_SEG_OFF_TO_SEGPTR(subheap->selector, (DWORD)ptr-(DWORD)subheap);
778 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
783 /***********************************************************************
784 * HeapCreate (KERNEL32.336)
786 HANDLE32 HeapCreate( DWORD flags, DWORD initialSize, DWORD maxSize )
791 FREE_LIST_ENTRY *pEntry;
793 /* Allocate the heap block */
797 maxSize = HEAP_DEF_SIZE;
798 flags |= HEAP_GROWABLE;
800 if (!(subheap = HEAP_CreateSubHeap( flags, initialSize, maxSize )))
802 SetLastError( ERROR_OUTOFMEMORY );
806 /* Fill the heap structure */
808 heap = (HEAP *)subheap;
809 subheap->heap = heap;
810 subheap->headerSize = sizeof(HEAP);
813 heap->magic = HEAP_MAGIC;
814 InitializeCriticalSection( &heap->critSection );
816 /* Build the free lists */
818 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
820 pEntry->size = HEAP_freeListSizes[i];
821 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
822 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
823 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
824 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
825 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
826 pEntry->arena.threadId = 0;
827 pEntry->arena.magic = ARENA_FREE_MAGIC;
830 /* Create the first free block */
832 HEAP_CreateFreeBlock( subheap, heap + 1, subheap->size - sizeof(*heap) );
837 return (HANDLE32)heap;
841 /***********************************************************************
842 * HeapDestroy (KERNEL32.337)
844 BOOL32 HeapDestroy( HANDLE32 heap )
846 HEAP *heapPtr = HEAP_GetPtr( heap );
849 dprintf_heap( stddeb, "HeapDestroy: %08x\n", heap );
850 if (!heapPtr) return FALSE;
852 DeleteCriticalSection( &heapPtr->critSection );
853 subheap = &heapPtr->subheap;
856 SUBHEAP *next = subheap->next;
857 if (subheap->selector) FreeSelector( subheap->selector );
858 VirtualFree( subheap, subheap->commitSize, MEM_DECOMMIT );
859 VirtualFree( subheap, 0, MEM_RELEASE );
866 /***********************************************************************
867 * HeapAlloc (KERNEL32.334)
869 LPVOID HeapAlloc( HANDLE32 heap, DWORD flags, DWORD size )
874 HEAP *heapPtr = HEAP_GetPtr( heap );
876 /* Validate the parameters */
878 if (!heapPtr) return NULL;
879 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
880 flags |= heapPtr->flags;
881 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
882 size = (size + 3) & ~3;
883 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
885 /* Locate a suitable free block */
887 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
889 dprintf_heap( stddeb, "HeapAlloc(%08x,%08lx,%08lx): returning NULL\n",
891 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
892 SetLastError( ERROR_OUTOFMEMORY );
896 /* Remove the arena from the free list */
898 pArena->next->prev = pArena->prev;
899 pArena->prev->next = pArena->next;
901 /* Build the in-use arena */
903 pInUse = (ARENA_INUSE *)pArena;
904 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
905 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
906 pInUse->callerEIP = *((DWORD *)&heap - 1); /* hack hack */
907 pInUse->threadId = GetCurrentTask();
908 pInUse->magic = ARENA_INUSE_MAGIC;
910 /* Shrink the block */
912 HEAP_ShrinkBlock( subheap, pInUse, size );
914 if (flags & HEAP_ZERO_MEMORY) memset( pInUse + 1, 0, size );
915 else if (debugging_heap) memset( pInUse + 1, ARENA_INUSE_FILLER, size );
917 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
920 dprintf_heap( stddeb, "HeapAlloc(%08x,%08lx,%08lx): returning %08lx\n",
921 heap, flags, size, (DWORD)(pInUse + 1) );
922 return (LPVOID)(pInUse + 1);
926 /***********************************************************************
927 * HeapFree (KERNEL32.338)
929 BOOL32 HeapFree( HANDLE32 heap, DWORD flags, LPVOID ptr )
933 HEAP *heapPtr = HEAP_GetPtr( heap );
935 /* Validate the parameters */
937 if (!heapPtr) return FALSE;
938 flags &= HEAP_NO_SERIALIZE;
939 flags |= heapPtr->flags;
940 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
941 if (!ptr || !HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
943 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
944 SetLastError( ERROR_INVALID_PARAMETER );
945 dprintf_heap( stddeb, "HeapFree(%08x,%08lx,%08lx): returning FALSE\n",
946 heap, flags, (DWORD)ptr );
950 /* Turn the block into a free block */
952 pInUse = (ARENA_INUSE *)ptr - 1;
953 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
954 HEAP_MakeInUseBlockFree( subheap, pInUse );
956 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
959 dprintf_heap( stddeb, "HeapFree(%08x,%08lx,%08lx): returning TRUE\n",
960 heap, flags, (DWORD)ptr );
965 /***********************************************************************
966 * HeapReAlloc (KERNEL32.340)
968 LPVOID HeapReAlloc( HANDLE32 heap, DWORD flags, LPVOID ptr, DWORD size )
975 if (!ptr) return HeapAlloc( heap, flags, size ); /* FIXME: correct? */
976 if (!(heapPtr = HEAP_GetPtr( heap ))) return FALSE;
978 /* Validate the parameters */
980 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
981 HEAP_REALLOC_IN_PLACE_ONLY;
982 flags |= heapPtr->flags;
983 size = (size + 3) & ~3;
984 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
986 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
987 if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
989 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
990 SetLastError( ERROR_INVALID_PARAMETER );
991 dprintf_heap( stddeb, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning NULL\n",
992 heap, flags, (DWORD)ptr, size );
996 /* Check if we need to grow the block */
998 pArena = (ARENA_INUSE *)ptr - 1;
999 pArena->threadId = GetCurrentTask();
1000 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1001 oldSize = (pArena->size & ARENA_SIZE_MASK);
1004 char *pNext = (char *)(pArena + 1) + oldSize;
1005 if ((pNext < (char *)subheap + subheap->size) &&
1006 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1007 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1009 /* The next block is free and large enough */
1010 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1011 pFree->next->prev = pFree->prev;
1012 pFree->prev->next = pFree->next;
1013 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1014 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1015 + size + HEAP_MIN_BLOCK_SIZE))
1017 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1018 SetLastError( ERROR_OUTOFMEMORY );
1021 HEAP_ShrinkBlock( subheap, pArena, size );
1023 else /* Do it the hard way */
1026 ARENA_INUSE *pInUse;
1027 SUBHEAP *newsubheap;
1029 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1030 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1032 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1033 SetLastError( ERROR_OUTOFMEMORY );
1037 /* Build the in-use arena */
1039 pNew->next->prev = pNew->prev;
1040 pNew->prev->next = pNew->next;
1041 pInUse = (ARENA_INUSE *)pNew;
1042 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1043 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1044 pInUse->threadId = GetCurrentTask();
1045 pInUse->magic = ARENA_INUSE_MAGIC;
1046 HEAP_ShrinkBlock( newsubheap, pInUse, size );
1047 memcpy( pInUse + 1, pArena + 1, oldSize );
1049 /* Free the previous block */
1051 HEAP_MakeInUseBlockFree( subheap, pArena );
1052 subheap = newsubheap;
1056 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1058 /* Clear the extra bytes if needed */
1062 if (flags & HEAP_ZERO_MEMORY)
1063 memset( (char *)(pArena + 1) + oldSize, 0,
1064 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1065 else if (debugging_heap)
1066 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1067 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1070 /* Return the new arena */
1072 pArena->callerEIP = *((DWORD *)&heap - 1); /* hack hack */
1073 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1075 dprintf_heap( stddeb, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1076 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1077 return (LPVOID)(pArena + 1);
1081 /***********************************************************************
1082 * HeapCompact (KERNEL32.335)
1084 DWORD HeapCompact( HANDLE32 heap, DWORD flags )
1090 /***********************************************************************
1091 * HeapLock (KERNEL32.339)
1093 BOOL32 HeapLock( HANDLE32 heap )
1095 HEAP *heapPtr = HEAP_GetPtr( heap );
1097 if (!heapPtr) return FALSE;
1098 EnterCriticalSection( &heapPtr->critSection );
1103 /***********************************************************************
1104 * HeapUnlock (KERNEL32.342)
1106 BOOL32 HeapUnlock( HANDLE32 heap )
1108 HEAP *heapPtr = HEAP_GetPtr( heap );
1110 if (!heapPtr) return FALSE;
1111 LeaveCriticalSection( &heapPtr->critSection );
1116 /***********************************************************************
1117 * HeapSize (KERNEL32.341)
1119 DWORD HeapSize( HANDLE32 heap, DWORD flags, LPVOID ptr )
1122 HEAP *heapPtr = HEAP_GetPtr( heap );
1124 if (!heapPtr) return FALSE;
1125 flags &= HEAP_NO_SERIALIZE;
1126 flags |= heapPtr->flags;
1127 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
1128 if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
1130 SetLastError( ERROR_INVALID_PARAMETER );
1135 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1136 ret = pArena->size & ARENA_SIZE_MASK;
1138 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1140 dprintf_heap( stddeb, "HeapSize(%08x,%08lx,%08lx): returning %08lx\n",
1141 heap, flags, (DWORD)ptr, ret );
1146 /***********************************************************************
1147 * HeapValidate (KERNEL32.343)
1149 BOOL32 HeapValidate( HANDLE32 heap, DWORD flags, LPVOID block )
1152 HEAP *heapPtr = (HEAP *)heap;
1154 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
1156 fprintf( stderr, "Invalid heap %08x!\n", heap );
1162 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
1163 ((char *)block < (char *)subheap + subheap->headerSize
1164 + sizeof(ARENA_INUSE)))
1166 fprintf( stderr, "Heap %08lx: block %08lx is not inside heap\n",
1167 (DWORD)heap, (DWORD)block );
1170 return HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1 );
1173 subheap = &heapPtr->subheap;
1176 char *ptr = (char *)subheap + subheap->headerSize;
1177 while (ptr < (char *)subheap + subheap->size)
1179 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1181 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr ))
1183 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1187 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr ))
1189 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1192 subheap = subheap->next;
1198 /***********************************************************************
1199 * HeapWalk (KERNEL32.344)
1201 BOOL32 HeapWalk( HANDLE32 heap, void *entry )
1203 fprintf( stderr, "HeapWalk(%08x): not implemented\n", heap );
1208 /***********************************************************************
1211 LPSTR HEAP_strdupA( HANDLE32 heap, DWORD flags, LPCSTR str )
1213 INT32 len = lstrlen32A(str) + 1;
1214 LPSTR p = HeapAlloc( heap, flags, len );
1215 if (p) strcpy( p, str );