Release 961222
[wine/multimedia.git] / memory / heap.c
blob6924de4e265faa30faeafef55049562fc9ecfde7
1 /*
2 * Win32 heap functions
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "debugger.h"
12 #include "selectors.h"
13 #include "winbase.h"
14 #include "winerror.h"
15 #include "winnt.h"
16 #include "stddebug.h"
17 #include "debug.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
22 * require it.
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 */
31 } ARENA_INUSE;
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 */
40 } ARENA_FREE;
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
59 typedef struct
61 DWORD size;
62 ARENA_FREE arena;
63 } FREE_LIST_ENTRY;
65 struct tagHEAP;
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 */
76 } SUBHEAP;
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 */
88 } HEAP;
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 /***********************************************************************
97 * HEAP_Dump
99 void HEAP_Dump( HEAP *heap )
101 int i;
102 SUBHEAP *subheap;
103 char *ptr;
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;
124 while (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),
151 pArena->callerEIP );
152 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
153 arenaSize += sizeof(ARENA_INUSE);
154 usedSize += pArena->size & ARENA_SIZE_MASK;
156 else
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 /***********************************************************************
176 * HEAP_GetPtr
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 );
185 return NULL;
187 if (debugging_heap && !HeapValidate( heap, 0, NULL ))
189 HEAP_Dump( heapPtr );
190 DEBUG_EnterDebugger();
191 SetLastError( ERROR_INVALID_HANDLE );
192 return NULL;
194 return heapPtr;
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 /***********************************************************************
216 * HEAP_FindSubHeap
218 * Find the sub-heap containing a given address.
220 static SUBHEAP *HEAP_FindSubHeap( HEAP *heap, LPCVOID ptr )
222 SUBHEAP *sub = &heap->subheap;
223 while (sub)
225 if (((char *)ptr >= (char *)sub) &&
226 ((char *)ptr < (char *)sub + sub->size)) return sub;
227 sub = sub->next;
229 return NULL;
233 /***********************************************************************
234 * HEAP_Commit
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 );
252 return FALSE;
254 subheap->commitSize = size;
255 return TRUE;
259 /***********************************************************************
260 * HEAP_Decommit
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 );
276 return FALSE;
278 subheap->commitSize = size;
279 return TRUE;
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 )
291 ARENA_FREE *pFree;
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 */
301 if (debugging_heap)
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);
320 if (debugging_heap)
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 )
348 ARENA_FREE *pFree;
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 */
383 subheap->magic = 0;
384 if (subheap->selector) FreeSelector( subheap->selector );
385 VirtualFree( subheap, subheap->size, MEM_DECOMMIT );
386 VirtualFree( subheap, 0, MEM_RELEASE );
387 return;
390 /* Decommit the end of the heap */
392 HEAP_Decommit( subheap, pFree + 1 );
396 /***********************************************************************
397 * HEAP_ShrinkBlock
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;
409 else
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 /***********************************************************************
420 * HEAP_CreateSubHeap
422 * Create a sub-heap of the given size.
424 static SUBHEAP *HEAP_CreateSubHeap( DWORD flags, DWORD commitSize,
425 DWORD totalSize )
427 SUBHEAP *subheap;
428 WORD selector = 0;
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 */
436 else
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",
450 totalSize );
451 return NULL;
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 );
458 return NULL;
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 );
468 if (!selector)
470 fprintf( stderr, "HEAP_CreateSubHeap: could not allocate selector\n" );
471 VirtualFree( subheap, 0, MEM_RELEASE );
472 return NULL;
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;
485 return subheap;
489 /***********************************************************************
490 * HEAP_FindFreeBlock
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 )
498 SUBHEAP *subheap;
499 ARENA_FREE *pArena;
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))
513 return NULL;
514 *ppSubHeap = subheap;
515 return pArena;
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",
526 (DWORD)heap, size );
527 return NULL;
529 size += sizeof(SUBHEAP) + sizeof(ARENA_FREE);
530 if (!(subheap = HEAP_CreateSubHeap( heap->flags, size,
531 MAX( HEAP_DEF_SIZE, size ) )))
532 return NULL;
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 )
556 int i;
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;
563 return FALSE;
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 );
579 return FALSE;
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 );
593 return FALSE;
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 );
600 return FALSE;
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 );
608 return FALSE;
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 );
615 return FALSE;
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 );
623 return FALSE;
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 );
633 return FALSE;
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));
642 return FALSE;
645 return TRUE;
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 );
661 return FALSE;
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 );
674 return FALSE;
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 );
682 return FALSE;
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 );
693 return FALSE;
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 );
701 return FALSE;
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 );
708 return FALSE;
711 return TRUE;
715 /***********************************************************************
716 * HEAP_IsInsideHeap
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 );
723 SUBHEAP *subheap;
724 int ret;
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 );
735 return ret;
739 /***********************************************************************
740 * HEAP_GetSegptr
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 );
748 SUBHEAP *subheap;
749 SEGPTR ret;
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",
758 heap );
759 return 0;
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",
768 ptr, heap );
769 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
770 return 0;
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 );
777 return ret;
781 /***********************************************************************
782 * HeapCreate (KERNEL32.336)
784 HANDLE32 HeapCreate( DWORD flags, DWORD initialSize, DWORD maxSize )
786 int i;
787 HEAP *heap;
788 SUBHEAP *subheap;
789 FREE_LIST_ENTRY *pEntry;
791 /* Allocate the heap block */
793 if (!maxSize)
795 maxSize = HEAP_DEF_SIZE;
796 flags |= HEAP_GROWABLE;
798 if (!(subheap = HEAP_CreateSubHeap( flags, initialSize, maxSize )))
800 SetLastError( ERROR_OUTOFMEMORY );
801 return 0;
804 /* Fill the heap structure */
806 heap = (HEAP *)subheap;
807 subheap->heap = heap;
808 subheap->headerSize = sizeof(HEAP);
809 heap->next = NULL;
810 heap->flags = flags;
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) );
832 /* We are done */
834 SetLastError( 0 );
835 return (HANDLE32)heap;
839 /***********************************************************************
840 * HeapDestroy (KERNEL32.337)
842 BOOL32 HeapDestroy( HANDLE32 heap )
844 HEAP *heapPtr = HEAP_GetPtr( heap );
845 SUBHEAP *subheap;
847 dprintf_heap( stddeb, "HeapDestroy: %08x\n", heap );
848 if (!heapPtr) return FALSE;
850 DeleteCriticalSection( &heapPtr->critSection );
851 subheap = &heapPtr->subheap;
852 while (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 );
858 subheap = next;
860 return TRUE;
864 /***********************************************************************
865 * HeapAlloc (KERNEL32.334)
867 LPVOID HeapAlloc( HANDLE32 heap, DWORD flags, DWORD size )
869 ARENA_FREE *pArena;
870 ARENA_INUSE *pInUse;
871 SUBHEAP *subheap;
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",
888 heap, flags, size );
889 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
890 SetLastError( ERROR_OUTOFMEMORY );
891 return NULL;
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 );
916 SetLastError( 0 );
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 )
929 ARENA_INUSE *pInUse;
930 SUBHEAP *subheap;
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 );
945 return FALSE;
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 );
959 return TRUE;
963 /***********************************************************************
964 * HeapReAlloc (KERNEL32.340)
966 LPVOID HeapReAlloc( HANDLE32 heap, DWORD flags, LPVOID ptr, DWORD size )
968 ARENA_INUSE *pArena;
969 DWORD oldSize;
970 HEAP *heapPtr;
971 SUBHEAP *subheap;
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 );
991 return NULL;
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);
1000 if (size > oldSize)
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 );
1017 return NULL;
1019 HEAP_ShrinkBlock( subheap, pArena, size );
1021 else /* Do it the hard way */
1023 ARENA_FREE *pNew;
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 );
1032 return NULL;
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;
1051 pArena = pInUse;
1054 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
1056 /* Clear the extra bytes if needed */
1058 if (size > oldSize)
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 )
1084 return 0;
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 );
1097 return TRUE;
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 );
1110 return TRUE;
1114 /***********************************************************************
1115 * HeapSize (KERNEL32.341)
1117 DWORD HeapSize( HANDLE32 heap, DWORD flags, LPVOID ptr )
1119 DWORD ret;
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 );
1129 ret = 0xffffffff;
1131 else
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 );
1140 return ret;
1144 /***********************************************************************
1145 * HeapValidate (KERNEL32.343)
1147 BOOL32 HeapValidate( HANDLE32 heap, DWORD flags, LPVOID block )
1149 SUBHEAP *subheap;
1150 HEAP *heapPtr = (HEAP *)heap;
1152 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
1154 fprintf( stderr, "Invalid heap %08x!\n", heap );
1155 return FALSE;
1158 if (block)
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 );
1166 return FALSE;
1168 return HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1 );
1171 subheap = &heapPtr->subheap;
1172 while (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 ))
1180 return FALSE;
1181 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1183 else
1185 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr ))
1186 return FALSE;
1187 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1190 subheap = subheap->next;
1192 return TRUE;
1196 /***********************************************************************
1197 * HeapWalk (KERNEL32.344)
1199 BOOL32 HeapWalk( HANDLE32 heap, void *entry )
1201 fprintf( stderr, "HeapWalk(%08x): not implemented\n", heap );
1202 return FALSE;
1206 /***********************************************************************
1207 * HEAP_strdupA
1209 LPSTR HEAP_strdupA( HANDLE32 heap, DWORD flags, LPCSTR str )
1211 INT32 len = lstrlen32A(str) + 1;
1212 LPSTR p = HeapAlloc( heap, flags, len );
1213 lstrcpy32A( p, str );
1214 return p;
1218 /***********************************************************************
1219 * HEAP_strdupW
1221 LPWSTR HEAP_strdupW( HANDLE32 heap, DWORD flags, LPCWSTR str )
1223 INT32 len = lstrlen32W(str) + 1;
1224 LPWSTR p = HeapAlloc( heap, flags, len * sizeof(WCHAR) );
1225 lstrcpy32W( p, str );
1226 return p;
1230 /***********************************************************************
1231 * HEAP_strdupAtoW
1233 LPWSTR HEAP_strdupAtoW( HANDLE32 heap, DWORD flags, LPCSTR str )
1235 LPWSTR ret;
1237 if (!str) return NULL;
1238 if (!(ret = HeapAlloc( heap, flags, (lstrlen32A(str)+1) * sizeof(WCHAR) )))
1240 fprintf( stderr, "Virtual memory exhausted.\n" );
1241 exit(1);
1243 lstrcpyAtoW( ret, str );
1244 return ret;
1248 /***********************************************************************
1249 * HEAP_strdupWtoA
1251 LPSTR HEAP_strdupWtoA( HANDLE32 heap, DWORD flags, LPCWSTR str )
1253 LPSTR ret;
1255 if (!str) return NULL;
1256 if (!(ret = HeapAlloc( heap, flags, lstrlen32W(str) + 1 )))
1258 fprintf( stderr, "Virtual memory exhausted.\n" );
1259 exit(1);
1261 lstrcpyWtoA( ret, str );
1262 return ret;