Release 960712
[wine] / memory / heap.c
1 /*
2  * Win32 heap functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #define NO_TRANSITION_TYPES  /* This file is Win32-clean */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "windows.h"
12 #include "debugger.h"
13 #include "kernel32.h"  /* for CRITICAL_SECTION */
14 #include "selectors.h"
15 #include "winbase.h"
16 #include "winerror.h"
17 #include "winnt.h"
18 #include "stddebug.h"
19 #include "debug.h"
20
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
24  * require it.
25  */
26
27 typedef struct tagARENA_INUSE
28 {
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 */
33 } ARENA_INUSE;
34
35 typedef struct tagARENA_FREE
36 {
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 */
42 } ARENA_FREE;
43
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 */
49
50 #define ARENA_INUSE_FILLER     0x55
51 #define ARENA_FREE_FILLER      0xaa
52
53 #define HEAP_NB_FREE_LISTS   4   /* Number of free lists */
54
55 /* Max size of the blocks on the free lists */
56 static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
57 {
58     0x20, 0x80, 0x200, 0xffffffff
59 };
60
61 typedef struct
62 {
63     DWORD       size;
64     ARENA_FREE  arena;
65 } FREE_LIST_ENTRY;
66
67 struct tagHEAP;
68
69 typedef struct tagSUBHEAP
70 {
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 */
78 } SUBHEAP;
79
80 #define SUBHEAP_MAGIC    ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
81
82 typedef struct tagHEAP
83 {
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 */
90 } HEAP;
91
92 #define HEAP_MAGIC       ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
93
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 */
96
97
98 /***********************************************************************
99  *           HEAP_Dump
100  */
101 void HEAP_Dump( HEAP *heap )
102 {
103     int i;
104     SUBHEAP *subheap;
105     char *ptr;
106
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)
112     {
113         printf( " -> %08lx", (DWORD)subheap->next );
114         subheap = subheap->next;
115     }
116
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 );
124
125     subheap = &heap->subheap;
126     while (subheap)
127     {
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 );
131
132         printf( "\n Block   Stat   Size    Id\n" );
133         ptr = (char*)subheap + subheap->headerSize;
134         while (ptr < (char *)subheap + subheap->size)
135         {
136             if (*(DWORD *)ptr & ARENA_FLAG_FREE)
137             {
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;
146             }
147             else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
148             {
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),
153                         pArena->callerEIP );
154                 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
155                 arenaSize += sizeof(ARENA_INUSE);
156                 usedSize += pArena->size & ARENA_SIZE_MASK;
157             }
158             else
159             {
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;
167             }
168         }
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;
173     }
174 }
175
176
177 /***********************************************************************
178  *           HEAP_GetPtr
179  */
180 static HEAP *HEAP_GetPtr( HANDLE32 heap )
181 {
182     HEAP *heapPtr = (HEAP *)heap;
183     if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
184     {
185         fprintf( stderr, "Invalid heap %08x!\n", heap );
186         SetLastError( ERROR_INVALID_HANDLE );
187         return NULL;
188     }
189     if (debugging_heap && !HeapValidate( heap, 0, NULL ))
190     {
191         HEAP_Dump( heapPtr );
192         DEBUG_EnterDebugger();
193         SetLastError( ERROR_INVALID_HANDLE );
194         return NULL;
195     }
196     return heapPtr;
197 }
198
199
200 /***********************************************************************
201  *           HEAP_InsertFreeBlock
202  *
203  * Insert a free block into the free list.
204  */
205 static void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena )
206 {
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;
214 }
215
216
217 /***********************************************************************
218  *           HEAP_FindSubHeap
219  *
220  * Find the sub-heap containing a given address.
221  */
222 static SUBHEAP *HEAP_FindSubHeap( HEAP *heap, LPCVOID ptr )
223 {
224     SUBHEAP *sub = &heap->subheap;
225     while (sub)
226     {
227         if (((char *)ptr >= (char *)sub) &&
228             ((char *)ptr < (char *)sub + sub->size)) return sub;
229         sub = sub->next;
230     }
231     return NULL;
232 }
233
234
235 /***********************************************************************
236  *           HEAP_Commit
237  *
238  * Make sure the heap storage is committed up to (not including) ptr.
239  */
240 static BOOL32 HEAP_Commit( SUBHEAP *subheap, void *ptr )
241 {
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))
249     {
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 );
254         return FALSE;
255     }
256     subheap->commitSize = size;
257     return TRUE;
258 }
259
260
261 /***********************************************************************
262  *           HEAP_Decommit
263  *
264  * If possible, decommit the heap storage from (including) 'ptr'.
265  */
266 static BOOL32 HEAP_Decommit( SUBHEAP *subheap, void *ptr )
267 {
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 ))
273     {
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 );
278         return FALSE;
279     }
280     subheap->commitSize = size;
281     return TRUE;
282 }
283
284
285 /***********************************************************************
286  *           HEAP_CreateFreeBlock
287  *
288  * Create a free block at a specified address. 'size' is the size of the
289  * whole block, including the new arena.
290  */
291 static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
292 {
293     ARENA_FREE *pFree;
294
295     /* Create a free arena */
296
297     pFree = (ARENA_FREE *)ptr;
298     pFree->threadId = GetCurrentTask();
299     pFree->magic = ARENA_FREE_MAGIC;
300
301     /* If debugging, erase the freed block content */
302
303     if (debugging_heap)
304     {
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) );
310     }
311
312     /* Check if next block is free also */
313
314     if (((char *)ptr + size < (char *)subheap + subheap->size) &&
315         (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
316     {
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);
322         if (debugging_heap)
323             memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
324     }
325
326     /* Set the next block PREV_FREE flag and pointer */
327
328     if ((char *)ptr + size < (char *)subheap + subheap->size)
329     {
330         DWORD *pNext = (DWORD *)((char *)ptr + size);
331         *pNext |= ARENA_FLAG_PREV_FREE;
332         *(ARENA_FREE **)(pNext - 1) = pFree;
333     }
334
335     /* Last, insert the new block into the free list */
336
337     pFree->size = size - sizeof(*pFree);
338     HEAP_InsertFreeBlock( subheap->heap, pFree );
339 }
340
341
342 /***********************************************************************
343  *           HEAP_MakeInUseBlockFree
344  *
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.
347  */
348 static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
349 {
350     ARENA_FREE *pFree;
351     DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
352
353     /* Check if we can merge with previous block */
354
355     if (pArena->size & ARENA_FLAG_PREV_FREE)
356     {
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;
362     }
363     else pFree = (ARENA_FREE *)pArena;
364
365     /* Create a free block */
366
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 */
371
372     /* Free the whole sub-heap if it's empty and not the original one */
373
374     if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
375         (subheap != &subheap->heap->subheap))
376     {
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 */
385         subheap->magic = 0;
386         if (subheap->selector) FreeSelector( subheap->selector );
387         VirtualFree( subheap, subheap->size, MEM_DECOMMIT );
388         VirtualFree( subheap, 0, MEM_RELEASE );
389         return;
390     }
391     
392     /* Decommit the end of the heap */
393
394     HEAP_Decommit( subheap, pFree + 1 );
395 }
396
397
398 /***********************************************************************
399  *           HEAP_ShrinkBlock
400  *
401  * Shrink an in-use block.
402  */
403 static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
404 {
405     if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
406     {
407         HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
408                               (pArena->size & ARENA_SIZE_MASK) - size );
409         pArena->size = (pArena->size & ~ARENA_SIZE_MASK) | size;
410     }
411     else
412     {
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;
417     }
418 }
419
420
421 /***********************************************************************
422  *           HEAP_CreateSubHeap
423  *
424  * Create a sub-heap of the given size.
425  */
426 static SUBHEAP *HEAP_CreateSubHeap( DWORD flags, DWORD commitSize,
427                                     DWORD totalSize )
428 {
429     SUBHEAP *subheap;
430     WORD selector = 0;
431
432     /* Round-up sizes on a 64K boundary */
433
434     if (flags & HEAP_WINE_SEGPTR)
435     {
436         totalSize = commitSize = 0x10000;  /* Only 64K at a time for SEGPTRs */
437     }
438     else
439     {
440         totalSize  = (totalSize + 0xffff) & 0xffff0000;
441         commitSize = (commitSize + 0xffff) & 0xffff0000;
442         if (!commitSize) commitSize = 0x10000;
443         if (totalSize < commitSize) totalSize = commitSize;
444     }
445
446     /* Allocate the memory block */
447
448     if (!(subheap = VirtualAlloc( NULL, totalSize,
449                                   MEM_RESERVE, PAGE_EXECUTE_READWRITE )))
450     {
451         fprintf( stderr, "HEAP_CreateSubHeap: could not VirtualAlloc %08lx bytes\n",
452                  totalSize );
453         return NULL;
454     }
455     if (!VirtualAlloc(subheap, commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
456     {
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 );
460         return NULL;
461     }
462
463     /* Allocate a selector if needed */
464
465     if (flags & HEAP_WINE_SEGPTR)
466     {
467         selector = SELECTOR_AllocBlock( subheap, totalSize,
468                      (flags & HEAP_WINE_CODESEG) ? SEGMENT_CODE : SEGMENT_DATA,
469                      (flags & HEAP_WINE_CODESEG) != 0, FALSE );
470         if (!selector)
471         {
472             fprintf( stderr, "HEAP_CreateSubHeap: could not allocate selector\n" );
473             VirtualFree( subheap, 0, MEM_RELEASE );
474             return NULL;
475         }
476     }
477
478     /* Fill the sub-heap structure */
479
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;
487     return subheap;
488 }
489
490
491 /***********************************************************************
492  *           HEAP_FindFreeBlock
493  *
494  * Find a free block at least as large as the requested size, and make sure
495  * the requested size is committed.
496  */
497 static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
498                                        SUBHEAP **ppSubHeap )
499 {
500     SUBHEAP *subheap;
501     ARENA_FREE *pArena;
502     FREE_LIST_ENTRY *pEntry = heap->freeList;
503
504     /* Find a suitable free list, and in it find a block large enough */
505
506     while (pEntry->size < size) pEntry++;
507     pArena = pEntry->arena.next;
508     while (pArena != &heap->freeList[0].arena)
509     {
510         if (pArena->size > size)
511         {
512             subheap = HEAP_FindSubHeap( heap, pArena );
513             if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
514                                                + size + HEAP_MIN_BLOCK_SIZE))
515                 return NULL;
516             *ppSubHeap = subheap;
517             return pArena;
518         }
519
520         pArena = pArena->next;
521     }
522
523     /* If no block was found, attempt to grow the heap */
524
525     if (!(heap->flags & HEAP_GROWABLE))
526     {
527         fprintf( stderr, "HEAP_FindFreeBlock: Not enough space in heap %08lx for %08lx bytes\n",
528                  (DWORD)heap, size );
529         return NULL;
530     }
531     size += sizeof(SUBHEAP) + sizeof(ARENA_FREE);
532     if (!(subheap = HEAP_CreateSubHeap( heap->flags, size,
533                                         MAX( HEAP_DEF_SIZE, size ) )))
534         return NULL;
535
536     /* Insert the new sub-heap in the list */
537
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 );
544
545     HEAP_CreateFreeBlock( subheap, subheap + 1, size - sizeof(*subheap) );
546     *ppSubHeap = subheap;
547     return (ARENA_FREE *)(subheap + 1);
548 }
549
550
551 /***********************************************************************
552  *           HEAP_IsValidArenaPtr
553  *
554  * Check that the pointer is inside the range possible for arenas.
555  */
556 static BOOL32 HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
557 {
558     int i;
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;
565     return FALSE;
566 }
567
568
569 /***********************************************************************
570  *           HEAP_ValidateFreeArena
571  */
572 static BOOL32 HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
573 {
574     char *heapEnd = (char *)subheap + subheap->size;
575
576     /* Check magic number */
577     if (pArena->magic != ARENA_FREE_MAGIC)
578     {
579         fprintf( stderr, "Heap %08lx: invalid free arena magic for %08lx\n",
580                  (DWORD)subheap->heap, (DWORD)pArena );
581         return FALSE;
582     }
583     /* Check size flags */
584     if (!(pArena->size & ARENA_FLAG_FREE) ||
585         (pArena->size & ARENA_FLAG_PREV_FREE))
586     {
587         fprintf( stderr, "Heap %08lx: bad flags %lx for free arena %08lx\n",
588                  (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
589     }
590     /* Check arena size */
591     if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
592     {
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 );
595         return FALSE;
596     }
597     /* Check that next pointer is valid */
598     if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
599     {
600         fprintf( stderr, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
601                  (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
602         return FALSE;
603     }
604     /* Check that next arena is free */
605     if (!(pArena->next->size & ARENA_FLAG_FREE) ||
606         (pArena->next->magic != ARENA_FREE_MAGIC))
607     { 
608         fprintf( stderr, "Heap %08lx: next arena %08lx invalid for %08lx\n", 
609                  (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
610         return FALSE;
611     }
612     /* Check that prev pointer is valid */
613     if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
614     {
615         fprintf( stderr, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
616                  (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
617         return FALSE;
618     }
619     /* Check that prev arena is free */
620     if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
621         (pArena->prev->magic != ARENA_FREE_MAGIC))
622     { 
623         fprintf( stderr, "Heap %08lx: prev arena %08lx invalid for %08lx\n", 
624                  (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
625         return FALSE;
626     }
627     /* Check that next block has PREV_FREE flag */
628     if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
629     {
630         if (!(*(DWORD *)((char *)(pArena + 1) +
631             (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
632         {
633             fprintf( stderr, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
634                      (DWORD)subheap->heap, (DWORD)pArena );
635             return FALSE;
636         }
637         /* Check next block back pointer */
638         if (*((ARENA_FREE **)((char *)(pArena + 1) +
639             (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
640         {
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));
644             return FALSE;
645         }
646     }
647     return TRUE;
648 }
649
650
651 /***********************************************************************
652  *           HEAP_ValidateInUseArena
653  */
654 static BOOL32 HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena )
655 {
656     char *heapEnd = (char *)subheap + subheap->size;
657
658     /* Check magic number */
659     if (pArena->magic != ARENA_INUSE_MAGIC)
660     {
661         fprintf( stderr, "Heap %08lx: invalid in-use arena magic for %08lx\n",
662                  (DWORD)subheap->heap, (DWORD)pArena );
663         return FALSE;
664     }
665     /* Check size flags */
666     if (pArena->size & ARENA_FLAG_FREE) 
667     {
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 );
670     }
671     /* Check arena size */
672     if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
673     {
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 );
676         return FALSE;
677     }
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))
681     {
682         fprintf( stderr, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
683                  (DWORD)subheap->heap, (DWORD)pArena );
684         return FALSE;
685     }
686     /* Check prev free arena */
687     if (pArena->size & ARENA_FLAG_PREV_FREE)
688     {
689         ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
690         /* Check prev pointer */
691         if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
692         {
693             fprintf(stderr, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
694                     (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
695             return FALSE;
696         }
697         /* Check that prev arena is free */
698         if (!(pPrev->size & ARENA_FLAG_FREE) ||
699             (pPrev->magic != ARENA_FREE_MAGIC))
700         { 
701             fprintf( stderr, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n", 
702                      (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
703             return FALSE;
704         }
705         /* Check that prev arena is really the previous block */
706         if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
707         {
708             fprintf( stderr, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
709                      (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
710             return FALSE;
711         }
712     }
713     return TRUE;
714 }
715
716
717 /***********************************************************************
718  *           HEAP_IsInsideHeap
719  *
720  * Check whether the pointer is to a block inside a given heap.
721  */
722 int HEAP_IsInsideHeap( HANDLE32 heap, DWORD flags, LPCVOID ptr )
723 {
724     HEAP *heapPtr = HEAP_GetPtr( heap );
725     SUBHEAP *subheap;
726     int ret;
727
728     /* Validate the parameters */
729
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 );
737     return ret;
738 }
739
740
741 /***********************************************************************
742  *           HEAP_GetSegptr
743  *
744  * Transform a linear pointer into a SEGPTR. The pointer must have been
745  * allocated from a HEAP_WINE_SEGPTR heap.
746  */
747 SEGPTR HEAP_GetSegptr( HANDLE32 heap, DWORD flags, LPCVOID ptr )
748 {
749     HEAP *heapPtr = HEAP_GetPtr( heap );
750     SUBHEAP *subheap;
751     SEGPTR ret;
752
753     /* Validate the parameters */
754
755     if (!heapPtr) return 0;
756     flags |= heapPtr->flags;
757     if (!(flags & HEAP_WINE_SEGPTR))
758     {
759         fprintf( stderr, "HEAP_GetSegptr: heap %08x is not a SEGPTR heap\n",
760                  heap );
761         return 0;
762     }
763     if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
764
765     /* Get the subheap */
766
767     if (!(subheap = HEAP_FindSubHeap( heapPtr, ptr )))
768     {
769         fprintf( stderr, "HEAP_GetSegptr: %p is not inside heap %08x\n",
770                  ptr, heap );
771         if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
772         return 0;
773     }
774
775     /* Build the SEGPTR */
776
777     ret = PTR_SEG_OFF_TO_SEGPTR(subheap->selector, (DWORD)ptr-(DWORD)subheap);
778     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
779     return ret;
780 }
781
782
783 /***********************************************************************
784  *           HeapCreate   (KERNEL32.336)
785  */
786 HANDLE32 HeapCreate( DWORD flags, DWORD initialSize, DWORD maxSize )
787 {
788     int i;
789     HEAP *heap;
790     SUBHEAP *subheap;
791     FREE_LIST_ENTRY *pEntry;
792
793     /* Allocate the heap block */
794
795     if (!maxSize)
796     {
797         maxSize = HEAP_DEF_SIZE;
798         flags |= HEAP_GROWABLE;
799     }
800     if (!(subheap = HEAP_CreateSubHeap( flags, initialSize, maxSize )))
801     {
802         SetLastError( ERROR_OUTOFMEMORY );
803         return 0;
804     }
805
806     /* Fill the heap structure */
807
808     heap = (HEAP *)subheap;
809     subheap->heap       = heap;
810     subheap->headerSize = sizeof(HEAP);
811     heap->next          = NULL;
812     heap->flags         = flags;
813     heap->magic         = HEAP_MAGIC;
814     InitializeCriticalSection( &heap->critSection );
815
816     /* Build the free lists */
817
818     for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
819     {
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;
828     }
829
830     /* Create the first free block */
831
832     HEAP_CreateFreeBlock( subheap, heap + 1, subheap->size - sizeof(*heap) );
833
834     /* We are done */
835
836     SetLastError( 0 );
837     return (HANDLE32)heap;
838 }
839
840
841 /***********************************************************************
842  *           HeapDestroy   (KERNEL32.337)
843  */
844 BOOL32 HeapDestroy( HANDLE32 heap )
845 {
846     HEAP *heapPtr = HEAP_GetPtr( heap );
847     SUBHEAP *subheap;
848
849     dprintf_heap( stddeb, "HeapDestroy: %08x\n", heap );
850     if (!heapPtr) return FALSE;
851
852     DeleteCriticalSection( &heapPtr->critSection );
853     subheap = &heapPtr->subheap;
854     while (subheap)
855     {
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 );
860         subheap = next;
861     }
862     return TRUE;
863 }
864
865
866 /***********************************************************************
867  *           HeapAlloc   (KERNEL32.334)
868  */
869 LPVOID HeapAlloc( HANDLE32 heap, DWORD flags, DWORD size )
870 {
871     ARENA_FREE *pArena;
872     ARENA_INUSE *pInUse;
873     SUBHEAP *subheap;
874     HEAP *heapPtr = HEAP_GetPtr( heap );
875
876     /* Validate the parameters */
877
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;
884
885     /* Locate a suitable free block */
886
887     if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
888     {
889         dprintf_heap( stddeb, "HeapAlloc(%08x,%08lx,%08lx): returning NULL\n",
890                   heap, flags, size  );
891         if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
892         SetLastError( ERROR_OUTOFMEMORY );
893         return NULL;
894     }
895
896     /* Remove the arena from the free list */
897
898     pArena->next->prev = pArena->prev;
899     pArena->prev->next = pArena->next;
900
901     /* Build the in-use arena */
902
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;
909
910     /* Shrink the block */
911
912     HEAP_ShrinkBlock( subheap, pInUse, size );
913
914     if (flags & HEAP_ZERO_MEMORY) memset( pInUse + 1, 0, size );
915     else if (debugging_heap) memset( pInUse + 1, ARENA_INUSE_FILLER, size );
916
917     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
918     SetLastError( 0 );
919
920     dprintf_heap( stddeb, "HeapAlloc(%08x,%08lx,%08lx): returning %08lx\n",
921                   heap, flags, size, (DWORD)(pInUse + 1) );
922     return (LPVOID)(pInUse + 1);
923 }
924
925
926 /***********************************************************************
927  *           HeapFree   (KERNEL32.338)
928  */
929 BOOL32 HeapFree( HANDLE32 heap, DWORD flags, LPVOID ptr )
930 {
931     ARENA_INUSE *pInUse;
932     SUBHEAP *subheap;
933     HEAP *heapPtr = HEAP_GetPtr( heap );
934
935     /* Validate the parameters */
936
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 ))
942     {
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 );
947         return FALSE;
948     }
949
950     /* Turn the block into a free block */
951
952     pInUse  = (ARENA_INUSE *)ptr - 1;
953     subheap = HEAP_FindSubHeap( heapPtr, pInUse );
954     HEAP_MakeInUseBlockFree( subheap, pInUse );
955
956     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
957     SetLastError( 0 );
958
959     dprintf_heap( stddeb, "HeapFree(%08x,%08lx,%08lx): returning TRUE\n",
960                   heap, flags, (DWORD)ptr );
961     return TRUE;
962 }
963
964
965 /***********************************************************************
966  *           HeapReAlloc   (KERNEL32.340)
967  */
968 LPVOID HeapReAlloc( HANDLE32 heap, DWORD flags, LPVOID ptr, DWORD size )
969 {
970     ARENA_INUSE *pArena;
971     DWORD oldSize;
972     HEAP *heapPtr;
973     SUBHEAP *subheap;
974
975     if (!ptr) return HeapAlloc( heap, flags, size );  /* FIXME: correct? */
976     if (!(heapPtr = HEAP_GetPtr( heap ))) return FALSE;
977
978     /* Validate the parameters */
979
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;
985
986     if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
987     if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
988     {
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 );
993         return NULL;
994     }
995
996     /* Check if we need to grow the block */
997
998     pArena = (ARENA_INUSE *)ptr - 1;
999     pArena->threadId = GetCurrentTask();
1000     subheap = HEAP_FindSubHeap( heapPtr, pArena );
1001     oldSize = (pArena->size & ARENA_SIZE_MASK);
1002     if (size > oldSize)
1003     {
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))
1008         {
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))
1016             {
1017                 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1018                 SetLastError( ERROR_OUTOFMEMORY );
1019                 return NULL;
1020             }
1021             HEAP_ShrinkBlock( subheap, pArena, size );
1022         }
1023         else  /* Do it the hard way */
1024         {
1025             ARENA_FREE *pNew;
1026             ARENA_INUSE *pInUse;
1027             SUBHEAP *newsubheap;
1028
1029             if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1030                 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1031             {
1032                 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1033                 SetLastError( ERROR_OUTOFMEMORY );
1034                 return NULL;
1035             }
1036
1037             /* Build the in-use arena */
1038
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 );
1048
1049             /* Free the previous block */
1050
1051             HEAP_MakeInUseBlockFree( subheap, pArena );
1052             subheap = newsubheap;
1053             pArena  = pInUse;
1054         }
1055     }
1056     else HEAP_ShrinkBlock( subheap, pArena, size );  /* Shrink the block */
1057
1058     /* Clear the extra bytes if needed */
1059
1060     if (size > oldSize)
1061     {
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 );
1068     }
1069
1070     /* Return the new arena */
1071
1072     pArena->callerEIP = *((DWORD *)&heap - 1);  /* hack hack */
1073     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1074
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);
1078 }
1079
1080
1081 /***********************************************************************
1082  *           HeapCompact   (KERNEL32.335)
1083  */
1084 DWORD HeapCompact( HANDLE32 heap, DWORD flags )
1085 {
1086     return 0;
1087 }
1088
1089
1090 /***********************************************************************
1091  *           HeapLock   (KERNEL32.339)
1092  */
1093 BOOL32 HeapLock( HANDLE32 heap )
1094 {
1095     HEAP *heapPtr = HEAP_GetPtr( heap );
1096
1097     if (!heapPtr) return FALSE;
1098     EnterCriticalSection( &heapPtr->critSection );
1099     return TRUE;
1100 }
1101
1102
1103 /***********************************************************************
1104  *           HeapUnlock   (KERNEL32.342)
1105  */
1106 BOOL32 HeapUnlock( HANDLE32 heap )
1107 {
1108     HEAP *heapPtr = HEAP_GetPtr( heap );
1109
1110     if (!heapPtr) return FALSE;
1111     LeaveCriticalSection( &heapPtr->critSection );
1112     return TRUE;
1113 }
1114
1115
1116 /***********************************************************************
1117  *           HeapSize   (KERNEL32.341)
1118  */
1119 DWORD HeapSize( HANDLE32 heap, DWORD flags, LPVOID ptr )
1120 {
1121     DWORD ret;
1122     HEAP *heapPtr = HEAP_GetPtr( heap );
1123
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 ))
1129     {
1130         SetLastError( ERROR_INVALID_PARAMETER );
1131         ret = 0xffffffff;
1132     }
1133     else
1134     {
1135         ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1136         ret = pArena->size & ARENA_SIZE_MASK;
1137     }
1138     if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1139
1140     dprintf_heap( stddeb, "HeapSize(%08x,%08lx,%08lx): returning %08lx\n",
1141                   heap, flags, (DWORD)ptr, ret );
1142     return ret;
1143 }
1144
1145
1146 /***********************************************************************
1147  *           HeapValidate   (KERNEL32.343)
1148  */
1149 BOOL32 HeapValidate( HANDLE32 heap, DWORD flags, LPVOID block )
1150 {
1151     SUBHEAP *subheap;
1152     HEAP *heapPtr = (HEAP *)heap;
1153
1154     if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
1155     {
1156         fprintf( stderr, "Invalid heap %08x!\n", heap );
1157         return FALSE;
1158     }
1159
1160     if (block)
1161     {
1162         if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
1163             ((char *)block < (char *)subheap + subheap->headerSize
1164                               + sizeof(ARENA_INUSE)))
1165         {
1166             fprintf( stderr, "Heap %08lx: block %08lx is not inside heap\n",
1167                      (DWORD)heap, (DWORD)block );
1168             return FALSE;
1169         }
1170         return HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1 );
1171     }
1172
1173     subheap = &heapPtr->subheap;
1174     while (subheap)
1175     {
1176         char *ptr = (char *)subheap + subheap->headerSize;
1177         while (ptr < (char *)subheap + subheap->size)
1178         {
1179             if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1180             {
1181                 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr ))
1182                     return FALSE;
1183                 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1184             }
1185             else
1186             {
1187                 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr ))
1188                     return FALSE;
1189                 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1190             }
1191         }
1192         subheap = subheap->next;
1193     }
1194     return TRUE;
1195 }
1196
1197
1198 /***********************************************************************
1199  *           HeapWalk   (KERNEL32.344)
1200  */
1201 BOOL32 HeapWalk( HANDLE32 heap, void *entry )
1202 {
1203     fprintf( stderr, "HeapWalk(%08x): not implemented\n", heap );
1204     return FALSE;
1205 }
1206
1207
1208 /***********************************************************************
1209  *           HEAP_strdupA
1210  */
1211 LPSTR HEAP_strdupA( HANDLE32 heap, DWORD flags, LPCSTR str )
1212 {
1213     INT32 len = lstrlen32A(str) + 1;
1214     LPSTR p = HeapAlloc( heap, flags, len );
1215     if (p) strcpy( p, str );
1216     return p;
1217 }