4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
11 #include "wine/winbase16.h"
12 #include "wine/winestring.h"
13 #include "selectors.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(heap
)
24 /* Note: the heap data structures are based on what Pietrek describes in his
25 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
26 * the same, but could be easily adapted if it turns out some programs
30 typedef struct tagARENA_INUSE
32 DWORD size
; /* Block size; must be the first field */
33 WORD threadId
; /* Allocating thread id */
34 WORD magic
; /* Magic number */
35 void *callerEIP
; /* EIP of caller upon allocation */
38 typedef struct tagARENA_FREE
40 DWORD size
; /* Block size; must be the first field */
41 WORD threadId
; /* Freeing thread id */
42 WORD magic
; /* Magic number */
43 struct tagARENA_FREE
*next
; /* Next free arena */
44 struct tagARENA_FREE
*prev
; /* Prev free arena */
47 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
48 #define ARENA_FLAG_PREV_FREE 0x00000002
49 #define ARENA_SIZE_MASK 0xfffffffc
50 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
51 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
53 #define ARENA_INUSE_FILLER 0x55
54 #define ARENA_FREE_FILLER 0xaa
56 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
58 /* Max size of the blocks on the free lists */
59 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
61 0x20, 0x80, 0x200, 0xffffffff
72 typedef struct tagSUBHEAP
74 DWORD size
; /* Size of the whole sub-heap */
75 DWORD commitSize
; /* Committed size of the sub-heap */
76 DWORD headerSize
; /* Size of the heap header */
77 struct tagSUBHEAP
*next
; /* Next sub-heap */
78 struct tagHEAP
*heap
; /* Main heap structure */
79 DWORD magic
; /* Magic number */
80 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
83 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
85 typedef struct tagHEAP
87 SUBHEAP subheap
; /* First sub-heap */
88 struct tagHEAP
*next
; /* Next heap for this process */
89 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
90 CRITICAL_SECTION critSection
; /* Critical section for serialization */
91 DWORD flags
; /* Heap flags */
92 DWORD magic
; /* Magic number */
95 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
97 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
98 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
100 HANDLE SystemHeap
= 0;
101 HANDLE SegptrHeap
= 0;
105 #define GET_EIP() (__builtin_return_address(0))
106 #define SET_EIP(ptr) ((ARENA_INUSE*)(ptr) - 1)->callerEIP = GET_EIP()
109 #define SET_EIP(ptr) /* nothing */
110 #endif /* __GNUC__ */
112 /***********************************************************************
115 void HEAP_Dump( HEAP
*heap
)
121 DPRINTF( "Heap: %08lx\n", (DWORD
)heap
);
122 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
123 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
124 subheap
= &heap
->subheap
;
125 while (subheap
->next
)
127 DPRINTF( " -> %08lx", (DWORD
)subheap
->next
);
128 subheap
= subheap
->next
;
131 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
132 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
133 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
134 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
135 heap
->freeList
[i
].arena
.threadId
,
136 (DWORD
)heap
->freeList
[i
].arena
.prev
,
137 (DWORD
)heap
->freeList
[i
].arena
.next
);
139 subheap
= &heap
->subheap
;
142 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
143 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
144 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
146 DPRINTF( "\n Block Stat Size Id\n" );
147 ptr
= (char*)subheap
+ subheap
->headerSize
;
148 while (ptr
< (char *)subheap
+ subheap
->size
)
150 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
152 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
153 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
154 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
155 pArena
->threadId
, (DWORD
)pArena
->prev
,
156 (DWORD
)pArena
->next
);
157 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
158 arenaSize
+= sizeof(ARENA_FREE
);
159 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
161 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
163 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
164 DPRINTF( "%08lx Used %08lx %04x back=%08lx EIP=%p\n",
165 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
166 pArena
->threadId
, *((DWORD
*)pArena
- 1),
168 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
169 arenaSize
+= sizeof(ARENA_INUSE
);
170 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
174 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
175 DPRINTF( "%08lx used %08lx %04x EIP=%p\n",
176 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
177 pArena
->threadId
, pArena
->callerEIP
);
178 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
179 arenaSize
+= sizeof(ARENA_INUSE
);
180 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
183 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
184 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
185 arenaSize
, (arenaSize
* 100) / subheap
->size
);
186 subheap
= subheap
->next
;
191 /***********************************************************************
194 * Pointer to the heap
197 static HEAP
*HEAP_GetPtr(
198 HANDLE heap
/* [in] Handle to the heap */
200 HEAP
*heapPtr
= (HEAP
*)heap
;
201 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
203 ERR("Invalid heap %08x!\n", heap
);
204 SetLastError( ERROR_INVALID_HANDLE
);
207 if (TRACE_ON(heap
) && !HeapValidate( heap
, 0, NULL
))
209 HEAP_Dump( heapPtr
);
211 SetLastError( ERROR_INVALID_HANDLE
);
218 /***********************************************************************
219 * HEAP_InsertFreeBlock
221 * Insert a free block into the free list.
223 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
225 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
226 while (pEntry
->size
< pArena
->size
) pEntry
++;
227 pArena
->size
|= ARENA_FLAG_FREE
;
228 pArena
->next
= pEntry
->arena
.next
;
229 pArena
->next
->prev
= pArena
;
230 pArena
->prev
= &pEntry
->arena
;
231 pEntry
->arena
.next
= pArena
;
235 /***********************************************************************
237 * Find the sub-heap containing a given address.
243 static SUBHEAP
*HEAP_FindSubHeap(
244 HEAP
*heap
, /* [in] Heap pointer */
245 LPCVOID ptr
/* [in] Address */
247 SUBHEAP
*sub
= &heap
->subheap
;
250 if (((char *)ptr
>= (char *)sub
) &&
251 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
258 /***********************************************************************
261 * Make sure the heap storage is committed up to (not including) ptr.
263 static BOOL
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
265 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
266 DWORD pageMask
= VIRTUAL_GetPageSize() - 1;
267 size
= (size
+ pageMask
) & ~pageMask
; /* Align size on a page boundary */
268 if (size
> subheap
->size
) size
= subheap
->size
;
269 if (size
<= subheap
->commitSize
) return TRUE
;
270 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
271 size
- subheap
->commitSize
, MEM_COMMIT
,
272 PAGE_EXECUTE_READWRITE
))
274 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
275 size
- subheap
->commitSize
,
276 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
277 (DWORD
)subheap
->heap
);
280 subheap
->commitSize
= size
;
285 /***********************************************************************
288 * If possible, decommit the heap storage from (including) 'ptr'.
290 static BOOL
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
292 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
293 DWORD pageMask
= VIRTUAL_GetPageSize() - 1;
294 size
= (size
+ pageMask
) & ~pageMask
; /* Align size on a page boundary */
295 if (size
>= subheap
->commitSize
) return TRUE
;
296 if (!VirtualFree( (char *)subheap
+ size
,
297 subheap
->commitSize
- size
, MEM_DECOMMIT
))
299 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
300 subheap
->commitSize
- size
,
301 (DWORD
)((char *)subheap
+ size
),
302 (DWORD
)subheap
->heap
);
305 subheap
->commitSize
= size
;
310 /***********************************************************************
311 * HEAP_CreateFreeBlock
313 * Create a free block at a specified address. 'size' is the size of the
314 * whole block, including the new arena.
316 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
320 /* Create a free arena */
322 pFree
= (ARENA_FREE
*)ptr
;
323 pFree
->threadId
= GetCurrentTask();
324 pFree
->magic
= ARENA_FREE_MAGIC
;
326 /* If debugging, erase the freed block content */
330 char *pEnd
= (char *)ptr
+ size
;
331 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
332 pEnd
= (char *)subheap
+ subheap
->commitSize
;
333 if (pEnd
> (char *)(pFree
+ 1))
334 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
337 /* Check if next block is free also */
339 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
340 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
342 /* Remove the next arena from the free list */
343 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
344 pNext
->next
->prev
= pNext
->prev
;
345 pNext
->prev
->next
= pNext
->next
;
346 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
348 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
351 /* Set the next block PREV_FREE flag and pointer */
353 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
355 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
356 *pNext
|= ARENA_FLAG_PREV_FREE
;
357 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
360 /* Last, insert the new block into the free list */
362 pFree
->size
= size
- sizeof(*pFree
);
363 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
367 /***********************************************************************
368 * HEAP_MakeInUseBlockFree
370 * Turn an in-use block into a free block. Can also decommit the end of
371 * the heap, and possibly even free the sub-heap altogether.
373 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
376 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
378 /* Check if we can merge with previous block */
380 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
382 pFree
= *((ARENA_FREE
**)pArena
- 1);
383 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
384 /* Remove it from the free list */
385 pFree
->next
->prev
= pFree
->prev
;
386 pFree
->prev
->next
= pFree
->next
;
388 else pFree
= (ARENA_FREE
*)pArena
;
390 /* Create a free block */
392 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
393 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
394 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
395 return; /* Not the last block, so nothing more to do */
397 /* Free the whole sub-heap if it's empty and not the original one */
399 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
400 (subheap
!= &subheap
->heap
->subheap
))
402 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
403 /* Remove the free block from the list */
404 pFree
->next
->prev
= pFree
->prev
;
405 pFree
->prev
->next
= pFree
->next
;
406 /* Remove the subheap from the list */
407 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
408 if (pPrev
) pPrev
->next
= subheap
->next
;
409 /* Free the memory */
411 if (subheap
->selector
) FreeSelector16( subheap
->selector
);
412 VirtualFree( subheap
, 0, MEM_RELEASE
);
416 /* Decommit the end of the heap */
418 HEAP_Decommit( subheap
, pFree
+ 1 );
422 /***********************************************************************
425 * Shrink an in-use block.
427 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
429 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
431 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
432 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
433 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
437 /* Turn off PREV_FREE flag in next block */
438 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
439 if (pNext
< (char *)subheap
+ subheap
->size
)
440 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
444 /***********************************************************************
447 static BOOL
HEAP_InitSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
448 DWORD commitSize
, DWORD totalSize
)
450 SUBHEAP
*subheap
= (SUBHEAP
*)address
;
452 FREE_LIST_ENTRY
*pEntry
;
457 if (!VirtualAlloc(address
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
459 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
460 commitSize
, (DWORD
)address
);
464 /* Allocate a selector if needed */
466 if (flags
& HEAP_WINE_SEGPTR
)
468 selector
= SELECTOR_AllocBlock( address
, totalSize
,
469 (flags
& (HEAP_WINE_CODESEG
|HEAP_WINE_CODE16SEG
))
470 ? SEGMENT_CODE
: SEGMENT_DATA
,
471 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
474 ERR("Could not allocate selector\n" );
479 /* Fill the sub-heap structure */
481 subheap
->heap
= heap
;
482 subheap
->selector
= selector
;
483 subheap
->size
= totalSize
;
484 subheap
->commitSize
= commitSize
;
485 subheap
->magic
= SUBHEAP_MAGIC
;
487 if ( subheap
!= (SUBHEAP
*)heap
)
489 /* If this is a secondary subheap, insert it into list */
491 subheap
->headerSize
= sizeof(SUBHEAP
);
492 subheap
->next
= heap
->subheap
.next
;
493 heap
->subheap
.next
= subheap
;
497 /* If this is a primary subheap, initialize main heap */
499 subheap
->headerSize
= sizeof(HEAP
);
500 subheap
->next
= NULL
;
503 heap
->magic
= HEAP_MAGIC
;
505 /* Build the free lists */
507 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
509 pEntry
->size
= HEAP_freeListSizes
[i
];
510 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
511 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
512 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
513 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
514 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
515 pEntry
->arena
.threadId
= 0;
516 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
519 /* Initialize critical section */
521 InitializeCriticalSection( &heap
->critSection
);
522 /* FIXME: once separate address spaces are implemented, only */
523 /* the SystemHeap critical section should be global */
524 /* if (!SystemHeap) */
525 MakeCriticalSectionGlobal( &heap
->critSection
);
528 /* Create the first free block */
530 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
+ subheap
->headerSize
,
531 subheap
->size
- subheap
->headerSize
);
536 /***********************************************************************
539 * Create a sub-heap of the given size.
540 * If heap == NULL, creates a main heap.
542 static SUBHEAP
*HEAP_CreateSubHeap( HEAP
*heap
, DWORD flags
,
543 DWORD commitSize
, DWORD totalSize
)
547 /* Round-up sizes on a 64K boundary */
549 if (flags
& HEAP_WINE_SEGPTR
)
551 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
555 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
556 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
557 if (!commitSize
) commitSize
= 0x10000;
558 if (totalSize
< commitSize
) totalSize
= commitSize
;
561 /* Allocate the memory block */
563 if (!(address
= VirtualAlloc( NULL
, totalSize
,
564 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
566 WARN("Could not VirtualAlloc %08lx bytes\n",
571 /* Initialize subheap */
573 if (!HEAP_InitSubHeap( heap
? heap
: (HEAP
*)address
,
574 address
, flags
, commitSize
, totalSize
))
576 VirtualFree( address
, 0, MEM_RELEASE
);
580 return (SUBHEAP
*)address
;
584 /***********************************************************************
587 * Find a free block at least as large as the requested size, and make sure
588 * the requested size is committed.
590 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
591 SUBHEAP
**ppSubHeap
)
595 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
597 /* Find a suitable free list, and in it find a block large enough */
599 while (pEntry
->size
< size
) pEntry
++;
600 pArena
= pEntry
->arena
.next
;
601 while (pArena
!= &heap
->freeList
[0].arena
)
603 if (pArena
->size
> size
)
605 subheap
= HEAP_FindSubHeap( heap
, pArena
);
606 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
607 + size
+ HEAP_MIN_BLOCK_SIZE
))
609 *ppSubHeap
= subheap
;
613 pArena
= pArena
->next
;
616 /* If no block was found, attempt to grow the heap */
618 if (!(heap
->flags
& HEAP_GROWABLE
))
620 WARN("Not enough space in heap %08lx for %08lx bytes\n",
624 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
625 if (!(subheap
= HEAP_CreateSubHeap( heap
, heap
->flags
, size
,
626 MAX( HEAP_DEF_SIZE
, size
) )))
629 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
630 (DWORD
)subheap
, size
, (DWORD
)heap
);
632 *ppSubHeap
= subheap
;
633 return (ARENA_FREE
*)(subheap
+ 1);
637 /***********************************************************************
638 * HEAP_IsValidArenaPtr
640 * Check that the pointer is inside the range possible for arenas.
642 static BOOL
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
645 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
646 if (!subheap
) return FALSE
;
647 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
648 if (subheap
!= &heap
->subheap
) return FALSE
;
649 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
650 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
655 /***********************************************************************
656 * HEAP_ValidateFreeArena
658 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
660 char *heapEnd
= (char *)subheap
+ subheap
->size
;
662 /* Check magic number */
663 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
665 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
666 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
669 /* Check size flags */
670 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
671 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
673 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
674 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
676 /* Check arena size */
677 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
679 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
680 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
683 /* Check that next pointer is valid */
684 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
686 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
687 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
690 /* Check that next arena is free */
691 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
692 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
694 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
695 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
698 /* Check that prev pointer is valid */
699 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
701 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
702 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
705 /* Check that prev arena is free */
706 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
707 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
709 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
710 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
713 /* Check that next block has PREV_FREE flag */
714 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
716 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
717 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
719 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
720 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
723 /* Check next block back pointer */
724 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
725 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
727 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
728 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
729 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
737 /***********************************************************************
738 * HEAP_ValidateInUseArena
740 static BOOL
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
742 char *heapEnd
= (char *)subheap
+ subheap
->size
;
744 /* Check magic number */
745 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
747 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
748 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
751 /* Check size flags */
752 if (pArena
->size
& ARENA_FLAG_FREE
)
754 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
755 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
757 /* Check arena size */
758 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
760 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
761 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
764 /* Check next arena PREV_FREE flag */
765 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
766 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
768 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
769 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
772 /* Check prev free arena */
773 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
775 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
776 /* Check prev pointer */
777 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
779 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
780 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
783 /* Check that prev arena is free */
784 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
785 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
787 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
788 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
791 /* Check that prev arena is really the previous block */
792 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
794 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
795 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
803 /***********************************************************************
805 * Checks whether the pointer points to a block inside a given heap.
808 * Should this return BOOL32?
814 int HEAP_IsInsideHeap(
815 HANDLE heap
, /* [in] Heap */
816 DWORD flags
, /* [in] Flags */
817 LPCVOID ptr
/* [in] Pointer */
819 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
823 /* Validate the parameters */
825 if (!heapPtr
) return 0;
826 flags
|= heapPtr
->flags
;
827 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
828 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
829 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
830 + sizeof(ARENA_INUSE
))));
831 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
836 /***********************************************************************
839 * Transform a linear pointer into a SEGPTR. The pointer must have been
840 * allocated from a HEAP_WINE_SEGPTR heap.
842 SEGPTR
HEAP_GetSegptr( HANDLE heap
, DWORD flags
, LPCVOID ptr
)
844 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
848 /* Validate the parameters */
850 if (!heapPtr
) return 0;
851 flags
|= heapPtr
->flags
;
852 if (!(flags
& HEAP_WINE_SEGPTR
))
854 ERR("Heap %08x is not a SEGPTR heap\n",
858 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
860 /* Get the subheap */
862 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
864 ERR("%p is not inside heap %08x\n",
866 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
870 /* Build the SEGPTR */
872 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
873 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
878 /***********************************************************************
879 * HeapCreate (KERNEL32.336)
881 * Handle of heap: Success
884 HANDLE WINAPI
HeapCreate(
885 DWORD flags
, /* [in] Heap allocation flag */
886 DWORD initialSize
, /* [in] Initial heap size */
887 DWORD maxSize
/* [in] Maximum heap size */
891 /* Allocate the heap block */
895 maxSize
= HEAP_DEF_SIZE
;
896 flags
|= HEAP_GROWABLE
;
898 if (!(subheap
= HEAP_CreateSubHeap( NULL
, flags
, initialSize
, maxSize
)))
900 SetLastError( ERROR_OUTOFMEMORY
);
904 return (HANDLE
)subheap
;
907 /***********************************************************************
908 * HeapDestroy (KERNEL32.337)
913 BOOL WINAPI
HeapDestroy(
914 HANDLE heap
/* [in] Handle of heap */
916 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
919 TRACE("%08x\n", heap
);
920 if (!heapPtr
) return FALSE
;
922 DeleteCriticalSection( &heapPtr
->critSection
);
923 subheap
= &heapPtr
->subheap
;
926 SUBHEAP
*next
= subheap
->next
;
927 if (subheap
->selector
) FreeSelector16( subheap
->selector
);
928 VirtualFree( subheap
, 0, MEM_RELEASE
);
935 /***********************************************************************
936 * HeapAlloc (KERNEL32.334)
938 * Pointer to allocated memory block
941 LPVOID WINAPI
HeapAlloc(
942 HANDLE heap
, /* [in] Handle of private heap block */
943 DWORD flags
, /* [in] Heap allocation control flags */
944 DWORD size
/* [in] Number of bytes to allocate */
949 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
951 /* Validate the parameters */
953 if (!heapPtr
) return NULL
;
954 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
955 flags
|= heapPtr
->flags
;
956 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
957 size
= (size
+ 3) & ~3;
958 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
960 /* Locate a suitable free block */
962 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
964 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
966 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
967 SetLastError( ERROR_COMMITMENT_LIMIT
);
971 /* Remove the arena from the free list */
973 pArena
->next
->prev
= pArena
->prev
;
974 pArena
->prev
->next
= pArena
->next
;
976 /* Build the in-use arena */
978 pInUse
= (ARENA_INUSE
*)pArena
;
979 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
980 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
981 pInUse
->callerEIP
= GET_EIP();
982 pInUse
->threadId
= GetCurrentTask();
983 pInUse
->magic
= ARENA_INUSE_MAGIC
;
985 /* Shrink the block */
987 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
989 if (flags
& HEAP_ZERO_MEMORY
) memset( pInUse
+ 1, 0, size
);
990 else if (TRACE_ON(heap
)) memset( pInUse
+ 1, ARENA_INUSE_FILLER
, size
);
992 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
994 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
995 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
996 return (LPVOID
)(pInUse
+ 1);
1000 /***********************************************************************
1001 * HeapFree (KERNEL32.338)
1006 BOOL WINAPI
HeapFree(
1007 HANDLE heap
, /* [in] Handle of heap */
1008 DWORD flags
, /* [in] Heap freeing flags */
1009 LPVOID ptr
/* [in] Address of memory to free */
1011 ARENA_INUSE
*pInUse
;
1013 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1015 /* Validate the parameters */
1017 if (!heapPtr
) return FALSE
;
1018 flags
&= HEAP_NO_SERIALIZE
;
1019 flags
|= heapPtr
->flags
;
1020 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1023 WARN("(%08x,%08lx,%08lx): asked to free NULL\n",
1024 heap
, flags
, (DWORD
)ptr
);
1026 if (!ptr
|| !HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1028 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1029 SetLastError( ERROR_INVALID_PARAMETER
);
1030 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1031 heap
, flags
, (DWORD
)ptr
);
1035 /* Turn the block into a free block */
1037 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1038 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
1039 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
1041 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1042 /* SetLastError( 0 ); */
1044 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1045 heap
, flags
, (DWORD
)ptr
);
1050 /***********************************************************************
1051 * HeapReAlloc (KERNEL32.340)
1053 * Pointer to reallocated memory block
1056 LPVOID WINAPI
HeapReAlloc(
1057 HANDLE heap
, /* [in] Handle of heap block */
1058 DWORD flags
, /* [in] Heap reallocation flags */
1059 LPVOID ptr
, /* [in] Address of memory to reallocate */
1060 DWORD size
/* [in] Number of bytes to reallocate */
1062 ARENA_INUSE
*pArena
;
1067 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
1068 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1070 /* Validate the parameters */
1072 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1073 HEAP_REALLOC_IN_PLACE_ONLY
;
1074 flags
|= heapPtr
->flags
;
1075 size
= (size
+ 3) & ~3;
1076 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1078 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1079 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1081 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1082 SetLastError( ERROR_INVALID_PARAMETER
);
1083 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1084 heap
, flags
, (DWORD
)ptr
, size
);
1088 /* Check if we need to grow the block */
1090 pArena
= (ARENA_INUSE
*)ptr
- 1;
1091 pArena
->threadId
= GetCurrentTask();
1092 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1093 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1096 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1097 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1098 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1099 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1101 /* The next block is free and large enough */
1102 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1103 pFree
->next
->prev
= pFree
->prev
;
1104 pFree
->prev
->next
= pFree
->next
;
1105 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1106 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1107 + size
+ HEAP_MIN_BLOCK_SIZE
))
1109 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1110 SetLastError( ERROR_OUTOFMEMORY
);
1113 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1115 else /* Do it the hard way */
1118 ARENA_INUSE
*pInUse
;
1119 SUBHEAP
*newsubheap
;
1121 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1122 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1124 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1125 SetLastError( ERROR_OUTOFMEMORY
);
1129 /* Build the in-use arena */
1131 pNew
->next
->prev
= pNew
->prev
;
1132 pNew
->prev
->next
= pNew
->next
;
1133 pInUse
= (ARENA_INUSE
*)pNew
;
1134 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1135 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1136 pInUse
->threadId
= GetCurrentTask();
1137 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1138 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1139 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1141 /* Free the previous block */
1143 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1144 subheap
= newsubheap
;
1148 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1150 /* Clear the extra bytes if needed */
1154 if (flags
& HEAP_ZERO_MEMORY
)
1155 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1156 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1157 else if (TRACE_ON(heap
))
1158 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1159 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1162 /* Return the new arena */
1164 pArena
->callerEIP
= GET_EIP();
1165 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1167 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1168 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1169 return (LPVOID
)(pArena
+ 1);
1173 /***********************************************************************
1174 * HeapCompact (KERNEL32.335)
1176 DWORD WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
1178 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1183 /***********************************************************************
1184 * HeapLock (KERNEL32.339)
1185 * Attempts to acquire the critical section object for a specified heap.
1191 BOOL WINAPI
HeapLock(
1192 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
1194 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1195 if (!heapPtr
) return FALSE
;
1196 EnterCriticalSection( &heapPtr
->critSection
);
1201 /***********************************************************************
1202 * HeapUnlock (KERNEL32.342)
1203 * Releases ownership of the critical section object.
1209 BOOL WINAPI
HeapUnlock(
1210 HANDLE heap
/* [in] Handle to the heap to unlock */
1212 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1213 if (!heapPtr
) return FALSE
;
1214 LeaveCriticalSection( &heapPtr
->critSection
);
1219 /***********************************************************************
1220 * HeapSize (KERNEL32.341)
1222 * Size in bytes of allocated memory
1223 * 0xffffffff: Failure
1225 DWORD WINAPI
HeapSize(
1226 HANDLE heap
, /* [in] Handle of heap */
1227 DWORD flags
, /* [in] Heap size control flags */
1228 LPVOID ptr
/* [in] Address of memory to return size for */
1231 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1233 if (!heapPtr
) return FALSE
;
1234 flags
&= HEAP_NO_SERIALIZE
;
1235 flags
|= heapPtr
->flags
;
1236 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1237 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1239 SetLastError( ERROR_INVALID_PARAMETER
);
1244 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1245 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1247 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1249 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1250 heap
, flags
, (DWORD
)ptr
, ret
);
1255 /***********************************************************************
1256 * HeapValidate (KERNEL32.343)
1257 * Validates a specified heap.
1266 BOOL WINAPI
HeapValidate(
1267 HANDLE heap
, /* [in] Handle to the heap */
1268 DWORD flags
, /* [in] Bit flags that control access during operation */
1269 LPCVOID block
/* [in] Optional pointer to memory block to validate */
1272 HEAP
*heapPtr
= (HEAP
*)(heap
);
1275 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
1277 ERR("Invalid heap %08x!\n", heap
);
1281 flags
&= HEAP_NO_SERIALIZE
;
1282 flags
|= heapPtr
->flags
;
1283 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
1284 if (!(flags
& HEAP_NO_SERIALIZE
))
1285 EnterCriticalSection( &heapPtr
->critSection
);
1288 /* Only check this single memory block */
1289 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
1290 ((char *)block
< (char *)subheap
+ subheap
->headerSize
1291 + sizeof(ARENA_INUSE
)))
1293 ERR("Heap %08lx: block %08lx is not inside heap\n",
1294 (DWORD
)heap
, (DWORD
)block
);
1297 ret
= HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1 );
1299 if (!(flags
& HEAP_NO_SERIALIZE
))
1300 LeaveCriticalSection( &heapPtr
->critSection
);
1304 subheap
= &heapPtr
->subheap
;
1305 while (subheap
&& ret
)
1307 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1308 while (ptr
< (char *)subheap
+ subheap
->size
)
1310 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1312 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
)) {
1316 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1320 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
)) {
1324 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1327 subheap
= subheap
->next
;
1329 if (!(flags
& HEAP_NO_SERIALIZE
))
1330 LeaveCriticalSection( &heapPtr
->critSection
);
1335 /***********************************************************************
1336 * HeapWalk (KERNEL32.344)
1337 * Enumerates the memory blocks in a specified heap.
1343 BOOL WINAPI
HeapWalk(
1344 HANDLE heap
, /* [in] Handle to heap to enumerate */
1345 LPPROCESS_HEAP_ENTRY
*entry
/* [out] Pointer to structure of enumeration info */
1347 FIXME("(%08x): stub.\n", heap
);
1352 /***********************************************************************
1355 * Same as HeapAlloc(), but die on failure.
1357 LPVOID
HEAP_xalloc( HANDLE heap
, DWORD flags
, DWORD size
)
1359 LPVOID p
= HeapAlloc( heap
, flags
, size
);
1362 MESSAGE("Virtual memory exhausted.\n" );
1370 /***********************************************************************
1373 * Same as HeapReAlloc(), but die on failure.
1375 LPVOID
HEAP_xrealloc( HANDLE heap
, DWORD flags
, LPVOID lpMem
, DWORD size
)
1377 LPVOID p
= HeapReAlloc( heap
, flags
, lpMem
, size
);
1380 MESSAGE("Virtual memory exhausted.\n" );
1388 /***********************************************************************
1391 LPSTR
HEAP_strdupA( HANDLE heap
, DWORD flags
, LPCSTR str
)
1393 LPSTR p
= HEAP_xalloc( heap
, flags
, strlen(str
) + 1 );
1400 /***********************************************************************
1403 LPWSTR
HEAP_strdupW( HANDLE heap
, DWORD flags
, LPCWSTR str
)
1405 INT len
= lstrlenW(str
) + 1;
1406 LPWSTR p
= HEAP_xalloc( heap
, flags
, len
* sizeof(WCHAR
) );
1413 /***********************************************************************
1416 LPWSTR
HEAP_strdupAtoW( HANDLE heap
, DWORD flags
, LPCSTR str
)
1420 if (!str
) return NULL
;
1421 ret
= HEAP_xalloc( heap
, flags
, (strlen(str
)+1) * sizeof(WCHAR
) );
1423 lstrcpyAtoW( ret
, str
);
1428 /***********************************************************************
1431 LPSTR
HEAP_strdupWtoA( HANDLE heap
, DWORD flags
, LPCWSTR str
)
1435 if (!str
) return NULL
;
1436 ret
= HEAP_xalloc( heap
, flags
, lstrlenW(str
) + 1 );
1438 lstrcpyWtoA( ret
, str
);
1444 /***********************************************************************
1445 * 32-bit local heap functions (Win95; undocumented)
1448 #define HTABLE_SIZE 0x10000
1449 #define HTABLE_PAGESIZE 0x1000
1450 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
1452 #include "pshpack1.h"
1453 typedef struct _LOCAL32HEADER
1455 WORD freeListFirst
[HTABLE_NPAGES
];
1456 WORD freeListSize
[HTABLE_NPAGES
];
1457 WORD freeListLast
[HTABLE_NPAGES
];
1459 DWORD selectorTableOffset
;
1460 WORD selectorTableSize
;
1473 #include "poppack.h"
1475 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
1477 /***********************************************************************
1478 * Local32Init (KERNEL.208)
1480 HANDLE WINAPI
Local32Init16( WORD segment
, DWORD tableSize
,
1481 DWORD heapSize
, DWORD flags
)
1483 DWORD totSize
, segSize
= 0;
1485 LOCAL32HEADER
*header
;
1487 WORD
*selectorTable
;
1488 WORD selectorEven
, selectorOdd
;
1491 /* Determine new heap size */
1495 if ( (segSize
= GetSelectorLimit16( segment
)) == 0 )
1501 if ( heapSize
== -1L )
1502 heapSize
= 1024L*1024L; /* FIXME */
1504 heapSize
= (heapSize
+ 0xffff) & 0xffff0000;
1505 segSize
= (segSize
+ 0x0fff) & 0xfffff000;
1506 totSize
= segSize
+ HTABLE_SIZE
+ heapSize
;
1509 /* Allocate memory and initialize heap */
1511 if ( !(base
= VirtualAlloc( NULL
, totSize
, MEM_RESERVE
, PAGE_READWRITE
)) )
1514 if ( !VirtualAlloc( base
, segSize
+ HTABLE_PAGESIZE
,
1515 MEM_COMMIT
, PAGE_READWRITE
) )
1517 VirtualFree( base
, 0, MEM_RELEASE
);
1521 heap
= (HEAP
*)(base
+ segSize
+ HTABLE_SIZE
);
1522 if ( !HEAP_InitSubHeap( heap
, (LPVOID
)heap
, 0, 0x10000, heapSize
) )
1524 VirtualFree( base
, 0, MEM_RELEASE
);
1529 /* Set up header and handle table */
1531 header
= (LOCAL32HEADER
*)(base
+ segSize
);
1532 header
->base
= base
;
1533 header
->limit
= HTABLE_PAGESIZE
-1;
1535 header
->magic
= LOCAL32_MAGIC
;
1536 header
->heap
= (HANDLE
)heap
;
1538 header
->freeListFirst
[0] = sizeof(LOCAL32HEADER
);
1539 header
->freeListLast
[0] = HTABLE_PAGESIZE
- 4;
1540 header
->freeListSize
[0] = (HTABLE_PAGESIZE
- sizeof(LOCAL32HEADER
)) / 4;
1542 for (i
= header
->freeListFirst
[0]; i
< header
->freeListLast
[0]; i
+= 4)
1543 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1545 header
->freeListFirst
[1] = 0xffff;
1548 /* Set up selector table */
1550 nrBlocks
= (totSize
+ 0x7fff) >> 15;
1551 selectorTable
= (LPWORD
) HeapAlloc( header
->heap
, 0, nrBlocks
* 2 );
1552 selectorEven
= SELECTOR_AllocBlock( base
, totSize
,
1553 SEGMENT_DATA
, FALSE
, FALSE
);
1554 selectorOdd
= SELECTOR_AllocBlock( base
+ 0x8000, totSize
- 0x8000,
1555 SEGMENT_DATA
, FALSE
, FALSE
);
1557 if ( !selectorTable
|| !selectorEven
|| !selectorOdd
)
1559 if ( selectorTable
) HeapFree( header
->heap
, 0, selectorTable
);
1560 if ( selectorEven
) SELECTOR_FreeBlock( selectorEven
, totSize
>> 16 );
1561 if ( selectorOdd
) SELECTOR_FreeBlock( selectorOdd
, (totSize
-0x8000) >> 16 );
1562 HeapDestroy( header
->heap
);
1563 VirtualFree( base
, 0, MEM_RELEASE
);
1567 header
->selectorTableOffset
= (LPBYTE
)selectorTable
- header
->base
;
1568 header
->selectorTableSize
= nrBlocks
* 4; /* ??? Win95 does it this way! */
1569 header
->selectorDelta
= selectorEven
- selectorOdd
;
1570 header
->segment
= segment
? segment
: selectorEven
;
1572 for (i
= 0; i
< nrBlocks
; i
++)
1573 selectorTable
[i
] = (i
& 1)? selectorOdd
+ ((i
>> 1) << __AHSHIFT
)
1574 : selectorEven
+ ((i
>> 1) << __AHSHIFT
);
1576 /* Move old segment */
1580 /* FIXME: This is somewhat ugly and relies on implementation
1581 details about 16-bit global memory handles ... */
1583 LPBYTE oldBase
= (LPBYTE
)GetSelectorBase( segment
);
1584 memcpy( base
, oldBase
, segSize
);
1585 GLOBAL_MoveBlock( segment
, base
, totSize
);
1586 HeapFree( SystemHeap
, 0, oldBase
);
1589 return (HANDLE
)header
;
1592 /***********************************************************************
1593 * Local32_SearchHandle
1595 static LPDWORD
Local32_SearchHandle( LOCAL32HEADER
*header
, DWORD addr
)
1599 for ( handle
= (LPDWORD
)((LPBYTE
)header
+ sizeof(LOCAL32HEADER
));
1600 handle
< (LPDWORD
)((LPBYTE
)header
+ header
->limit
);
1603 if (*handle
== addr
)
1610 /***********************************************************************
1613 static VOID
Local32_ToHandle( LOCAL32HEADER
*header
, INT16 type
,
1614 DWORD addr
, LPDWORD
*handle
, LPBYTE
*ptr
)
1621 case -2: /* 16:16 pointer, no handles */
1622 *ptr
= PTR_SEG_TO_LIN( addr
);
1623 *handle
= (LPDWORD
)*ptr
;
1626 case -1: /* 32-bit offset, no handles */
1627 *ptr
= header
->base
+ addr
;
1628 *handle
= (LPDWORD
)*ptr
;
1631 case 0: /* handle */
1632 if ( addr
>= sizeof(LOCAL32HEADER
)
1633 && addr
< header
->limit
&& !(addr
& 3)
1634 && *(LPDWORD
)((LPBYTE
)header
+ addr
) >= HTABLE_SIZE
)
1636 *handle
= (LPDWORD
)((LPBYTE
)header
+ addr
);
1637 *ptr
= header
->base
+ **handle
;
1641 case 1: /* 16:16 pointer */
1642 *ptr
= PTR_SEG_TO_LIN( addr
);
1643 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1646 case 2: /* 32-bit offset */
1647 *ptr
= header
->base
+ addr
;
1648 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1653 /***********************************************************************
1654 * Local32_FromHandle
1656 static VOID
Local32_FromHandle( LOCAL32HEADER
*header
, INT16 type
,
1657 DWORD
*addr
, LPDWORD handle
, LPBYTE ptr
)
1661 case -2: /* 16:16 pointer */
1664 WORD
*selTable
= (LPWORD
)(header
->base
+ header
->selectorTableOffset
);
1665 DWORD offset
= (LPBYTE
)ptr
- header
->base
;
1666 *addr
= MAKELONG( offset
& 0x7fff, selTable
[offset
>> 15] );
1670 case -1: /* 32-bit offset */
1672 *addr
= ptr
- header
->base
;
1675 case 0: /* handle */
1676 *addr
= (LPBYTE
)handle
- (LPBYTE
)header
;
1681 /***********************************************************************
1682 * Local32Alloc (KERNEL.209)
1684 DWORD WINAPI
Local32Alloc16( HANDLE heap
, DWORD size
, INT16 type
, DWORD flags
)
1686 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1691 /* Allocate memory */
1692 ptr
= HeapAlloc( header
->heap
,
1693 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0, size
);
1697 /* Allocate handle if requested */
1702 /* Find first page of handle table with free slots */
1703 for (page
= 0; page
< HTABLE_NPAGES
; page
++)
1704 if (header
->freeListFirst
[page
] != 0)
1706 if (page
== HTABLE_NPAGES
)
1708 WARN("Out of handles!\n" );
1709 HeapFree( header
->heap
, 0, ptr
);
1713 /* If virgin page, initialize it */
1714 if (header
->freeListFirst
[page
] == 0xffff)
1716 if ( !VirtualAlloc( (LPBYTE
)header
+ (page
<< 12),
1717 0x1000, MEM_COMMIT
, PAGE_READWRITE
) )
1719 WARN("Cannot grow handle table!\n" );
1720 HeapFree( header
->heap
, 0, ptr
);
1724 header
->limit
+= HTABLE_PAGESIZE
;
1726 header
->freeListFirst
[page
] = 0;
1727 header
->freeListLast
[page
] = HTABLE_PAGESIZE
- 4;
1728 header
->freeListSize
[page
] = HTABLE_PAGESIZE
/ 4;
1730 for (i
= 0; i
< HTABLE_PAGESIZE
; i
+= 4)
1731 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1733 if (page
< HTABLE_NPAGES
-1)
1734 header
->freeListFirst
[page
+1] = 0xffff;
1737 /* Allocate handle slot from page */
1738 handle
= (LPDWORD
)((LPBYTE
)header
+ header
->freeListFirst
[page
]);
1739 if (--header
->freeListSize
[page
] == 0)
1740 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = 0;
1742 header
->freeListFirst
[page
] = *handle
;
1744 /* Store 32-bit offset in handle slot */
1745 *handle
= ptr
- header
->base
;
1749 handle
= (LPDWORD
)ptr
;
1754 /* Convert handle to requested output type */
1755 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
1759 /***********************************************************************
1760 * Local32ReAlloc (KERNEL.210)
1762 DWORD WINAPI
Local32ReAlloc16( HANDLE heap
, DWORD addr
, INT16 type
,
1763 DWORD size
, DWORD flags
)
1765 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1770 return Local32Alloc16( heap
, size
, type
, flags
);
1772 /* Retrieve handle and pointer */
1773 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
1774 if (!handle
) return FALSE
;
1776 /* Reallocate memory block */
1777 ptr
= HeapReAlloc( header
->heap
,
1778 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0,
1784 *handle
= ptr
- header
->base
;
1786 handle
= (LPDWORD
)ptr
;
1788 /* Convert handle to requested output type */
1789 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
1793 /***********************************************************************
1794 * Local32Free (KERNEL.211)
1796 BOOL WINAPI
Local32Free16( HANDLE heap
, DWORD addr
, INT16 type
)
1798 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1802 /* Retrieve handle and pointer */
1803 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
1804 if (!handle
) return FALSE
;
1806 /* Free handle if necessary */
1809 int offset
= (LPBYTE
)handle
- (LPBYTE
)header
;
1810 int page
= offset
>> 12;
1812 /* Return handle slot to page free list */
1813 if (header
->freeListSize
[page
]++ == 0)
1814 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = offset
;
1816 *(LPDWORD
)((LPBYTE
)header
+ header
->freeListLast
[page
]) = offset
,
1817 header
->freeListLast
[page
] = offset
;
1821 /* Shrink handle table when possible */
1822 while (page
> 0 && header
->freeListSize
[page
] == HTABLE_PAGESIZE
/ 4)
1824 if ( VirtualFree( (LPBYTE
)header
+
1825 (header
->limit
& ~(HTABLE_PAGESIZE
-1)),
1826 HTABLE_PAGESIZE
, MEM_DECOMMIT
) )
1829 header
->limit
-= HTABLE_PAGESIZE
;
1830 header
->freeListFirst
[page
] = 0xffff;
1836 return HeapFree( header
->heap
, 0, ptr
);
1839 /***********************************************************************
1840 * Local32Translate (KERNEL.213)
1842 DWORD WINAPI
Local32Translate16( HANDLE heap
, DWORD addr
, INT16 type1
, INT16 type2
)
1844 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1848 Local32_ToHandle( header
, type1
, addr
, &handle
, &ptr
);
1849 if (!handle
) return 0;
1851 Local32_FromHandle( header
, type2
, &addr
, handle
, ptr
);
1855 /***********************************************************************
1856 * Local32Size (KERNEL.214)
1858 DWORD WINAPI
Local32Size16( HANDLE heap
, DWORD addr
, INT16 type
)
1860 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1864 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
1865 if (!handle
) return 0;
1867 return HeapSize( header
->heap
, 0, ptr
);
1870 /***********************************************************************
1871 * Local32ValidHandle (KERNEL.215)
1873 BOOL WINAPI
Local32ValidHandle16( HANDLE heap
, WORD addr
)
1875 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1879 Local32_ToHandle( header
, 0, addr
, &handle
, &ptr
);
1880 return handle
!= NULL
;
1883 /***********************************************************************
1884 * Local32GetSegment (KERNEL.229)
1886 WORD WINAPI
Local32GetSegment16( HANDLE heap
)
1888 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1889 return header
->segment
;
1892 /***********************************************************************
1895 static LOCAL32HEADER
*Local32_GetHeap( HGLOBAL16 handle
)
1897 WORD selector
= GlobalHandleToSel16( handle
);
1898 DWORD base
= GetSelectorBase( selector
);
1899 DWORD limit
= GetSelectorLimit16( selector
);
1901 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
1904 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
1905 return (LOCAL32HEADER
*)base
;
1910 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
1911 return (LOCAL32HEADER
*)base
;
1916 /***********************************************************************
1917 * Local32Info (KERNEL.444) (TOOLHELP.84)
1919 BOOL16 WINAPI
Local32Info16( LOCAL32INFO
*pLocal32Info
, HGLOBAL16 handle
)
1925 LOCAL32HEADER
*header
= Local32_GetHeap( handle
);
1926 if ( !header
) return FALSE
;
1928 if ( !pLocal32Info
|| pLocal32Info
->dwSize
< sizeof(LOCAL32INFO
) )
1931 heapPtr
= (SUBHEAP
*)HEAP_GetPtr( header
->heap
);
1932 pLocal32Info
->dwMemReserved
= heapPtr
->size
;
1933 pLocal32Info
->dwMemCommitted
= heapPtr
->commitSize
;
1934 pLocal32Info
->dwTotalFree
= 0L;
1935 pLocal32Info
->dwLargestFreeBlock
= 0L;
1937 /* Note: Local32 heaps always have only one subheap! */
1938 ptr
= (LPBYTE
)heapPtr
+ heapPtr
->headerSize
;
1939 while ( ptr
< (LPBYTE
)heapPtr
+ heapPtr
->size
)
1941 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1943 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
1944 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
1945 ptr
+= sizeof(*pArena
) + size
;
1947 pLocal32Info
->dwTotalFree
+= size
;
1948 if ( size
> pLocal32Info
->dwLargestFreeBlock
)
1949 pLocal32Info
->dwLargestFreeBlock
= size
;
1953 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
1954 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
1955 ptr
+= sizeof(*pArena
) + size
;
1959 pLocal32Info
->dwcFreeHandles
= 0;
1960 for ( i
= 0; i
< HTABLE_NPAGES
; i
++ )
1962 if ( header
->freeListFirst
[i
] == 0xffff ) break;
1963 pLocal32Info
->dwcFreeHandles
+= header
->freeListSize
[i
];
1965 pLocal32Info
->dwcFreeHandles
+= (HTABLE_NPAGES
- i
) * HTABLE_PAGESIZE
/4;
1970 /***********************************************************************
1971 * Local32First (KERNEL.445) (TOOLHELP.85)
1973 BOOL16 WINAPI
Local32First16( LOCAL32ENTRY
*pLocal32Entry
, HGLOBAL16 handle
)
1975 FIXME("(%p, %04X): stub!\n", pLocal32Entry
, handle
);
1979 /***********************************************************************
1980 * Local32Next (KERNEL.446) (TOOLHELP.86)
1982 BOOL16 WINAPI
Local32Next16( LOCAL32ENTRY
*pLocal32Entry
)
1984 FIXME("(%p): stub!\n", pLocal32Entry
);