Release 970914
[wine] / memory / heap.c
1 /*
2  * Win32 heap functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "windows.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"
18
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.
23  */
24
25 typedef struct tagARENA_INUSE
26 {
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;
32
33 typedef struct tagARENA_FREE
34 {
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;
41
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 */
47
48 #define ARENA_INUSE_FILLER     0x55
49 #define ARENA_FREE_FILLER      0xaa
50
51 #define HEAP_NB_FREE_LISTS   4   /* Number of free lists */
52
53 /* Max size of the blocks on the free lists */
54 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
55 {
56     0x20, 0x80, 0x200, 0xffffffff
57 };
58
59 typedef struct
60 {
61     DWORD       size;
62     ARENA_FREE  arena;
63 } FREE_LIST_ENTRY;
64
65 struct tagHEAP;
66
67 typedef struct tagSUBHEAP
68 {
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;
77
78 #define SUBHEAP_MAGIC    ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
79
80 typedef struct tagHEAP
81 {
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;
89
90 #define HEAP_MAGIC       ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
91
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 */
94
95
96 /***********************************************************************
97  *           HEAP_Dump
98  */
99 void HEAP_Dump( HEAP *heap )
100 {
101     int i;
102     SUBHEAP *subheap;
103     char *ptr;
104
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)
110     {
111         printf( " -> %08lx", (DWORD)subheap->next );
112         subheap = subheap->next;
113     }
114
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 );
122
123     subheap = &heap->subheap;
124     while (subheap)
125     {
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 );
129
130         printf( "\n Block   Stat   Size    Id\n" );
131         ptr = (char*)subheap + subheap->headerSize;
132         while (ptr < (char *)subheap + subheap->size)
133         {
134             if (*(DWORD *)ptr & ARENA_FLAG_FREE)
135             {
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;
144             }
145             else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
146             {
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;
155             }
156             else
157             {
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;
165             }
166         }
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;
171     }
172 }
173
174
175 /***********************************************************************
176  *           HEAP_GetPtr
177  */
178 static HEAP *HEAP_GetPtr( HANDLE32 heap )
179 {
180     HEAP *heapPtr = (HEAP *)heap;
181     if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
182     {
183         fprintf( stderr, "Invalid heap %08x!\n", heap );
184         SetLastError( ERROR_INVALID_HANDLE );
185         return NULL;
186     }
187     if (debugging_heap && !HeapValidate( heap, 0, NULL ))
188     {
189         HEAP_Dump( heapPtr );
190         assert( FALSE );
191         SetLastError( ERROR_INVALID_HANDLE );
192         return NULL;
193     }
194     return heapPtr;
195 }
196
197
198 /***********************************************************************
199  *           HEAP_InsertFreeBlock
200  *
201  * Insert a free block into the free list.
202  */
203 static void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena )
204 {
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;
212 }
213
214
215 /***********************************************************************
216  *           HEAP_FindSubHeap
217  *
218  * Find the sub-heap containing a given address.
219  */
220 static SUBHEAP *HEAP_FindSubHeap( HEAP *heap, LPCVOID ptr )
221 {
222     SUBHEAP *sub = &heap->subheap;
223     while (sub)
224     {
225         if (((char *)ptr >= (char *)sub) &&
226             ((char *)ptr < (char *)sub + sub->size)) return sub;
227         sub = sub->next;
228     }
229     return NULL;
230 }
231
232
233 /***********************************************************************
234  *           HEAP_Commit
235  *
236  * Make sure the heap storage is committed up to (not including) ptr.
237  */
238 static BOOL32 HEAP_Commit( SUBHEAP *subheap, void *ptr )
239 {
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))
247     {
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;
253     }
254     subheap->commitSize = size;
255     return TRUE;
256 }
257
258
259 /***********************************************************************
260  *           HEAP_Decommit
261  *
262  * If possible, decommit the heap storage from (including) 'ptr'.
263  */
264 static BOOL32 HEAP_Decommit( SUBHEAP *subheap, void *ptr )
265 {
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 ))
271     {
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 );
276         return FALSE;
277     }
278     subheap->commitSize = size;
279     return TRUE;
280 }
281
282
283 /***********************************************************************
284  *           HEAP_CreateFreeBlock
285  *
286  * Create a free block at a specified address. 'size' is the size of the
287  * whole block, including the new arena.
288  */
289 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
290 {
291     ARENA_FREE *pFree;
292
293     /* Create a free arena */
294
295     pFree = (ARENA_FREE *)ptr;
296     pFree->threadId = GetCurrentTask();
297     pFree->magic = ARENA_FREE_MAGIC;
298
299     /* If debugging, erase the freed block content */
300
301     if (debugging_heap)
302     {
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) );
308     }
309
310     /* Check if next block is free also */
311
312     if (((char *)ptr + size < (char *)subheap + subheap->size) &&
313         (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
314     {
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) );
322     }
323
324     /* Set the next block PREV_FREE flag and pointer */
325
326     if ((char *)ptr + size < (char *)subheap + subheap->size)
327     {
328         DWORD *pNext = (DWORD *)((char *)ptr + size);
329         *pNext |= ARENA_FLAG_PREV_FREE;
330         *(ARENA_FREE **)(pNext - 1) = pFree;
331     }
332
333     /* Last, insert the new block into the free list */
334
335     pFree->size = size - sizeof(*pFree);
336     HEAP_InsertFreeBlock( subheap->heap, pFree );
337 }
338
339
340 /***********************************************************************
341  *           HEAP_MakeInUseBlockFree
342  *
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.
345  */
346 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
347 {
348     ARENA_FREE *pFree;
349     DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
350
351     /* Check if we can merge with previous block */
352
353     if (pArena->size & ARENA_FLAG_PREV_FREE)
354     {
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;
360     }
361     else pFree = (ARENA_FREE *)pArena;
362
363     /* Create a free block */
364
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 */
369
370     /* Free the whole sub-heap if it's empty and not the original one */
371
372     if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
373         (subheap != &subheap->heap->subheap))
374     {
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, 0, MEM_RELEASE );
386         return;
387     }
388     
389     /* Decommit the end of the heap */
390
391     HEAP_Decommit( subheap, pFree + 1 );
392 }
393
394
395 /***********************************************************************
396  *           HEAP_ShrinkBlock
397  *
398  * Shrink an in-use block.
399  */
400 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
401 {
402     if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
403     {
404         HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
405                               (pArena->size & ARENA_SIZE_MASK) - size );
406         pArena->size = (pArena->size & ~ARENA_SIZE_MASK) | size;
407     }
408     else
409     {
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;
414     }
415 }
416
417
418 /***********************************************************************
419  *           HEAP_CreateSubHeap
420  *
421  * Create a sub-heap of the given size.
422  */
423 static SUBHEAP *HEAP_CreateSubHeap( DWORD flags, DWORD commitSize,
424                                     DWORD totalSize )
425 {
426     SUBHEAP *subheap;
427     WORD selector = 0;
428
429     /* Round-up sizes on a 64K boundary */
430
431     if (flags & HEAP_WINE_SEGPTR)
432     {
433         totalSize = commitSize = 0x10000;  /* Only 64K at a time for SEGPTRs */
434     }
435     else
436     {
437         totalSize  = (totalSize + 0xffff) & 0xffff0000;
438         commitSize = (commitSize + 0xffff) & 0xffff0000;
439         if (!commitSize) commitSize = 0x10000;
440         if (totalSize < commitSize) totalSize = commitSize;
441     }
442
443     /* Allocate the memory block */
444
445     if (!(subheap = VirtualAlloc( NULL, totalSize,
446                                   MEM_RESERVE, PAGE_EXECUTE_READWRITE )))
447     {
448         fprintf( stderr, "HEAP_CreateSubHeap: could not VirtualAlloc %08lx bytes\n",
449                  totalSize );
450         return NULL;
451     }
452     if (!VirtualAlloc(subheap, commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
453     {
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 );
457         return NULL;
458     }
459
460     /* Allocate a selector if needed */
461
462     if (flags & HEAP_WINE_SEGPTR)
463     {
464         selector = SELECTOR_AllocBlock( subheap, totalSize,
465                      (flags & HEAP_WINE_CODESEG) ? SEGMENT_CODE : SEGMENT_DATA,
466                      (flags & HEAP_WINE_CODESEG) != 0, FALSE );
467         if (!selector)
468         {
469             fprintf( stderr, "HEAP_CreateSubHeap: could not allocate selector\n" );
470             VirtualFree( subheap, 0, MEM_RELEASE );
471             return NULL;
472         }
473     }
474
475     /* Fill the sub-heap structure */
476
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;
484     return subheap;
485 }
486
487
488 /***********************************************************************
489  *           HEAP_FindFreeBlock
490  *
491  * Find a free block at least as large as the requested size, and make sure
492  * the requested size is committed.
493  */
494 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
495                                        SUBHEAP **ppSubHeap )
496 {
497     SUBHEAP *subheap;
498     ARENA_FREE *pArena;
499     FREE_LIST_ENTRY *pEntry = heap->freeList;
500
501     /* Find a suitable free list, and in it find a block large enough */
502
503     while (pEntry->size < size) pEntry++;
504     pArena = pEntry->arena.next;
505     while (pArena != &heap->freeList[0].arena)
506     {
507         if (pArena->size > size)
508         {
509             subheap = HEAP_FindSubHeap( heap, pArena );
510             if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
511                                                + size + HEAP_MIN_BLOCK_SIZE))
512                 return NULL;
513             *ppSubHeap = subheap;
514             return pArena;
515         }
516
517         pArena = pArena->next;
518     }
519
520     /* If no block was found, attempt to grow the heap */
521
522     if (!(heap->flags & HEAP_GROWABLE))
523     {
524         fprintf( stderr, "HEAP_FindFreeBlock: Not enough space in heap %08lx for %08lx bytes\n",
525                  (DWORD)heap, size );
526         return NULL;
527     }
528     size += sizeof(SUBHEAP) + sizeof(ARENA_FREE);
529     if (!(subheap = HEAP_CreateSubHeap( heap->flags, size,
530                                         MAX( HEAP_DEF_SIZE, size ) )))
531         return NULL;
532
533     /* Insert the new sub-heap in the list */
534
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 );
541
542     HEAP_CreateFreeBlock( subheap, subheap + 1, size - sizeof(*subheap) );
543     *ppSubHeap = subheap;
544     return (ARENA_FREE *)(subheap + 1);
545 }
546
547
548 /***********************************************************************
549  *           HEAP_IsValidArenaPtr
550  *
551  * Check that the pointer is inside the range possible for arenas.
552  */
553 static BOOL32 HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
554 {
555     int i;
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;
562     return FALSE;
563 }
564
565
566 /***********************************************************************
567  *           HEAP_ValidateFreeArena
568  */
569 static BOOL32 HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
570 {
571     char *heapEnd = (char *)subheap + subheap->size;
572
573     /* Check magic number */
574     if (pArena->magic != ARENA_FREE_MAGIC)
575     {
576         fprintf( stderr, "Heap %08lx: invalid free arena magic for %08lx\n",
577                  (DWORD)subheap->heap, (DWORD)pArena );
578         return FALSE;
579     }
580     /* Check size flags */
581     if (!(pArena->size & ARENA_FLAG_FREE) ||
582         (pArena->size & ARENA_FLAG_PREV_FREE))
583     {
584         fprintf( stderr, "Heap %08lx: bad flags %lx for free arena %08lx\n",
585                  (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
586     }
587     /* Check arena size */
588     if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
589     {
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 );
592         return FALSE;
593     }
594     /* Check that next pointer is valid */
595     if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
596     {
597         fprintf( stderr, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
598                  (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
599         return FALSE;
600     }
601     /* Check that next arena is free */
602     if (!(pArena->next->size & ARENA_FLAG_FREE) ||
603         (pArena->next->magic != ARENA_FREE_MAGIC))
604     { 
605         fprintf( stderr, "Heap %08lx: next arena %08lx invalid for %08lx\n", 
606                  (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
607         return FALSE;
608     }
609     /* Check that prev pointer is valid */
610     if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
611     {
612         fprintf( stderr, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
613                  (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
614         return FALSE;
615     }
616     /* Check that prev arena is free */
617     if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
618         (pArena->prev->magic != ARENA_FREE_MAGIC))
619     { 
620         fprintf( stderr, "Heap %08lx: prev arena %08lx invalid for %08lx\n", 
621                  (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
622         return FALSE;
623     }
624     /* Check that next block has PREV_FREE flag */
625     if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
626     {
627         if (!(*(DWORD *)((char *)(pArena + 1) +
628             (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
629         {
630             fprintf( stderr, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
631                      (DWORD)subheap->heap, (DWORD)pArena );
632             return FALSE;
633         }
634         /* Check next block back pointer */
635         if (*((ARENA_FREE **)((char *)(pArena + 1) +
636             (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
637         {
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));
641             return FALSE;
642         }
643     }
644     return TRUE;
645 }
646
647
648 /***********************************************************************
649  *           HEAP_ValidateInUseArena
650  */
651 static BOOL32 HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena )
652 {
653     char *heapEnd = (char *)subheap + subheap->size;
654
655     /* Check magic number */
656     if (pArena->magic != ARENA_INUSE_MAGIC)
657     {
658         fprintf( stderr, "Heap %08lx: invalid in-use arena magic for %08lx\n",
659                  (DWORD)subheap->heap, (DWORD)pArena );
660         return FALSE;
661     }
662     /* Check size flags */
663     if (pArena->size & ARENA_FLAG_FREE) 
664     {
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 );
667     }
668     /* Check arena size */
669     if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
670     {
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 );
673         return FALSE;
674     }
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))
678     {
679         fprintf( stderr, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
680                  (DWORD)subheap->heap, (DWORD)pArena );
681         return FALSE;
682     }
683     /* Check prev free arena */
684     if (pArena->size & ARENA_FLAG_PREV_FREE)
685     {
686         ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
687         /* Check prev pointer */
688         if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
689         {
690             fprintf(stderr, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
691                     (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
692             return FALSE;
693         }
694         /* Check that prev arena is free */
695         if (!(pPrev->size & ARENA_FLAG_FREE) ||
696             (pPrev->magic != ARENA_FREE_MAGIC))
697         { 
698             fprintf( stderr, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n", 
699                      (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
700             return FALSE;
701         }
702         /* Check that prev arena is really the previous block */
703         if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
704         {
705             fprintf( stderr, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
706                      (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
707             return FALSE;
708         }
709     }
710     return TRUE;
711 }
712
713
714 /***********************************************************************
715  *           HEAP_IsInsideHeap
716  *
717  * Check whether the pointer is to a block inside a given heap.
718  */
719 int HEAP_IsInsideHeap( HANDLE32 heap, DWORD flags, LPCVOID ptr )
720 {
721     HEAP *heapPtr = HEAP_GetPtr( heap );
722     SUBHEAP *subheap;
723     int ret;
724
725     /* Validate the parameters */
726
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 );
734     return ret;
735 }
736
737
738 /***********************************************************************
739  *           HEAP_GetSegptr
740  *
741  * Transform a linear pointer into a SEGPTR. The pointer must have been
742  * allocated from a HEAP_WINE_SEGPTR heap.
743  */
744 SEGPTR HEAP_GetSegptr( HANDLE32 heap, DWORD flags, LPCVOID ptr )
745 {
746     HEAP *heapPtr = HEAP_GetPtr( heap );
747     SUBHEAP *subheap;
748     SEGPTR ret;
749
750     /* Validate the parameters */
751
752     if (!heapPtr) return 0;
753     flags |= heapPtr->flags;
754     if (!(flags & HEAP_WINE_SEGPTR))
755     {
756         fprintf( stderr, "HEAP_GetSegptr: heap %08x is not a SEGPTR heap\n",
757                  heap );
758         return 0;
759     }
760     if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
761
762     /* Get the subheap */
763
764     if (!(subheap = HEAP_FindSubHeap( heapPtr, ptr )))
765     {
766         fprintf( stderr, "HEAP_GetSegptr: %p is not inside heap %08x\n",
767                  ptr, heap );
768         if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
769         return 0;
770     }
771
772     /* Build the SEGPTR */
773
774     ret = PTR_SEG_OFF_TO_SEGPTR(subheap->selector, (DWORD)ptr-(DWORD)subheap);
775     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
776     return ret;
777 }
778
779
780 /***********************************************************************
781  *           HeapCreate   (KERNEL32.336)
782  */
783 HANDLE32 WINAPI HeapCreate( DWORD flags, DWORD initialSize, DWORD maxSize )
784 {
785     int i;
786     HEAP *heap;
787     SUBHEAP *subheap;
788     FREE_LIST_ENTRY *pEntry;
789
790     /* Allocate the heap block */
791
792     if (!maxSize)
793     {
794         maxSize = HEAP_DEF_SIZE;
795         flags |= HEAP_GROWABLE;
796     }
797     if (!(subheap = HEAP_CreateSubHeap( flags, initialSize, maxSize )))
798     {
799         SetLastError( ERROR_OUTOFMEMORY );
800         return 0;
801     }
802
803     /* Fill the heap structure */
804
805     heap = (HEAP *)subheap;
806     subheap->heap       = heap;
807     subheap->headerSize = sizeof(HEAP);
808     heap->next          = NULL;
809     heap->flags         = flags;
810     heap->magic         = HEAP_MAGIC;
811     InitializeCriticalSection( &heap->critSection );
812
813     /* Build the free lists */
814
815     for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
816     {
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;
825     }
826
827     /* Create the first free block */
828
829     HEAP_CreateFreeBlock( subheap, heap + 1, subheap->size - sizeof(*heap) );
830
831     /* We are done */
832
833     SetLastError( 0 );
834     return (HANDLE32)heap;
835 }
836
837
838 /***********************************************************************
839  *           HeapDestroy   (KERNEL32.337)
840  */
841 BOOL32 WINAPI HeapDestroy( HANDLE32 heap )
842 {
843     HEAP *heapPtr = HEAP_GetPtr( heap );
844     SUBHEAP *subheap;
845
846     dprintf_heap( stddeb, "HeapDestroy: %08x\n", heap );
847     if (!heapPtr) return FALSE;
848
849     DeleteCriticalSection( &heapPtr->critSection );
850     subheap = &heapPtr->subheap;
851     while (subheap)
852     {
853         SUBHEAP *next = subheap->next;
854         if (subheap->selector) FreeSelector( subheap->selector );
855         VirtualFree( subheap, 0, MEM_RELEASE );
856         subheap = next;
857     }
858     return TRUE;
859 }
860
861
862 /***********************************************************************
863  *           HeapAlloc   (KERNEL32.334)
864  */
865 LPVOID WINAPI HeapAlloc( HANDLE32 heap, DWORD flags, DWORD size )
866 {
867     ARENA_FREE *pArena;
868     ARENA_INUSE *pInUse;
869     SUBHEAP *subheap;
870     HEAP *heapPtr = HEAP_GetPtr( heap );
871
872     /* Validate the parameters */
873
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;
880
881     /* Locate a suitable free block */
882
883     if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
884     {
885         dprintf_heap( stddeb, "HeapAlloc(%08x,%08lx,%08lx): returning NULL\n",
886                   heap, flags, size  );
887         if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
888         SetLastError( ERROR_OUTOFMEMORY );
889         return NULL;
890     }
891
892     /* Remove the arena from the free list */
893
894     pArena->next->prev = pArena->prev;
895     pArena->prev->next = pArena->next;
896
897     /* Build the in-use arena */
898
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;
905
906     /* Shrink the block */
907
908     HEAP_ShrinkBlock( subheap, pInUse, size );
909
910     if (flags & HEAP_ZERO_MEMORY) memset( pInUse + 1, 0, size );
911     else if (debugging_heap) memset( pInUse + 1, ARENA_INUSE_FILLER, size );
912
913     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
914     SetLastError( 0 );
915
916     dprintf_heap( stddeb, "HeapAlloc(%08x,%08lx,%08lx): returning %08lx\n",
917                   heap, flags, size, (DWORD)(pInUse + 1) );
918     return (LPVOID)(pInUse + 1);
919 }
920
921
922 /***********************************************************************
923  *           HeapFree   (KERNEL32.338)
924  */
925 BOOL32 WINAPI HeapFree( HANDLE32 heap, DWORD flags, LPVOID ptr )
926 {
927     ARENA_INUSE *pInUse;
928     SUBHEAP *subheap;
929     HEAP *heapPtr = HEAP_GetPtr( heap );
930
931     /* Validate the parameters */
932
933     if (!heapPtr) return FALSE;
934     flags &= HEAP_NO_SERIALIZE;
935     flags |= heapPtr->flags;
936     if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
937     if (!ptr || !HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
938     {
939         if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
940         SetLastError( ERROR_INVALID_PARAMETER );
941         dprintf_heap( stddeb, "HeapFree(%08x,%08lx,%08lx): returning FALSE\n",
942                       heap, flags, (DWORD)ptr );
943         return FALSE;
944     }
945
946     /* Turn the block into a free block */
947
948     pInUse  = (ARENA_INUSE *)ptr - 1;
949     subheap = HEAP_FindSubHeap( heapPtr, pInUse );
950     HEAP_MakeInUseBlockFree( subheap, pInUse );
951
952     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
953 /*    SetLastError( 0 ); */
954
955     dprintf_heap( stddeb, "HeapFree(%08x,%08lx,%08lx): returning TRUE\n",
956                   heap, flags, (DWORD)ptr );
957     return TRUE;
958 }
959
960
961 /***********************************************************************
962  *           HeapReAlloc   (KERNEL32.340)
963  */
964 LPVOID WINAPI HeapReAlloc( HANDLE32 heap, DWORD flags, LPVOID ptr, DWORD size )
965 {
966     ARENA_INUSE *pArena;
967     DWORD oldSize;
968     HEAP *heapPtr;
969     SUBHEAP *subheap;
970
971     if (!ptr) return HeapAlloc( heap, flags, size );  /* FIXME: correct? */
972     if (!(heapPtr = HEAP_GetPtr( heap ))) return FALSE;
973
974     /* Validate the parameters */
975
976     flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
977              HEAP_REALLOC_IN_PLACE_ONLY;
978     flags |= heapPtr->flags;
979     size = (size + 3) & ~3;
980     if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
981
982     if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
983     if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
984     {
985         if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
986         SetLastError( ERROR_INVALID_PARAMETER );
987         dprintf_heap( stddeb, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning NULL\n",
988                       heap, flags, (DWORD)ptr, size );
989         return NULL;
990     }
991
992     /* Check if we need to grow the block */
993
994     pArena = (ARENA_INUSE *)ptr - 1;
995     pArena->threadId = GetCurrentTask();
996     subheap = HEAP_FindSubHeap( heapPtr, pArena );
997     oldSize = (pArena->size & ARENA_SIZE_MASK);
998     if (size > oldSize)
999     {
1000         char *pNext = (char *)(pArena + 1) + oldSize;
1001         if ((pNext < (char *)subheap + subheap->size) &&
1002             (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1003             (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1004         {
1005             /* The next block is free and large enough */
1006             ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1007             pFree->next->prev = pFree->prev;
1008             pFree->prev->next = pFree->next;
1009             pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
1010             if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1011                                                + size + HEAP_MIN_BLOCK_SIZE))
1012             {
1013                 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1014                 SetLastError( ERROR_OUTOFMEMORY );
1015                 return NULL;
1016             }
1017             HEAP_ShrinkBlock( subheap, pArena, size );
1018         }
1019         else  /* Do it the hard way */
1020         {
1021             ARENA_FREE *pNew;
1022             ARENA_INUSE *pInUse;
1023             SUBHEAP *newsubheap;
1024
1025             if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1026                 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1027             {
1028                 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1029                 SetLastError( ERROR_OUTOFMEMORY );
1030                 return NULL;
1031             }
1032
1033             /* Build the in-use arena */
1034
1035             pNew->next->prev = pNew->prev;
1036             pNew->prev->next = pNew->next;
1037             pInUse = (ARENA_INUSE *)pNew;
1038             pInUse->size     = (pInUse->size & ~ARENA_FLAG_FREE)
1039                                + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1040             pInUse->threadId = GetCurrentTask();
1041             pInUse->magic    = ARENA_INUSE_MAGIC;
1042             HEAP_ShrinkBlock( newsubheap, pInUse, size );
1043             memcpy( pInUse + 1, pArena + 1, oldSize );
1044
1045             /* Free the previous block */
1046
1047             HEAP_MakeInUseBlockFree( subheap, pArena );
1048             subheap = newsubheap;
1049             pArena  = pInUse;
1050         }
1051     }
1052     else HEAP_ShrinkBlock( subheap, pArena, size );  /* Shrink the block */
1053
1054     /* Clear the extra bytes if needed */
1055
1056     if (size > oldSize)
1057     {
1058         if (flags & HEAP_ZERO_MEMORY)
1059             memset( (char *)(pArena + 1) + oldSize, 0,
1060                     (pArena->size & ARENA_SIZE_MASK) - oldSize );
1061         else if (debugging_heap)
1062             memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1063                     (pArena->size & ARENA_SIZE_MASK) - oldSize );
1064     }
1065
1066     /* Return the new arena */
1067
1068     pArena->callerEIP = *((DWORD *)&heap - 1);  /* hack hack */
1069     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1070
1071     dprintf_heap( stddeb, "HeapReAlloc(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1072                   heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1073     return (LPVOID)(pArena + 1);
1074 }
1075
1076
1077 /***********************************************************************
1078  *           HeapCompact   (KERNEL32.335)
1079  */
1080 DWORD WINAPI HeapCompact( HANDLE32 heap, DWORD flags )
1081 {
1082     return 0;
1083 }
1084
1085
1086 /***********************************************************************
1087  *           HeapLock   (KERNEL32.339)
1088  */
1089 BOOL32 WINAPI HeapLock( HANDLE32 heap )
1090 {
1091     HEAP *heapPtr = HEAP_GetPtr( heap );
1092
1093     if (!heapPtr) return FALSE;
1094     EnterCriticalSection( &heapPtr->critSection );
1095     return TRUE;
1096 }
1097
1098
1099 /***********************************************************************
1100  *           HeapUnlock   (KERNEL32.342)
1101  */
1102 BOOL32 WINAPI HeapUnlock( HANDLE32 heap )
1103 {
1104     HEAP *heapPtr = HEAP_GetPtr( heap );
1105
1106     if (!heapPtr) return FALSE;
1107     LeaveCriticalSection( &heapPtr->critSection );
1108     return TRUE;
1109 }
1110
1111
1112 /***********************************************************************
1113  *           HeapSize   (KERNEL32.341)
1114  */
1115 DWORD WINAPI HeapSize( HANDLE32 heap, DWORD flags, LPVOID ptr )
1116 {
1117     DWORD ret;
1118     HEAP *heapPtr = HEAP_GetPtr( heap );
1119
1120     if (!heapPtr) return FALSE;
1121     flags &= HEAP_NO_SERIALIZE;
1122     flags |= heapPtr->flags;
1123     if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
1124     if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
1125     {
1126         SetLastError( ERROR_INVALID_PARAMETER );
1127         ret = 0xffffffff;
1128     }
1129     else
1130     {
1131         ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1132         ret = pArena->size & ARENA_SIZE_MASK;
1133     }
1134     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1135
1136     dprintf_heap( stddeb, "HeapSize(%08x,%08lx,%08lx): returning %08lx\n",
1137                   heap, flags, (DWORD)ptr, ret );
1138     return ret;
1139 }
1140
1141
1142 /***********************************************************************
1143  *           HeapValidate   (KERNEL32.343)
1144  */
1145 BOOL32 WINAPI HeapValidate( HANDLE32 heap, DWORD flags, LPVOID block )
1146 {
1147     SUBHEAP *subheap;
1148     HEAP *heapPtr = (HEAP *)heap;
1149
1150     if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
1151     {
1152         fprintf( stderr, "Invalid heap %08x!\n", heap );
1153         return FALSE;
1154     }
1155
1156     if (block)
1157     {
1158         if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
1159             ((char *)block < (char *)subheap + subheap->headerSize
1160                               + sizeof(ARENA_INUSE)))
1161         {
1162             fprintf( stderr, "Heap %08lx: block %08lx is not inside heap\n",
1163                      (DWORD)heap, (DWORD)block );
1164             return FALSE;
1165         }
1166         return HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1 );
1167     }
1168
1169     subheap = &heapPtr->subheap;
1170     while (subheap)
1171     {
1172         char *ptr = (char *)subheap + subheap->headerSize;
1173         while (ptr < (char *)subheap + subheap->size)
1174         {
1175             if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1176             {
1177                 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr ))
1178                     return FALSE;
1179                 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1180             }
1181             else
1182             {
1183                 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr ))
1184                     return FALSE;
1185                 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1186             }
1187         }
1188         subheap = subheap->next;
1189     }
1190     return TRUE;
1191 }
1192
1193
1194 /***********************************************************************
1195  *           HeapWalk   (KERNEL32.344)
1196  */
1197 BOOL32 WINAPI HeapWalk( HANDLE32 heap, void *entry )
1198 {
1199     fprintf( stderr, "HeapWalk(%08x): not implemented\n", heap );
1200     return FALSE;
1201 }
1202
1203
1204 /***********************************************************************
1205  *           HEAP_xalloc
1206  *
1207  * Same as HeapAlloc(), but die on failure.
1208  */
1209 LPVOID HEAP_xalloc( HANDLE32 heap, DWORD flags, DWORD size )
1210 {
1211     LPVOID p = HeapAlloc( heap, flags, size );
1212     if (!p)
1213     {
1214         fprintf( stderr, "Virtual memory exhausted.\n" );
1215         exit(1);
1216     }
1217     return p;
1218 }
1219
1220
1221 /***********************************************************************
1222  *           HEAP_strdupA
1223  */
1224 LPSTR HEAP_strdupA( HANDLE32 heap, DWORD flags, LPCSTR str )
1225 {
1226     LPSTR p = HEAP_xalloc( heap, flags, lstrlen32A(str) + 1 );
1227     lstrcpy32A( p, str );
1228     return p;
1229 }
1230
1231
1232 /***********************************************************************
1233  *           HEAP_strdupW
1234  */
1235 LPWSTR HEAP_strdupW( HANDLE32 heap, DWORD flags, LPCWSTR str )
1236 {
1237     INT32 len = lstrlen32W(str) + 1;
1238     LPWSTR p = HEAP_xalloc( heap, flags, len * sizeof(WCHAR) );
1239     lstrcpy32W( p, str );
1240     return p;
1241 }
1242
1243
1244 /***********************************************************************
1245  *           HEAP_strdupAtoW
1246  */
1247 LPWSTR HEAP_strdupAtoW( HANDLE32 heap, DWORD flags, LPCSTR str )
1248 {
1249     LPWSTR ret;
1250
1251     if (!str) return NULL;
1252     ret = HEAP_xalloc( heap, flags, (lstrlen32A(str)+1) * sizeof(WCHAR) );
1253     lstrcpyAtoW( ret, str );
1254     return ret;
1255 }
1256
1257
1258 /***********************************************************************
1259  *           HEAP_strdupWtoA
1260  */
1261 LPSTR HEAP_strdupWtoA( HANDLE32 heap, DWORD flags, LPCWSTR str )
1262 {
1263     LPSTR ret;
1264
1265     if (!str) return NULL;
1266     ret = HEAP_xalloc( heap, flags, lstrlen32W(str) + 1 );
1267     lstrcpyWtoA( ret, str );
1268     return ret;
1269 }