4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
12 #include "selectors.h"
20 /* Note: the heap data structures are based on what Pietrek describes in his
21 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
22 * the same, but could be easily adapted if it turns out some programs
26 typedef struct tagARENA_INUSE
28 DWORD size
; /* Block size; must be the first field */
29 WORD threadId
; /* Allocating thread id */
30 WORD magic
; /* Magic number */
31 DWORD callerEIP
; /* EIP of caller upon allocation */
34 typedef struct tagARENA_FREE
36 DWORD size
; /* Block size; must be the first field */
37 WORD threadId
; /* Freeing thread id */
38 WORD magic
; /* Magic number */
39 struct tagARENA_FREE
*next
; /* Next free arena */
40 struct tagARENA_FREE
*prev
; /* Prev free arena */
43 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
44 #define ARENA_FLAG_PREV_FREE 0x00000002
45 #define ARENA_SIZE_MASK 0xfffffffc
46 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
47 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
49 #define ARENA_INUSE_FILLER 0x55
50 #define ARENA_FREE_FILLER 0xaa
52 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
54 /* Max size of the blocks on the free lists */
55 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
57 0x20, 0x80, 0x200, 0xffffffff
68 typedef struct tagSUBHEAP
70 DWORD size
; /* Size of the whole sub-heap */
71 DWORD commitSize
; /* Committed size of the sub-heap */
72 DWORD headerSize
; /* Size of the heap header */
73 struct tagSUBHEAP
*next
; /* Next sub-heap */
74 struct tagHEAP
*heap
; /* Main heap structure */
75 DWORD magic
; /* Magic number */
76 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
79 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
81 typedef struct tagHEAP
83 SUBHEAP subheap
; /* First sub-heap */
84 struct tagHEAP
*next
; /* Next heap for this process */
85 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
86 CRITICAL_SECTION critSection
; /* Critical section for serialization */
87 DWORD flags
; /* Heap flags */
88 DWORD magic
; /* Magic number */
91 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
93 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
94 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
96 HANDLE32 SystemHeap
= 0;
97 HANDLE32 SegptrHeap
= 0;
98 CRITICAL_SECTION
*HEAP_SystemLock
= NULL
;
101 /***********************************************************************
104 void HEAP_Dump( HEAP
*heap
)
110 DUMP( "Heap: %08lx\n", (DWORD
)heap
);
111 DUMP( "Next: %08lx Sub-heaps: %08lx",
112 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
113 subheap
= &heap
->subheap
;
114 while (subheap
->next
)
116 DUMP( " -> %08lx", (DWORD
)subheap
->next
);
117 subheap
= subheap
->next
;
120 DUMP( "\nFree lists:\n Block Stat Size Id\n" );
121 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
122 DUMP( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
123 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
124 heap
->freeList
[i
].arena
.threadId
,
125 (DWORD
)heap
->freeList
[i
].arena
.prev
,
126 (DWORD
)heap
->freeList
[i
].arena
.next
);
128 subheap
= &heap
->subheap
;
131 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
132 DUMP( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
133 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
135 DUMP( "\n Block Stat Size Id\n" );
136 ptr
= (char*)subheap
+ subheap
->headerSize
;
137 while (ptr
< (char *)subheap
+ subheap
->size
)
139 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
141 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
142 DUMP( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
143 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
144 pArena
->threadId
, (DWORD
)pArena
->prev
,
145 (DWORD
)pArena
->next
);
146 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
147 arenaSize
+= sizeof(ARENA_FREE
);
148 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
150 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
152 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
153 DUMP( "%08lx Used %08lx %04x back=%08lx EIP=%08lx\n",
154 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
155 pArena
->threadId
, *((DWORD
*)pArena
- 1),
157 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
158 arenaSize
+= sizeof(ARENA_INUSE
);
159 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
163 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
164 DUMP( "%08lx used %08lx %04x EIP=%08lx\n",
165 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
166 pArena
->threadId
, pArena
->callerEIP
);
167 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
168 arenaSize
+= sizeof(ARENA_INUSE
);
169 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
172 DUMP( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
173 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
174 arenaSize
, (arenaSize
* 100) / subheap
->size
);
175 subheap
= subheap
->next
;
180 /***********************************************************************
183 * Pointer to the heap
186 static HEAP
*HEAP_GetPtr(
187 HANDLE32 heap
/* [in] Handle to the heap */
189 HEAP
*heapPtr
= (HEAP
*)heap
;
190 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
192 WARN(heap
, "Invalid heap %08x!\n", heap
);
193 SetLastError( ERROR_INVALID_HANDLE
);
196 if (TRACE_ON(heap
) && !HeapValidate( heap
, 0, NULL
))
198 HEAP_Dump( heapPtr
);
200 SetLastError( ERROR_INVALID_HANDLE
);
207 /***********************************************************************
208 * HEAP_InsertFreeBlock
210 * Insert a free block into the free list.
212 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
214 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
215 while (pEntry
->size
< pArena
->size
) pEntry
++;
216 pArena
->size
|= ARENA_FLAG_FREE
;
217 pArena
->next
= pEntry
->arena
.next
;
218 pArena
->next
->prev
= pArena
;
219 pArena
->prev
= &pEntry
->arena
;
220 pEntry
->arena
.next
= pArena
;
224 /***********************************************************************
226 * Find the sub-heap containing a given address.
232 static SUBHEAP
*HEAP_FindSubHeap(
233 HEAP
*heap
, /* [in] Heap pointer */
234 LPCVOID ptr
/* [in] Address */
236 SUBHEAP
*sub
= &heap
->subheap
;
239 if (((char *)ptr
>= (char *)sub
) &&
240 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
247 /***********************************************************************
250 * Make sure the heap storage is committed up to (not including) ptr.
252 static BOOL32
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
254 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
255 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
256 if (size
> subheap
->size
) size
= subheap
->size
;
257 if (size
<= subheap
->commitSize
) return TRUE
;
258 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
259 size
- subheap
->commitSize
, MEM_COMMIT
,
260 PAGE_EXECUTE_READWRITE
))
262 WARN(heap
, "Could not commit %08lx bytes at %08lx for heap %08lx\n",
263 size
- subheap
->commitSize
,
264 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
265 (DWORD
)subheap
->heap
);
268 subheap
->commitSize
= size
;
273 /***********************************************************************
276 * If possible, decommit the heap storage from (including) 'ptr'.
278 static BOOL32
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
280 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
281 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
282 if (size
>= subheap
->commitSize
) return TRUE
;
283 if (!VirtualFree( (char *)subheap
+ size
,
284 subheap
->commitSize
- size
, MEM_DECOMMIT
))
286 WARN(heap
, "Could not decommit %08lx bytes at %08lx for heap %08lx\n",
287 subheap
->commitSize
- size
,
288 (DWORD
)((char *)subheap
+ size
),
289 (DWORD
)subheap
->heap
);
292 subheap
->commitSize
= size
;
297 /***********************************************************************
298 * HEAP_CreateFreeBlock
300 * Create a free block at a specified address. 'size' is the size of the
301 * whole block, including the new arena.
303 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
307 /* Create a free arena */
309 pFree
= (ARENA_FREE
*)ptr
;
310 pFree
->threadId
= GetCurrentTask();
311 pFree
->magic
= ARENA_FREE_MAGIC
;
313 /* If debugging, erase the freed block content */
317 char *pEnd
= (char *)ptr
+ size
;
318 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
319 pEnd
= (char *)subheap
+ subheap
->commitSize
;
320 if (pEnd
> (char *)(pFree
+ 1))
321 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
324 /* Check if next block is free also */
326 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
327 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
329 /* Remove the next arena from the free list */
330 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
331 pNext
->next
->prev
= pNext
->prev
;
332 pNext
->prev
->next
= pNext
->next
;
333 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
335 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
338 /* Set the next block PREV_FREE flag and pointer */
340 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
342 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
343 *pNext
|= ARENA_FLAG_PREV_FREE
;
344 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
347 /* Last, insert the new block into the free list */
349 pFree
->size
= size
- sizeof(*pFree
);
350 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
354 /***********************************************************************
355 * HEAP_MakeInUseBlockFree
357 * Turn an in-use block into a free block. Can also decommit the end of
358 * the heap, and possibly even free the sub-heap altogether.
360 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
363 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
365 /* Check if we can merge with previous block */
367 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
369 pFree
= *((ARENA_FREE
**)pArena
- 1);
370 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
371 /* Remove it from the free list */
372 pFree
->next
->prev
= pFree
->prev
;
373 pFree
->prev
->next
= pFree
->next
;
375 else pFree
= (ARENA_FREE
*)pArena
;
377 /* Create a free block */
379 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
380 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
381 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
382 return; /* Not the last block, so nothing more to do */
384 /* Free the whole sub-heap if it's empty and not the original one */
386 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
387 (subheap
!= &subheap
->heap
->subheap
))
389 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
390 /* Remove the free block from the list */
391 pFree
->next
->prev
= pFree
->prev
;
392 pFree
->prev
->next
= pFree
->next
;
393 /* Remove the subheap from the list */
394 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
395 if (pPrev
) pPrev
->next
= subheap
->next
;
396 /* Free the memory */
398 if (subheap
->selector
) FreeSelector( subheap
->selector
);
399 VirtualFree( subheap
, 0, MEM_RELEASE
);
403 /* Decommit the end of the heap */
405 HEAP_Decommit( subheap
, pFree
+ 1 );
409 /***********************************************************************
412 * Shrink an in-use block.
414 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
416 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
418 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
419 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
420 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
424 /* Turn off PREV_FREE flag in next block */
425 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
426 if (pNext
< (char *)subheap
+ subheap
->size
)
427 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
431 /***********************************************************************
434 static BOOL32
HEAP_InitSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
435 DWORD commitSize
, DWORD totalSize
)
437 SUBHEAP
*subheap
= (SUBHEAP
*)address
;
439 FREE_LIST_ENTRY
*pEntry
;
444 if (!VirtualAlloc(address
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
446 WARN(heap
, "Could not commit %08lx bytes for sub-heap %08lx\n",
447 commitSize
, (DWORD
)address
);
451 /* Allocate a selector if needed */
453 if (flags
& HEAP_WINE_SEGPTR
)
455 selector
= SELECTOR_AllocBlock( address
, totalSize
,
456 (flags
& HEAP_WINE_CODESEG
) ? SEGMENT_CODE
: SEGMENT_DATA
,
457 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
460 WARN(heap
, "Could not allocate selector\n" );
465 /* Fill the sub-heap structure */
467 subheap
->heap
= heap
;
468 subheap
->selector
= selector
;
469 subheap
->size
= totalSize
;
470 subheap
->commitSize
= commitSize
;
471 subheap
->magic
= SUBHEAP_MAGIC
;
473 if ( subheap
!= (SUBHEAP
*)heap
)
475 /* If this is a secondary subheap, insert it into list */
477 subheap
->headerSize
= sizeof(SUBHEAP
);
478 subheap
->next
= heap
->subheap
.next
;
479 heap
->subheap
.next
= subheap
;
483 /* If this is a primary subheap, initialize main heap */
485 subheap
->headerSize
= sizeof(HEAP
);
486 subheap
->next
= NULL
;
489 heap
->magic
= HEAP_MAGIC
;
491 /* Build the free lists */
493 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
495 pEntry
->size
= HEAP_freeListSizes
[i
];
496 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
497 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
498 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
499 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
500 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
501 pEntry
->arena
.threadId
= 0;
502 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
505 /* Initialize critical section */
507 InitializeCriticalSection( &heap
->critSection
);
508 if (!SystemHeap
) HEAP_SystemLock
= &heap
->critSection
;
511 /* Create the first free block */
513 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
+ subheap
->headerSize
,
514 subheap
->size
- subheap
->headerSize
);
519 /***********************************************************************
522 * Create a sub-heap of the given size.
523 * If heap == NULL, creates a main heap.
525 static SUBHEAP
*HEAP_CreateSubHeap( HEAP
*heap
, DWORD flags
,
526 DWORD commitSize
, DWORD totalSize
)
530 /* Round-up sizes on a 64K boundary */
532 if (flags
& HEAP_WINE_SEGPTR
)
534 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
538 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
539 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
540 if (!commitSize
) commitSize
= 0x10000;
541 if (totalSize
< commitSize
) totalSize
= commitSize
;
544 /* Allocate the memory block */
546 if (!(address
= VirtualAlloc( NULL
, totalSize
,
547 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
549 WARN(heap
, "Could not VirtualAlloc %08lx bytes\n",
554 /* Initialize subheap */
556 if (!HEAP_InitSubHeap( heap
? heap
: (HEAP
*)address
,
557 address
, flags
, commitSize
, totalSize
))
559 VirtualFree( address
, 0, MEM_RELEASE
);
563 return (SUBHEAP
*)address
;
567 /***********************************************************************
570 * Find a free block at least as large as the requested size, and make sure
571 * the requested size is committed.
573 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
574 SUBHEAP
**ppSubHeap
)
578 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
580 /* Find a suitable free list, and in it find a block large enough */
582 while (pEntry
->size
< size
) pEntry
++;
583 pArena
= pEntry
->arena
.next
;
584 while (pArena
!= &heap
->freeList
[0].arena
)
586 if (pArena
->size
> size
)
588 subheap
= HEAP_FindSubHeap( heap
, pArena
);
589 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
590 + size
+ HEAP_MIN_BLOCK_SIZE
))
592 *ppSubHeap
= subheap
;
596 pArena
= pArena
->next
;
599 /* If no block was found, attempt to grow the heap */
601 if (!(heap
->flags
& HEAP_GROWABLE
))
603 WARN(heap
, "Not enough space in heap %08lx for %08lx bytes\n",
607 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
608 if (!(subheap
= HEAP_CreateSubHeap( heap
, heap
->flags
, size
,
609 MAX( HEAP_DEF_SIZE
, size
) )))
612 TRACE(heap
, "created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
613 (DWORD
)subheap
, size
, (DWORD
)heap
);
615 *ppSubHeap
= subheap
;
616 return (ARENA_FREE
*)(subheap
+ 1);
620 /***********************************************************************
621 * HEAP_IsValidArenaPtr
623 * Check that the pointer is inside the range possible for arenas.
625 static BOOL32
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
628 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
629 if (!subheap
) return FALSE
;
630 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
631 if (subheap
!= &heap
->subheap
) return FALSE
;
632 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
633 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
638 /***********************************************************************
639 * HEAP_ValidateFreeArena
641 static BOOL32
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
643 char *heapEnd
= (char *)subheap
+ subheap
->size
;
645 /* Check magic number */
646 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
648 WARN(heap
, "Heap %08lx: invalid free arena magic for %08lx\n",
649 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
652 /* Check size flags */
653 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
654 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
656 WARN(heap
, "Heap %08lx: bad flags %lx for free arena %08lx\n",
657 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
659 /* Check arena size */
660 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
662 WARN(heap
, "Heap %08lx: bad size %08lx for free arena %08lx\n",
663 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
666 /* Check that next pointer is valid */
667 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
669 WARN(heap
, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
670 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
673 /* Check that next arena is free */
674 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
675 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
677 WARN(heap
, "Heap %08lx: next arena %08lx invalid for %08lx\n",
678 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
681 /* Check that prev pointer is valid */
682 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
684 WARN(heap
, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
685 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
688 /* Check that prev arena is free */
689 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
690 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
692 WARN(heap
, "Heap %08lx: prev arena %08lx invalid for %08lx\n",
693 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
696 /* Check that next block has PREV_FREE flag */
697 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
699 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
700 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
702 WARN(heap
, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
703 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
706 /* Check next block back pointer */
707 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
708 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
710 WARN(heap
, "Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
711 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
712 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
720 /***********************************************************************
721 * HEAP_ValidateInUseArena
723 static BOOL32
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
725 char *heapEnd
= (char *)subheap
+ subheap
->size
;
727 /* Check magic number */
728 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
730 WARN(heap
, "Heap %08lx: invalid in-use arena magic for %08lx\n",
731 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
734 /* Check size flags */
735 if (pArena
->size
& ARENA_FLAG_FREE
)
737 WARN(heap
, "Heap %08lx: bad flags %lx for in-use arena %08lx\n",
738 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
740 /* Check arena size */
741 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
743 WARN(heap
, "Heap %08lx: bad size %08lx for in-use arena %08lx\n",
744 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
747 /* Check next arena PREV_FREE flag */
748 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
749 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
751 WARN(heap
, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
752 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
755 /* Check prev free arena */
756 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
758 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
759 /* Check prev pointer */
760 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
762 WARN(heap
, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
763 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
766 /* Check that prev arena is free */
767 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
768 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
770 WARN(heap
, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
771 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
774 /* Check that prev arena is really the previous block */
775 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
777 WARN(heap
, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
778 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
786 /***********************************************************************
788 * Checks whether the pointer points to a block inside a given heap.
791 * Should this return BOOL32?
797 int HEAP_IsInsideHeap(
798 HANDLE32 heap
, /* [in] Heap */
799 DWORD flags
, /* [in] Flags */
800 LPCVOID ptr
/* [in] Pointer */
802 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
806 /* Validate the parameters */
808 if (!heapPtr
) return 0;
809 flags
|= heapPtr
->flags
;
810 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
811 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
812 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
813 + sizeof(ARENA_INUSE
))));
814 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
819 /***********************************************************************
822 * Transform a linear pointer into a SEGPTR. The pointer must have been
823 * allocated from a HEAP_WINE_SEGPTR heap.
825 SEGPTR
HEAP_GetSegptr( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
827 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
831 /* Validate the parameters */
833 if (!heapPtr
) return 0;
834 flags
|= heapPtr
->flags
;
835 if (!(flags
& HEAP_WINE_SEGPTR
))
837 WARN(heap
, "Heap %08x is not a SEGPTR heap\n",
841 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
843 /* Get the subheap */
845 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
847 WARN(heap
, "%p is not inside heap %08x\n",
849 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
853 /* Build the SEGPTR */
855 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
856 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
861 /***********************************************************************
862 * HeapCreate (KERNEL32.336)
864 * Handle of heap: Success
867 HANDLE32 WINAPI
HeapCreate(
868 DWORD flags
, /* [in] Heap allocation flag */
869 DWORD initialSize
, /* [in] Initial heap size */
870 DWORD maxSize
/* [in] Maximum heap size */
874 /* Allocate the heap block */
878 maxSize
= HEAP_DEF_SIZE
;
879 flags
|= HEAP_GROWABLE
;
881 if (!(subheap
= HEAP_CreateSubHeap( NULL
, flags
, initialSize
, maxSize
)))
883 SetLastError( ERROR_OUTOFMEMORY
);
887 return (HANDLE32
)subheap
;
890 /***********************************************************************
891 * HeapDestroy (KERNEL32.337)
896 BOOL32 WINAPI
HeapDestroy(
897 HANDLE32 heap
/* [in] Handle of heap */
899 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
902 TRACE(heap
, "%08x\n", heap
);
903 if (!heapPtr
) return FALSE
;
905 DeleteCriticalSection( &heapPtr
->critSection
);
906 subheap
= &heapPtr
->subheap
;
909 SUBHEAP
*next
= subheap
->next
;
910 if (subheap
->selector
) FreeSelector( subheap
->selector
);
911 VirtualFree( subheap
, 0, MEM_RELEASE
);
918 /***********************************************************************
919 * HeapAlloc (KERNEL32.334)
921 * Pointer to allocated memory block
924 LPVOID WINAPI
HeapAlloc(
925 HANDLE32 heap
, /* [in] Handle of private heap block */
926 DWORD flags
, /* [in] Heap allocation control flags */
927 DWORD size
/* [in] Number of bytes to allocate */
932 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
934 /* Validate the parameters */
936 if (!heapPtr
) return NULL
;
937 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
938 flags
|= heapPtr
->flags
;
939 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
940 size
= (size
+ 3) & ~3;
941 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
943 /* Locate a suitable free block */
945 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
947 TRACE(heap
, "(%08x,%08lx,%08lx): returning NULL\n",
949 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
950 SetLastError( ERROR_COMMITMENT_LIMIT
);
954 /* Remove the arena from the free list */
956 pArena
->next
->prev
= pArena
->prev
;
957 pArena
->prev
->next
= pArena
->next
;
959 /* Build the in-use arena */
961 pInUse
= (ARENA_INUSE
*)pArena
;
962 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
963 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
964 pInUse
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
965 pInUse
->threadId
= GetCurrentTask();
966 pInUse
->magic
= ARENA_INUSE_MAGIC
;
968 /* Shrink the block */
970 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
972 if (flags
& HEAP_ZERO_MEMORY
) memset( pInUse
+ 1, 0, size
);
973 else if (TRACE_ON(heap
)) memset( pInUse
+ 1, ARENA_INUSE_FILLER
, size
);
975 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
977 TRACE(heap
, "(%08x,%08lx,%08lx): returning %08lx\n",
978 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
979 return (LPVOID
)(pInUse
+ 1);
983 /***********************************************************************
984 * HeapFree (KERNEL32.338)
989 BOOL32 WINAPI
HeapFree(
990 HANDLE32 heap
, /* [in] Handle of heap */
991 DWORD flags
, /* [in] Heap freeing flags */
992 LPVOID ptr
/* [in] Address of memory to free */
996 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
998 /* Validate the parameters */
1000 if (!heapPtr
) return FALSE
;
1001 flags
&= HEAP_NO_SERIALIZE
;
1002 flags
|= heapPtr
->flags
;
1003 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1006 WARN(heap
, "(%08x,%08lx,%08lx): asked to free NULL\n",
1007 heap
, flags
, (DWORD
)ptr
);
1009 if (!ptr
|| !HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1011 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1012 SetLastError( ERROR_INVALID_PARAMETER
);
1013 TRACE(heap
, "(%08x,%08lx,%08lx): returning FALSE\n",
1014 heap
, flags
, (DWORD
)ptr
);
1018 /* Turn the block into a free block */
1020 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1021 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
1022 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
1024 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1025 /* SetLastError( 0 ); */
1027 TRACE(heap
, "(%08x,%08lx,%08lx): returning TRUE\n",
1028 heap
, flags
, (DWORD
)ptr
);
1033 /***********************************************************************
1034 * HeapReAlloc (KERNEL32.340)
1036 * Pointer to reallocated memory block
1039 LPVOID WINAPI
HeapReAlloc(
1040 HANDLE32 heap
, /* [in] Handle of heap block */
1041 DWORD flags
, /* [in] Heap reallocation flags */
1042 LPVOID ptr
, /* [in] Address of memory to reallocate */
1043 DWORD size
/* [in] Number of bytes to reallocate */
1045 ARENA_INUSE
*pArena
;
1050 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
1051 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1053 /* Validate the parameters */
1055 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1056 HEAP_REALLOC_IN_PLACE_ONLY
;
1057 flags
|= heapPtr
->flags
;
1058 size
= (size
+ 3) & ~3;
1059 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1061 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1062 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1064 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1065 SetLastError( ERROR_INVALID_PARAMETER
);
1066 TRACE(heap
, "(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1067 heap
, flags
, (DWORD
)ptr
, size
);
1071 /* Check if we need to grow the block */
1073 pArena
= (ARENA_INUSE
*)ptr
- 1;
1074 pArena
->threadId
= GetCurrentTask();
1075 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1076 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1079 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1080 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1081 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1082 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1084 /* The next block is free and large enough */
1085 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1086 pFree
->next
->prev
= pFree
->prev
;
1087 pFree
->prev
->next
= pFree
->next
;
1088 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1089 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1090 + size
+ HEAP_MIN_BLOCK_SIZE
))
1092 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1093 SetLastError( ERROR_OUTOFMEMORY
);
1096 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1098 else /* Do it the hard way */
1101 ARENA_INUSE
*pInUse
;
1102 SUBHEAP
*newsubheap
;
1104 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1105 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1107 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1108 SetLastError( ERROR_OUTOFMEMORY
);
1112 /* Build the in-use arena */
1114 pNew
->next
->prev
= pNew
->prev
;
1115 pNew
->prev
->next
= pNew
->next
;
1116 pInUse
= (ARENA_INUSE
*)pNew
;
1117 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1118 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1119 pInUse
->threadId
= GetCurrentTask();
1120 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1121 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1122 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1124 /* Free the previous block */
1126 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1127 subheap
= newsubheap
;
1131 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1133 /* Clear the extra bytes if needed */
1137 if (flags
& HEAP_ZERO_MEMORY
)
1138 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1139 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1140 else if (TRACE_ON(heap
))
1141 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1142 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1145 /* Return the new arena */
1147 pArena
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
1148 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1150 TRACE(heap
, "(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1151 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1152 return (LPVOID
)(pArena
+ 1);
1156 /***********************************************************************
1157 * HeapCompact (KERNEL32.335)
1159 DWORD WINAPI
HeapCompact( HANDLE32 heap
, DWORD flags
)
1165 /***********************************************************************
1166 * HeapLock (KERNEL32.339)
1167 * Attempts to acquire the critical section object for a specified heap.
1173 BOOL32 WINAPI
HeapLock(
1174 HANDLE32 heap
/* [in] Handle of heap to lock for exclusive access */
1176 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1177 if (!heapPtr
) return FALSE
;
1178 EnterCriticalSection( &heapPtr
->critSection
);
1183 /***********************************************************************
1184 * HeapUnlock (KERNEL32.342)
1185 * Releases ownership of the critical section object.
1191 BOOL32 WINAPI
HeapUnlock(
1192 HANDLE32 heap
/* [in] Handle to the heap to unlock */
1194 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1195 if (!heapPtr
) return FALSE
;
1196 LeaveCriticalSection( &heapPtr
->critSection
);
1201 /***********************************************************************
1202 * HeapSize (KERNEL32.341)
1204 * Size in bytes of allocated memory
1207 DWORD WINAPI
HeapSize(
1208 HANDLE32 heap
, /* [in] Handle of heap */
1209 DWORD flags
, /* [in] Heap size control flags */
1210 LPVOID ptr
/* [in] Address of memory to return size for */
1213 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1215 if (!heapPtr
) return FALSE
;
1216 flags
&= HEAP_NO_SERIALIZE
;
1217 flags
|= heapPtr
->flags
;
1218 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1219 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1221 SetLastError( ERROR_INVALID_PARAMETER
);
1226 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1227 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1229 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1231 TRACE(heap
, "(%08x,%08lx,%08lx): returning %08lx\n",
1232 heap
, flags
, (DWORD
)ptr
, ret
);
1237 /***********************************************************************
1238 * HeapValidate (KERNEL32.343)
1239 * Validates a specified heap.
1248 BOOL32 WINAPI
HeapValidate(
1249 HANDLE32 heap
, /* [in] Handle to the heap */
1250 DWORD flags
, /* [in] Bit flags that control access during operation */
1251 LPCVOID block
/* [in] Optional pointer to memory block to validate */
1254 HEAP
*heapPtr
= (HEAP
*)(heap
);
1256 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
1258 WARN(heap
, "Invalid heap %08x!\n", heap
);
1264 /* Only check this single memory block */
1265 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
1266 ((char *)block
< (char *)subheap
+ subheap
->headerSize
1267 + sizeof(ARENA_INUSE
)))
1269 WARN(heap
, "Heap %08lx: block %08lx is not inside heap\n",
1270 (DWORD
)heap
, (DWORD
)block
);
1273 return HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1 );
1276 subheap
= &heapPtr
->subheap
;
1279 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1280 while (ptr
< (char *)subheap
+ subheap
->size
)
1282 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1284 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
))
1286 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1290 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
))
1292 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1295 subheap
= subheap
->next
;
1301 /***********************************************************************
1302 * HeapWalk (KERNEL32.344)
1303 * Enumerates the memory blocks in a specified heap.
1309 BOOL32 WINAPI
HeapWalk(
1310 HANDLE32 heap
, /* [in] Handle to heap to enumerate */
1311 LPPROCESS_HEAP_ENTRY
*entry
/* [out] Pointer to structure of enumeration info */
1313 FIXME(heap
, "(%08x): stub.\n", heap
);
1318 /***********************************************************************
1321 * Same as HeapAlloc(), but die on failure.
1323 LPVOID
HEAP_xalloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
1325 LPVOID p
= HeapAlloc( heap
, flags
, size
);
1328 MSG("Virtual memory exhausted.\n" );
1335 /***********************************************************************
1338 LPSTR
HEAP_strdupA( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1340 LPSTR p
= HEAP_xalloc( heap
, flags
, strlen(str
) + 1 );
1346 /***********************************************************************
1349 LPWSTR
HEAP_strdupW( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1351 INT32 len
= lstrlen32W(str
) + 1;
1352 LPWSTR p
= HEAP_xalloc( heap
, flags
, len
* sizeof(WCHAR
) );
1353 lstrcpy32W( p
, str
);
1358 /***********************************************************************
1361 LPWSTR
HEAP_strdupAtoW( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1365 if (!str
) return NULL
;
1366 ret
= HEAP_xalloc( heap
, flags
, (strlen(str
)+1) * sizeof(WCHAR
) );
1367 lstrcpyAtoW( ret
, str
);
1372 /***********************************************************************
1375 LPSTR
HEAP_strdupWtoA( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1379 if (!str
) return NULL
;
1380 ret
= HEAP_xalloc( heap
, flags
, lstrlen32W(str
) + 1 );
1381 lstrcpyWtoA( ret
, str
);
1387 /***********************************************************************
1388 * 32-bit local heap functions (Win95; undocumented)
1391 #define HTABLE_SIZE 0x10000
1392 #define HTABLE_PAGESIZE 0x1000
1393 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
1396 typedef struct _LOCAL32HEADER
1398 WORD freeListFirst
[HTABLE_NPAGES
];
1399 WORD freeListSize
[HTABLE_NPAGES
];
1400 WORD freeListLast
[HTABLE_NPAGES
];
1402 DWORD selectorTableOffset
;
1403 WORD selectorTableSize
;
1418 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
1420 /***********************************************************************
1421 * Local32Init (KERNEL.208)
1423 HANDLE32 WINAPI
Local32Init( WORD segment
, DWORD tableSize
,
1424 DWORD heapSize
, DWORD flags
)
1426 DWORD totSize
, segSize
= 0;
1428 LOCAL32HEADER
*header
;
1430 WORD
*selectorTable
;
1431 WORD selectorEven
, selectorOdd
;
1434 /* Determine new heap size */
1437 if ( (segSize
= GetSelectorLimit( segment
)) == 0 )
1442 if ( heapSize
== -1L )
1443 heapSize
= 1024L*1024L; /* FIXME */
1445 heapSize
= (heapSize
+ 0xffff) & 0xffff0000;
1446 segSize
= (segSize
+ 0x0fff) & 0xfffff000;
1447 totSize
= segSize
+ HTABLE_SIZE
+ heapSize
;
1450 /* Allocate memory and initialize heap */
1452 if ( !(base
= VirtualAlloc( NULL
, totSize
, MEM_RESERVE
, PAGE_READWRITE
)) )
1455 if ( !VirtualAlloc( base
, segSize
+ HTABLE_PAGESIZE
,
1456 MEM_COMMIT
, PAGE_READWRITE
) )
1458 VirtualFree( base
, 0, MEM_RELEASE
);
1462 heap
= (HEAP
*)(base
+ segSize
+ HTABLE_SIZE
);
1463 if ( !HEAP_InitSubHeap( heap
, (LPVOID
)heap
, 0, 0x10000, heapSize
) )
1465 VirtualFree( base
, 0, MEM_RELEASE
);
1470 /* Set up header and handle table */
1472 header
= (LOCAL32HEADER
*)(base
+ segSize
);
1473 header
->base
= base
;
1474 header
->limit
= HTABLE_PAGESIZE
-1;
1476 header
->magic
= LOCAL32_MAGIC
;
1477 header
->heap
= (HANDLE32
)heap
;
1479 header
->freeListFirst
[0] = sizeof(LOCAL32HEADER
);
1480 header
->freeListLast
[0] = HTABLE_PAGESIZE
- 4;
1481 header
->freeListSize
[0] = (HTABLE_PAGESIZE
- sizeof(LOCAL32HEADER
)) / 4;
1483 for (i
= header
->freeListFirst
[0]; i
< header
->freeListLast
[0]; i
+= 4)
1484 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1486 header
->freeListFirst
[1] = 0xffff;
1489 /* Set up selector table */
1491 nrBlocks
= (totSize
+ 0x7fff) >> 15;
1492 selectorTable
= (LPWORD
) HeapAlloc( header
->heap
, 0, nrBlocks
* 2 );
1493 selectorEven
= SELECTOR_AllocBlock( base
, totSize
,
1494 SEGMENT_DATA
, FALSE
, FALSE
);
1495 selectorOdd
= SELECTOR_AllocBlock( base
+ 0x8000, totSize
- 0x8000,
1496 SEGMENT_DATA
, FALSE
, FALSE
);
1498 if ( !selectorTable
|| !selectorEven
|| !selectorOdd
)
1500 if ( selectorTable
) HeapFree( header
->heap
, 0, selectorTable
);
1501 if ( selectorEven
) SELECTOR_FreeBlock( selectorEven
, totSize
>> 16 );
1502 if ( selectorOdd
) SELECTOR_FreeBlock( selectorOdd
, (totSize
-0x8000) >> 16 );
1503 HeapDestroy( header
->heap
);
1504 VirtualFree( base
, 0, MEM_RELEASE
);
1508 header
->selectorTableOffset
= (LPBYTE
)selectorTable
- header
->base
;
1509 header
->selectorTableSize
= nrBlocks
* 4; /* ??? Win95 does it this way! */
1510 header
->selectorDelta
= selectorEven
- selectorOdd
;
1511 header
->segment
= segment
? segment
: selectorEven
;
1513 for (i
= 0; i
< nrBlocks
; i
++)
1514 selectorTable
[i
] = (i
& 1)? selectorOdd
+ ((i
>> 1) << __AHSHIFT
)
1515 : selectorEven
+ ((i
>> 1) << __AHSHIFT
);
1517 /* Move old segment */
1521 /* FIXME: This is somewhat ugly and relies on implementation
1522 details about 16-bit global memory handles ... */
1524 LPBYTE oldBase
= (LPBYTE
)GetSelectorBase( segment
);
1525 memcpy( base
, oldBase
, segSize
);
1526 GLOBAL_MoveBlock( segment
, base
, totSize
);
1527 HeapFree( SystemHeap
, 0, oldBase
);
1530 return (HANDLE32
)header
;
1533 /***********************************************************************
1534 * Local32_SearchHandle
1536 static LPDWORD
Local32_SearchHandle( LOCAL32HEADER
*header
, DWORD addr
)
1540 for ( handle
= (LPDWORD
)((LPBYTE
)header
+ sizeof(LOCAL32HEADER
));
1541 handle
< (LPDWORD
)((LPBYTE
)header
+ header
->limit
);
1544 if (*handle
== addr
)
1551 /***********************************************************************
1554 static VOID
Local32_ToHandle( LOCAL32HEADER
*header
, INT16 type
,
1555 DWORD addr
, LPDWORD
*handle
, LPBYTE
*ptr
)
1562 case -2: /* 16:16 pointer, no handles */
1563 *ptr
= PTR_SEG_TO_LIN( addr
);
1564 *handle
= (LPDWORD
)*ptr
;
1567 case -1: /* 32-bit offset, no handles */
1568 *ptr
= header
->base
+ addr
;
1569 *handle
= (LPDWORD
)*ptr
;
1572 case 0: /* handle */
1573 if ( addr
>= sizeof(LOCAL32HEADER
)
1574 && addr
< header
->limit
&& !(addr
& 3)
1575 && *(LPDWORD
)((LPBYTE
)header
+ addr
) >= HTABLE_SIZE
)
1577 *handle
= (LPDWORD
)((LPBYTE
)header
+ addr
);
1578 *ptr
= header
->base
+ **handle
;
1582 case 1: /* 16:16 pointer */
1583 *ptr
= PTR_SEG_TO_LIN( addr
);
1584 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1587 case 2: /* 32-bit offset */
1588 *ptr
= header
->base
+ addr
;
1589 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1594 /***********************************************************************
1595 * Local32_FromHandle
1597 static VOID
Local32_FromHandle( LOCAL32HEADER
*header
, INT16 type
,
1598 DWORD
*addr
, LPDWORD handle
, LPBYTE ptr
)
1602 case -2: /* 16:16 pointer */
1605 WORD
*selTable
= (LPWORD
)(header
->base
+ header
->selectorTableOffset
);
1606 DWORD offset
= (LPBYTE
)ptr
- header
->base
;
1607 *addr
= MAKELONG( offset
& 0x7fff, selTable
[offset
>> 15] );
1611 case -1: /* 32-bit offset */
1613 *addr
= ptr
- header
->base
;
1616 case 0: /* handle */
1617 *addr
= (LPBYTE
)handle
- (LPBYTE
)header
;
1622 /***********************************************************************
1623 * Local32Alloc (KERNEL.209)
1625 DWORD WINAPI
Local32Alloc( HANDLE32 heap
, DWORD size
, INT16 type
, DWORD flags
)
1627 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1632 /* Allocate memory */
1633 ptr
= HeapAlloc( header
->heap
,
1634 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0, size
);
1638 /* Allocate handle if requested */
1643 /* Find first page of handle table with free slots */
1644 for (page
= 0; page
< HTABLE_NPAGES
; page
++)
1645 if (header
->freeListFirst
[page
] != 0)
1647 if (page
== HTABLE_NPAGES
)
1649 WARN( heap
, "Out of handles!\n" );
1650 HeapFree( header
->heap
, 0, ptr
);
1654 /* If virgin page, initialize it */
1655 if (header
->freeListFirst
[page
] == 0xffff)
1657 if ( !VirtualAlloc( (LPBYTE
)header
+ (page
<< 12),
1658 0x1000, MEM_COMMIT
, PAGE_READWRITE
) )
1660 WARN( heap
, "Cannot grow handle table!\n" );
1661 HeapFree( header
->heap
, 0, ptr
);
1665 header
->limit
+= HTABLE_PAGESIZE
;
1667 header
->freeListFirst
[page
] = 0;
1668 header
->freeListLast
[page
] = HTABLE_PAGESIZE
- 4;
1669 header
->freeListSize
[page
] = HTABLE_PAGESIZE
/ 4;
1671 for (i
= 0; i
< HTABLE_PAGESIZE
; i
+= 4)
1672 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1675 header
->freeListFirst
[page
+1] = 0xffff;
1678 /* Allocate handle slot from page */
1679 handle
= (LPDWORD
)((LPBYTE
)header
+ header
->freeListFirst
[page
]);
1680 if (--header
->freeListSize
[page
] == 0)
1681 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = 0;
1683 header
->freeListFirst
[page
] = *handle
;
1685 /* Store 32-bit offset in handle slot */
1686 *handle
= ptr
- header
->base
;
1690 handle
= (LPDWORD
)ptr
;
1695 /* Convert handle to requested output type */
1696 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
1700 /***********************************************************************
1701 * Local32ReAlloc (KERNEL.210)
1703 DWORD WINAPI
Local32ReAlloc( HANDLE32 heap
, DWORD addr
, INT16 type
,
1704 DWORD size
, DWORD flags
)
1706 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1711 return Local32Alloc( heap
, size
, type
, flags
);
1713 /* Retrieve handle and pointer */
1714 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
1715 if (!handle
) return FALSE
;
1717 /* Reallocate memory block */
1718 ptr
= HeapReAlloc( header
->heap
,
1719 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0,
1725 *handle
= ptr
- header
->base
;
1727 handle
= (LPDWORD
)ptr
;
1729 /* Convert handle to requested output type */
1730 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
1734 /***********************************************************************
1735 * Local32Free (KERNEL.211)
1737 BOOL32 WINAPI
Local32Free( HANDLE32 heap
, DWORD addr
, INT16 type
)
1739 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1743 /* Retrieve handle and pointer */
1744 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
1745 if (!handle
) return FALSE
;
1747 /* Free handle if necessary */
1750 int offset
= (LPBYTE
)handle
- (LPBYTE
)header
;
1751 int page
= offset
>> 12;
1753 /* Return handle slot to page free list */
1754 if (header
->freeListSize
[page
]++ == 0)
1755 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = offset
;
1757 *(LPDWORD
)((LPBYTE
)header
+ header
->freeListLast
[page
]) = offset
,
1758 header
->freeListLast
[page
] = *handle
;
1762 /* Shrink handle table when possible */
1763 while (page
> 0 && header
->freeListSize
[page
] == HTABLE_PAGESIZE
/ 4)
1765 if ( VirtualFree( (LPBYTE
)header
+
1766 (header
->limit
& ~(HTABLE_PAGESIZE
-1)),
1767 HTABLE_PAGESIZE
, MEM_DECOMMIT
) )
1770 header
->limit
-= HTABLE_PAGESIZE
;
1771 header
->freeListFirst
[page
] = -1;
1777 return HeapFree( header
->heap
, 0, ptr
);
1780 /***********************************************************************
1781 * Local32Translate (KERNEL.213)
1783 DWORD WINAPI
Local32Translate( HANDLE32 heap
, DWORD addr
, INT16 type1
, INT16 type2
)
1785 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1789 Local32_ToHandle( header
, type1
, addr
, &handle
, &ptr
);
1790 if (!handle
) return 0;
1792 Local32_FromHandle( header
, type2
, &addr
, handle
, ptr
);
1796 /***********************************************************************
1797 * Local32Size (KERNEL.214)
1799 DWORD WINAPI
Local32Size( HANDLE32 heap
, DWORD addr
, INT16 type
)
1801 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1805 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
1806 if (!handle
) return 0;
1808 return HeapSize( header
->heap
, 0, ptr
);
1811 /***********************************************************************
1812 * Local32ValidHandle (KERNEL.215)
1814 BOOL32 WINAPI
Local32ValidHandle( HANDLE32 heap
, WORD addr
)
1816 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1820 Local32_ToHandle( header
, 0, addr
, &handle
, &ptr
);
1821 return handle
!= NULL
;
1824 /***********************************************************************
1825 * Local32GetSegment (KERNEL.229)
1827 WORD WINAPI
Local32GetSegment( HANDLE32 heap
)
1829 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1830 return header
->segment
;