4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
12 #include "wine/winbase16.h"
13 #include "wine/winestring.h"
14 #include "selectors.h"
21 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(heap
);
26 /* Note: the heap data structures are based on what Pietrek describes in his
27 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
28 * the same, but could be easily adapted if it turns out some programs
32 typedef struct tagARENA_INUSE
34 DWORD size
; /* Block size; must be the first field */
35 WORD threadId
; /* Allocating thread id */
36 WORD magic
; /* Magic number */
37 void *callerEIP
; /* EIP of caller upon allocation */
40 typedef struct tagARENA_FREE
42 DWORD size
; /* Block size; must be the first field */
43 WORD threadId
; /* Freeing thread id */
44 WORD magic
; /* Magic number */
45 struct tagARENA_FREE
*next
; /* Next free arena */
46 struct tagARENA_FREE
*prev
; /* Prev free arena */
49 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
50 #define ARENA_FLAG_PREV_FREE 0x00000002
51 #define ARENA_SIZE_MASK 0xfffffffc
52 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
53 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
55 #define ARENA_INUSE_FILLER 0x55
56 #define ARENA_FREE_FILLER 0xaa
58 #define QUIET 1 /* Suppress messages */
59 #define NOISY 0 /* Report all errors */
61 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
63 /* Max size of the blocks on the free lists */
64 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
66 0x20, 0x80, 0x200, 0xffffffff
77 typedef struct tagSUBHEAP
79 DWORD size
; /* Size of the whole sub-heap */
80 DWORD commitSize
; /* Committed size of the sub-heap */
81 DWORD headerSize
; /* Size of the heap header */
82 struct tagSUBHEAP
*next
; /* Next sub-heap */
83 struct tagHEAP
*heap
; /* Main heap structure */
84 DWORD magic
; /* Magic number */
85 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
88 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
90 typedef struct tagHEAP
92 SUBHEAP subheap
; /* First sub-heap */
93 struct tagHEAP
*next
; /* Next heap for this process */
94 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
95 CRITICAL_SECTION critSection
; /* Critical section for serialization */
96 DWORD flags
; /* Heap flags */
97 DWORD magic
; /* Magic number */
98 void *private; /* Private pointer for the user of the heap */
101 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
103 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
104 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
105 #define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
107 HANDLE SystemHeap
= 0;
108 HANDLE SegptrHeap
= 0;
110 SYSTEM_HEAP_DESCR
*SystemHeapDescr
= 0;
112 static HEAP
*processHeap
; /* main process heap */
113 static HEAP
*firstHeap
; /* head of secondary heaps list */
115 /* address where we try to map the system heap */
116 #define SYSTEM_HEAP_BASE ((void*)0x65430000)
118 static BOOL
HEAP_IsRealArena( HANDLE heap
, DWORD flags
, LPCVOID block
, BOOL quiet
);
121 #define GET_EIP() (__builtin_return_address(0))
122 #define SET_EIP(ptr) ((ARENA_INUSE*)(ptr) - 1)->callerEIP = GET_EIP()
125 #define SET_EIP(ptr) /* nothing */
126 #endif /* __GNUC__ */
128 /***********************************************************************
131 void HEAP_Dump( HEAP
*heap
)
137 DPRINTF( "Heap: %08lx\n", (DWORD
)heap
);
138 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
139 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
140 subheap
= &heap
->subheap
;
141 while (subheap
->next
)
143 DPRINTF( " -> %08lx", (DWORD
)subheap
->next
);
144 subheap
= subheap
->next
;
147 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
148 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
149 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
150 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
151 heap
->freeList
[i
].arena
.threadId
,
152 (DWORD
)heap
->freeList
[i
].arena
.prev
,
153 (DWORD
)heap
->freeList
[i
].arena
.next
);
155 subheap
= &heap
->subheap
;
158 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
159 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
160 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
162 DPRINTF( "\n Block Stat Size Id\n" );
163 ptr
= (char*)subheap
+ subheap
->headerSize
;
164 while (ptr
< (char *)subheap
+ subheap
->size
)
166 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
168 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
169 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
170 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
171 pArena
->threadId
, (DWORD
)pArena
->prev
,
172 (DWORD
)pArena
->next
);
173 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
174 arenaSize
+= sizeof(ARENA_FREE
);
175 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
177 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
179 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
180 DPRINTF( "%08lx Used %08lx %04x back=%08lx EIP=%p\n",
181 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
182 pArena
->threadId
, *((DWORD
*)pArena
- 1),
184 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
185 arenaSize
+= sizeof(ARENA_INUSE
);
186 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
190 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
191 DPRINTF( "%08lx used %08lx %04x EIP=%p\n",
192 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
193 pArena
->threadId
, pArena
->callerEIP
);
194 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
195 arenaSize
+= sizeof(ARENA_INUSE
);
196 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
199 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
200 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
201 arenaSize
, (arenaSize
* 100) / subheap
->size
);
202 subheap
= subheap
->next
;
207 /***********************************************************************
210 * Pointer to the heap
213 static HEAP
*HEAP_GetPtr(
214 HANDLE heap
/* [in] Handle to the heap */
216 HEAP
*heapPtr
= (HEAP
*)heap
;
217 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
219 ERR("Invalid heap %08x!\n", heap
);
220 SetLastError( ERROR_INVALID_HANDLE
);
223 if (TRACE_ON(heap
) && !HEAP_IsRealArena( heap
, 0, NULL
, NOISY
))
225 HEAP_Dump( heapPtr
);
227 SetLastError( ERROR_INVALID_HANDLE
);
234 /***********************************************************************
235 * HEAP_InsertFreeBlock
237 * Insert a free block into the free list.
239 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
241 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
242 while (pEntry
->size
< pArena
->size
) pEntry
++;
243 pArena
->size
|= ARENA_FLAG_FREE
;
244 pArena
->next
= pEntry
->arena
.next
;
245 pArena
->next
->prev
= pArena
;
246 pArena
->prev
= &pEntry
->arena
;
247 pEntry
->arena
.next
= pArena
;
251 /***********************************************************************
253 * Find the sub-heap containing a given address.
259 static SUBHEAP
*HEAP_FindSubHeap(
260 HEAP
*heap
, /* [in] Heap pointer */
261 LPCVOID ptr
/* [in] Address */
263 SUBHEAP
*sub
= &heap
->subheap
;
266 if (((char *)ptr
>= (char *)sub
) &&
267 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
274 /***********************************************************************
277 * Make sure the heap storage is committed up to (not including) ptr.
279 static inline BOOL
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
281 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
282 size
= (size
+ COMMIT_MASK
) & ~COMMIT_MASK
;
283 if (size
> subheap
->size
) size
= subheap
->size
;
284 if (size
<= subheap
->commitSize
) return TRUE
;
285 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
286 size
- subheap
->commitSize
, MEM_COMMIT
,
287 PAGE_EXECUTE_READWRITE
))
289 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
290 size
- subheap
->commitSize
,
291 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
292 (DWORD
)subheap
->heap
);
295 subheap
->commitSize
= size
;
300 /***********************************************************************
303 * If possible, decommit the heap storage from (including) 'ptr'.
305 static inline BOOL
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
307 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
308 /* round to next block and add one full block */
309 size
= ((size
+ COMMIT_MASK
) & ~COMMIT_MASK
) + COMMIT_MASK
+ 1;
310 if (size
>= subheap
->commitSize
) return TRUE
;
311 if (!VirtualFree( (char *)subheap
+ size
,
312 subheap
->commitSize
- size
, MEM_DECOMMIT
))
314 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
315 subheap
->commitSize
- size
,
316 (DWORD
)((char *)subheap
+ size
),
317 (DWORD
)subheap
->heap
);
320 subheap
->commitSize
= size
;
325 /***********************************************************************
326 * HEAP_CreateFreeBlock
328 * Create a free block at a specified address. 'size' is the size of the
329 * whole block, including the new arena.
331 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
335 /* Create a free arena */
337 pFree
= (ARENA_FREE
*)ptr
;
338 pFree
->threadId
= GetCurrentTask();
339 pFree
->magic
= ARENA_FREE_MAGIC
;
341 /* If debugging, erase the freed block content */
345 char *pEnd
= (char *)ptr
+ size
;
346 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
347 pEnd
= (char *)subheap
+ subheap
->commitSize
;
348 if (pEnd
> (char *)(pFree
+ 1))
349 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
352 /* Check if next block is free also */
354 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
355 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
357 /* Remove the next arena from the free list */
358 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
359 pNext
->next
->prev
= pNext
->prev
;
360 pNext
->prev
->next
= pNext
->next
;
361 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
363 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
366 /* Set the next block PREV_FREE flag and pointer */
368 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
370 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
371 *pNext
|= ARENA_FLAG_PREV_FREE
;
372 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
375 /* Last, insert the new block into the free list */
377 pFree
->size
= size
- sizeof(*pFree
);
378 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
382 /***********************************************************************
383 * HEAP_MakeInUseBlockFree
385 * Turn an in-use block into a free block. Can also decommit the end of
386 * the heap, and possibly even free the sub-heap altogether.
388 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
391 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
393 /* Check if we can merge with previous block */
395 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
397 pFree
= *((ARENA_FREE
**)pArena
- 1);
398 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
399 /* Remove it from the free list */
400 pFree
->next
->prev
= pFree
->prev
;
401 pFree
->prev
->next
= pFree
->next
;
403 else pFree
= (ARENA_FREE
*)pArena
;
405 /* Create a free block */
407 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
408 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
409 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
410 return; /* Not the last block, so nothing more to do */
412 /* Free the whole sub-heap if it's empty and not the original one */
414 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
415 (subheap
!= &subheap
->heap
->subheap
))
417 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
418 /* Remove the free block from the list */
419 pFree
->next
->prev
= pFree
->prev
;
420 pFree
->prev
->next
= pFree
->next
;
421 /* Remove the subheap from the list */
422 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
423 if (pPrev
) pPrev
->next
= subheap
->next
;
424 /* Free the memory */
426 if (subheap
->selector
) FreeSelector16( subheap
->selector
);
427 VirtualFree( subheap
, 0, MEM_RELEASE
);
431 /* Decommit the end of the heap */
433 if (!(subheap
->heap
->flags
& HEAP_WINE_SHARED
)) HEAP_Decommit( subheap
, pFree
+ 1 );
437 /***********************************************************************
440 * Shrink an in-use block.
442 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
444 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
446 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
447 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
448 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
452 /* Turn off PREV_FREE flag in next block */
453 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
454 if (pNext
< (char *)subheap
+ subheap
->size
)
455 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
459 /***********************************************************************
462 static BOOL
HEAP_InitSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
463 DWORD commitSize
, DWORD totalSize
)
465 SUBHEAP
*subheap
= (SUBHEAP
*)address
;
467 FREE_LIST_ENTRY
*pEntry
;
472 if (flags
& HEAP_WINE_SHARED
)
473 commitSize
= totalSize
; /* always commit everything in a shared heap */
474 if (!VirtualAlloc(address
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
476 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
477 commitSize
, (DWORD
)address
);
481 /* Allocate a selector if needed */
483 if (flags
& HEAP_WINE_SEGPTR
)
485 selector
= SELECTOR_AllocBlock( address
, totalSize
,
486 (flags
& (HEAP_WINE_CODESEG
|HEAP_WINE_CODE16SEG
))
487 ? SEGMENT_CODE
: SEGMENT_DATA
,
488 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
491 ERR("Could not allocate selector\n" );
496 /* Fill the sub-heap structure */
498 subheap
->heap
= heap
;
499 subheap
->selector
= selector
;
500 subheap
->size
= totalSize
;
501 subheap
->commitSize
= commitSize
;
502 subheap
->magic
= SUBHEAP_MAGIC
;
504 if ( subheap
!= (SUBHEAP
*)heap
)
506 /* If this is a secondary subheap, insert it into list */
508 subheap
->headerSize
= sizeof(SUBHEAP
);
509 subheap
->next
= heap
->subheap
.next
;
510 heap
->subheap
.next
= subheap
;
514 /* If this is a primary subheap, initialize main heap */
516 subheap
->headerSize
= sizeof(HEAP
);
517 subheap
->next
= NULL
;
520 heap
->magic
= HEAP_MAGIC
;
522 /* Build the free lists */
524 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
526 pEntry
->size
= HEAP_freeListSizes
[i
];
527 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
528 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
529 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
530 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
531 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
532 pEntry
->arena
.threadId
= 0;
533 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
536 /* Initialize critical section */
538 InitializeCriticalSection( &heap
->critSection
);
539 if (!SystemHeap
) MakeCriticalSectionGlobal( &heap
->critSection
);
542 /* Create the first free block */
544 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
+ subheap
->headerSize
,
545 subheap
->size
- subheap
->headerSize
);
550 /***********************************************************************
553 * Create a sub-heap of the given size.
554 * If heap == NULL, creates a main heap.
556 static SUBHEAP
*HEAP_CreateSubHeap( HEAP
*heap
, DWORD flags
,
557 DWORD commitSize
, DWORD totalSize
)
561 /* Round-up sizes on a 64K boundary */
563 if (flags
& HEAP_WINE_SEGPTR
)
565 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
569 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
570 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
571 if (!commitSize
) commitSize
= 0x10000;
572 if (totalSize
< commitSize
) totalSize
= commitSize
;
575 /* Allocate the memory block */
577 if (!(address
= VirtualAlloc( NULL
, totalSize
,
578 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
580 WARN("Could not VirtualAlloc %08lx bytes\n",
585 /* Initialize subheap */
587 if (!HEAP_InitSubHeap( heap
? heap
: (HEAP
*)address
,
588 address
, flags
, commitSize
, totalSize
))
590 VirtualFree( address
, 0, MEM_RELEASE
);
594 return (SUBHEAP
*)address
;
598 /***********************************************************************
601 * Find a free block at least as large as the requested size, and make sure
602 * the requested size is committed.
604 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
605 SUBHEAP
**ppSubHeap
)
609 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
611 /* Find a suitable free list, and in it find a block large enough */
613 while (pEntry
->size
< size
) pEntry
++;
614 pArena
= pEntry
->arena
.next
;
615 while (pArena
!= &heap
->freeList
[0].arena
)
617 if (pArena
->size
> size
)
619 subheap
= HEAP_FindSubHeap( heap
, pArena
);
620 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
621 + size
+ HEAP_MIN_BLOCK_SIZE
))
623 *ppSubHeap
= subheap
;
627 pArena
= pArena
->next
;
630 /* If no block was found, attempt to grow the heap */
632 if (!(heap
->flags
& HEAP_GROWABLE
))
634 WARN("Not enough space in heap %08lx for %08lx bytes\n",
638 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
639 if (!(subheap
= HEAP_CreateSubHeap( heap
, heap
->flags
, size
,
640 max( HEAP_DEF_SIZE
, size
) )))
643 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
644 (DWORD
)subheap
, size
, (DWORD
)heap
);
646 *ppSubHeap
= subheap
;
647 return (ARENA_FREE
*)(subheap
+ 1);
651 /***********************************************************************
652 * HEAP_IsValidArenaPtr
654 * Check that the pointer is inside the range possible for arenas.
656 static BOOL
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
659 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
660 if (!subheap
) return FALSE
;
661 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
662 if (subheap
!= &heap
->subheap
) return FALSE
;
663 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
664 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
669 /***********************************************************************
670 * HEAP_ValidateFreeArena
672 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
674 char *heapEnd
= (char *)subheap
+ subheap
->size
;
676 /* Check magic number */
677 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
679 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
680 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
683 /* Check size flags */
684 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
685 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
687 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
688 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
690 /* Check arena size */
691 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
693 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
694 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
697 /* Check that next pointer is valid */
698 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
700 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
701 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
704 /* Check that next arena is free */
705 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
706 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
708 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
709 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
712 /* Check that prev pointer is valid */
713 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
715 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
716 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
719 /* Check that prev arena is free */
720 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
721 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
723 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
724 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
727 /* Check that next block has PREV_FREE flag */
728 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
730 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
731 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
733 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
734 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
737 /* Check next block back pointer */
738 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
739 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
741 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
742 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
743 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
751 /***********************************************************************
752 * HEAP_ValidateInUseArena
754 static BOOL
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, BOOL quiet
)
756 char *heapEnd
= (char *)subheap
+ subheap
->size
;
758 /* Check magic number */
759 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
761 if (quiet
== NOISY
) {
762 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
763 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
765 HEAP_Dump( subheap
->heap
);
766 } else if (WARN_ON(heap
)) {
767 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
768 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
770 HEAP_Dump( subheap
->heap
);
774 /* Check size flags */
775 if (pArena
->size
& ARENA_FLAG_FREE
)
777 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
778 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
780 /* Check arena size */
781 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
783 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
784 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
787 /* Check next arena PREV_FREE flag */
788 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
789 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
791 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
792 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
795 /* Check prev free arena */
796 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
798 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
799 /* Check prev pointer */
800 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
802 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
803 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
806 /* Check that prev arena is free */
807 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
808 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
810 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
811 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
814 /* Check that prev arena is really the previous block */
815 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
817 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
818 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
826 /***********************************************************************
828 * Checks whether the pointer points to a block inside a given heap.
831 * Should this return BOOL32?
837 int HEAP_IsInsideHeap(
838 HANDLE heap
, /* [in] Heap */
839 DWORD flags
, /* [in] Flags */
840 LPCVOID ptr
/* [in] Pointer */
842 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
846 /* Validate the parameters */
848 if (!heapPtr
) return 0;
849 flags
|= heapPtr
->flags
;
850 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
851 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
852 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
853 + sizeof(ARENA_INUSE
))));
854 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
859 /***********************************************************************
862 * Transform a linear pointer into a SEGPTR. The pointer must have been
863 * allocated from a HEAP_WINE_SEGPTR heap.
865 SEGPTR
HEAP_GetSegptr( HANDLE heap
, DWORD flags
, LPCVOID ptr
)
867 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
871 /* Validate the parameters */
873 if (!heapPtr
) return 0;
874 flags
|= heapPtr
->flags
;
875 if (!(flags
& HEAP_WINE_SEGPTR
))
877 ERR("Heap %08x is not a SEGPTR heap\n",
881 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
883 /* Get the subheap */
885 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
887 ERR("%p is not inside heap %08x\n",
889 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
893 /* Build the SEGPTR */
895 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
896 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
900 /***********************************************************************
901 * HEAP_IsRealArena [Internal]
902 * Validates a block is a valid arena.
908 static BOOL
HEAP_IsRealArena(
909 HANDLE heap
, /* [in] Handle to the heap */
910 DWORD flags
, /* [in] Bit flags that control access during operation */
911 LPCVOID block
, /* [in] Optional pointer to memory block to validate */
912 BOOL quiet
/* [in] Flag - if true, HEAP_ValidateInUseArena
913 * does not complain */
916 HEAP
*heapPtr
= (HEAP
*)(heap
);
919 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
921 ERR("Invalid heap %08x!\n", heap
);
925 flags
&= HEAP_NO_SERIALIZE
;
926 flags
|= heapPtr
->flags
;
927 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
928 if (!(flags
& HEAP_NO_SERIALIZE
))
929 EnterCriticalSection( &heapPtr
->critSection
);
933 /* Only check this single memory block */
935 /* The following code is really HEAP_IsInsideHeap *
936 * with serialization already done. */
937 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
938 ((char *)block
< (char *)subheap
+ subheap
->headerSize
939 + sizeof(ARENA_INUSE
)))
942 ERR("Heap %08lx: block %08lx is not inside heap\n",
943 (DWORD
)heap
, (DWORD
)block
);
944 else if (WARN_ON(heap
))
945 WARN("Heap %08lx: block %08lx is not inside heap\n",
946 (DWORD
)heap
, (DWORD
)block
);
949 ret
= HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1, quiet
);
951 if (!(flags
& HEAP_NO_SERIALIZE
))
952 LeaveCriticalSection( &heapPtr
->critSection
);
956 subheap
= &heapPtr
->subheap
;
957 while (subheap
&& ret
)
959 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
960 while (ptr
< (char *)subheap
+ subheap
->size
)
962 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
964 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
)) {
968 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
972 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
, NOISY
)) {
976 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
979 subheap
= subheap
->next
;
982 if (!(flags
& HEAP_NO_SERIALIZE
))
983 LeaveCriticalSection( &heapPtr
->critSection
);
988 /***********************************************************************
989 * HeapCreate (KERNEL32.336)
991 * Handle of heap: Success
994 HANDLE WINAPI
HeapCreate(
995 DWORD flags
, /* [in] Heap allocation flag */
996 DWORD initialSize
, /* [in] Initial heap size */
997 DWORD maxSize
/* [in] Maximum heap size */
1001 /* Allocate the heap block */
1005 maxSize
= HEAP_DEF_SIZE
;
1006 flags
|= HEAP_GROWABLE
;
1008 if (!(subheap
= HEAP_CreateSubHeap( NULL
, flags
, initialSize
, maxSize
)))
1010 SetLastError( ERROR_OUTOFMEMORY
);
1014 /* link it into the per-process heap list */
1017 HEAP
*heapPtr
= subheap
->heap
;
1018 EnterCriticalSection( &processHeap
->critSection
);
1019 heapPtr
->next
= firstHeap
;
1020 firstHeap
= heapPtr
;
1021 LeaveCriticalSection( &processHeap
->critSection
);
1023 else /* assume the first heap we create is the process main heap */
1025 processHeap
= subheap
->heap
;
1028 return (HANDLE
)subheap
;
1031 /***********************************************************************
1032 * HeapDestroy (KERNEL32.337)
1037 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
1039 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1042 TRACE("%08x\n", heap
);
1043 if (!heapPtr
) return FALSE
;
1045 if (heapPtr
== processHeap
) /* cannot delete the main process heap */
1047 SetLastError( ERROR_INVALID_PARAMETER
);
1050 else /* remove it from the per-process list */
1053 EnterCriticalSection( &processHeap
->critSection
);
1055 while (*pptr
&& *pptr
!= heapPtr
) pptr
= &(*pptr
)->next
;
1056 if (*pptr
) *pptr
= (*pptr
)->next
;
1057 LeaveCriticalSection( &processHeap
->critSection
);
1060 DeleteCriticalSection( &heapPtr
->critSection
);
1061 subheap
= &heapPtr
->subheap
;
1064 SUBHEAP
*next
= subheap
->next
;
1065 if (subheap
->selector
) FreeSelector16( subheap
->selector
);
1066 VirtualFree( subheap
, 0, MEM_RELEASE
);
1073 /***********************************************************************
1074 * HeapAlloc (KERNEL32.334)
1076 * Pointer to allocated memory block
1079 LPVOID WINAPI
HeapAlloc(
1080 HANDLE heap
, /* [in] Handle of private heap block */
1081 DWORD flags
, /* [in] Heap allocation control flags */
1082 DWORD size
/* [in] Number of bytes to allocate */
1085 ARENA_INUSE
*pInUse
;
1087 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1089 /* Validate the parameters */
1091 if (!heapPtr
) return NULL
;
1092 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
1093 flags
|= heapPtr
->flags
;
1094 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1095 size
= (size
+ 3) & ~3;
1096 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1098 /* Locate a suitable free block */
1100 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
1102 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1103 heap
, flags
, size
);
1104 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1105 SetLastError( ERROR_COMMITMENT_LIMIT
);
1109 /* Remove the arena from the free list */
1111 pArena
->next
->prev
= pArena
->prev
;
1112 pArena
->prev
->next
= pArena
->next
;
1114 /* Build the in-use arena */
1116 pInUse
= (ARENA_INUSE
*)pArena
;
1117 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1118 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1119 pInUse
->callerEIP
= GET_EIP();
1120 pInUse
->threadId
= GetCurrentTask();
1121 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1123 /* Shrink the block */
1125 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
1127 if (flags
& HEAP_ZERO_MEMORY
)
1128 memset( pInUse
+ 1, 0, pInUse
->size
& ARENA_SIZE_MASK
);
1129 else if (TRACE_ON(heap
))
1130 memset( pInUse
+ 1, ARENA_INUSE_FILLER
, pInUse
->size
& ARENA_SIZE_MASK
);
1132 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1134 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1135 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
1136 return (LPVOID
)(pInUse
+ 1);
1140 /***********************************************************************
1141 * HeapFree (KERNEL32.338)
1146 BOOL WINAPI
HeapFree(
1147 HANDLE heap
, /* [in] Handle of heap */
1148 DWORD flags
, /* [in] Heap freeing flags */
1149 LPVOID ptr
/* [in] Address of memory to free */
1151 ARENA_INUSE
*pInUse
;
1153 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1155 /* Validate the parameters */
1157 if (!heapPtr
) return FALSE
;
1158 if (!ptr
) /* Freeing a NULL ptr is doesn't indicate an error in Win2k */
1160 WARN("(%08x,%08lx,%08lx): asked to free NULL\n",
1161 heap
, flags
, (DWORD
)ptr
);
1165 flags
&= HEAP_NO_SERIALIZE
;
1166 flags
|= heapPtr
->flags
;
1167 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1168 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1170 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1171 SetLastError( ERROR_INVALID_PARAMETER
);
1172 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1173 heap
, flags
, (DWORD
)ptr
);
1177 /* Turn the block into a free block */
1179 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1180 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
1181 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
1183 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1185 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1186 heap
, flags
, (DWORD
)ptr
);
1191 /***********************************************************************
1192 * HeapReAlloc (KERNEL32.340)
1194 * Pointer to reallocated memory block
1197 LPVOID WINAPI
HeapReAlloc(
1198 HANDLE heap
, /* [in] Handle of heap block */
1199 DWORD flags
, /* [in] Heap reallocation flags */
1200 LPVOID ptr
, /* [in] Address of memory to reallocate */
1201 DWORD size
/* [in] Number of bytes to reallocate */
1203 ARENA_INUSE
*pArena
;
1208 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
1209 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1211 /* Validate the parameters */
1213 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1214 HEAP_REALLOC_IN_PLACE_ONLY
;
1215 flags
|= heapPtr
->flags
;
1216 size
= (size
+ 3) & ~3;
1217 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1219 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1220 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1222 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1223 SetLastError( ERROR_INVALID_PARAMETER
);
1224 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1225 heap
, flags
, (DWORD
)ptr
, size
);
1229 /* Check if we need to grow the block */
1231 pArena
= (ARENA_INUSE
*)ptr
- 1;
1232 pArena
->threadId
= GetCurrentTask();
1233 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1234 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1237 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1238 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1239 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1240 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1242 /* The next block is free and large enough */
1243 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1244 pFree
->next
->prev
= pFree
->prev
;
1245 pFree
->prev
->next
= pFree
->next
;
1246 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1247 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1248 + size
+ HEAP_MIN_BLOCK_SIZE
))
1250 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1251 SetLastError( ERROR_OUTOFMEMORY
);
1254 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1256 else /* Do it the hard way */
1259 ARENA_INUSE
*pInUse
;
1260 SUBHEAP
*newsubheap
;
1262 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1263 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1265 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1266 SetLastError( ERROR_OUTOFMEMORY
);
1270 /* Build the in-use arena */
1272 pNew
->next
->prev
= pNew
->prev
;
1273 pNew
->prev
->next
= pNew
->next
;
1274 pInUse
= (ARENA_INUSE
*)pNew
;
1275 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1276 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1277 pInUse
->threadId
= GetCurrentTask();
1278 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1279 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1280 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1282 /* Free the previous block */
1284 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1285 subheap
= newsubheap
;
1289 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1291 /* Clear the extra bytes if needed */
1295 if (flags
& HEAP_ZERO_MEMORY
)
1296 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1297 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1298 else if (TRACE_ON(heap
))
1299 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1300 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1303 /* Return the new arena */
1305 pArena
->callerEIP
= GET_EIP();
1306 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1308 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1309 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1310 return (LPVOID
)(pArena
+ 1);
1314 /***********************************************************************
1315 * HeapCompact (KERNEL32.335)
1317 DWORD WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
1319 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1324 /***********************************************************************
1325 * HeapLock (KERNEL32.339)
1326 * Attempts to acquire the critical section object for a specified heap.
1332 BOOL WINAPI
HeapLock(
1333 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
1335 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1336 if (!heapPtr
) return FALSE
;
1337 EnterCriticalSection( &heapPtr
->critSection
);
1342 /***********************************************************************
1343 * HeapUnlock (KERNEL32.342)
1344 * Releases ownership of the critical section object.
1350 BOOL WINAPI
HeapUnlock(
1351 HANDLE heap
/* [in] Handle to the heap to unlock */
1353 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1354 if (!heapPtr
) return FALSE
;
1355 LeaveCriticalSection( &heapPtr
->critSection
);
1360 /***********************************************************************
1361 * HeapSize (KERNEL32.341)
1363 * Size in bytes of allocated memory
1364 * 0xffffffff: Failure
1366 DWORD WINAPI
HeapSize(
1367 HANDLE heap
, /* [in] Handle of heap */
1368 DWORD flags
, /* [in] Heap size control flags */
1369 LPVOID ptr
/* [in] Address of memory to return size for */
1372 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1374 if (!heapPtr
) return FALSE
;
1375 flags
&= HEAP_NO_SERIALIZE
;
1376 flags
|= heapPtr
->flags
;
1377 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1378 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1380 SetLastError( ERROR_INVALID_PARAMETER
);
1385 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1386 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1388 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1390 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1391 heap
, flags
, (DWORD
)ptr
, ret
);
1396 /***********************************************************************
1397 * HeapValidate (KERNEL32.343)
1398 * Validates a specified heap.
1407 BOOL WINAPI
HeapValidate(
1408 HANDLE heap
, /* [in] Handle to the heap */
1409 DWORD flags
, /* [in] Bit flags that control access during operation */
1410 LPCVOID block
/* [in] Optional pointer to memory block to validate */
1413 return HEAP_IsRealArena( heap
, flags
, block
, QUIET
);
1417 /***********************************************************************
1418 * HeapWalk (KERNEL32.344)
1419 * Enumerates the memory blocks in a specified heap.
1420 * See HEAP_Dump() for info on heap structure.
1423 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
1424 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
1430 BOOL WINAPI
HeapWalk(
1431 HANDLE heap
, /* [in] Handle to heap to enumerate */
1432 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
1434 HEAP
*heapPtr
= HEAP_GetPtr(heap
);
1435 SUBHEAP
*sub
, *currentheap
= NULL
;
1438 int region_index
= 0;
1440 if (!heapPtr
|| !entry
)
1442 SetLastError(ERROR_INVALID_PARAMETER
);
1446 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1448 /* set ptr to the next arena to be examined */
1450 if (!entry
->lpData
) /* first call (init) ? */
1452 TRACE("begin walking of heap 0x%08x.\n", heap
);
1453 /*HEAP_Dump(heapPtr);*/
1454 currentheap
= &heapPtr
->subheap
;
1455 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1459 ptr
= entry
->lpData
;
1460 sub
= &heapPtr
->subheap
;
1463 if (((char *)ptr
>= (char *)sub
) &&
1464 ((char *)ptr
< (char *)sub
+ sub
->size
))
1472 if (currentheap
== NULL
)
1474 ERR("no matching subheap found, shouldn't happen !\n");
1475 SetLastError(ERROR_NO_MORE_ITEMS
);
1479 ptr
+= entry
->cbData
; /* point to next arena */
1480 if (ptr
> (char *)currentheap
+ currentheap
->size
- 1)
1481 { /* proceed with next subheap */
1482 if (!(currentheap
= currentheap
->next
))
1483 { /* successfully finished */
1484 TRACE("end reached.\n");
1485 SetLastError(ERROR_NO_MORE_ITEMS
);
1488 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1493 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1495 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
1497 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1499 entry
->lpData
= pArena
+ 1;
1500 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1501 entry
->cbOverhead
= sizeof(ARENA_FREE
);
1502 entry
->wFlags
= PROCESS_HEAP_UNCOMMITTED_RANGE
;
1506 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
1508 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1510 entry
->lpData
= pArena
+ 1;
1511 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1512 entry
->cbOverhead
= sizeof(ARENA_INUSE
);
1513 entry
->wFlags
= PROCESS_HEAP_ENTRY_BUSY
;
1514 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1515 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1518 entry
->iRegionIndex
= region_index
;
1520 /* first element of heap ? */
1521 if (ptr
== (char *)(currentheap
+ currentheap
->headerSize
))
1523 entry
->wFlags
|= PROCESS_HEAP_REGION
;
1524 entry
->Foo
.Region
.dwCommittedSize
= currentheap
->commitSize
;
1525 entry
->Foo
.Region
.dwUnCommittedSize
=
1526 currentheap
->size
- currentheap
->commitSize
;
1527 entry
->Foo
.Region
.lpFirstBlock
= /* first valid block */
1528 currentheap
+ currentheap
->headerSize
;
1529 entry
->Foo
.Region
.lpLastBlock
= /* first invalid block */
1530 currentheap
+ currentheap
->size
;
1535 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1541 /***********************************************************************
1542 * HEAP_CreateSystemHeap
1544 * Create the system heap.
1546 BOOL
HEAP_CreateSystemHeap(void)
1548 SYSTEM_HEAP_DESCR
*descr
;
1553 HANDLE map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
1554 0, HEAP_DEF_SIZE
, "__SystemHeap" );
1555 if (!map
) return FALSE
;
1556 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
1558 if (!(heapPtr
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
1560 /* pre-defined address not available, use any one */
1561 fprintf( stderr
, "Warning: system heap base address %p not available\n",
1563 if (!(heapPtr
= MapViewOfFile( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0 )))
1569 heap
= (HANDLE
)heapPtr
;
1571 if (created
) /* newly created heap */
1573 HEAP_InitSubHeap( heapPtr
, heapPtr
, HEAP_WINE_SHARED
, 0, HEAP_DEF_SIZE
);
1575 descr
= heapPtr
->private = HeapAlloc( heap
, HEAP_ZERO_MEMORY
, sizeof(*descr
) );
1580 /* wait for the heap to be initialized */
1581 while (!heapPtr
->private) Sleep(1);
1583 /* remap it to the right address if necessary */
1584 if (heapPtr
->subheap
.heap
!= heapPtr
)
1586 void *base
= heapPtr
->subheap
.heap
;
1588 UnmapViewOfFile( heapPtr
);
1589 if (!(heapPtr
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, base
)))
1591 fprintf( stderr
, "Couldn't map system heap at the correct address (%p)\n", base
);
1595 heap
= (HANDLE
)heapPtr
;
1598 descr
= heapPtr
->private;
1602 SystemHeapDescr
= descr
;
1609 /***********************************************************************
1610 * GetProcessHeap (KERNEL32.259)
1612 HANDLE WINAPI
GetProcessHeap(void)
1614 return (HANDLE
)processHeap
;
1618 /***********************************************************************
1619 * GetProcessHeaps (KERNEL32.376)
1621 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
1626 if (!processHeap
) return 0; /* should never happen */
1627 total
= 1; /* main heap */
1628 EnterCriticalSection( &processHeap
->critSection
);
1629 for (ptr
= firstHeap
; ptr
; ptr
= ptr
->next
) total
++;
1632 *heaps
++ = (HANDLE
)processHeap
;
1633 for (ptr
= firstHeap
; ptr
; ptr
= ptr
->next
) *heaps
++ = (HANDLE
)ptr
;
1635 LeaveCriticalSection( &processHeap
->critSection
);
1640 /***********************************************************************
1643 LPSTR
HEAP_strdupA( HANDLE heap
, DWORD flags
, LPCSTR str
)
1645 LPSTR p
= HeapAlloc( heap
, flags
, strlen(str
) + 1 );
1654 /***********************************************************************
1657 LPWSTR
HEAP_strdupW( HANDLE heap
, DWORD flags
, LPCWSTR str
)
1659 INT len
= lstrlenW(str
) + 1;
1660 LPWSTR p
= HeapAlloc( heap
, flags
, len
* sizeof(WCHAR
) );
1669 /***********************************************************************
1672 LPWSTR
HEAP_strdupAtoW( HANDLE heap
, DWORD flags
, LPCSTR str
)
1676 if (!str
) return NULL
;
1677 ret
= HeapAlloc( heap
, flags
, (strlen(str
)+1) * sizeof(WCHAR
) );
1680 lstrcpyAtoW( ret
, str
);
1686 /***********************************************************************
1689 LPSTR
HEAP_strdupWtoA( HANDLE heap
, DWORD flags
, LPCWSTR str
)
1694 if (!str
) return NULL
;
1695 len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
1696 ret
= HeapAlloc( heap
, flags
, len
);
1699 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
1706 /***********************************************************************
1707 * 32-bit local heap functions (Win95; undocumented)
1710 #define HTABLE_SIZE 0x10000
1711 #define HTABLE_PAGESIZE 0x1000
1712 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
1714 #include "pshpack1.h"
1715 typedef struct _LOCAL32HEADER
1717 WORD freeListFirst
[HTABLE_NPAGES
];
1718 WORD freeListSize
[HTABLE_NPAGES
];
1719 WORD freeListLast
[HTABLE_NPAGES
];
1721 DWORD selectorTableOffset
;
1722 WORD selectorTableSize
;
1735 #include "poppack.h"
1737 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
1739 /***********************************************************************
1740 * Local32Init (KERNEL.208)
1742 HANDLE WINAPI
Local32Init16( WORD segment
, DWORD tableSize
,
1743 DWORD heapSize
, DWORD flags
)
1745 DWORD totSize
, segSize
= 0;
1747 LOCAL32HEADER
*header
;
1749 WORD
*selectorTable
;
1750 WORD selectorEven
, selectorOdd
;
1753 /* Determine new heap size */
1757 if ( (segSize
= GetSelectorLimit16( segment
)) == 0 )
1763 if ( heapSize
== -1L )
1764 heapSize
= 1024L*1024L; /* FIXME */
1766 heapSize
= (heapSize
+ 0xffff) & 0xffff0000;
1767 segSize
= (segSize
+ 0x0fff) & 0xfffff000;
1768 totSize
= segSize
+ HTABLE_SIZE
+ heapSize
;
1771 /* Allocate memory and initialize heap */
1773 if ( !(base
= VirtualAlloc( NULL
, totSize
, MEM_RESERVE
, PAGE_READWRITE
)) )
1776 if ( !VirtualAlloc( base
, segSize
+ HTABLE_PAGESIZE
,
1777 MEM_COMMIT
, PAGE_READWRITE
) )
1779 VirtualFree( base
, 0, MEM_RELEASE
);
1783 heap
= (HEAP
*)(base
+ segSize
+ HTABLE_SIZE
);
1784 if ( !HEAP_InitSubHeap( heap
, (LPVOID
)heap
, 0, 0x10000, heapSize
) )
1786 VirtualFree( base
, 0, MEM_RELEASE
);
1791 /* Set up header and handle table */
1793 header
= (LOCAL32HEADER
*)(base
+ segSize
);
1794 header
->base
= base
;
1795 header
->limit
= HTABLE_PAGESIZE
-1;
1797 header
->magic
= LOCAL32_MAGIC
;
1798 header
->heap
= (HANDLE
)heap
;
1800 header
->freeListFirst
[0] = sizeof(LOCAL32HEADER
);
1801 header
->freeListLast
[0] = HTABLE_PAGESIZE
- 4;
1802 header
->freeListSize
[0] = (HTABLE_PAGESIZE
- sizeof(LOCAL32HEADER
)) / 4;
1804 for (i
= header
->freeListFirst
[0]; i
< header
->freeListLast
[0]; i
+= 4)
1805 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1807 header
->freeListFirst
[1] = 0xffff;
1810 /* Set up selector table */
1812 nrBlocks
= (totSize
+ 0x7fff) >> 15;
1813 selectorTable
= (LPWORD
) HeapAlloc( header
->heap
, 0, nrBlocks
* 2 );
1814 selectorEven
= SELECTOR_AllocBlock( base
, totSize
,
1815 SEGMENT_DATA
, FALSE
, FALSE
);
1816 selectorOdd
= SELECTOR_AllocBlock( base
+ 0x8000, totSize
- 0x8000,
1817 SEGMENT_DATA
, FALSE
, FALSE
);
1819 if ( !selectorTable
|| !selectorEven
|| !selectorOdd
)
1821 if ( selectorTable
) HeapFree( header
->heap
, 0, selectorTable
);
1822 if ( selectorEven
) SELECTOR_FreeBlock( selectorEven
, totSize
>> 16 );
1823 if ( selectorOdd
) SELECTOR_FreeBlock( selectorOdd
, (totSize
-0x8000) >> 16 );
1824 HeapDestroy( header
->heap
);
1825 VirtualFree( base
, 0, MEM_RELEASE
);
1829 header
->selectorTableOffset
= (LPBYTE
)selectorTable
- header
->base
;
1830 header
->selectorTableSize
= nrBlocks
* 4; /* ??? Win95 does it this way! */
1831 header
->selectorDelta
= selectorEven
- selectorOdd
;
1832 header
->segment
= segment
? segment
: selectorEven
;
1834 for (i
= 0; i
< nrBlocks
; i
++)
1835 selectorTable
[i
] = (i
& 1)? selectorOdd
+ ((i
>> 1) << __AHSHIFT
)
1836 : selectorEven
+ ((i
>> 1) << __AHSHIFT
);
1838 /* Move old segment */
1842 /* FIXME: This is somewhat ugly and relies on implementation
1843 details about 16-bit global memory handles ... */
1845 LPBYTE oldBase
= (LPBYTE
)GetSelectorBase( segment
);
1846 memcpy( base
, oldBase
, segSize
);
1847 GLOBAL_MoveBlock( segment
, base
, totSize
);
1848 HeapFree( SystemHeap
, 0, oldBase
);
1851 return (HANDLE
)header
;
1854 /***********************************************************************
1855 * Local32_SearchHandle
1857 static LPDWORD
Local32_SearchHandle( LOCAL32HEADER
*header
, DWORD addr
)
1861 for ( handle
= (LPDWORD
)((LPBYTE
)header
+ sizeof(LOCAL32HEADER
));
1862 handle
< (LPDWORD
)((LPBYTE
)header
+ header
->limit
);
1865 if (*handle
== addr
)
1872 /***********************************************************************
1875 static VOID
Local32_ToHandle( LOCAL32HEADER
*header
, INT16 type
,
1876 DWORD addr
, LPDWORD
*handle
, LPBYTE
*ptr
)
1883 case -2: /* 16:16 pointer, no handles */
1884 *ptr
= PTR_SEG_TO_LIN( addr
);
1885 *handle
= (LPDWORD
)*ptr
;
1888 case -1: /* 32-bit offset, no handles */
1889 *ptr
= header
->base
+ addr
;
1890 *handle
= (LPDWORD
)*ptr
;
1893 case 0: /* handle */
1894 if ( addr
>= sizeof(LOCAL32HEADER
)
1895 && addr
< header
->limit
&& !(addr
& 3)
1896 && *(LPDWORD
)((LPBYTE
)header
+ addr
) >= HTABLE_SIZE
)
1898 *handle
= (LPDWORD
)((LPBYTE
)header
+ addr
);
1899 *ptr
= header
->base
+ **handle
;
1903 case 1: /* 16:16 pointer */
1904 *ptr
= PTR_SEG_TO_LIN( addr
);
1905 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1908 case 2: /* 32-bit offset */
1909 *ptr
= header
->base
+ addr
;
1910 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1915 /***********************************************************************
1916 * Local32_FromHandle
1918 static VOID
Local32_FromHandle( LOCAL32HEADER
*header
, INT16 type
,
1919 DWORD
*addr
, LPDWORD handle
, LPBYTE ptr
)
1923 case -2: /* 16:16 pointer */
1926 WORD
*selTable
= (LPWORD
)(header
->base
+ header
->selectorTableOffset
);
1927 DWORD offset
= (LPBYTE
)ptr
- header
->base
;
1928 *addr
= MAKELONG( offset
& 0x7fff, selTable
[offset
>> 15] );
1932 case -1: /* 32-bit offset */
1934 *addr
= ptr
- header
->base
;
1937 case 0: /* handle */
1938 *addr
= (LPBYTE
)handle
- (LPBYTE
)header
;
1943 /***********************************************************************
1944 * Local32Alloc (KERNEL.209)
1946 DWORD WINAPI
Local32Alloc16( HANDLE heap
, DWORD size
, INT16 type
, DWORD flags
)
1948 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1953 /* Allocate memory */
1954 ptr
= HeapAlloc( header
->heap
,
1955 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0, size
);
1959 /* Allocate handle if requested */
1964 /* Find first page of handle table with free slots */
1965 for (page
= 0; page
< HTABLE_NPAGES
; page
++)
1966 if (header
->freeListFirst
[page
] != 0)
1968 if (page
== HTABLE_NPAGES
)
1970 WARN("Out of handles!\n" );
1971 HeapFree( header
->heap
, 0, ptr
);
1975 /* If virgin page, initialize it */
1976 if (header
->freeListFirst
[page
] == 0xffff)
1978 if ( !VirtualAlloc( (LPBYTE
)header
+ (page
<< 12),
1979 0x1000, MEM_COMMIT
, PAGE_READWRITE
) )
1981 WARN("Cannot grow handle table!\n" );
1982 HeapFree( header
->heap
, 0, ptr
);
1986 header
->limit
+= HTABLE_PAGESIZE
;
1988 header
->freeListFirst
[page
] = 0;
1989 header
->freeListLast
[page
] = HTABLE_PAGESIZE
- 4;
1990 header
->freeListSize
[page
] = HTABLE_PAGESIZE
/ 4;
1992 for (i
= 0; i
< HTABLE_PAGESIZE
; i
+= 4)
1993 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1995 if (page
< HTABLE_NPAGES
-1)
1996 header
->freeListFirst
[page
+1] = 0xffff;
1999 /* Allocate handle slot from page */
2000 handle
= (LPDWORD
)((LPBYTE
)header
+ header
->freeListFirst
[page
]);
2001 if (--header
->freeListSize
[page
] == 0)
2002 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = 0;
2004 header
->freeListFirst
[page
] = *handle
;
2006 /* Store 32-bit offset in handle slot */
2007 *handle
= ptr
- header
->base
;
2011 handle
= (LPDWORD
)ptr
;
2016 /* Convert handle to requested output type */
2017 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
2021 /***********************************************************************
2022 * Local32ReAlloc (KERNEL.210)
2024 DWORD WINAPI
Local32ReAlloc16( HANDLE heap
, DWORD addr
, INT16 type
,
2025 DWORD size
, DWORD flags
)
2027 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2032 return Local32Alloc16( heap
, size
, type
, flags
);
2034 /* Retrieve handle and pointer */
2035 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2036 if (!handle
) return FALSE
;
2038 /* Reallocate memory block */
2039 ptr
= HeapReAlloc( header
->heap
,
2040 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0,
2046 *handle
= ptr
- header
->base
;
2048 handle
= (LPDWORD
)ptr
;
2050 /* Convert handle to requested output type */
2051 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
2055 /***********************************************************************
2056 * Local32Free (KERNEL.211)
2058 BOOL WINAPI
Local32Free16( HANDLE heap
, DWORD addr
, INT16 type
)
2060 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2064 /* Retrieve handle and pointer */
2065 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2066 if (!handle
) return FALSE
;
2068 /* Free handle if necessary */
2071 int offset
= (LPBYTE
)handle
- (LPBYTE
)header
;
2072 int page
= offset
>> 12;
2074 /* Return handle slot to page free list */
2075 if (header
->freeListSize
[page
]++ == 0)
2076 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = offset
;
2078 *(LPDWORD
)((LPBYTE
)header
+ header
->freeListLast
[page
]) = offset
,
2079 header
->freeListLast
[page
] = offset
;
2083 /* Shrink handle table when possible */
2084 while (page
> 0 && header
->freeListSize
[page
] == HTABLE_PAGESIZE
/ 4)
2086 if ( VirtualFree( (LPBYTE
)header
+
2087 (header
->limit
& ~(HTABLE_PAGESIZE
-1)),
2088 HTABLE_PAGESIZE
, MEM_DECOMMIT
) )
2091 header
->limit
-= HTABLE_PAGESIZE
;
2092 header
->freeListFirst
[page
] = 0xffff;
2098 return HeapFree( header
->heap
, 0, ptr
);
2101 /***********************************************************************
2102 * Local32Translate (KERNEL.213)
2104 DWORD WINAPI
Local32Translate16( HANDLE heap
, DWORD addr
, INT16 type1
, INT16 type2
)
2106 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2110 Local32_ToHandle( header
, type1
, addr
, &handle
, &ptr
);
2111 if (!handle
) return 0;
2113 Local32_FromHandle( header
, type2
, &addr
, handle
, ptr
);
2117 /***********************************************************************
2118 * Local32Size (KERNEL.214)
2120 DWORD WINAPI
Local32Size16( HANDLE heap
, DWORD addr
, INT16 type
)
2122 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2126 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2127 if (!handle
) return 0;
2129 return HeapSize( header
->heap
, 0, ptr
);
2132 /***********************************************************************
2133 * Local32ValidHandle (KERNEL.215)
2135 BOOL WINAPI
Local32ValidHandle16( HANDLE heap
, WORD addr
)
2137 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2141 Local32_ToHandle( header
, 0, addr
, &handle
, &ptr
);
2142 return handle
!= NULL
;
2145 /***********************************************************************
2146 * Local32GetSegment (KERNEL.229)
2148 WORD WINAPI
Local32GetSegment16( HANDLE heap
)
2150 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2151 return header
->segment
;
2154 /***********************************************************************
2157 static LOCAL32HEADER
*Local32_GetHeap( HGLOBAL16 handle
)
2159 WORD selector
= GlobalHandleToSel16( handle
);
2160 DWORD base
= GetSelectorBase( selector
);
2161 DWORD limit
= GetSelectorLimit16( selector
);
2163 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2166 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2167 return (LOCAL32HEADER
*)base
;
2172 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2173 return (LOCAL32HEADER
*)base
;
2178 /***********************************************************************
2179 * Local32Info (KERNEL.444) (TOOLHELP.84)
2181 BOOL16 WINAPI
Local32Info16( LOCAL32INFO
*pLocal32Info
, HGLOBAL16 handle
)
2187 LOCAL32HEADER
*header
= Local32_GetHeap( handle
);
2188 if ( !header
) return FALSE
;
2190 if ( !pLocal32Info
|| pLocal32Info
->dwSize
< sizeof(LOCAL32INFO
) )
2193 heapPtr
= (SUBHEAP
*)HEAP_GetPtr( header
->heap
);
2194 pLocal32Info
->dwMemReserved
= heapPtr
->size
;
2195 pLocal32Info
->dwMemCommitted
= heapPtr
->commitSize
;
2196 pLocal32Info
->dwTotalFree
= 0L;
2197 pLocal32Info
->dwLargestFreeBlock
= 0L;
2199 /* Note: Local32 heaps always have only one subheap! */
2200 ptr
= (LPBYTE
)heapPtr
+ heapPtr
->headerSize
;
2201 while ( ptr
< (LPBYTE
)heapPtr
+ heapPtr
->size
)
2203 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
2205 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
2206 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
2207 ptr
+= sizeof(*pArena
) + size
;
2209 pLocal32Info
->dwTotalFree
+= size
;
2210 if ( size
> pLocal32Info
->dwLargestFreeBlock
)
2211 pLocal32Info
->dwLargestFreeBlock
= size
;
2215 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
2216 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
2217 ptr
+= sizeof(*pArena
) + size
;
2221 pLocal32Info
->dwcFreeHandles
= 0;
2222 for ( i
= 0; i
< HTABLE_NPAGES
; i
++ )
2224 if ( header
->freeListFirst
[i
] == 0xffff ) break;
2225 pLocal32Info
->dwcFreeHandles
+= header
->freeListSize
[i
];
2227 pLocal32Info
->dwcFreeHandles
+= (HTABLE_NPAGES
- i
) * HTABLE_PAGESIZE
/4;
2232 /***********************************************************************
2233 * Local32First (KERNEL.445) (TOOLHELP.85)
2235 BOOL16 WINAPI
Local32First16( LOCAL32ENTRY
*pLocal32Entry
, HGLOBAL16 handle
)
2237 FIXME("(%p, %04X): stub!\n", pLocal32Entry
, handle
);
2241 /***********************************************************************
2242 * Local32Next (KERNEL.446) (TOOLHELP.86)
2244 BOOL16 WINAPI
Local32Next16( LOCAL32ENTRY
*pLocal32Entry
)
2246 FIXME("(%p): stub!\n", pLocal32Entry
);