4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
12 #include "wine/winbase16.h"
13 #include "wine/unicode.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 magic
; /* Magic number */
36 WORD threadId
; /* Allocating thread id */
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 magic
; /* Magic number */
44 WORD threadId
; /* Freeing thread id */
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_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 /* assign size plus previous arena flags */
449 pArena
->size
= size
| (pArena
->size
& ~ARENA_SIZE_MASK
);
453 /* Turn off PREV_FREE flag in next block */
454 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
455 if (pNext
< (char *)subheap
+ subheap
->size
)
456 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
460 /***********************************************************************
463 static BOOL
HEAP_InitSubHeap( HEAP
*heap
, LPVOID address
, DWORD flags
,
464 DWORD commitSize
, DWORD totalSize
)
466 SUBHEAP
*subheap
= (SUBHEAP
*)address
;
468 FREE_LIST_ENTRY
*pEntry
;
473 if (flags
& HEAP_SHARED
)
474 commitSize
= totalSize
; /* always commit everything in a shared heap */
475 if (!VirtualAlloc(address
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
477 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
478 commitSize
, (DWORD
)address
);
482 /* Allocate a selector if needed */
484 if (flags
& HEAP_WINE_SEGPTR
)
486 unsigned char selflags
= WINE_LDT_FLAGS_DATA
;
488 if (flags
& (HEAP_WINE_CODESEG
| HEAP_WINE_CODE16SEG
))
489 selflags
= WINE_LDT_FLAGS_CODE
;
490 if (flags
& HEAP_WINE_CODESEG
)
491 selflags
|= WINE_LDT_FLAGS_32BIT
;
492 selector
= SELECTOR_AllocBlock( address
, totalSize
, selflags
);
495 ERR("Could not allocate selector\n" );
500 /* Fill the sub-heap structure */
502 subheap
->heap
= heap
;
503 subheap
->selector
= selector
;
504 subheap
->size
= totalSize
;
505 subheap
->commitSize
= commitSize
;
506 subheap
->magic
= SUBHEAP_MAGIC
;
508 if ( subheap
!= (SUBHEAP
*)heap
)
510 /* If this is a secondary subheap, insert it into list */
512 subheap
->headerSize
= sizeof(SUBHEAP
);
513 subheap
->next
= heap
->subheap
.next
;
514 heap
->subheap
.next
= subheap
;
518 /* If this is a primary subheap, initialize main heap */
520 subheap
->headerSize
= sizeof(HEAP
);
521 subheap
->next
= NULL
;
524 heap
->magic
= HEAP_MAGIC
;
526 /* Build the free lists */
528 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
530 pEntry
->size
= HEAP_freeListSizes
[i
];
531 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
532 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
533 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
534 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
535 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
536 pEntry
->arena
.threadId
= 0;
537 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
540 /* Initialize critical section */
542 InitializeCriticalSection( &heap
->critSection
);
543 if (!SystemHeap
) MakeCriticalSectionGlobal( &heap
->critSection
);
546 /* Create the first free block */
548 HEAP_CreateFreeBlock( subheap
, (LPBYTE
)subheap
+ subheap
->headerSize
,
549 subheap
->size
- subheap
->headerSize
);
554 /***********************************************************************
557 * Create a sub-heap of the given size.
558 * If heap == NULL, creates a main heap.
560 static SUBHEAP
*HEAP_CreateSubHeap( HEAP
*heap
, DWORD flags
,
561 DWORD commitSize
, DWORD totalSize
)
565 /* Round-up sizes on a 64K boundary */
567 if (flags
& HEAP_WINE_SEGPTR
)
569 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
573 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
574 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
575 if (!commitSize
) commitSize
= 0x10000;
576 if (totalSize
< commitSize
) totalSize
= commitSize
;
579 /* Allocate the memory block */
581 if (!(address
= VirtualAlloc( NULL
, totalSize
,
582 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
584 WARN("Could not VirtualAlloc %08lx bytes\n",
589 /* Initialize subheap */
591 if (!HEAP_InitSubHeap( heap
? heap
: (HEAP
*)address
,
592 address
, flags
, commitSize
, totalSize
))
594 VirtualFree( address
, 0, MEM_RELEASE
);
598 return (SUBHEAP
*)address
;
602 /***********************************************************************
605 * Find a free block at least as large as the requested size, and make sure
606 * the requested size is committed.
608 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
609 SUBHEAP
**ppSubHeap
)
613 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
615 /* Find a suitable free list, and in it find a block large enough */
617 while (pEntry
->size
< size
) pEntry
++;
618 pArena
= pEntry
->arena
.next
;
619 while (pArena
!= &heap
->freeList
[0].arena
)
621 if (pArena
->size
> size
)
623 subheap
= HEAP_FindSubHeap( heap
, pArena
);
624 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
625 + size
+ HEAP_MIN_BLOCK_SIZE
))
627 *ppSubHeap
= subheap
;
631 pArena
= pArena
->next
;
634 /* If no block was found, attempt to grow the heap */
636 if (!(heap
->flags
& HEAP_GROWABLE
))
638 WARN("Not enough space in heap %08lx for %08lx bytes\n",
642 /* make sure that we have a big enough size *committed* to fit another
643 * last free arena in !
644 * So just one heap struct, one first free arena which will eventually
645 * get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
646 * might get assigned all remaining free space in HEAP_ShrinkBlock() */
647 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_INUSE
) + HEAP_MIN_BLOCK_SIZE
;
648 if (!(subheap
= HEAP_CreateSubHeap( heap
, heap
->flags
, size
,
649 max( HEAP_DEF_SIZE
, size
) )))
652 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
653 (DWORD
)subheap
, size
, (DWORD
)heap
);
655 *ppSubHeap
= subheap
;
656 return (ARENA_FREE
*)(subheap
+ 1);
660 /***********************************************************************
661 * HEAP_IsValidArenaPtr
663 * Check that the pointer is inside the range possible for arenas.
665 static BOOL
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
668 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
669 if (!subheap
) return FALSE
;
670 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
671 if (subheap
!= &heap
->subheap
) return FALSE
;
672 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
673 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
678 /***********************************************************************
679 * HEAP_ValidateFreeArena
681 static BOOL
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
683 char *heapEnd
= (char *)subheap
+ subheap
->size
;
685 /* Check magic number */
686 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
688 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
689 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
692 /* Check size flags */
693 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
694 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
696 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
697 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
699 /* Check arena size */
700 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
702 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
703 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
706 /* Check that next pointer is valid */
707 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
709 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
710 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
713 /* Check that next arena is free */
714 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
715 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
717 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
718 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
721 /* Check that prev pointer is valid */
722 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
724 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
725 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
728 /* Check that prev arena is free */
729 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
730 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
732 /* this often means that the prev arena got overwritten
733 * by a memory write before that prev arena */
734 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
735 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
738 /* Check that next block has PREV_FREE flag */
739 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
741 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
742 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
744 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
745 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
748 /* Check next block back pointer */
749 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
750 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
752 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
753 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
754 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
762 /***********************************************************************
763 * HEAP_ValidateInUseArena
765 static BOOL
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, BOOL quiet
)
767 char *heapEnd
= (char *)subheap
+ subheap
->size
;
769 /* Check magic number */
770 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
772 if (quiet
== NOISY
) {
773 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
774 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
776 HEAP_Dump( subheap
->heap
);
777 } else if (WARN_ON(heap
)) {
778 WARN("Heap %08lx: invalid in-use arena magic for %08lx\n",
779 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
781 HEAP_Dump( subheap
->heap
);
785 /* Check size flags */
786 if (pArena
->size
& ARENA_FLAG_FREE
)
788 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
789 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
791 /* Check arena size */
792 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
794 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
795 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
798 /* Check next arena PREV_FREE flag */
799 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
800 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
802 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
803 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
806 /* Check prev free arena */
807 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
809 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
810 /* Check prev pointer */
811 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
813 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
814 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
817 /* Check that prev arena is free */
818 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
819 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
821 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
822 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
825 /* Check that prev arena is really the previous block */
826 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
828 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
829 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
837 /***********************************************************************
839 * Checks whether the pointer points to a block inside a given heap.
842 * Should this return BOOL32?
848 int HEAP_IsInsideHeap(
849 HANDLE heap
, /* [in] Heap */
850 DWORD flags
, /* [in] Flags */
851 LPCVOID ptr
/* [in] Pointer */
853 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
857 /* Validate the parameters */
859 if (!heapPtr
) return 0;
860 flags
|= heapPtr
->flags
;
861 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
862 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
863 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
864 + sizeof(ARENA_INUSE
))));
865 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
870 /***********************************************************************
873 * Transform a linear pointer into a SEGPTR. The pointer must have been
874 * allocated from a HEAP_WINE_SEGPTR heap.
876 SEGPTR
HEAP_GetSegptr( HANDLE heap
, DWORD flags
, LPCVOID ptr
)
878 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
882 /* Validate the parameters */
884 if (!heapPtr
) return 0;
885 flags
|= heapPtr
->flags
;
886 if (!(flags
& HEAP_WINE_SEGPTR
))
888 ERR("Heap %08x is not a SEGPTR heap\n",
892 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
894 /* Get the subheap */
896 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
898 ERR("%p is not inside heap %08x\n",
900 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
904 /* Build the SEGPTR */
906 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
907 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
911 /***********************************************************************
912 * HEAP_IsRealArena [Internal]
913 * Validates a block is a valid arena.
919 static BOOL
HEAP_IsRealArena(
920 HANDLE heap
, /* [in] Handle to the heap */
921 DWORD flags
, /* [in] Bit flags that control access during operation */
922 LPCVOID block
, /* [in] Optional pointer to memory block to validate */
923 BOOL quiet
/* [in] Flag - if true, HEAP_ValidateInUseArena
924 * does not complain */
927 HEAP
*heapPtr
= (HEAP
*)(heap
);
930 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
932 ERR("Invalid heap %08x!\n", heap
);
936 flags
&= HEAP_NO_SERIALIZE
;
937 flags
|= heapPtr
->flags
;
938 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
939 if (!(flags
& HEAP_NO_SERIALIZE
))
940 EnterCriticalSection( &heapPtr
->critSection
);
944 /* Only check this single memory block */
946 /* The following code is really HEAP_IsInsideHeap *
947 * with serialization already done. */
948 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
949 ((char *)block
< (char *)subheap
+ subheap
->headerSize
950 + sizeof(ARENA_INUSE
)))
953 ERR("Heap %08lx: block %08lx is not inside heap\n",
954 (DWORD
)heap
, (DWORD
)block
);
955 else if (WARN_ON(heap
))
956 WARN("Heap %08lx: block %08lx is not inside heap\n",
957 (DWORD
)heap
, (DWORD
)block
);
960 ret
= HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1, quiet
);
962 if (!(flags
& HEAP_NO_SERIALIZE
))
963 LeaveCriticalSection( &heapPtr
->critSection
);
967 subheap
= &heapPtr
->subheap
;
968 while (subheap
&& ret
)
970 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
971 while (ptr
< (char *)subheap
+ subheap
->size
)
973 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
975 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
)) {
979 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
983 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
, NOISY
)) {
987 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
990 subheap
= subheap
->next
;
993 if (!(flags
& HEAP_NO_SERIALIZE
))
994 LeaveCriticalSection( &heapPtr
->critSection
);
999 /***********************************************************************
1000 * HeapCreate (KERNEL32.336)
1002 * Handle of heap: Success
1005 HANDLE WINAPI
HeapCreate(
1006 DWORD flags
, /* [in] Heap allocation flag */
1007 DWORD initialSize
, /* [in] Initial heap size */
1008 DWORD maxSize
/* [in] Maximum heap size */
1012 if ( flags
& HEAP_SHARED
) {
1013 WARN( "Shared Heap requested, returning system heap.\n" );
1017 /* Allocate the heap block */
1021 maxSize
= HEAP_DEF_SIZE
;
1022 flags
|= HEAP_GROWABLE
;
1024 if (!(subheap
= HEAP_CreateSubHeap( NULL
, flags
, initialSize
, maxSize
)))
1026 SetLastError( ERROR_OUTOFMEMORY
);
1030 /* link it into the per-process heap list */
1033 HEAP
*heapPtr
= subheap
->heap
;
1034 EnterCriticalSection( &processHeap
->critSection
);
1035 heapPtr
->next
= firstHeap
;
1036 firstHeap
= heapPtr
;
1037 LeaveCriticalSection( &processHeap
->critSection
);
1039 else /* assume the first heap we create is the process main heap */
1041 processHeap
= subheap
->heap
;
1044 return (HANDLE
)subheap
;
1047 /***********************************************************************
1048 * HeapDestroy (KERNEL32.337)
1053 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
1055 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1058 if ( heap
== SystemHeap
) {
1059 WARN( "attempt to destroy system heap, returning TRUE!\n" );
1063 TRACE("%08x\n", heap
);
1064 if (!heapPtr
) return FALSE
;
1066 if (heapPtr
== processHeap
) /* cannot delete the main process heap */
1068 SetLastError( ERROR_INVALID_PARAMETER
);
1071 else /* remove it from the per-process list */
1074 EnterCriticalSection( &processHeap
->critSection
);
1076 while (*pptr
&& *pptr
!= heapPtr
) pptr
= &(*pptr
)->next
;
1077 if (*pptr
) *pptr
= (*pptr
)->next
;
1078 LeaveCriticalSection( &processHeap
->critSection
);
1081 DeleteCriticalSection( &heapPtr
->critSection
);
1082 subheap
= &heapPtr
->subheap
;
1085 SUBHEAP
*next
= subheap
->next
;
1086 if (subheap
->selector
) FreeSelector16( subheap
->selector
);
1087 VirtualFree( subheap
, 0, MEM_RELEASE
);
1094 /***********************************************************************
1095 * HeapAlloc (KERNEL32.334)
1097 * Pointer to allocated memory block
1100 LPVOID WINAPI
HeapAlloc(
1101 HANDLE heap
, /* [in] Handle of private heap block */
1102 DWORD flags
, /* [in] Heap allocation control flags */
1103 DWORD size
/* [in] Number of bytes to allocate */
1106 ARENA_INUSE
*pInUse
;
1108 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1110 /* Validate the parameters */
1112 if (!heapPtr
) return NULL
;
1113 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
1114 flags
|= heapPtr
->flags
;
1115 size
= (size
+ 3) & ~3;
1116 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1118 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1119 /* Locate a suitable free block */
1121 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
1123 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
1124 heap
, flags
, size
);
1125 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1126 SetLastError( ERROR_COMMITMENT_LIMIT
);
1130 /* Remove the arena from the free list */
1132 pArena
->next
->prev
= pArena
->prev
;
1133 pArena
->prev
->next
= pArena
->next
;
1135 /* Build the in-use arena */
1137 pInUse
= (ARENA_INUSE
*)pArena
;
1139 /* in-use arena is smaller than free arena,
1140 * so we have to add the difference to the size */
1141 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1142 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1143 pInUse
->callerEIP
= GET_EIP();
1144 pInUse
->threadId
= GetCurrentTask();
1145 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1147 /* Shrink the block */
1149 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
1151 if (flags
& HEAP_ZERO_MEMORY
)
1152 memset( pInUse
+ 1, 0, pInUse
->size
& ARENA_SIZE_MASK
);
1153 else if (TRACE_ON(heap
))
1154 memset( pInUse
+ 1, ARENA_INUSE_FILLER
, pInUse
->size
& ARENA_SIZE_MASK
);
1156 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1158 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1159 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
1160 return (LPVOID
)(pInUse
+ 1);
1164 /***********************************************************************
1165 * HeapFree (KERNEL32.338)
1170 BOOL WINAPI
HeapFree(
1171 HANDLE heap
, /* [in] Handle of heap */
1172 DWORD flags
, /* [in] Heap freeing flags */
1173 LPVOID ptr
/* [in] Address of memory to free */
1175 ARENA_INUSE
*pInUse
;
1177 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1179 /* Validate the parameters */
1181 if (!heapPtr
) return FALSE
;
1182 if (!ptr
) /* Freeing a NULL ptr is doesn't indicate an error in Win2k */
1184 WARN("(%08x,%08lx,%08lx): asked to free NULL\n",
1185 heap
, flags
, (DWORD
)ptr
);
1189 flags
&= HEAP_NO_SERIALIZE
;
1190 flags
|= heapPtr
->flags
;
1191 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1192 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1194 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1195 SetLastError( ERROR_INVALID_PARAMETER
);
1196 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
1197 heap
, flags
, (DWORD
)ptr
);
1201 /* Turn the block into a free block */
1203 pInUse
= (ARENA_INUSE
*)ptr
- 1;
1204 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
1205 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
1207 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1209 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
1210 heap
, flags
, (DWORD
)ptr
);
1215 /***********************************************************************
1216 * HeapReAlloc (KERNEL32.340)
1218 * Pointer to reallocated memory block
1221 LPVOID WINAPI
HeapReAlloc(
1222 HANDLE heap
, /* [in] Handle of heap block */
1223 DWORD flags
, /* [in] Heap reallocation flags */
1224 LPVOID ptr
, /* [in] Address of memory to reallocate */
1225 DWORD size
/* [in] Number of bytes to reallocate */
1227 ARENA_INUSE
*pArena
;
1232 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
1233 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1235 /* Validate the parameters */
1237 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1238 HEAP_REALLOC_IN_PLACE_ONLY
;
1239 flags
|= heapPtr
->flags
;
1240 size
= (size
+ 3) & ~3;
1241 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1243 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1244 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1246 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1247 SetLastError( ERROR_INVALID_PARAMETER
);
1248 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1249 heap
, flags
, (DWORD
)ptr
, size
);
1253 /* Check if we need to grow the block */
1255 pArena
= (ARENA_INUSE
*)ptr
- 1;
1256 pArena
->threadId
= GetCurrentTask();
1257 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1258 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1261 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1262 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1263 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1264 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1266 /* The next block is free and large enough */
1267 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1268 pFree
->next
->prev
= pFree
->prev
;
1269 pFree
->prev
->next
= pFree
->next
;
1270 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1271 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1272 + size
+ HEAP_MIN_BLOCK_SIZE
))
1274 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1275 SetLastError( ERROR_OUTOFMEMORY
);
1278 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1280 else /* Do it the hard way */
1283 ARENA_INUSE
*pInUse
;
1284 SUBHEAP
*newsubheap
;
1286 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1287 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1289 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1290 SetLastError( ERROR_OUTOFMEMORY
);
1294 /* Build the in-use arena */
1296 pNew
->next
->prev
= pNew
->prev
;
1297 pNew
->prev
->next
= pNew
->next
;
1298 pInUse
= (ARENA_INUSE
*)pNew
;
1299 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1300 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1301 pInUse
->threadId
= GetCurrentTask();
1302 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1303 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1304 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1306 /* Free the previous block */
1308 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1309 subheap
= newsubheap
;
1313 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1315 /* Clear the extra bytes if needed */
1319 if (flags
& HEAP_ZERO_MEMORY
)
1320 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1321 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1322 else if (TRACE_ON(heap
))
1323 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1324 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1327 /* Return the new arena */
1329 pArena
->callerEIP
= GET_EIP();
1330 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1332 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1333 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1334 return (LPVOID
)(pArena
+ 1);
1338 /***********************************************************************
1339 * HeapCompact (KERNEL32.335)
1341 DWORD WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
1343 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1348 /***********************************************************************
1349 * HeapLock (KERNEL32.339)
1350 * Attempts to acquire the critical section object for a specified heap.
1356 BOOL WINAPI
HeapLock(
1357 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
1359 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1360 if (!heapPtr
) return FALSE
;
1361 EnterCriticalSection( &heapPtr
->critSection
);
1366 /***********************************************************************
1367 * HeapUnlock (KERNEL32.342)
1368 * Releases ownership of the critical section object.
1374 BOOL WINAPI
HeapUnlock(
1375 HANDLE heap
/* [in] Handle to the heap to unlock */
1377 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1378 if (!heapPtr
) return FALSE
;
1379 LeaveCriticalSection( &heapPtr
->critSection
);
1384 /***********************************************************************
1385 * HeapSize (KERNEL32.341)
1387 * Size in bytes of allocated memory
1388 * 0xffffffff: Failure
1390 DWORD WINAPI
HeapSize(
1391 HANDLE heap
, /* [in] Handle of heap */
1392 DWORD flags
, /* [in] Heap size control flags */
1393 LPVOID ptr
/* [in] Address of memory to return size for */
1396 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1398 if (!heapPtr
) return FALSE
;
1399 flags
&= HEAP_NO_SERIALIZE
;
1400 flags
|= heapPtr
->flags
;
1401 if (!(flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1402 if (!HEAP_IsRealArena( heap
, HEAP_NO_SERIALIZE
, ptr
, QUIET
))
1404 SetLastError( ERROR_INVALID_PARAMETER
);
1409 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1410 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1412 if (!(flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1414 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
1415 heap
, flags
, (DWORD
)ptr
, ret
);
1420 /***********************************************************************
1421 * HeapValidate (KERNEL32.343)
1422 * Validates a specified heap.
1431 BOOL WINAPI
HeapValidate(
1432 HANDLE heap
, /* [in] Handle to the heap */
1433 DWORD flags
, /* [in] Bit flags that control access during operation */
1434 LPCVOID block
/* [in] Optional pointer to memory block to validate */
1437 return HEAP_IsRealArena( heap
, flags
, block
, QUIET
);
1441 /***********************************************************************
1442 * HeapWalk (KERNEL32.344)
1443 * Enumerates the memory blocks in a specified heap.
1444 * See HEAP_Dump() for info on heap structure.
1447 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
1448 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
1454 BOOL WINAPI
HeapWalk(
1455 HANDLE heap
, /* [in] Handle to heap to enumerate */
1456 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
1458 HEAP
*heapPtr
= HEAP_GetPtr(heap
);
1459 SUBHEAP
*sub
, *currentheap
= NULL
;
1462 int region_index
= 0;
1464 if (!heapPtr
|| !entry
)
1466 SetLastError(ERROR_INVALID_PARAMETER
);
1470 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) EnterCriticalSection( &heapPtr
->critSection
);
1472 /* set ptr to the next arena to be examined */
1474 if (!entry
->lpData
) /* first call (init) ? */
1476 TRACE("begin walking of heap 0x%08x.\n", heap
);
1477 /*HEAP_Dump(heapPtr);*/
1478 currentheap
= &heapPtr
->subheap
;
1479 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1483 ptr
= entry
->lpData
;
1484 sub
= &heapPtr
->subheap
;
1487 if (((char *)ptr
>= (char *)sub
) &&
1488 ((char *)ptr
< (char *)sub
+ sub
->size
))
1496 if (currentheap
== NULL
)
1498 ERR("no matching subheap found, shouldn't happen !\n");
1499 SetLastError(ERROR_NO_MORE_ITEMS
);
1503 ptr
+= entry
->cbData
; /* point to next arena */
1504 if (ptr
> (char *)currentheap
+ currentheap
->size
- 1)
1505 { /* proceed with next subheap */
1506 if (!(currentheap
= currentheap
->next
))
1507 { /* successfully finished */
1508 TRACE("end reached.\n");
1509 SetLastError(ERROR_NO_MORE_ITEMS
);
1512 ptr
= (char*)currentheap
+ currentheap
->headerSize
;
1517 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1519 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
1521 /*TRACE("free, magic: %04x\n", pArena->magic);*/
1523 entry
->lpData
= pArena
+ 1;
1524 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1525 entry
->cbOverhead
= sizeof(ARENA_FREE
);
1526 entry
->wFlags
= PROCESS_HEAP_UNCOMMITTED_RANGE
;
1530 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
1532 /*TRACE("busy, magic: %04x\n", pArena->magic);*/
1534 entry
->lpData
= pArena
+ 1;
1535 entry
->cbData
= pArena
->size
& ARENA_SIZE_MASK
;
1536 entry
->cbOverhead
= sizeof(ARENA_INUSE
);
1537 entry
->wFlags
= PROCESS_HEAP_ENTRY_BUSY
;
1538 /* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
1539 and PROCESS_HEAP_ENTRY_DDESHARE yet */
1542 entry
->iRegionIndex
= region_index
;
1544 /* first element of heap ? */
1545 if (ptr
== (char *)(currentheap
+ currentheap
->headerSize
))
1547 entry
->wFlags
|= PROCESS_HEAP_REGION
;
1548 entry
->u
.Region
.dwCommittedSize
= currentheap
->commitSize
;
1549 entry
->u
.Region
.dwUnCommittedSize
=
1550 currentheap
->size
- currentheap
->commitSize
;
1551 entry
->u
.Region
.lpFirstBlock
= /* first valid block */
1552 currentheap
+ currentheap
->headerSize
;
1553 entry
->u
.Region
.lpLastBlock
= /* first invalid block */
1554 currentheap
+ currentheap
->size
;
1559 if (!(heapPtr
->flags
& HEAP_NO_SERIALIZE
)) LeaveCriticalSection( &heapPtr
->critSection
);
1565 /***********************************************************************
1566 * HEAP_CreateSystemHeap
1568 * Create the system heap.
1570 BOOL
HEAP_CreateSystemHeap(void)
1572 SYSTEM_HEAP_DESCR
*descr
;
1577 HANDLE map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
1578 0, HEAP_DEF_SIZE
, "__SystemHeap" );
1579 if (!map
) return FALSE
;
1580 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
1582 if (!(heapPtr
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
1584 /* pre-defined address not available, use any one */
1585 fprintf( stderr
, "Warning: system heap base address %p not available\n",
1587 if (!(heapPtr
= MapViewOfFile( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0 )))
1593 heap
= (HANDLE
)heapPtr
;
1595 if (created
) /* newly created heap */
1597 HEAP_InitSubHeap( heapPtr
, heapPtr
, HEAP_SHARED
, 0, HEAP_DEF_SIZE
);
1599 descr
= heapPtr
->private = HeapAlloc( heap
, HEAP_ZERO_MEMORY
, sizeof(*descr
) );
1604 /* wait for the heap to be initialized */
1605 while (!heapPtr
->private) Sleep(1);
1607 /* remap it to the right address if necessary */
1608 if (heapPtr
->subheap
.heap
!= heapPtr
)
1610 void *base
= heapPtr
->subheap
.heap
;
1612 UnmapViewOfFile( heapPtr
);
1613 if (!(heapPtr
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, base
)))
1615 fprintf( stderr
, "Couldn't map system heap at the correct address (%p)\n", base
);
1619 heap
= (HANDLE
)heapPtr
;
1622 descr
= heapPtr
->private;
1626 SystemHeapDescr
= descr
;
1633 /***********************************************************************
1634 * GetProcessHeap (KERNEL32.259)
1636 HANDLE WINAPI
GetProcessHeap(void)
1638 return (HANDLE
)processHeap
;
1642 /***********************************************************************
1643 * GetProcessHeaps (KERNEL32.376)
1645 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
1650 if (!processHeap
) return 0; /* should never happen */
1651 total
= 1; /* main heap */
1652 EnterCriticalSection( &processHeap
->critSection
);
1653 for (ptr
= firstHeap
; ptr
; ptr
= ptr
->next
) total
++;
1656 *heaps
++ = (HANDLE
)processHeap
;
1657 for (ptr
= firstHeap
; ptr
; ptr
= ptr
->next
) *heaps
++ = (HANDLE
)ptr
;
1659 LeaveCriticalSection( &processHeap
->critSection
);
1664 /***********************************************************************
1665 * 32-bit local heap functions (Win95; undocumented)
1668 #define HTABLE_SIZE 0x10000
1669 #define HTABLE_PAGESIZE 0x1000
1670 #define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
1672 #include "pshpack1.h"
1673 typedef struct _LOCAL32HEADER
1675 WORD freeListFirst
[HTABLE_NPAGES
];
1676 WORD freeListSize
[HTABLE_NPAGES
];
1677 WORD freeListLast
[HTABLE_NPAGES
];
1679 DWORD selectorTableOffset
;
1680 WORD selectorTableSize
;
1693 #include "poppack.h"
1695 #define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
1697 /***********************************************************************
1698 * Local32Init (KERNEL.208)
1700 HANDLE WINAPI
Local32Init16( WORD segment
, DWORD tableSize
,
1701 DWORD heapSize
, DWORD flags
)
1703 DWORD totSize
, segSize
= 0;
1705 LOCAL32HEADER
*header
;
1707 WORD
*selectorTable
;
1708 WORD selectorEven
, selectorOdd
;
1711 /* Determine new heap size */
1715 if ( (segSize
= GetSelectorLimit16( segment
)) == 0 )
1721 if ( heapSize
== -1L )
1722 heapSize
= 1024L*1024L; /* FIXME */
1724 heapSize
= (heapSize
+ 0xffff) & 0xffff0000;
1725 segSize
= (segSize
+ 0x0fff) & 0xfffff000;
1726 totSize
= segSize
+ HTABLE_SIZE
+ heapSize
;
1729 /* Allocate memory and initialize heap */
1731 if ( !(base
= VirtualAlloc( NULL
, totSize
, MEM_RESERVE
, PAGE_READWRITE
)) )
1734 if ( !VirtualAlloc( base
, segSize
+ HTABLE_PAGESIZE
,
1735 MEM_COMMIT
, PAGE_READWRITE
) )
1737 VirtualFree( base
, 0, MEM_RELEASE
);
1741 heap
= (HEAP
*)(base
+ segSize
+ HTABLE_SIZE
);
1742 if ( !HEAP_InitSubHeap( heap
, (LPVOID
)heap
, 0, 0x10000, heapSize
) )
1744 VirtualFree( base
, 0, MEM_RELEASE
);
1749 /* Set up header and handle table */
1751 header
= (LOCAL32HEADER
*)(base
+ segSize
);
1752 header
->base
= base
;
1753 header
->limit
= HTABLE_PAGESIZE
-1;
1755 header
->magic
= LOCAL32_MAGIC
;
1756 header
->heap
= (HANDLE
)heap
;
1758 header
->freeListFirst
[0] = sizeof(LOCAL32HEADER
);
1759 header
->freeListLast
[0] = HTABLE_PAGESIZE
- 4;
1760 header
->freeListSize
[0] = (HTABLE_PAGESIZE
- sizeof(LOCAL32HEADER
)) / 4;
1762 for (i
= header
->freeListFirst
[0]; i
< header
->freeListLast
[0]; i
+= 4)
1763 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1765 header
->freeListFirst
[1] = 0xffff;
1768 /* Set up selector table */
1770 nrBlocks
= (totSize
+ 0x7fff) >> 15;
1771 selectorTable
= (LPWORD
) HeapAlloc( header
->heap
, 0, nrBlocks
* 2 );
1772 selectorEven
= SELECTOR_AllocBlock( base
, totSize
, WINE_LDT_FLAGS_DATA
);
1773 selectorOdd
= SELECTOR_AllocBlock( base
+ 0x8000, totSize
- 0x8000, WINE_LDT_FLAGS_DATA
);
1774 if ( !selectorTable
|| !selectorEven
|| !selectorOdd
)
1776 if ( selectorTable
) HeapFree( header
->heap
, 0, selectorTable
);
1777 if ( selectorEven
) SELECTOR_FreeBlock( selectorEven
);
1778 if ( selectorOdd
) SELECTOR_FreeBlock( selectorOdd
);
1779 HeapDestroy( header
->heap
);
1780 VirtualFree( base
, 0, MEM_RELEASE
);
1784 header
->selectorTableOffset
= (LPBYTE
)selectorTable
- header
->base
;
1785 header
->selectorTableSize
= nrBlocks
* 4; /* ??? Win95 does it this way! */
1786 header
->selectorDelta
= selectorEven
- selectorOdd
;
1787 header
->segment
= segment
? segment
: selectorEven
;
1789 for (i
= 0; i
< nrBlocks
; i
++)
1790 selectorTable
[i
] = (i
& 1)? selectorOdd
+ ((i
>> 1) << __AHSHIFT
)
1791 : selectorEven
+ ((i
>> 1) << __AHSHIFT
);
1793 /* Move old segment */
1797 /* FIXME: This is somewhat ugly and relies on implementation
1798 details about 16-bit global memory handles ... */
1800 LPBYTE oldBase
= (LPBYTE
)GetSelectorBase( segment
);
1801 memcpy( base
, oldBase
, segSize
);
1802 GLOBAL_MoveBlock( segment
, base
, totSize
);
1803 HeapFree( SystemHeap
, 0, oldBase
);
1806 return (HANDLE
)header
;
1809 /***********************************************************************
1810 * Local32_SearchHandle
1812 static LPDWORD
Local32_SearchHandle( LOCAL32HEADER
*header
, DWORD addr
)
1816 for ( handle
= (LPDWORD
)((LPBYTE
)header
+ sizeof(LOCAL32HEADER
));
1817 handle
< (LPDWORD
)((LPBYTE
)header
+ header
->limit
);
1820 if (*handle
== addr
)
1827 /***********************************************************************
1830 static VOID
Local32_ToHandle( LOCAL32HEADER
*header
, INT16 type
,
1831 DWORD addr
, LPDWORD
*handle
, LPBYTE
*ptr
)
1838 case -2: /* 16:16 pointer, no handles */
1839 *ptr
= PTR_SEG_TO_LIN( addr
);
1840 *handle
= (LPDWORD
)*ptr
;
1843 case -1: /* 32-bit offset, no handles */
1844 *ptr
= header
->base
+ addr
;
1845 *handle
= (LPDWORD
)*ptr
;
1848 case 0: /* handle */
1849 if ( addr
>= sizeof(LOCAL32HEADER
)
1850 && addr
< header
->limit
&& !(addr
& 3)
1851 && *(LPDWORD
)((LPBYTE
)header
+ addr
) >= HTABLE_SIZE
)
1853 *handle
= (LPDWORD
)((LPBYTE
)header
+ addr
);
1854 *ptr
= header
->base
+ **handle
;
1858 case 1: /* 16:16 pointer */
1859 *ptr
= PTR_SEG_TO_LIN( addr
);
1860 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1863 case 2: /* 32-bit offset */
1864 *ptr
= header
->base
+ addr
;
1865 *handle
= Local32_SearchHandle( header
, *ptr
- header
->base
);
1870 /***********************************************************************
1871 * Local32_FromHandle
1873 static VOID
Local32_FromHandle( LOCAL32HEADER
*header
, INT16 type
,
1874 DWORD
*addr
, LPDWORD handle
, LPBYTE ptr
)
1878 case -2: /* 16:16 pointer */
1881 WORD
*selTable
= (LPWORD
)(header
->base
+ header
->selectorTableOffset
);
1882 DWORD offset
= (LPBYTE
)ptr
- header
->base
;
1883 *addr
= MAKELONG( offset
& 0x7fff, selTable
[offset
>> 15] );
1887 case -1: /* 32-bit offset */
1889 *addr
= ptr
- header
->base
;
1892 case 0: /* handle */
1893 *addr
= (LPBYTE
)handle
- (LPBYTE
)header
;
1898 /***********************************************************************
1899 * Local32Alloc (KERNEL.209)
1901 DWORD WINAPI
Local32Alloc16( HANDLE heap
, DWORD size
, INT16 type
, DWORD flags
)
1903 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1908 /* Allocate memory */
1909 ptr
= HeapAlloc( header
->heap
,
1910 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0, size
);
1914 /* Allocate handle if requested */
1919 /* Find first page of handle table with free slots */
1920 for (page
= 0; page
< HTABLE_NPAGES
; page
++)
1921 if (header
->freeListFirst
[page
] != 0)
1923 if (page
== HTABLE_NPAGES
)
1925 WARN("Out of handles!\n" );
1926 HeapFree( header
->heap
, 0, ptr
);
1930 /* If virgin page, initialize it */
1931 if (header
->freeListFirst
[page
] == 0xffff)
1933 if ( !VirtualAlloc( (LPBYTE
)header
+ (page
<< 12),
1934 0x1000, MEM_COMMIT
, PAGE_READWRITE
) )
1936 WARN("Cannot grow handle table!\n" );
1937 HeapFree( header
->heap
, 0, ptr
);
1941 header
->limit
+= HTABLE_PAGESIZE
;
1943 header
->freeListFirst
[page
] = 0;
1944 header
->freeListLast
[page
] = HTABLE_PAGESIZE
- 4;
1945 header
->freeListSize
[page
] = HTABLE_PAGESIZE
/ 4;
1947 for (i
= 0; i
< HTABLE_PAGESIZE
; i
+= 4)
1948 *(DWORD
*)((LPBYTE
)header
+ i
) = i
+4;
1950 if (page
< HTABLE_NPAGES
-1)
1951 header
->freeListFirst
[page
+1] = 0xffff;
1954 /* Allocate handle slot from page */
1955 handle
= (LPDWORD
)((LPBYTE
)header
+ header
->freeListFirst
[page
]);
1956 if (--header
->freeListSize
[page
] == 0)
1957 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = 0;
1959 header
->freeListFirst
[page
] = *handle
;
1961 /* Store 32-bit offset in handle slot */
1962 *handle
= ptr
- header
->base
;
1966 handle
= (LPDWORD
)ptr
;
1971 /* Convert handle to requested output type */
1972 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
1976 /***********************************************************************
1977 * Local32ReAlloc (KERNEL.210)
1979 DWORD WINAPI
Local32ReAlloc16( HANDLE heap
, DWORD addr
, INT16 type
,
1980 DWORD size
, DWORD flags
)
1982 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
1987 return Local32Alloc16( heap
, size
, type
, flags
);
1989 /* Retrieve handle and pointer */
1990 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
1991 if (!handle
) return FALSE
;
1993 /* Reallocate memory block */
1994 ptr
= HeapReAlloc( header
->heap
,
1995 (flags
& LMEM_MOVEABLE
)? HEAP_ZERO_MEMORY
: 0,
2001 *handle
= ptr
- header
->base
;
2003 handle
= (LPDWORD
)ptr
;
2005 /* Convert handle to requested output type */
2006 Local32_FromHandle( header
, type
, &addr
, handle
, ptr
);
2010 /***********************************************************************
2011 * Local32Free (KERNEL.211)
2013 BOOL WINAPI
Local32Free16( HANDLE heap
, DWORD addr
, INT16 type
)
2015 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2019 /* Retrieve handle and pointer */
2020 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2021 if (!handle
) return FALSE
;
2023 /* Free handle if necessary */
2026 int offset
= (LPBYTE
)handle
- (LPBYTE
)header
;
2027 int page
= offset
>> 12;
2029 /* Return handle slot to page free list */
2030 if (header
->freeListSize
[page
]++ == 0)
2031 header
->freeListFirst
[page
] = header
->freeListLast
[page
] = offset
;
2033 *(LPDWORD
)((LPBYTE
)header
+ header
->freeListLast
[page
]) = offset
,
2034 header
->freeListLast
[page
] = offset
;
2038 /* Shrink handle table when possible */
2039 while (page
> 0 && header
->freeListSize
[page
] == HTABLE_PAGESIZE
/ 4)
2041 if ( VirtualFree( (LPBYTE
)header
+
2042 (header
->limit
& ~(HTABLE_PAGESIZE
-1)),
2043 HTABLE_PAGESIZE
, MEM_DECOMMIT
) )
2046 header
->limit
-= HTABLE_PAGESIZE
;
2047 header
->freeListFirst
[page
] = 0xffff;
2053 return HeapFree( header
->heap
, 0, ptr
);
2056 /***********************************************************************
2057 * Local32Translate (KERNEL.213)
2059 DWORD WINAPI
Local32Translate16( HANDLE heap
, DWORD addr
, INT16 type1
, INT16 type2
)
2061 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2065 Local32_ToHandle( header
, type1
, addr
, &handle
, &ptr
);
2066 if (!handle
) return 0;
2068 Local32_FromHandle( header
, type2
, &addr
, handle
, ptr
);
2072 /***********************************************************************
2073 * Local32Size (KERNEL.214)
2075 DWORD WINAPI
Local32Size16( HANDLE heap
, DWORD addr
, INT16 type
)
2077 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2081 Local32_ToHandle( header
, type
, addr
, &handle
, &ptr
);
2082 if (!handle
) return 0;
2084 return HeapSize( header
->heap
, 0, ptr
);
2087 /***********************************************************************
2088 * Local32ValidHandle (KERNEL.215)
2090 BOOL WINAPI
Local32ValidHandle16( HANDLE heap
, WORD addr
)
2092 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2096 Local32_ToHandle( header
, 0, addr
, &handle
, &ptr
);
2097 return handle
!= NULL
;
2100 /***********************************************************************
2101 * Local32GetSegment (KERNEL.229)
2103 WORD WINAPI
Local32GetSegment16( HANDLE heap
)
2105 LOCAL32HEADER
*header
= (LOCAL32HEADER
*)heap
;
2106 return header
->segment
;
2109 /***********************************************************************
2112 static LOCAL32HEADER
*Local32_GetHeap( HGLOBAL16 handle
)
2114 WORD selector
= GlobalHandleToSel16( handle
);
2115 DWORD base
= GetSelectorBase( selector
);
2116 DWORD limit
= GetSelectorLimit16( selector
);
2118 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
2121 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2122 return (LOCAL32HEADER
*)base
;
2127 if ( limit
> 0x10000 && ((LOCAL32HEADER
*)base
)->magic
== LOCAL32_MAGIC
)
2128 return (LOCAL32HEADER
*)base
;
2133 /***********************************************************************
2134 * Local32Info (KERNEL.444) (TOOLHELP.84)
2136 BOOL16 WINAPI
Local32Info16( LOCAL32INFO
*pLocal32Info
, HGLOBAL16 handle
)
2142 LOCAL32HEADER
*header
= Local32_GetHeap( handle
);
2143 if ( !header
) return FALSE
;
2145 if ( !pLocal32Info
|| pLocal32Info
->dwSize
< sizeof(LOCAL32INFO
) )
2148 heapPtr
= (SUBHEAP
*)HEAP_GetPtr( header
->heap
);
2149 pLocal32Info
->dwMemReserved
= heapPtr
->size
;
2150 pLocal32Info
->dwMemCommitted
= heapPtr
->commitSize
;
2151 pLocal32Info
->dwTotalFree
= 0L;
2152 pLocal32Info
->dwLargestFreeBlock
= 0L;
2154 /* Note: Local32 heaps always have only one subheap! */
2155 ptr
= (LPBYTE
)heapPtr
+ heapPtr
->headerSize
;
2156 while ( ptr
< (LPBYTE
)heapPtr
+ heapPtr
->size
)
2158 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
2160 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
2161 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
2162 ptr
+= sizeof(*pArena
) + size
;
2164 pLocal32Info
->dwTotalFree
+= size
;
2165 if ( size
> pLocal32Info
->dwLargestFreeBlock
)
2166 pLocal32Info
->dwLargestFreeBlock
= size
;
2170 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
2171 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
);
2172 ptr
+= sizeof(*pArena
) + size
;
2176 pLocal32Info
->dwcFreeHandles
= 0;
2177 for ( i
= 0; i
< HTABLE_NPAGES
; i
++ )
2179 if ( header
->freeListFirst
[i
] == 0xffff ) break;
2180 pLocal32Info
->dwcFreeHandles
+= header
->freeListSize
[i
];
2182 pLocal32Info
->dwcFreeHandles
+= (HTABLE_NPAGES
- i
) * HTABLE_PAGESIZE
/4;
2187 /***********************************************************************
2188 * Local32First (KERNEL.445) (TOOLHELP.85)
2190 BOOL16 WINAPI
Local32First16( LOCAL32ENTRY
*pLocal32Entry
, HGLOBAL16 handle
)
2192 FIXME("(%p, %04X): stub!\n", pLocal32Entry
, handle
);
2196 /***********************************************************************
2197 * Local32Next (KERNEL.446) (TOOLHELP.86)
2199 BOOL16 WINAPI
Local32Next16( LOCAL32ENTRY
*pLocal32Entry
)
2201 FIXME("(%p): stub!\n", pLocal32Entry
);