blob: 4ce0ded99e27389e8c8c10a79e206548a7c51586 [file] [log] [blame]
Alexandre Julliard329f0681996-04-14 13:21:20 +00001/*
2 * Win32 heap functions
3 *
4 * Copyright 1996 Alexandre Julliard
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00005 * Copyright 1998 Ulrich Weigand
Alexandre Julliard329f0681996-04-14 13:21:20 +00006 */
7
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00008#include <assert.h>
Alexandre Julliard329f0681996-04-14 13:21:20 +00009#include <stdlib.h>
10#include <string.h>
Marcus Meissner04c3e1d1999-02-19 10:37:02 +000011#include "wine/winbase16.h"
12#include "wine/winestring.h"
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +000013#include "selectors.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000014#include "global.h"
Alexandre Julliard329f0681996-04-14 13:21:20 +000015#include "winbase.h"
16#include "winerror.h"
17#include "winnt.h"
Alexandre Julliard02e90081998-01-04 17:49:09 +000018#include "heap.h"
Ulrich Weigand416d39e1998-12-01 14:45:37 +000019#include "toolhelp.h"
Alexandre Julliard15657091999-05-23 10:25:25 +000020#include "debugtools.h"
Alexandre Julliard329f0681996-04-14 13:21:20 +000021
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000022DEFAULT_DEBUG_CHANNEL(heap)
23
Alexandre Julliard329f0681996-04-14 13:21:20 +000024/* Note: the heap data structures are based on what Pietrek describes in his
25 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
26 * the same, but could be easily adapted if it turns out some programs
27 * require it.
28 */
29
30typedef struct tagARENA_INUSE
31{
32 DWORD size; /* Block size; must be the first field */
33 WORD threadId; /* Allocating thread id */
34 WORD magic; /* Magic number */
Ove Kaaven2d127431999-04-25 09:09:15 +000035 void *callerEIP; /* EIP of caller upon allocation */
Alexandre Julliard329f0681996-04-14 13:21:20 +000036} ARENA_INUSE;
37
38typedef struct tagARENA_FREE
39{
40 DWORD size; /* Block size; must be the first field */
41 WORD threadId; /* Freeing thread id */
42 WORD magic; /* Magic number */
43 struct tagARENA_FREE *next; /* Next free arena */
44 struct tagARENA_FREE *prev; /* Prev free arena */
45} ARENA_FREE;
46
47#define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
48#define ARENA_FLAG_PREV_FREE 0x00000002
49#define ARENA_SIZE_MASK 0xfffffffc
50#define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
51#define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
52
53#define ARENA_INUSE_FILLER 0x55
54#define ARENA_FREE_FILLER 0xaa
55
56#define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
57
58/* Max size of the blocks on the free lists */
59static const DWORD HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
60{
61 0x20, 0x80, 0x200, 0xffffffff
62};
63
64typedef struct
65{
66 DWORD size;
67 ARENA_FREE arena;
68} FREE_LIST_ENTRY;
69
70struct tagHEAP;
71
72typedef struct tagSUBHEAP
73{
74 DWORD size; /* Size of the whole sub-heap */
75 DWORD commitSize; /* Committed size of the sub-heap */
76 DWORD headerSize; /* Size of the heap header */
77 struct tagSUBHEAP *next; /* Next sub-heap */
78 struct tagHEAP *heap; /* Main heap structure */
79 DWORD magic; /* Magic number */
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +000080 WORD selector; /* Selector for HEAP_WINE_SEGPTR heaps */
Alexandre Julliard329f0681996-04-14 13:21:20 +000081} SUBHEAP;
82
83#define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
84
85typedef struct tagHEAP
86{
87 SUBHEAP subheap; /* First sub-heap */
88 struct tagHEAP *next; /* Next heap for this process */
89 FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
90 CRITICAL_SECTION critSection; /* Critical section for serialization */
91 DWORD flags; /* Heap flags */
92 DWORD magic; /* Magic number */
93} HEAP;
94
95#define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
96
97#define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
98#define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
99
Alexandre Julliarda3960291999-02-26 11:11:13 +0000100HANDLE SystemHeap = 0;
101HANDLE SegptrHeap = 0;
Alexandre Julliard02e90081998-01-04 17:49:09 +0000102
Alexandre Julliard329f0681996-04-14 13:21:20 +0000103
Ove Kaaven2d127431999-04-25 09:09:15 +0000104#ifdef __GNUC__
Alexandre Julliardb6b8bf81999-04-25 19:04:42 +0000105#define GET_EIP() (__builtin_return_address(0))
Ove Kaaven2d127431999-04-25 09:09:15 +0000106#define SET_EIP(ptr) ((ARENA_INUSE*)(ptr) - 1)->callerEIP = GET_EIP()
107#else
108#define GET_EIP() 0
109#define SET_EIP(ptr) /* nothing */
110#endif /* __GNUC__ */
111
Alexandre Julliard329f0681996-04-14 13:21:20 +0000112/***********************************************************************
113 * HEAP_Dump
114 */
115void HEAP_Dump( HEAP *heap )
116{
117 int i;
118 SUBHEAP *subheap;
119 char *ptr;
120
Alexandre Julliard15657091999-05-23 10:25:25 +0000121 DPRINTF( "Heap: %08lx\n", (DWORD)heap );
122 DPRINTF( "Next: %08lx Sub-heaps: %08lx",
Alexandre Julliard54c27111998-03-29 19:44:57 +0000123 (DWORD)heap->next, (DWORD)&heap->subheap );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000124 subheap = &heap->subheap;
125 while (subheap->next)
126 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000127 DPRINTF( " -> %08lx", (DWORD)subheap->next );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000128 subheap = subheap->next;
129 }
130
Alexandre Julliard15657091999-05-23 10:25:25 +0000131 DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000132 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
Alexandre Julliard15657091999-05-23 10:25:25 +0000133 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +0000134 (DWORD)&heap->freeList[i].arena, heap->freeList[i].arena.size,
135 heap->freeList[i].arena.threadId,
136 (DWORD)heap->freeList[i].arena.prev,
137 (DWORD)heap->freeList[i].arena.next );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000138
139 subheap = &heap->subheap;
140 while (subheap)
141 {
142 DWORD freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
Alexandre Julliard15657091999-05-23 10:25:25 +0000143 DPRINTF( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +0000144 (DWORD)subheap, subheap->size, subheap->commitSize );
145
Alexandre Julliard15657091999-05-23 10:25:25 +0000146 DPRINTF( "\n Block Stat Size Id\n" );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000147 ptr = (char*)subheap + subheap->headerSize;
148 while (ptr < (char *)subheap + subheap->size)
149 {
150 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
151 {
152 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
Alexandre Julliard15657091999-05-23 10:25:25 +0000153 DPRINTF( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +0000154 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
155 pArena->threadId, (DWORD)pArena->prev,
156 (DWORD)pArena->next);
Alexandre Julliard329f0681996-04-14 13:21:20 +0000157 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
158 arenaSize += sizeof(ARENA_FREE);
159 freeSize += pArena->size & ARENA_SIZE_MASK;
160 }
161 else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
162 {
163 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
Alexandre Julliard15657091999-05-23 10:25:25 +0000164 DPRINTF( "%08lx Used %08lx %04x back=%08lx EIP=%p\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +0000165 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
166 pArena->threadId, *((DWORD *)pArena - 1),
167 pArena->callerEIP );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000168 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
169 arenaSize += sizeof(ARENA_INUSE);
170 usedSize += pArena->size & ARENA_SIZE_MASK;
171 }
172 else
173 {
174 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
Alexandre Julliard15657091999-05-23 10:25:25 +0000175 DPRINTF( "%08lx used %08lx %04x EIP=%p\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +0000176 (DWORD)pArena, pArena->size & ARENA_SIZE_MASK,
177 pArena->threadId, pArena->callerEIP );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000178 ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
179 arenaSize += sizeof(ARENA_INUSE);
180 usedSize += pArena->size & ARENA_SIZE_MASK;
181 }
182 }
Alexandre Julliard15657091999-05-23 10:25:25 +0000183 DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +0000184 subheap->size, subheap->commitSize, freeSize, usedSize,
185 arenaSize, (arenaSize * 100) / subheap->size );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000186 subheap = subheap->next;
187 }
188}
189
190
191/***********************************************************************
192 * HEAP_GetPtr
Alexandre Julliard54c27111998-03-29 19:44:57 +0000193 * RETURNS
194 * Pointer to the heap
195 * NULL: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +0000196 */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000197static HEAP *HEAP_GetPtr(
Alexandre Julliarda3960291999-02-26 11:11:13 +0000198 HANDLE heap /* [in] Handle to the heap */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000199) {
Alexandre Julliard329f0681996-04-14 13:21:20 +0000200 HEAP *heapPtr = (HEAP *)heap;
201 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
202 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000203 ERR("Invalid heap %08x!\n", heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000204 SetLastError( ERROR_INVALID_HANDLE );
205 return NULL;
206 }
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000207 if (TRACE_ON(heap) && !HeapValidate( heap, 0, NULL ))
Alexandre Julliard329f0681996-04-14 13:21:20 +0000208 {
209 HEAP_Dump( heapPtr );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000210 assert( FALSE );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000211 SetLastError( ERROR_INVALID_HANDLE );
212 return NULL;
213 }
214 return heapPtr;
215}
216
217
218/***********************************************************************
219 * HEAP_InsertFreeBlock
220 *
221 * Insert a free block into the free list.
222 */
223static void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena )
224{
225 FREE_LIST_ENTRY *pEntry = heap->freeList;
226 while (pEntry->size < pArena->size) pEntry++;
227 pArena->size |= ARENA_FLAG_FREE;
228 pArena->next = pEntry->arena.next;
229 pArena->next->prev = pArena;
230 pArena->prev = &pEntry->arena;
231 pEntry->arena.next = pArena;
232}
233
234
235/***********************************************************************
236 * HEAP_FindSubHeap
Alexandre Julliard329f0681996-04-14 13:21:20 +0000237 * Find the sub-heap containing a given address.
Alexandre Julliard54c27111998-03-29 19:44:57 +0000238 *
239 * RETURNS
240 * Pointer: Success
241 * NULL: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +0000242 */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000243static SUBHEAP *HEAP_FindSubHeap(
244 HEAP *heap, /* [in] Heap pointer */
245 LPCVOID ptr /* [in] Address */
246) {
Alexandre Julliard329f0681996-04-14 13:21:20 +0000247 SUBHEAP *sub = &heap->subheap;
248 while (sub)
249 {
250 if (((char *)ptr >= (char *)sub) &&
251 ((char *)ptr < (char *)sub + sub->size)) return sub;
252 sub = sub->next;
253 }
254 return NULL;
255}
256
257
258/***********************************************************************
259 * HEAP_Commit
260 *
261 * Make sure the heap storage is committed up to (not including) ptr.
262 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000263static BOOL HEAP_Commit( SUBHEAP *subheap, void *ptr )
Alexandre Julliard329f0681996-04-14 13:21:20 +0000264{
265 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
Ulrich Weigand73bd8d21999-09-03 16:45:04 +0000266 DWORD pageMask = VIRTUAL_GetPageSize() - 1;
267 size = (size + pageMask) & ~pageMask; /* Align size on a page boundary */
Alexandre Julliard329f0681996-04-14 13:21:20 +0000268 if (size > subheap->size) size = subheap->size;
269 if (size <= subheap->commitSize) return TRUE;
270 if (!VirtualAlloc( (char *)subheap + subheap->commitSize,
271 size - subheap->commitSize, MEM_COMMIT,
272 PAGE_EXECUTE_READWRITE))
273 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000274 WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000275 size - subheap->commitSize,
276 (DWORD)((char *)subheap + subheap->commitSize),
277 (DWORD)subheap->heap );
278 return FALSE;
279 }
280 subheap->commitSize = size;
281 return TRUE;
282}
283
284
285/***********************************************************************
286 * HEAP_Decommit
287 *
288 * If possible, decommit the heap storage from (including) 'ptr'.
289 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000290static BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
Alexandre Julliard329f0681996-04-14 13:21:20 +0000291{
292 DWORD size = (DWORD)((char *)ptr - (char *)subheap);
Ulrich Weigand73bd8d21999-09-03 16:45:04 +0000293 DWORD pageMask = VIRTUAL_GetPageSize() - 1;
294 size = (size + pageMask) & ~pageMask; /* Align size on a page boundary */
Alexandre Julliard329f0681996-04-14 13:21:20 +0000295 if (size >= subheap->commitSize) return TRUE;
Alexandre Julliard21979011997-03-05 08:22:35 +0000296 if (!VirtualFree( (char *)subheap + size,
297 subheap->commitSize - size, MEM_DECOMMIT ))
Alexandre Julliard329f0681996-04-14 13:21:20 +0000298 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000299 WARN("Could not decommit %08lx bytes at %08lx for heap %08lx\n",
Alexandre Julliard21979011997-03-05 08:22:35 +0000300 subheap->commitSize - size,
301 (DWORD)((char *)subheap + size),
Alexandre Julliard329f0681996-04-14 13:21:20 +0000302 (DWORD)subheap->heap );
303 return FALSE;
304 }
305 subheap->commitSize = size;
306 return TRUE;
307}
308
309
310/***********************************************************************
311 * HEAP_CreateFreeBlock
312 *
313 * Create a free block at a specified address. 'size' is the size of the
314 * whole block, including the new arena.
315 */
316static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, DWORD size )
317{
318 ARENA_FREE *pFree;
319
320 /* Create a free arena */
321
322 pFree = (ARENA_FREE *)ptr;
323 pFree->threadId = GetCurrentTask();
324 pFree->magic = ARENA_FREE_MAGIC;
325
326 /* If debugging, erase the freed block content */
327
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000328 if (TRACE_ON(heap))
Alexandre Julliard329f0681996-04-14 13:21:20 +0000329 {
330 char *pEnd = (char *)ptr + size;
331 if (pEnd > (char *)subheap + subheap->commitSize)
332 pEnd = (char *)subheap + subheap->commitSize;
333 if (pEnd > (char *)(pFree + 1))
334 memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
335 }
336
337 /* Check if next block is free also */
338
339 if (((char *)ptr + size < (char *)subheap + subheap->size) &&
340 (*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
341 {
342 /* Remove the next arena from the free list */
343 ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
344 pNext->next->prev = pNext->prev;
345 pNext->prev->next = pNext->next;
346 size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000347 if (TRACE_ON(heap))
Alexandre Julliard329f0681996-04-14 13:21:20 +0000348 memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
349 }
350
351 /* Set the next block PREV_FREE flag and pointer */
352
353 if ((char *)ptr + size < (char *)subheap + subheap->size)
354 {
355 DWORD *pNext = (DWORD *)((char *)ptr + size);
356 *pNext |= ARENA_FLAG_PREV_FREE;
357 *(ARENA_FREE **)(pNext - 1) = pFree;
358 }
359
360 /* Last, insert the new block into the free list */
361
362 pFree->size = size - sizeof(*pFree);
363 HEAP_InsertFreeBlock( subheap->heap, pFree );
364}
365
366
367/***********************************************************************
368 * HEAP_MakeInUseBlockFree
369 *
370 * Turn an in-use block into a free block. Can also decommit the end of
371 * the heap, and possibly even free the sub-heap altogether.
372 */
373static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
374{
375 ARENA_FREE *pFree;
376 DWORD size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
377
378 /* Check if we can merge with previous block */
379
380 if (pArena->size & ARENA_FLAG_PREV_FREE)
381 {
382 pFree = *((ARENA_FREE **)pArena - 1);
383 size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
384 /* Remove it from the free list */
385 pFree->next->prev = pFree->prev;
386 pFree->prev->next = pFree->next;
387 }
388 else pFree = (ARENA_FREE *)pArena;
389
390 /* Create a free block */
391
392 HEAP_CreateFreeBlock( subheap, pFree, size );
393 size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
394 if ((char *)pFree + size < (char *)subheap + subheap->size)
395 return; /* Not the last block, so nothing more to do */
396
397 /* Free the whole sub-heap if it's empty and not the original one */
398
399 if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
400 (subheap != &subheap->heap->subheap))
401 {
Alexandre Julliard3051b641996-07-05 17:14:13 +0000402 SUBHEAP *pPrev = &subheap->heap->subheap;
403 /* Remove the free block from the list */
404 pFree->next->prev = pFree->prev;
405 pFree->prev->next = pFree->next;
406 /* Remove the subheap from the list */
407 while (pPrev && (pPrev->next != subheap)) pPrev = pPrev->next;
408 if (pPrev) pPrev->next = subheap->next;
409 /* Free the memory */
410 subheap->magic = 0;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000411 if (subheap->selector) FreeSelector16( subheap->selector );
Alexandre Julliard3051b641996-07-05 17:14:13 +0000412 VirtualFree( subheap, 0, MEM_RELEASE );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000413 return;
414 }
415
416 /* Decommit the end of the heap */
417
418 HEAP_Decommit( subheap, pFree + 1 );
419}
420
421
422/***********************************************************************
423 * HEAP_ShrinkBlock
424 *
425 * Shrink an in-use block.
426 */
427static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, DWORD size)
428{
429 if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
430 {
431 HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
432 (pArena->size & ARENA_SIZE_MASK) - size );
433 pArena->size = (pArena->size & ~ARENA_SIZE_MASK) | size;
434 }
435 else
436 {
437 /* Turn off PREV_FREE flag in next block */
438 char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
439 if (pNext < (char *)subheap + subheap->size)
440 *(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
441 }
442}
443
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000444/***********************************************************************
445 * HEAP_InitSubHeap
446 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000447static BOOL HEAP_InitSubHeap( HEAP *heap, LPVOID address, DWORD flags,
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000448 DWORD commitSize, DWORD totalSize )
449{
450 SUBHEAP *subheap = (SUBHEAP *)address;
451 WORD selector = 0;
452 FREE_LIST_ENTRY *pEntry;
453 int i;
454
455 /* Commit memory */
456
457 if (!VirtualAlloc(address, commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
458 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000459 WARN("Could not commit %08lx bytes for sub-heap %08lx\n",
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000460 commitSize, (DWORD)address );
461 return FALSE;
462 }
463
464 /* Allocate a selector if needed */
465
466 if (flags & HEAP_WINE_SEGPTR)
467 {
468 selector = SELECTOR_AllocBlock( address, totalSize,
Ulrich Weigandbf5f6931998-10-11 19:12:16 +0000469 (flags & (HEAP_WINE_CODESEG|HEAP_WINE_CODE16SEG))
470 ? SEGMENT_CODE : SEGMENT_DATA,
471 (flags & HEAP_WINE_CODESEG) != 0, FALSE );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000472 if (!selector)
473 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000474 ERR("Could not allocate selector\n" );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000475 return FALSE;
476 }
477 }
478
479 /* Fill the sub-heap structure */
480
481 subheap->heap = heap;
482 subheap->selector = selector;
483 subheap->size = totalSize;
484 subheap->commitSize = commitSize;
485 subheap->magic = SUBHEAP_MAGIC;
486
487 if ( subheap != (SUBHEAP *)heap )
488 {
489 /* If this is a secondary subheap, insert it into list */
490
491 subheap->headerSize = sizeof(SUBHEAP);
492 subheap->next = heap->subheap.next;
493 heap->subheap.next = subheap;
494 }
495 else
496 {
497 /* If this is a primary subheap, initialize main heap */
498
499 subheap->headerSize = sizeof(HEAP);
500 subheap->next = NULL;
501 heap->next = NULL;
502 heap->flags = flags;
503 heap->magic = HEAP_MAGIC;
504
505 /* Build the free lists */
506
507 for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
508 {
509 pEntry->size = HEAP_freeListSizes[i];
510 pEntry->arena.size = 0 | ARENA_FLAG_FREE;
511 pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
512 &heap->freeList[i+1].arena : &heap->freeList[0].arena;
513 pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
514 &heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
515 pEntry->arena.threadId = 0;
516 pEntry->arena.magic = ARENA_FREE_MAGIC;
517 }
518
519 /* Initialize critical section */
520
521 InitializeCriticalSection( &heap->critSection );
James Abbatiello161693e1999-11-07 19:22:46 +0000522 /* FIXME: once separate address spaces are implemented, only */
523 /* the SystemHeap critical section should be global */
524 /* if (!SystemHeap) */
525 MakeCriticalSectionGlobal( &heap->critSection );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000526 }
527
528 /* Create the first free block */
529
530 HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap + subheap->headerSize,
531 subheap->size - subheap->headerSize );
532
533 return TRUE;
534}
Alexandre Julliard329f0681996-04-14 13:21:20 +0000535
536/***********************************************************************
537 * HEAP_CreateSubHeap
538 *
539 * Create a sub-heap of the given size.
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000540 * If heap == NULL, creates a main heap.
Alexandre Julliard329f0681996-04-14 13:21:20 +0000541 */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000542static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, DWORD flags,
543 DWORD commitSize, DWORD totalSize )
Alexandre Julliard329f0681996-04-14 13:21:20 +0000544{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000545 LPVOID address;
Alexandre Julliard329f0681996-04-14 13:21:20 +0000546
547 /* Round-up sizes on a 64K boundary */
548
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000549 if (flags & HEAP_WINE_SEGPTR)
550 {
551 totalSize = commitSize = 0x10000; /* Only 64K at a time for SEGPTRs */
552 }
553 else
554 {
555 totalSize = (totalSize + 0xffff) & 0xffff0000;
556 commitSize = (commitSize + 0xffff) & 0xffff0000;
557 if (!commitSize) commitSize = 0x10000;
558 if (totalSize < commitSize) totalSize = commitSize;
559 }
Alexandre Julliard329f0681996-04-14 13:21:20 +0000560
561 /* Allocate the memory block */
562
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000563 if (!(address = VirtualAlloc( NULL, totalSize,
Alexandre Julliard329f0681996-04-14 13:21:20 +0000564 MEM_RESERVE, PAGE_EXECUTE_READWRITE )))
565 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000566 WARN("Could not VirtualAlloc %08lx bytes\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000567 totalSize );
568 return NULL;
569 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000570
571 /* Initialize subheap */
572
573 if (!HEAP_InitSubHeap( heap? heap : (HEAP *)address,
574 address, flags, commitSize, totalSize ))
Alexandre Julliard329f0681996-04-14 13:21:20 +0000575 {
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000576 VirtualFree( address, 0, MEM_RELEASE );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000577 return NULL;
578 }
579
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000580 return (SUBHEAP *)address;
Alexandre Julliard329f0681996-04-14 13:21:20 +0000581}
582
583
584/***********************************************************************
585 * HEAP_FindFreeBlock
586 *
587 * Find a free block at least as large as the requested size, and make sure
588 * the requested size is committed.
589 */
590static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, DWORD size,
591 SUBHEAP **ppSubHeap )
592{
593 SUBHEAP *subheap;
594 ARENA_FREE *pArena;
595 FREE_LIST_ENTRY *pEntry = heap->freeList;
596
597 /* Find a suitable free list, and in it find a block large enough */
598
599 while (pEntry->size < size) pEntry++;
600 pArena = pEntry->arena.next;
601 while (pArena != &heap->freeList[0].arena)
602 {
603 if (pArena->size > size)
604 {
605 subheap = HEAP_FindSubHeap( heap, pArena );
606 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
607 + size + HEAP_MIN_BLOCK_SIZE))
608 return NULL;
609 *ppSubHeap = subheap;
610 return pArena;
611 }
612
613 pArena = pArena->next;
614 }
615
616 /* If no block was found, attempt to grow the heap */
617
618 if (!(heap->flags & HEAP_GROWABLE))
619 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000620 WARN("Not enough space in heap %08lx for %08lx bytes\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000621 (DWORD)heap, size );
622 return NULL;
623 }
624 size += sizeof(SUBHEAP) + sizeof(ARENA_FREE);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000625 if (!(subheap = HEAP_CreateSubHeap( heap, heap->flags, size,
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000626 MAX( HEAP_DEF_SIZE, size ) )))
Alexandre Julliard329f0681996-04-14 13:21:20 +0000627 return NULL;
628
Alexandre Julliard15657091999-05-23 10:25:25 +0000629 TRACE("created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000630 (DWORD)subheap, size, (DWORD)heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000631
Alexandre Julliard329f0681996-04-14 13:21:20 +0000632 *ppSubHeap = subheap;
633 return (ARENA_FREE *)(subheap + 1);
634}
635
636
637/***********************************************************************
638 * HEAP_IsValidArenaPtr
639 *
640 * Check that the pointer is inside the range possible for arenas.
641 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000642static BOOL HEAP_IsValidArenaPtr( HEAP *heap, void *ptr )
Alexandre Julliard329f0681996-04-14 13:21:20 +0000643{
644 int i;
645 SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
646 if (!subheap) return FALSE;
647 if ((char *)ptr >= (char *)subheap + subheap->headerSize) return TRUE;
648 if (subheap != &heap->subheap) return FALSE;
649 for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
650 if (ptr == (void *)&heap->freeList[i].arena) return TRUE;
651 return FALSE;
652}
653
654
655/***********************************************************************
656 * HEAP_ValidateFreeArena
657 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000658static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
Alexandre Julliard329f0681996-04-14 13:21:20 +0000659{
660 char *heapEnd = (char *)subheap + subheap->size;
661
662 /* Check magic number */
663 if (pArena->magic != ARENA_FREE_MAGIC)
664 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000665 ERR("Heap %08lx: invalid free arena magic for %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000666 (DWORD)subheap->heap, (DWORD)pArena );
667 return FALSE;
668 }
669 /* Check size flags */
670 if (!(pArena->size & ARENA_FLAG_FREE) ||
671 (pArena->size & ARENA_FLAG_PREV_FREE))
672 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000673 ERR("Heap %08lx: bad flags %lx for free arena %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000674 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
675 }
676 /* Check arena size */
677 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
678 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000679 ERR("Heap %08lx: bad size %08lx for free arena %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000680 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
681 return FALSE;
682 }
683 /* Check that next pointer is valid */
684 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
685 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000686 ERR("Heap %08lx: bad next ptr %08lx for arena %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000687 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
688 return FALSE;
689 }
690 /* Check that next arena is free */
691 if (!(pArena->next->size & ARENA_FLAG_FREE) ||
692 (pArena->next->magic != ARENA_FREE_MAGIC))
693 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000694 ERR("Heap %08lx: next arena %08lx invalid for %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000695 (DWORD)subheap->heap, (DWORD)pArena->next, (DWORD)pArena );
696 return FALSE;
697 }
698 /* Check that prev pointer is valid */
699 if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
700 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000701 ERR("Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000702 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
703 return FALSE;
704 }
705 /* Check that prev arena is free */
706 if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
707 (pArena->prev->magic != ARENA_FREE_MAGIC))
708 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000709 ERR("Heap %08lx: prev arena %08lx invalid for %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000710 (DWORD)subheap->heap, (DWORD)pArena->prev, (DWORD)pArena );
711 return FALSE;
712 }
713 /* Check that next block has PREV_FREE flag */
714 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
715 {
716 if (!(*(DWORD *)((char *)(pArena + 1) +
717 (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
718 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000719 ERR("Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000720 (DWORD)subheap->heap, (DWORD)pArena );
721 return FALSE;
722 }
723 /* Check next block back pointer */
724 if (*((ARENA_FREE **)((char *)(pArena + 1) +
725 (pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
726 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000727 ERR("Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000728 (DWORD)subheap->heap, (DWORD)pArena,
729 *((DWORD *)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
730 return FALSE;
731 }
732 }
733 return TRUE;
734}
735
736
737/***********************************************************************
738 * HEAP_ValidateInUseArena
739 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000740static BOOL HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena )
Alexandre Julliard329f0681996-04-14 13:21:20 +0000741{
742 char *heapEnd = (char *)subheap + subheap->size;
743
744 /* Check magic number */
745 if (pArena->magic != ARENA_INUSE_MAGIC)
746 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000747 ERR("Heap %08lx: invalid in-use arena magic for %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000748 (DWORD)subheap->heap, (DWORD)pArena );
749 return FALSE;
750 }
751 /* Check size flags */
752 if (pArena->size & ARENA_FLAG_FREE)
753 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000754 ERR("Heap %08lx: bad flags %lx for in-use arena %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000755 (DWORD)subheap->heap, pArena->size & ~ARENA_SIZE_MASK, (DWORD)pArena );
756 }
757 /* Check arena size */
758 if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
759 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000760 ERR("Heap %08lx: bad size %08lx for in-use arena %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000761 (DWORD)subheap->heap, (DWORD)pArena->size & ARENA_SIZE_MASK, (DWORD)pArena );
762 return FALSE;
763 }
764 /* Check next arena PREV_FREE flag */
765 if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
766 (*(DWORD *)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
767 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000768 ERR("Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000769 (DWORD)subheap->heap, (DWORD)pArena );
770 return FALSE;
771 }
772 /* Check prev free arena */
773 if (pArena->size & ARENA_FLAG_PREV_FREE)
774 {
775 ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
776 /* Check prev pointer */
777 if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
778 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000779 ERR("Heap %08lx: bad back ptr %08lx for arena %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000780 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
781 return FALSE;
782 }
783 /* Check that prev arena is free */
784 if (!(pPrev->size & ARENA_FLAG_FREE) ||
785 (pPrev->magic != ARENA_FREE_MAGIC))
786 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000787 ERR("Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000788 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
789 return FALSE;
790 }
791 /* Check that prev arena is really the previous block */
792 if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
793 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000794 ERR("Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000795 (DWORD)subheap->heap, (DWORD)pPrev, (DWORD)pArena );
796 return FALSE;
797 }
798 }
799 return TRUE;
800}
801
802
803/***********************************************************************
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000804 * HEAP_IsInsideHeap
Alexandre Julliard54c27111998-03-29 19:44:57 +0000805 * Checks whether the pointer points to a block inside a given heap.
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000806 *
Alexandre Julliard54c27111998-03-29 19:44:57 +0000807 * NOTES
808 * Should this return BOOL32?
809 *
810 * RETURNS
811 * !0: Success
812 * 0: Failure
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000813 */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000814int HEAP_IsInsideHeap(
Alexandre Julliarda3960291999-02-26 11:11:13 +0000815 HANDLE heap, /* [in] Heap */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000816 DWORD flags, /* [in] Flags */
817 LPCVOID ptr /* [in] Pointer */
818) {
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000819 HEAP *heapPtr = HEAP_GetPtr( heap );
820 SUBHEAP *subheap;
821 int ret;
822
823 /* Validate the parameters */
824
825 if (!heapPtr) return 0;
826 flags |= heapPtr->flags;
827 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
828 ret = (((subheap = HEAP_FindSubHeap( heapPtr, ptr )) != NULL) &&
829 (((char *)ptr >= (char *)subheap + subheap->headerSize
830 + sizeof(ARENA_INUSE))));
831 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
832 return ret;
833}
834
835
836/***********************************************************************
837 * HEAP_GetSegptr
838 *
839 * Transform a linear pointer into a SEGPTR. The pointer must have been
840 * allocated from a HEAP_WINE_SEGPTR heap.
841 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000842SEGPTR HEAP_GetSegptr( HANDLE heap, DWORD flags, LPCVOID ptr )
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000843{
844 HEAP *heapPtr = HEAP_GetPtr( heap );
845 SUBHEAP *subheap;
846 SEGPTR ret;
847
848 /* Validate the parameters */
849
850 if (!heapPtr) return 0;
851 flags |= heapPtr->flags;
852 if (!(flags & HEAP_WINE_SEGPTR))
853 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000854 ERR("Heap %08x is not a SEGPTR heap\n",
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000855 heap );
856 return 0;
857 }
858 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
859
860 /* Get the subheap */
861
862 if (!(subheap = HEAP_FindSubHeap( heapPtr, ptr )))
863 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000864 ERR("%p is not inside heap %08x\n",
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000865 ptr, heap );
866 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
867 return 0;
868 }
869
870 /* Build the SEGPTR */
871
872 ret = PTR_SEG_OFF_TO_SEGPTR(subheap->selector, (DWORD)ptr-(DWORD)subheap);
873 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
874 return ret;
875}
876
877
878/***********************************************************************
Alexandre Julliard329f0681996-04-14 13:21:20 +0000879 * HeapCreate (KERNEL32.336)
Alexandre Julliard54c27111998-03-29 19:44:57 +0000880 * RETURNS
881 * Handle of heap: Success
882 * NULL: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +0000883 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000884HANDLE WINAPI HeapCreate(
Alexandre Julliard54c27111998-03-29 19:44:57 +0000885 DWORD flags, /* [in] Heap allocation flag */
886 DWORD initialSize, /* [in] Initial heap size */
887 DWORD maxSize /* [in] Maximum heap size */
888) {
Alexandre Julliard329f0681996-04-14 13:21:20 +0000889 SUBHEAP *subheap;
Alexandre Julliard329f0681996-04-14 13:21:20 +0000890
891 /* Allocate the heap block */
892
893 if (!maxSize)
894 {
895 maxSize = HEAP_DEF_SIZE;
896 flags |= HEAP_GROWABLE;
897 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000898 if (!(subheap = HEAP_CreateSubHeap( NULL, flags, initialSize, maxSize )))
Alexandre Julliard329f0681996-04-14 13:21:20 +0000899 {
900 SetLastError( ERROR_OUTOFMEMORY );
901 return 0;
902 }
903
Alexandre Julliarda3960291999-02-26 11:11:13 +0000904 return (HANDLE)subheap;
Alexandre Julliard329f0681996-04-14 13:21:20 +0000905}
906
Alexandre Julliard329f0681996-04-14 13:21:20 +0000907/***********************************************************************
908 * HeapDestroy (KERNEL32.337)
Alexandre Julliard54c27111998-03-29 19:44:57 +0000909 * RETURNS
910 * TRUE: Success
911 * FALSE: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +0000912 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000913BOOL WINAPI HeapDestroy(
914 HANDLE heap /* [in] Handle of heap */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000915) {
Alexandre Julliard329f0681996-04-14 13:21:20 +0000916 HEAP *heapPtr = HEAP_GetPtr( heap );
917 SUBHEAP *subheap;
918
Alexandre Julliard15657091999-05-23 10:25:25 +0000919 TRACE("%08x\n", heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000920 if (!heapPtr) return FALSE;
921
922 DeleteCriticalSection( &heapPtr->critSection );
923 subheap = &heapPtr->subheap;
924 while (subheap)
925 {
926 SUBHEAP *next = subheap->next;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000927 if (subheap->selector) FreeSelector16( subheap->selector );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000928 VirtualFree( subheap, 0, MEM_RELEASE );
929 subheap = next;
930 }
931 return TRUE;
932}
933
934
935/***********************************************************************
936 * HeapAlloc (KERNEL32.334)
Alexandre Julliard54c27111998-03-29 19:44:57 +0000937 * RETURNS
938 * Pointer to allocated memory block
939 * NULL: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +0000940 */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000941LPVOID WINAPI HeapAlloc(
Alexandre Julliarda3960291999-02-26 11:11:13 +0000942 HANDLE heap, /* [in] Handle of private heap block */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000943 DWORD flags, /* [in] Heap allocation control flags */
944 DWORD size /* [in] Number of bytes to allocate */
945) {
Alexandre Julliard329f0681996-04-14 13:21:20 +0000946 ARENA_FREE *pArena;
947 ARENA_INUSE *pInUse;
948 SUBHEAP *subheap;
949 HEAP *heapPtr = HEAP_GetPtr( heap );
950
951 /* Validate the parameters */
952
953 if (!heapPtr) return NULL;
954 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
955 flags |= heapPtr->flags;
956 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
957 size = (size + 3) & ~3;
958 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
959
960 /* Locate a suitable free block */
961
962 if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
963 {
Alexandre Julliard15657091999-05-23 10:25:25 +0000964 TRACE("(%08x,%08lx,%08lx): returning NULL\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000965 heap, flags, size );
966 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000967 SetLastError( ERROR_COMMITMENT_LIMIT );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000968 return NULL;
969 }
970
971 /* Remove the arena from the free list */
972
973 pArena->next->prev = pArena->prev;
974 pArena->prev->next = pArena->next;
975
976 /* Build the in-use arena */
977
978 pInUse = (ARENA_INUSE *)pArena;
979 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
980 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
Ove Kaaven2d127431999-04-25 09:09:15 +0000981 pInUse->callerEIP = GET_EIP();
Alexandre Julliard329f0681996-04-14 13:21:20 +0000982 pInUse->threadId = GetCurrentTask();
983 pInUse->magic = ARENA_INUSE_MAGIC;
984
985 /* Shrink the block */
986
987 HEAP_ShrinkBlock( subheap, pInUse, size );
988
989 if (flags & HEAP_ZERO_MEMORY) memset( pInUse + 1, 0, size );
Alexandre Julliarda69b88b1998-03-15 20:29:56 +0000990 else if (TRACE_ON(heap)) memset( pInUse + 1, ARENA_INUSE_FILLER, size );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000991
992 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +0000993
Alexandre Julliard15657091999-05-23 10:25:25 +0000994 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +0000995 heap, flags, size, (DWORD)(pInUse + 1) );
996 return (LPVOID)(pInUse + 1);
997}
998
999
1000/***********************************************************************
1001 * HeapFree (KERNEL32.338)
Alexandre Julliard54c27111998-03-29 19:44:57 +00001002 * RETURNS
1003 * TRUE: Success
1004 * FALSE: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +00001005 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001006BOOL WINAPI HeapFree(
1007 HANDLE heap, /* [in] Handle of heap */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001008 DWORD flags, /* [in] Heap freeing flags */
1009 LPVOID ptr /* [in] Address of memory to free */
1010) {
Alexandre Julliard329f0681996-04-14 13:21:20 +00001011 ARENA_INUSE *pInUse;
1012 SUBHEAP *subheap;
1013 HEAP *heapPtr = HEAP_GetPtr( heap );
1014
1015 /* Validate the parameters */
1016
1017 if (!heapPtr) return FALSE;
1018 flags &= HEAP_NO_SERIALIZE;
1019 flags |= heapPtr->flags;
1020 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
David Luyer39413f81998-10-11 14:36:56 +00001021 if (!ptr)
1022 {
Alexandre Julliard15657091999-05-23 10:25:25 +00001023 WARN("(%08x,%08lx,%08lx): asked to free NULL\n",
David Luyer39413f81998-10-11 14:36:56 +00001024 heap, flags, (DWORD)ptr );
1025 }
Alexandre Julliardca22b331996-07-12 19:02:39 +00001026 if (!ptr || !HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
Alexandre Julliard329f0681996-04-14 13:21:20 +00001027 {
1028 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1029 SetLastError( ERROR_INVALID_PARAMETER );
Alexandre Julliard15657091999-05-23 10:25:25 +00001030 TRACE("(%08x,%08lx,%08lx): returning FALSE\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +00001031 heap, flags, (DWORD)ptr );
1032 return FALSE;
1033 }
1034
1035 /* Turn the block into a free block */
1036
1037 pInUse = (ARENA_INUSE *)ptr - 1;
1038 subheap = HEAP_FindSubHeap( heapPtr, pInUse );
1039 HEAP_MakeInUseBlockFree( subheap, pInUse );
1040
1041 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001042/* SetLastError( 0 ); */
Alexandre Julliard329f0681996-04-14 13:21:20 +00001043
Alexandre Julliard15657091999-05-23 10:25:25 +00001044 TRACE("(%08x,%08lx,%08lx): returning TRUE\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +00001045 heap, flags, (DWORD)ptr );
1046 return TRUE;
1047}
1048
1049
1050/***********************************************************************
1051 * HeapReAlloc (KERNEL32.340)
Alexandre Julliard54c27111998-03-29 19:44:57 +00001052 * RETURNS
1053 * Pointer to reallocated memory block
1054 * NULL: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +00001055 */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001056LPVOID WINAPI HeapReAlloc(
Alexandre Julliarda3960291999-02-26 11:11:13 +00001057 HANDLE heap, /* [in] Handle of heap block */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001058 DWORD flags, /* [in] Heap reallocation flags */
1059 LPVOID ptr, /* [in] Address of memory to reallocate */
1060 DWORD size /* [in] Number of bytes to reallocate */
1061) {
Alexandre Julliard329f0681996-04-14 13:21:20 +00001062 ARENA_INUSE *pArena;
1063 DWORD oldSize;
1064 HEAP *heapPtr;
1065 SUBHEAP *subheap;
1066
1067 if (!ptr) return HeapAlloc( heap, flags, size ); /* FIXME: correct? */
1068 if (!(heapPtr = HEAP_GetPtr( heap ))) return FALSE;
1069
1070 /* Validate the parameters */
1071
1072 flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
1073 HEAP_REALLOC_IN_PLACE_ONLY;
1074 flags |= heapPtr->flags;
1075 size = (size + 3) & ~3;
1076 if (size < HEAP_MIN_BLOCK_SIZE) size = HEAP_MIN_BLOCK_SIZE;
1077
1078 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
1079 if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
1080 {
1081 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1082 SetLastError( ERROR_INVALID_PARAMETER );
Alexandre Julliard15657091999-05-23 10:25:25 +00001083 TRACE("(%08x,%08lx,%08lx,%08lx): returning NULL\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +00001084 heap, flags, (DWORD)ptr, size );
1085 return NULL;
1086 }
1087
1088 /* Check if we need to grow the block */
1089
1090 pArena = (ARENA_INUSE *)ptr - 1;
1091 pArena->threadId = GetCurrentTask();
1092 subheap = HEAP_FindSubHeap( heapPtr, pArena );
1093 oldSize = (pArena->size & ARENA_SIZE_MASK);
1094 if (size > oldSize)
1095 {
1096 char *pNext = (char *)(pArena + 1) + oldSize;
1097 if ((pNext < (char *)subheap + subheap->size) &&
1098 (*(DWORD *)pNext & ARENA_FLAG_FREE) &&
1099 (oldSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= size))
1100 {
1101 /* The next block is free and large enough */
1102 ARENA_FREE *pFree = (ARENA_FREE *)pNext;
1103 pFree->next->prev = pFree->prev;
1104 pFree->prev->next = pFree->next;
1105 pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
Alexandre Julliard2ace16a1996-04-28 15:09:19 +00001106 if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
1107 + size + HEAP_MIN_BLOCK_SIZE))
1108 {
1109 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1110 SetLastError( ERROR_OUTOFMEMORY );
1111 return NULL;
1112 }
Alexandre Julliard8b915631996-06-16 16:16:05 +00001113 HEAP_ShrinkBlock( subheap, pArena, size );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001114 }
1115 else /* Do it the hard way */
1116 {
1117 ARENA_FREE *pNew;
1118 ARENA_INUSE *pInUse;
1119 SUBHEAP *newsubheap;
1120
1121 if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
1122 !(pNew = HEAP_FindFreeBlock( heapPtr, size, &newsubheap )))
1123 {
1124 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
Alexandre Julliard2ace16a1996-04-28 15:09:19 +00001125 SetLastError( ERROR_OUTOFMEMORY );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001126 return NULL;
1127 }
1128
1129 /* Build the in-use arena */
1130
1131 pNew->next->prev = pNew->prev;
1132 pNew->prev->next = pNew->next;
1133 pInUse = (ARENA_INUSE *)pNew;
1134 pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
1135 + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
1136 pInUse->threadId = GetCurrentTask();
1137 pInUse->magic = ARENA_INUSE_MAGIC;
Alexandre Julliard3051b641996-07-05 17:14:13 +00001138 HEAP_ShrinkBlock( newsubheap, pInUse, size );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001139 memcpy( pInUse + 1, pArena + 1, oldSize );
1140
1141 /* Free the previous block */
1142
1143 HEAP_MakeInUseBlockFree( subheap, pArena );
1144 subheap = newsubheap;
1145 pArena = pInUse;
1146 }
1147 }
Alexandre Julliard8b915631996-06-16 16:16:05 +00001148 else HEAP_ShrinkBlock( subheap, pArena, size ); /* Shrink the block */
Alexandre Julliard329f0681996-04-14 13:21:20 +00001149
1150 /* Clear the extra bytes if needed */
1151
1152 if (size > oldSize)
1153 {
1154 if (flags & HEAP_ZERO_MEMORY)
1155 memset( (char *)(pArena + 1) + oldSize, 0,
1156 (pArena->size & ARENA_SIZE_MASK) - oldSize );
Alexandre Julliarda69b88b1998-03-15 20:29:56 +00001157 else if (TRACE_ON(heap))
Alexandre Julliard329f0681996-04-14 13:21:20 +00001158 memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
1159 (pArena->size & ARENA_SIZE_MASK) - oldSize );
1160 }
1161
1162 /* Return the new arena */
1163
Ove Kaaven2d127431999-04-25 09:09:15 +00001164 pArena->callerEIP = GET_EIP();
Alexandre Julliard329f0681996-04-14 13:21:20 +00001165 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1166
Alexandre Julliard15657091999-05-23 10:25:25 +00001167 TRACE("(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +00001168 heap, flags, (DWORD)ptr, size, (DWORD)(pArena + 1) );
1169 return (LPVOID)(pArena + 1);
1170}
1171
1172
1173/***********************************************************************
1174 * HeapCompact (KERNEL32.335)
1175 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001176DWORD WINAPI HeapCompact( HANDLE heap, DWORD flags )
Alexandre Julliard329f0681996-04-14 13:21:20 +00001177{
Huw D M Daviesd38ae1f1999-05-02 11:27:14 +00001178 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
Alexandre Julliard329f0681996-04-14 13:21:20 +00001179 return 0;
1180}
1181
1182
1183/***********************************************************************
1184 * HeapLock (KERNEL32.339)
Alexandre Julliard54c27111998-03-29 19:44:57 +00001185 * Attempts to acquire the critical section object for a specified heap.
1186 *
1187 * RETURNS
1188 * TRUE: Success
1189 * FALSE: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +00001190 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001191BOOL WINAPI HeapLock(
1192 HANDLE heap /* [in] Handle of heap to lock for exclusive access */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001193) {
Alexandre Julliard329f0681996-04-14 13:21:20 +00001194 HEAP *heapPtr = HEAP_GetPtr( heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001195 if (!heapPtr) return FALSE;
1196 EnterCriticalSection( &heapPtr->critSection );
1197 return TRUE;
1198}
1199
1200
1201/***********************************************************************
1202 * HeapUnlock (KERNEL32.342)
Alexandre Julliard54c27111998-03-29 19:44:57 +00001203 * Releases ownership of the critical section object.
1204 *
1205 * RETURNS
1206 * TRUE: Success
1207 * FALSE: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +00001208 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001209BOOL WINAPI HeapUnlock(
1210 HANDLE heap /* [in] Handle to the heap to unlock */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001211) {
Alexandre Julliard329f0681996-04-14 13:21:20 +00001212 HEAP *heapPtr = HEAP_GetPtr( heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001213 if (!heapPtr) return FALSE;
1214 LeaveCriticalSection( &heapPtr->critSection );
1215 return TRUE;
1216}
1217
1218
1219/***********************************************************************
1220 * HeapSize (KERNEL32.341)
Alexandre Julliard54c27111998-03-29 19:44:57 +00001221 * RETURNS
1222 * Size in bytes of allocated memory
Guy Albertelli3db8e191999-02-14 17:38:24 +00001223 * 0xffffffff: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +00001224 */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001225DWORD WINAPI HeapSize(
Alexandre Julliarda3960291999-02-26 11:11:13 +00001226 HANDLE heap, /* [in] Handle of heap */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001227 DWORD flags, /* [in] Heap size control flags */
1228 LPVOID ptr /* [in] Address of memory to return size for */
1229) {
Alexandre Julliard329f0681996-04-14 13:21:20 +00001230 DWORD ret;
1231 HEAP *heapPtr = HEAP_GetPtr( heap );
1232
1233 if (!heapPtr) return FALSE;
1234 flags &= HEAP_NO_SERIALIZE;
1235 flags |= heapPtr->flags;
1236 if (!(flags & HEAP_NO_SERIALIZE)) HeapLock( heap );
1237 if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
1238 {
1239 SetLastError( ERROR_INVALID_PARAMETER );
1240 ret = 0xffffffff;
1241 }
1242 else
1243 {
1244 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
1245 ret = pArena->size & ARENA_SIZE_MASK;
1246 }
1247 if (!(flags & HEAP_NO_SERIALIZE)) HeapUnlock( heap );
1248
Alexandre Julliard15657091999-05-23 10:25:25 +00001249 TRACE("(%08x,%08lx,%08lx): returning %08lx\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +00001250 heap, flags, (DWORD)ptr, ret );
1251 return ret;
1252}
1253
1254
1255/***********************************************************************
1256 * HeapValidate (KERNEL32.343)
Alexandre Julliard54c27111998-03-29 19:44:57 +00001257 * Validates a specified heap.
1258 *
1259 * NOTES
1260 * Flags is ignored.
1261 *
1262 * RETURNS
1263 * TRUE: Success
1264 * FALSE: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +00001265 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001266BOOL WINAPI HeapValidate(
1267 HANDLE heap, /* [in] Handle to the heap */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001268 DWORD flags, /* [in] Bit flags that control access during operation */
1269 LPCVOID block /* [in] Optional pointer to memory block to validate */
1270) {
Alexandre Julliard329f0681996-04-14 13:21:20 +00001271 SUBHEAP *subheap;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001272 HEAP *heapPtr = (HEAP *)(heap);
Ove Kaaven2d127431999-04-25 09:09:15 +00001273 BOOL ret = TRUE;
Alexandre Julliard329f0681996-04-14 13:21:20 +00001274
1275 if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
1276 {
Alexandre Julliard15657091999-05-23 10:25:25 +00001277 ERR("Invalid heap %08x!\n", heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001278 return FALSE;
1279 }
1280
Ove Kaaven2d127431999-04-25 09:09:15 +00001281 flags &= HEAP_NO_SERIALIZE;
1282 flags |= heapPtr->flags;
1283 /* calling HeapLock may result in infinite recursion, so do the critsect directly */
1284 if (!(flags & HEAP_NO_SERIALIZE))
1285 EnterCriticalSection( &heapPtr->critSection );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001286 if (block)
1287 {
Alexandre Julliard54c27111998-03-29 19:44:57 +00001288 /* Only check this single memory block */
Alexandre Julliard329f0681996-04-14 13:21:20 +00001289 if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
1290 ((char *)block < (char *)subheap + subheap->headerSize
1291 + sizeof(ARENA_INUSE)))
1292 {
Alexandre Julliard15657091999-05-23 10:25:25 +00001293 ERR("Heap %08lx: block %08lx is not inside heap\n",
Alexandre Julliard329f0681996-04-14 13:21:20 +00001294 (DWORD)heap, (DWORD)block );
Ove Kaaven2d127431999-04-25 09:09:15 +00001295 ret = FALSE;
1296 } else
1297 ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1 );
1298
1299 if (!(flags & HEAP_NO_SERIALIZE))
1300 LeaveCriticalSection( &heapPtr->critSection );
1301 return ret;
Alexandre Julliard329f0681996-04-14 13:21:20 +00001302 }
1303
1304 subheap = &heapPtr->subheap;
Ove Kaaven2d127431999-04-25 09:09:15 +00001305 while (subheap && ret)
Alexandre Julliard329f0681996-04-14 13:21:20 +00001306 {
1307 char *ptr = (char *)subheap + subheap->headerSize;
1308 while (ptr < (char *)subheap + subheap->size)
1309 {
1310 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1311 {
Ove Kaaven2d127431999-04-25 09:09:15 +00001312 if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
1313 ret = FALSE;
1314 break;
1315 }
Alexandre Julliard329f0681996-04-14 13:21:20 +00001316 ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1317 }
1318 else
1319 {
Ove Kaaven2d127431999-04-25 09:09:15 +00001320 if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr )) {
1321 ret = FALSE;
1322 break;
1323 }
Alexandre Julliard329f0681996-04-14 13:21:20 +00001324 ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
1325 }
1326 }
1327 subheap = subheap->next;
1328 }
Ove Kaaven2d127431999-04-25 09:09:15 +00001329 if (!(flags & HEAP_NO_SERIALIZE))
1330 LeaveCriticalSection( &heapPtr->critSection );
1331 return ret;
Alexandre Julliard329f0681996-04-14 13:21:20 +00001332}
1333
1334
1335/***********************************************************************
1336 * HeapWalk (KERNEL32.344)
Alexandre Julliard54c27111998-03-29 19:44:57 +00001337 * Enumerates the memory blocks in a specified heap.
1338 *
1339 * RETURNS
1340 * TRUE: Success
1341 * FALSE: Failure
Alexandre Julliard329f0681996-04-14 13:21:20 +00001342 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001343BOOL WINAPI HeapWalk(
1344 HANDLE heap, /* [in] Handle to heap to enumerate */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001345 LPPROCESS_HEAP_ENTRY *entry /* [out] Pointer to structure of enumeration info */
1346) {
Alexandre Julliard15657091999-05-23 10:25:25 +00001347 FIXME("(%08x): stub.\n", heap );
Alexandre Julliard329f0681996-04-14 13:21:20 +00001348 return FALSE;
1349}
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +00001350
1351
1352/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001353 * HEAP_xalloc
1354 *
1355 * Same as HeapAlloc(), but die on failure.
1356 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001357LPVOID HEAP_xalloc( HANDLE heap, DWORD flags, DWORD size )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001358{
1359 LPVOID p = HeapAlloc( heap, flags, size );
1360 if (!p)
1361 {
Alexandre Julliard15657091999-05-23 10:25:25 +00001362 MESSAGE("Virtual memory exhausted.\n" );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001363 exit(1);
1364 }
Ove Kaaven2d127431999-04-25 09:09:15 +00001365 SET_EIP(p);
1366 return p;
1367}
1368
1369
1370/***********************************************************************
1371 * HEAP_xrealloc
1372 *
1373 * Same as HeapReAlloc(), but die on failure.
1374 */
1375LPVOID HEAP_xrealloc( HANDLE heap, DWORD flags, LPVOID lpMem, DWORD size )
1376{
1377 LPVOID p = HeapReAlloc( heap, flags, lpMem, size );
1378 if (!p)
1379 {
Alexandre Julliard15657091999-05-23 10:25:25 +00001380 MESSAGE("Virtual memory exhausted.\n" );
Ove Kaaven2d127431999-04-25 09:09:15 +00001381 exit(1);
1382 }
1383 SET_EIP(p);
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001384 return p;
1385}
1386
1387
1388/***********************************************************************
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +00001389 * HEAP_strdupA
1390 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001391LPSTR HEAP_strdupA( HANDLE heap, DWORD flags, LPCSTR str )
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +00001392{
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001393 LPSTR p = HEAP_xalloc( heap, flags, strlen(str) + 1 );
Ove Kaaven2d127431999-04-25 09:09:15 +00001394 SET_EIP(p);
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001395 strcpy( p, str );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001396 return p;
1397}
1398
1399
1400/***********************************************************************
1401 * HEAP_strdupW
1402 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001403LPWSTR HEAP_strdupW( HANDLE heap, DWORD flags, LPCWSTR str )
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001404{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001405 INT len = lstrlenW(str) + 1;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001406 LPWSTR p = HEAP_xalloc( heap, flags, len * sizeof(WCHAR) );
Ove Kaaven2d127431999-04-25 09:09:15 +00001407 SET_EIP(p);
Alexandre Julliarda3960291999-02-26 11:11:13 +00001408 lstrcpyW( p, str );
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +00001409 return p;
1410}
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001411
1412
1413/***********************************************************************
1414 * HEAP_strdupAtoW
1415 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001416LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001417{
1418 LPWSTR ret;
1419
1420 if (!str) return NULL;
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001421 ret = HEAP_xalloc( heap, flags, (strlen(str)+1) * sizeof(WCHAR) );
Ove Kaaven2d127431999-04-25 09:09:15 +00001422 SET_EIP(ret);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001423 lstrcpyAtoW( ret, str );
1424 return ret;
1425}
1426
1427
1428/***********************************************************************
1429 * HEAP_strdupWtoA
1430 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001431LPSTR HEAP_strdupWtoA( HANDLE heap, DWORD flags, LPCWSTR str )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001432{
1433 LPSTR ret;
1434
1435 if (!str) return NULL;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001436 ret = HEAP_xalloc( heap, flags, lstrlenW(str) + 1 );
Ove Kaaven2d127431999-04-25 09:09:15 +00001437 SET_EIP(ret);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001438 lstrcpyWtoA( ret, str );
1439 return ret;
1440}
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001441
1442
1443
1444/***********************************************************************
1445 * 32-bit local heap functions (Win95; undocumented)
1446 */
1447
1448#define HTABLE_SIZE 0x10000
1449#define HTABLE_PAGESIZE 0x1000
1450#define HTABLE_NPAGES (HTABLE_SIZE / HTABLE_PAGESIZE)
1451
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001452#include "pshpack1.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001453typedef struct _LOCAL32HEADER
1454{
1455 WORD freeListFirst[HTABLE_NPAGES];
1456 WORD freeListSize[HTABLE_NPAGES];
1457 WORD freeListLast[HTABLE_NPAGES];
1458
1459 DWORD selectorTableOffset;
1460 WORD selectorTableSize;
1461 WORD selectorDelta;
1462
1463 DWORD segment;
1464 LPBYTE base;
1465
1466 DWORD limit;
1467 DWORD flags;
1468
1469 DWORD magic;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001470 HANDLE heap;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001471
1472} LOCAL32HEADER;
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001473#include "poppack.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001474
1475#define LOCAL32_MAGIC ((DWORD)('L' | ('H'<<8) | ('3'<<16) | ('2'<<24)))
1476
1477/***********************************************************************
1478 * Local32Init (KERNEL.208)
1479 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001480HANDLE WINAPI Local32Init16( WORD segment, DWORD tableSize,
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001481 DWORD heapSize, DWORD flags )
1482{
1483 DWORD totSize, segSize = 0;
1484 LPBYTE base;
1485 LOCAL32HEADER *header;
1486 HEAP *heap;
1487 WORD *selectorTable;
1488 WORD selectorEven, selectorOdd;
1489 int i, nrBlocks;
1490
1491 /* Determine new heap size */
1492
1493 if ( segment )
Jesper Skov5c3e4571998-11-01 19:27:22 +00001494 {
Alexandre Julliarda3960291999-02-26 11:11:13 +00001495 if ( (segSize = GetSelectorLimit16( segment )) == 0 )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001496 return 0;
1497 else
1498 segSize++;
Jesper Skov5c3e4571998-11-01 19:27:22 +00001499 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001500
1501 if ( heapSize == -1L )
1502 heapSize = 1024L*1024L; /* FIXME */
1503
1504 heapSize = (heapSize + 0xffff) & 0xffff0000;
1505 segSize = (segSize + 0x0fff) & 0xfffff000;
1506 totSize = segSize + HTABLE_SIZE + heapSize;
1507
1508
1509 /* Allocate memory and initialize heap */
1510
1511 if ( !(base = VirtualAlloc( NULL, totSize, MEM_RESERVE, PAGE_READWRITE )) )
1512 return 0;
1513
1514 if ( !VirtualAlloc( base, segSize + HTABLE_PAGESIZE,
1515 MEM_COMMIT, PAGE_READWRITE ) )
1516 {
1517 VirtualFree( base, 0, MEM_RELEASE );
1518 return 0;
1519 }
1520
1521 heap = (HEAP *)(base + segSize + HTABLE_SIZE);
1522 if ( !HEAP_InitSubHeap( heap, (LPVOID)heap, 0, 0x10000, heapSize ) )
1523 {
1524 VirtualFree( base, 0, MEM_RELEASE );
1525 return 0;
1526 }
1527
1528
1529 /* Set up header and handle table */
1530
1531 header = (LOCAL32HEADER *)(base + segSize);
1532 header->base = base;
1533 header->limit = HTABLE_PAGESIZE-1;
1534 header->flags = 0;
1535 header->magic = LOCAL32_MAGIC;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001536 header->heap = (HANDLE)heap;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001537
1538 header->freeListFirst[0] = sizeof(LOCAL32HEADER);
1539 header->freeListLast[0] = HTABLE_PAGESIZE - 4;
1540 header->freeListSize[0] = (HTABLE_PAGESIZE - sizeof(LOCAL32HEADER)) / 4;
1541
1542 for (i = header->freeListFirst[0]; i < header->freeListLast[0]; i += 4)
1543 *(DWORD *)((LPBYTE)header + i) = i+4;
1544
1545 header->freeListFirst[1] = 0xffff;
1546
1547
1548 /* Set up selector table */
1549
1550 nrBlocks = (totSize + 0x7fff) >> 15;
1551 selectorTable = (LPWORD) HeapAlloc( header->heap, 0, nrBlocks * 2 );
1552 selectorEven = SELECTOR_AllocBlock( base, totSize,
1553 SEGMENT_DATA, FALSE, FALSE );
1554 selectorOdd = SELECTOR_AllocBlock( base + 0x8000, totSize - 0x8000,
1555 SEGMENT_DATA, FALSE, FALSE );
1556
1557 if ( !selectorTable || !selectorEven || !selectorOdd )
1558 {
1559 if ( selectorTable ) HeapFree( header->heap, 0, selectorTable );
1560 if ( selectorEven ) SELECTOR_FreeBlock( selectorEven, totSize >> 16 );
1561 if ( selectorOdd ) SELECTOR_FreeBlock( selectorOdd, (totSize-0x8000) >> 16 );
1562 HeapDestroy( header->heap );
1563 VirtualFree( base, 0, MEM_RELEASE );
1564 return 0;
1565 }
1566
1567 header->selectorTableOffset = (LPBYTE)selectorTable - header->base;
1568 header->selectorTableSize = nrBlocks * 4; /* ??? Win95 does it this way! */
1569 header->selectorDelta = selectorEven - selectorOdd;
1570 header->segment = segment? segment : selectorEven;
1571
1572 for (i = 0; i < nrBlocks; i++)
1573 selectorTable[i] = (i & 1)? selectorOdd + ((i >> 1) << __AHSHIFT)
1574 : selectorEven + ((i >> 1) << __AHSHIFT);
1575
1576 /* Move old segment */
1577
1578 if ( segment )
1579 {
1580 /* FIXME: This is somewhat ugly and relies on implementation
1581 details about 16-bit global memory handles ... */
1582
1583 LPBYTE oldBase = (LPBYTE)GetSelectorBase( segment );
1584 memcpy( base, oldBase, segSize );
1585 GLOBAL_MoveBlock( segment, base, totSize );
1586 HeapFree( SystemHeap, 0, oldBase );
1587 }
1588
Alexandre Julliarda3960291999-02-26 11:11:13 +00001589 return (HANDLE)header;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001590}
1591
1592/***********************************************************************
1593 * Local32_SearchHandle
1594 */
1595static LPDWORD Local32_SearchHandle( LOCAL32HEADER *header, DWORD addr )
1596{
1597 LPDWORD handle;
1598
1599 for ( handle = (LPDWORD)((LPBYTE)header + sizeof(LOCAL32HEADER));
1600 handle < (LPDWORD)((LPBYTE)header + header->limit);
1601 handle++)
1602 {
1603 if (*handle == addr)
1604 return handle;
1605 }
1606
1607 return NULL;
1608}
1609
1610/***********************************************************************
1611 * Local32_ToHandle
1612 */
1613static VOID Local32_ToHandle( LOCAL32HEADER *header, INT16 type,
1614 DWORD addr, LPDWORD *handle, LPBYTE *ptr )
1615{
1616 *handle = NULL;
1617 *ptr = NULL;
1618
1619 switch (type)
1620 {
1621 case -2: /* 16:16 pointer, no handles */
1622 *ptr = PTR_SEG_TO_LIN( addr );
1623 *handle = (LPDWORD)*ptr;
1624 break;
1625
1626 case -1: /* 32-bit offset, no handles */
1627 *ptr = header->base + addr;
1628 *handle = (LPDWORD)*ptr;
1629 break;
1630
1631 case 0: /* handle */
1632 if ( addr >= sizeof(LOCAL32HEADER)
1633 && addr < header->limit && !(addr & 3)
1634 && *(LPDWORD)((LPBYTE)header + addr) >= HTABLE_SIZE )
1635 {
1636 *handle = (LPDWORD)((LPBYTE)header + addr);
1637 *ptr = header->base + **handle;
1638 }
1639 break;
1640
1641 case 1: /* 16:16 pointer */
1642 *ptr = PTR_SEG_TO_LIN( addr );
1643 *handle = Local32_SearchHandle( header, *ptr - header->base );
1644 break;
1645
1646 case 2: /* 32-bit offset */
1647 *ptr = header->base + addr;
1648 *handle = Local32_SearchHandle( header, *ptr - header->base );
1649 break;
1650 }
1651}
1652
1653/***********************************************************************
1654 * Local32_FromHandle
1655 */
1656static VOID Local32_FromHandle( LOCAL32HEADER *header, INT16 type,
1657 DWORD *addr, LPDWORD handle, LPBYTE ptr )
1658{
1659 switch (type)
1660 {
1661 case -2: /* 16:16 pointer */
1662 case 1:
1663 {
1664 WORD *selTable = (LPWORD)(header->base + header->selectorTableOffset);
1665 DWORD offset = (LPBYTE)ptr - header->base;
1666 *addr = MAKELONG( offset & 0x7fff, selTable[offset >> 15] );
1667 }
1668 break;
1669
1670 case -1: /* 32-bit offset */
1671 case 2:
1672 *addr = ptr - header->base;
1673 break;
1674
1675 case 0: /* handle */
1676 *addr = (LPBYTE)handle - (LPBYTE)header;
1677 break;
1678 }
1679}
1680
1681/***********************************************************************
1682 * Local32Alloc (KERNEL.209)
1683 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001684DWORD WINAPI Local32Alloc16( HANDLE heap, DWORD size, INT16 type, DWORD flags )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001685{
1686 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1687 LPDWORD handle;
1688 LPBYTE ptr;
1689 DWORD addr;
1690
1691 /* Allocate memory */
1692 ptr = HeapAlloc( header->heap,
1693 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0, size );
1694 if (!ptr) return 0;
1695
1696
1697 /* Allocate handle if requested */
1698 if (type >= 0)
1699 {
1700 int page, i;
1701
1702 /* Find first page of handle table with free slots */
1703 for (page = 0; page < HTABLE_NPAGES; page++)
1704 if (header->freeListFirst[page] != 0)
1705 break;
1706 if (page == HTABLE_NPAGES)
1707 {
Alexandre Julliard15657091999-05-23 10:25:25 +00001708 WARN("Out of handles!\n" );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001709 HeapFree( header->heap, 0, ptr );
1710 return 0;
1711 }
1712
1713 /* If virgin page, initialize it */
1714 if (header->freeListFirst[page] == 0xffff)
1715 {
1716 if ( !VirtualAlloc( (LPBYTE)header + (page << 12),
1717 0x1000, MEM_COMMIT, PAGE_READWRITE ) )
1718 {
Alexandre Julliard15657091999-05-23 10:25:25 +00001719 WARN("Cannot grow handle table!\n" );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001720 HeapFree( header->heap, 0, ptr );
1721 return 0;
1722 }
1723
1724 header->limit += HTABLE_PAGESIZE;
1725
1726 header->freeListFirst[page] = 0;
1727 header->freeListLast[page] = HTABLE_PAGESIZE - 4;
1728 header->freeListSize[page] = HTABLE_PAGESIZE / 4;
1729
1730 for (i = 0; i < HTABLE_PAGESIZE; i += 4)
1731 *(DWORD *)((LPBYTE)header + i) = i+4;
1732
Ulrich Weigand38882461998-10-14 18:00:23 +00001733 if (page < HTABLE_NPAGES-1)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001734 header->freeListFirst[page+1] = 0xffff;
1735 }
1736
1737 /* Allocate handle slot from page */
1738 handle = (LPDWORD)((LPBYTE)header + header->freeListFirst[page]);
1739 if (--header->freeListSize[page] == 0)
1740 header->freeListFirst[page] = header->freeListLast[page] = 0;
1741 else
1742 header->freeListFirst[page] = *handle;
1743
1744 /* Store 32-bit offset in handle slot */
1745 *handle = ptr - header->base;
1746 }
1747 else
1748 {
1749 handle = (LPDWORD)ptr;
1750 header->flags |= 1;
1751 }
1752
1753
1754 /* Convert handle to requested output type */
1755 Local32_FromHandle( header, type, &addr, handle, ptr );
1756 return addr;
1757}
1758
1759/***********************************************************************
1760 * Local32ReAlloc (KERNEL.210)
1761 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001762DWORD WINAPI Local32ReAlloc16( HANDLE heap, DWORD addr, INT16 type,
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001763 DWORD size, DWORD flags )
1764{
1765 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1766 LPDWORD handle;
1767 LPBYTE ptr;
1768
1769 if (!addr)
Alexandre Julliarda3960291999-02-26 11:11:13 +00001770 return Local32Alloc16( heap, size, type, flags );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001771
1772 /* Retrieve handle and pointer */
1773 Local32_ToHandle( header, type, addr, &handle, &ptr );
1774 if (!handle) return FALSE;
1775
1776 /* Reallocate memory block */
1777 ptr = HeapReAlloc( header->heap,
1778 (flags & LMEM_MOVEABLE)? HEAP_ZERO_MEMORY : 0,
1779 ptr, size );
1780 if (!ptr) return 0;
1781
1782 /* Modify handle */
1783 if (type >= 0)
1784 *handle = ptr - header->base;
1785 else
1786 handle = (LPDWORD)ptr;
1787
1788 /* Convert handle to requested output type */
1789 Local32_FromHandle( header, type, &addr, handle, ptr );
1790 return addr;
1791}
1792
1793/***********************************************************************
1794 * Local32Free (KERNEL.211)
1795 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001796BOOL WINAPI Local32Free16( HANDLE heap, DWORD addr, INT16 type )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001797{
1798 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1799 LPDWORD handle;
1800 LPBYTE ptr;
1801
1802 /* Retrieve handle and pointer */
1803 Local32_ToHandle( header, type, addr, &handle, &ptr );
1804 if (!handle) return FALSE;
1805
1806 /* Free handle if necessary */
1807 if (type >= 0)
1808 {
1809 int offset = (LPBYTE)handle - (LPBYTE)header;
1810 int page = offset >> 12;
1811
1812 /* Return handle slot to page free list */
1813 if (header->freeListSize[page]++ == 0)
1814 header->freeListFirst[page] = header->freeListLast[page] = offset;
1815 else
1816 *(LPDWORD)((LPBYTE)header + header->freeListLast[page]) = offset,
Ulrich Weigand38882461998-10-14 18:00:23 +00001817 header->freeListLast[page] = offset;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001818
1819 *handle = 0;
1820
1821 /* Shrink handle table when possible */
1822 while (page > 0 && header->freeListSize[page] == HTABLE_PAGESIZE / 4)
1823 {
1824 if ( VirtualFree( (LPBYTE)header +
1825 (header->limit & ~(HTABLE_PAGESIZE-1)),
1826 HTABLE_PAGESIZE, MEM_DECOMMIT ) )
1827 break;
1828
1829 header->limit -= HTABLE_PAGESIZE;
Ulrich Weigand38882461998-10-14 18:00:23 +00001830 header->freeListFirst[page] = 0xffff;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001831 page--;
1832 }
1833 }
1834
1835 /* Free memory */
1836 return HeapFree( header->heap, 0, ptr );
1837}
1838
1839/***********************************************************************
1840 * Local32Translate (KERNEL.213)
1841 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001842DWORD WINAPI Local32Translate16( HANDLE heap, DWORD addr, INT16 type1, INT16 type2 )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001843{
1844 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1845 LPDWORD handle;
1846 LPBYTE ptr;
1847
1848 Local32_ToHandle( header, type1, addr, &handle, &ptr );
1849 if (!handle) return 0;
1850
1851 Local32_FromHandle( header, type2, &addr, handle, ptr );
1852 return addr;
1853}
1854
1855/***********************************************************************
1856 * Local32Size (KERNEL.214)
1857 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001858DWORD WINAPI Local32Size16( HANDLE heap, DWORD addr, INT16 type )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001859{
1860 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1861 LPDWORD handle;
1862 LPBYTE ptr;
1863
1864 Local32_ToHandle( header, type, addr, &handle, &ptr );
1865 if (!handle) return 0;
1866
1867 return HeapSize( header->heap, 0, ptr );
1868}
1869
1870/***********************************************************************
1871 * Local32ValidHandle (KERNEL.215)
1872 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001873BOOL WINAPI Local32ValidHandle16( HANDLE heap, WORD addr )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001874{
1875 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1876 LPDWORD handle;
1877 LPBYTE ptr;
1878
1879 Local32_ToHandle( header, 0, addr, &handle, &ptr );
1880 return handle != NULL;
1881}
1882
1883/***********************************************************************
1884 * Local32GetSegment (KERNEL.229)
1885 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001886WORD WINAPI Local32GetSegment16( HANDLE heap )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001887{
1888 LOCAL32HEADER *header = (LOCAL32HEADER *)heap;
1889 return header->segment;
1890}
1891
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001892/***********************************************************************
1893 * Local32_GetHeap
1894 */
1895static LOCAL32HEADER *Local32_GetHeap( HGLOBAL16 handle )
1896{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001897 WORD selector = GlobalHandleToSel16( handle );
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001898 DWORD base = GetSelectorBase( selector );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001899 DWORD limit = GetSelectorLimit16( selector );
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001900
1901 /* Hmmm. This is a somewhat stupid heuristic, but Windows 95 does
1902 it this way ... */
1903
1904 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
1905 return (LOCAL32HEADER *)base;
1906
1907 base += 0x10000;
1908 limit -= 0x10000;
1909
1910 if ( limit > 0x10000 && ((LOCAL32HEADER *)base)->magic == LOCAL32_MAGIC )
1911 return (LOCAL32HEADER *)base;
1912
1913 return NULL;
1914}
1915
1916/***********************************************************************
1917 * Local32Info (KERNEL.444) (TOOLHELP.84)
1918 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001919BOOL16 WINAPI Local32Info16( LOCAL32INFO *pLocal32Info, HGLOBAL16 handle )
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001920{
1921 SUBHEAP *heapPtr;
1922 LPBYTE ptr;
1923 int i;
1924
1925 LOCAL32HEADER *header = Local32_GetHeap( handle );
1926 if ( !header ) return FALSE;
1927
1928 if ( !pLocal32Info || pLocal32Info->dwSize < sizeof(LOCAL32INFO) )
1929 return FALSE;
1930
1931 heapPtr = (SUBHEAP *)HEAP_GetPtr( header->heap );
1932 pLocal32Info->dwMemReserved = heapPtr->size;
1933 pLocal32Info->dwMemCommitted = heapPtr->commitSize;
1934 pLocal32Info->dwTotalFree = 0L;
1935 pLocal32Info->dwLargestFreeBlock = 0L;
1936
1937 /* Note: Local32 heaps always have only one subheap! */
1938 ptr = (LPBYTE)heapPtr + heapPtr->headerSize;
1939 while ( ptr < (LPBYTE)heapPtr + heapPtr->size )
1940 {
1941 if (*(DWORD *)ptr & ARENA_FLAG_FREE)
1942 {
1943 ARENA_FREE *pArena = (ARENA_FREE *)ptr;
1944 DWORD size = (pArena->size & ARENA_SIZE_MASK);
1945 ptr += sizeof(*pArena) + size;
1946
1947 pLocal32Info->dwTotalFree += size;
1948 if ( size > pLocal32Info->dwLargestFreeBlock )
1949 pLocal32Info->dwLargestFreeBlock = size;
1950 }
1951 else
1952 {
1953 ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
1954 DWORD size = (pArena->size & ARENA_SIZE_MASK);
1955 ptr += sizeof(*pArena) + size;
1956 }
1957 }
1958
1959 pLocal32Info->dwcFreeHandles = 0;
1960 for ( i = 0; i < HTABLE_NPAGES; i++ )
1961 {
1962 if ( header->freeListFirst[i] == 0xffff ) break;
1963 pLocal32Info->dwcFreeHandles += header->freeListSize[i];
1964 }
1965 pLocal32Info->dwcFreeHandles += (HTABLE_NPAGES - i) * HTABLE_PAGESIZE/4;
1966
1967 return TRUE;
1968}
1969
1970/***********************************************************************
1971 * Local32First (KERNEL.445) (TOOLHELP.85)
1972 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001973BOOL16 WINAPI Local32First16( LOCAL32ENTRY *pLocal32Entry, HGLOBAL16 handle )
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001974{
Alexandre Julliard15657091999-05-23 10:25:25 +00001975 FIXME("(%p, %04X): stub!\n", pLocal32Entry, handle );
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001976 return FALSE;
1977}
1978
1979/***********************************************************************
1980 * Local32Next (KERNEL.446) (TOOLHELP.86)
1981 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001982BOOL16 WINAPI Local32Next16( LOCAL32ENTRY *pLocal32Entry )
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001983{
Alexandre Julliard15657091999-05-23 10:25:25 +00001984 FIXME("(%p): stub!\n", pLocal32Entry );
Ulrich Weigand416d39e1998-12-01 14:45:37 +00001985 return FALSE;
1986}
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001987