4 * Copyright 1996 Alexandre Julliard
12 #include "selectors.h"
19 /* Note: the heap data structures are based on what Pietrek describes in his
20 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
21 * the same, but could be easily adapted if it turns out some programs
25 typedef struct tagARENA_INUSE
27 DWORD size
; /* Block size; must be the first field */
28 WORD threadId
; /* Allocating thread id */
29 WORD magic
; /* Magic number */
30 DWORD callerEIP
; /* EIP of caller upon allocation */
33 typedef struct tagARENA_FREE
35 DWORD size
; /* Block size; must be the first field */
36 WORD threadId
; /* Freeing thread id */
37 WORD magic
; /* Magic number */
38 struct tagARENA_FREE
*next
; /* Next free arena */
39 struct tagARENA_FREE
*prev
; /* Prev free arena */
42 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
43 #define ARENA_FLAG_PREV_FREE 0x00000002
44 #define ARENA_SIZE_MASK 0xfffffffc
45 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
46 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
48 #define ARENA_INUSE_FILLER 0x55
49 #define ARENA_FREE_FILLER 0xaa
51 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
53 /* Max size of the blocks on the free lists */
54 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
56 0x20, 0x80, 0x200, 0xffffffff
67 typedef struct tagSUBHEAP
69 DWORD size
; /* Size of the whole sub-heap */
70 DWORD commitSize
; /* Committed size of the sub-heap */
71 DWORD headerSize
; /* Size of the heap header */
72 struct tagSUBHEAP
*next
; /* Next sub-heap */
73 struct tagHEAP
*heap
; /* Main heap structure */
74 DWORD magic
; /* Magic number */
75 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
78 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
80 typedef struct tagHEAP
82 SUBHEAP subheap
; /* First sub-heap */
83 struct tagHEAP
*next
; /* Next heap for this process */
84 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
85 CRITICAL_SECTION critSection
; /* Critical section for serialization */
86 DWORD flags
; /* Heap flags */
87 DWORD magic
; /* Magic number */
90 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
92 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
93 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
95 HANDLE32 SystemHeap
= 0;
96 HANDLE32 SegptrHeap
= 0;
97 CRITICAL_SECTION
*HEAP_SystemLock
= NULL
;
100 /***********************************************************************
103 void HEAP_Dump( HEAP
*heap
)
109 DUMP( "Heap: %08lx\n", (DWORD
)heap
);
110 DUMP( "Next: %08lx Sub-heaps: %08lx",
111 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
112 subheap
= &heap
->subheap
;
113 while (subheap
->next
)
115 DUMP( " -> %08lx", (DWORD
)subheap
->next
);
116 subheap
= subheap
->next
;
119 DUMP( "\nFree lists:\n Block Stat Size Id\n" );
120 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
121 DUMP( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
122 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
123 heap
->freeList
[i
].arena
.threadId
,
124 (DWORD
)heap
->freeList
[i
].arena
.prev
,
125 (DWORD
)heap
->freeList
[i
].arena
.next
);
127 subheap
= &heap
->subheap
;
130 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
131 DUMP( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
132 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
134 DUMP( "\n Block Stat Size Id\n" );
135 ptr
= (char*)subheap
+ subheap
->headerSize
;
136 while (ptr
< (char *)subheap
+ subheap
->size
)
138 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
140 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
141 DUMP( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
142 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
143 pArena
->threadId
, (DWORD
)pArena
->prev
,
144 (DWORD
)pArena
->next
);
145 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
146 arenaSize
+= sizeof(ARENA_FREE
);
147 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
149 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
151 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
152 DUMP( "%08lx Used %08lx %04x back=%08lx EIP=%08lx\n",
153 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
154 pArena
->threadId
, *((DWORD
*)pArena
- 1),
156 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
157 arenaSize
+= sizeof(ARENA_INUSE
);
158 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
162 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
163 DUMP( "%08lx used %08lx %04x EIP=%08lx\n",
164 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
165 pArena
->threadId
, pArena
->callerEIP
);
166 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
167 arenaSize
+= sizeof(ARENA_INUSE
);
168 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
171 DUMP( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
172 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
173 arenaSize
, (arenaSize
* 100) / subheap
->size
);
174 subheap
= subheap
->next
;
179 /***********************************************************************
182 * Pointer to the heap
185 static HEAP
*HEAP_GetPtr(
186 HANDLE32 heap
/* [in] Handle to the heap */
188 HEAP
*heapPtr
= (HEAP
*)heap
;
189 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
191 fprintf( stderr
, "Invalid heap %08x!\n", heap
);
192 SetLastError( ERROR_INVALID_HANDLE
);
195 if (TRACE_ON(heap
) && !HeapValidate( heap
, 0, NULL
))
197 HEAP_Dump( heapPtr
);
199 SetLastError( ERROR_INVALID_HANDLE
);
206 /***********************************************************************
207 * HEAP_InsertFreeBlock
209 * Insert a free block into the free list.
211 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
213 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
214 while (pEntry
->size
< pArena
->size
) pEntry
++;
215 pArena
->size
|= ARENA_FLAG_FREE
;
216 pArena
->next
= pEntry
->arena
.next
;
217 pArena
->next
->prev
= pArena
;
218 pArena
->prev
= &pEntry
->arena
;
219 pEntry
->arena
.next
= pArena
;
223 /***********************************************************************
225 * Find the sub-heap containing a given address.
231 static SUBHEAP
*HEAP_FindSubHeap(
232 HEAP
*heap
, /* [in] Heap pointer */
233 LPCVOID ptr
/* [in] Address */
235 SUBHEAP
*sub
= &heap
->subheap
;
238 if (((char *)ptr
>= (char *)sub
) &&
239 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
246 /***********************************************************************
249 * Make sure the heap storage is committed up to (not including) ptr.
251 static BOOL32
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
253 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
254 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
255 if (size
> subheap
->size
) size
= subheap
->size
;
256 if (size
<= subheap
->commitSize
) return TRUE
;
257 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
258 size
- subheap
->commitSize
, MEM_COMMIT
,
259 PAGE_EXECUTE_READWRITE
))
261 fprintf( stderr
, "HEAP_Commit: could not commit %08lx bytes at %08lx for heap %08lx\n",
262 size
- subheap
->commitSize
,
263 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
264 (DWORD
)subheap
->heap
);
267 subheap
->commitSize
= size
;
272 /***********************************************************************
275 * If possible, decommit the heap storage from (including) 'ptr'.
277 static BOOL32
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
279 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
280 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
281 if (size
>= subheap
->commitSize
) return TRUE
;
282 if (!VirtualFree( (char *)subheap
+ size
,
283 subheap
->commitSize
- size
, MEM_DECOMMIT
))
285 fprintf( stderr
, "HEAP_Decommit: could not decommit %08lx bytes at %08lx for heap %08lx\n",
286 subheap
->commitSize
- size
,
287 (DWORD
)((char *)subheap
+ size
),
288 (DWORD
)subheap
->heap
);
291 subheap
->commitSize
= size
;
296 /***********************************************************************
297 * HEAP_CreateFreeBlock
299 * Create a free block at a specified address. 'size' is the size of the
300 * whole block, including the new arena.
302 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
306 /* Create a free arena */
308 pFree
= (ARENA_FREE
*)ptr
;
309 pFree
->threadId
= GetCurrentTask();
310 pFree
->magic
= ARENA_FREE_MAGIC
;
312 /* If debugging, erase the freed block content */
316 char *pEnd
= (char *)ptr
+ size
;
317 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
318 pEnd
= (char *)subheap
+ subheap
->commitSize
;
319 if (pEnd
> (char *)(pFree
+ 1))
320 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
323 /* Check if next block is free also */
325 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
326 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
328 /* Remove the next arena from the free list */
329 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
330 pNext
->next
->prev
= pNext
->prev
;
331 pNext
->prev
->next
= pNext
->next
;
332 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
334 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
337 /* Set the next block PREV_FREE flag and pointer */
339 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
341 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
342 *pNext
|= ARENA_FLAG_PREV_FREE
;
343 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
346 /* Last, insert the new block into the free list */
348 pFree
->size
= size
- sizeof(*pFree
);
349 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
353 /***********************************************************************
354 * HEAP_MakeInUseBlockFree
356 * Turn an in-use block into a free block. Can also decommit the end of
357 * the heap, and possibly even free the sub-heap altogether.
359 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
362 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
364 /* Check if we can merge with previous block */
366 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
368 pFree
= *((ARENA_FREE
**)pArena
- 1);
369 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
370 /* Remove it from the free list */
371 pFree
->next
->prev
= pFree
->prev
;
372 pFree
->prev
->next
= pFree
->next
;
374 else pFree
= (ARENA_FREE
*)pArena
;
376 /* Create a free block */
378 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
379 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
380 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
381 return; /* Not the last block, so nothing more to do */
383 /* Free the whole sub-heap if it's empty and not the original one */
385 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
386 (subheap
!= &subheap
->heap
->subheap
))
388 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
389 /* Remove the free block from the list */
390 pFree
->next
->prev
= pFree
->prev
;
391 pFree
->prev
->next
= pFree
->next
;
392 /* Remove the subheap from the list */
393 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
394 if (pPrev
) pPrev
->next
= subheap
->next
;
395 /* Free the memory */
397 if (subheap
->selector
) FreeSelector( subheap
->selector
);
398 VirtualFree( subheap
, 0, MEM_RELEASE
);
402 /* Decommit the end of the heap */
404 HEAP_Decommit( subheap
, pFree
+ 1 );
408 /***********************************************************************
411 * Shrink an in-use block.
413 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
415 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
417 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
418 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
419 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
423 /* Turn off PREV_FREE flag in next block */
424 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
425 if (pNext
< (char *)subheap
+ subheap
->size
)
426 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
431 /***********************************************************************
434 * Create a sub-heap of the given size.
436 static SUBHEAP
*HEAP_CreateSubHeap( DWORD flags
, DWORD commitSize
,
442 /* Round-up sizes on a 64K boundary */
444 if (flags
& HEAP_WINE_SEGPTR
)
446 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
450 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
451 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
452 if (!commitSize
) commitSize
= 0x10000;
453 if (totalSize
< commitSize
) totalSize
= commitSize
;
456 /* Allocate the memory block */
458 if (!(subheap
= VirtualAlloc( NULL
, totalSize
,
459 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
461 fprintf( stderr
, "HEAP_CreateSubHeap: could not VirtualAlloc %08lx bytes\n",
465 if (!VirtualAlloc(subheap
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
467 fprintf( stderr
, "HEAP_CreateSubHeap: could not commit %08lx bytes for sub-heap %08lx\n",
468 commitSize
, (DWORD
)subheap
);
469 VirtualFree( subheap
, 0, MEM_RELEASE
);
473 /* Allocate a selector if needed */
475 if (flags
& HEAP_WINE_SEGPTR
)
477 selector
= SELECTOR_AllocBlock( subheap
, totalSize
,
478 (flags
& HEAP_WINE_CODESEG
) ? SEGMENT_CODE
: SEGMENT_DATA
,
479 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
482 fprintf( stderr
, "HEAP_CreateSubHeap: could not allocate selector\n" );
483 VirtualFree( subheap
, 0, MEM_RELEASE
);
488 /* Fill the sub-heap structure */
490 subheap
->size
= totalSize
;
491 subheap
->commitSize
= commitSize
;
492 subheap
->headerSize
= sizeof(*subheap
);
493 subheap
->next
= NULL
;
494 subheap
->heap
= NULL
;
495 subheap
->magic
= SUBHEAP_MAGIC
;
496 subheap
->selector
= selector
;
501 /***********************************************************************
504 * Find a free block at least as large as the requested size, and make sure
505 * the requested size is committed.
507 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
508 SUBHEAP
**ppSubHeap
)
512 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
514 /* Find a suitable free list, and in it find a block large enough */
516 while (pEntry
->size
< size
) pEntry
++;
517 pArena
= pEntry
->arena
.next
;
518 while (pArena
!= &heap
->freeList
[0].arena
)
520 if (pArena
->size
> size
)
522 subheap
= HEAP_FindSubHeap( heap
, pArena
);
523 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
524 + size
+ HEAP_MIN_BLOCK_SIZE
))
526 *ppSubHeap
= subheap
;
530 pArena
= pArena
->next
;
533 /* If no block was found, attempt to grow the heap */
535 if (!(heap
->flags
& HEAP_GROWABLE
))
537 fprintf( stderr
, "HEAP_FindFreeBlock: Not enough space in heap %08lx for %08lx bytes\n",
541 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
542 if (!(subheap
= HEAP_CreateSubHeap( heap
->flags
, size
,
543 MAX( HEAP_DEF_SIZE
, size
) )))
546 /* Insert the new sub-heap in the list */
548 subheap
->heap
= heap
;
549 subheap
->next
= heap
->subheap
.next
;
550 heap
->subheap
.next
= subheap
;
551 size
= subheap
->size
;
552 TRACE(heap
, "created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
553 (DWORD
)subheap
, size
, (DWORD
)heap
);
555 HEAP_CreateFreeBlock( subheap
, subheap
+ 1, size
- sizeof(*subheap
) );
556 *ppSubHeap
= subheap
;
557 return (ARENA_FREE
*)(subheap
+ 1);
561 /***********************************************************************
562 * HEAP_IsValidArenaPtr
564 * Check that the pointer is inside the range possible for arenas.
566 static BOOL32
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
569 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
570 if (!subheap
) return FALSE
;
571 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
572 if (subheap
!= &heap
->subheap
) return FALSE
;
573 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
574 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
579 /***********************************************************************
580 * HEAP_ValidateFreeArena
582 static BOOL32
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
584 char *heapEnd
= (char *)subheap
+ subheap
->size
;
586 /* Check magic number */
587 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
589 fprintf( stderr
, "Heap %08lx: invalid free arena magic for %08lx\n",
590 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
593 /* Check size flags */
594 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
595 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
597 fprintf( stderr
, "Heap %08lx: bad flags %lx for free arena %08lx\n",
598 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
600 /* Check arena size */
601 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
603 fprintf( stderr
, "Heap %08lx: bad size %08lx for free arena %08lx\n",
604 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
607 /* Check that next pointer is valid */
608 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
610 fprintf( stderr
, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
611 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
614 /* Check that next arena is free */
615 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
616 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
618 fprintf( stderr
, "Heap %08lx: next arena %08lx invalid for %08lx\n",
619 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
622 /* Check that prev pointer is valid */
623 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
625 fprintf( stderr
, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
626 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
629 /* Check that prev arena is free */
630 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
631 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
633 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for %08lx\n",
634 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
637 /* Check that next block has PREV_FREE flag */
638 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
640 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
641 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
643 fprintf( stderr
, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
644 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
647 /* Check next block back pointer */
648 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
649 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
651 fprintf( stderr
, "Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
652 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
653 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
661 /***********************************************************************
662 * HEAP_ValidateInUseArena
664 static BOOL32
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
666 char *heapEnd
= (char *)subheap
+ subheap
->size
;
668 /* Check magic number */
669 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
671 fprintf( stderr
, "Heap %08lx: invalid in-use arena magic for %08lx\n",
672 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
675 /* Check size flags */
676 if (pArena
->size
& ARENA_FLAG_FREE
)
678 fprintf( stderr
, "Heap %08lx: bad flags %lx for in-use arena %08lx\n",
679 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
681 /* Check arena size */
682 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
684 fprintf( stderr
, "Heap %08lx: bad size %08lx for in-use arena %08lx\n",
685 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
688 /* Check next arena PREV_FREE flag */
689 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
690 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
692 fprintf( stderr
, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
693 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
696 /* Check prev free arena */
697 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
699 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
700 /* Check prev pointer */
701 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
703 fprintf(stderr
, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
704 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
707 /* Check that prev arena is free */
708 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
709 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
711 fprintf( stderr
, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
712 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
715 /* Check that prev arena is really the previous block */
716 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
718 fprintf( stderr
, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
719 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
727 /***********************************************************************
729 * Checks whether the pointer points to a block inside a given heap.
732 * Should this return BOOL32?
738 int HEAP_IsInsideHeap(
739 HANDLE32 heap
, /* [in] Heap */
740 DWORD flags
, /* [in] Flags */
741 LPCVOID ptr
/* [in] Pointer */
743 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
747 /* Validate the parameters */
749 if (!heapPtr
) return 0;
750 flags
|= heapPtr
->flags
;
751 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
752 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
753 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
754 + sizeof(ARENA_INUSE
))));
755 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
760 /***********************************************************************
763 * Transform a linear pointer into a SEGPTR. The pointer must have been
764 * allocated from a HEAP_WINE_SEGPTR heap.
766 SEGPTR
HEAP_GetSegptr( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
768 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
772 /* Validate the parameters */
774 if (!heapPtr
) return 0;
775 flags
|= heapPtr
->flags
;
776 if (!(flags
& HEAP_WINE_SEGPTR
))
778 fprintf( stderr
, "HEAP_GetSegptr: heap %08x is not a SEGPTR heap\n",
782 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
784 /* Get the subheap */
786 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
788 fprintf( stderr
, "HEAP_GetSegptr: %p is not inside heap %08x\n",
790 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
794 /* Build the SEGPTR */
796 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
797 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
802 /***********************************************************************
803 * HeapCreate (KERNEL32.336)
805 * Handle of heap: Success
808 HANDLE32 WINAPI
HeapCreate(
809 DWORD flags
, /* [in] Heap allocation flag */
810 DWORD initialSize
, /* [in] Initial heap size */
811 DWORD maxSize
/* [in] Maximum heap size */
816 FREE_LIST_ENTRY
*pEntry
;
818 /* Allocate the heap block */
822 maxSize
= HEAP_DEF_SIZE
;
823 flags
|= HEAP_GROWABLE
;
825 if (!(subheap
= HEAP_CreateSubHeap( flags
, initialSize
, maxSize
)))
827 SetLastError( ERROR_OUTOFMEMORY
);
831 /* Fill the heap structure */
833 heap
= (HEAP
*)subheap
;
834 subheap
->heap
= heap
;
835 subheap
->headerSize
= sizeof(HEAP
);
838 heap
->magic
= HEAP_MAGIC
;
840 /* Build the free lists */
842 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
844 pEntry
->size
= HEAP_freeListSizes
[i
];
845 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
846 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
847 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
848 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
849 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
850 pEntry
->arena
.threadId
= 0;
851 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
854 /* Create the first free block */
856 HEAP_CreateFreeBlock( subheap
, heap
+ 1, subheap
->size
- sizeof(*heap
) );
858 /* Initialize critical section */
860 InitializeCriticalSection( &heap
->critSection
);
861 if (!SystemHeap
) HEAP_SystemLock
= &heap
->critSection
;
865 return (HANDLE32
)heap
;
869 /***********************************************************************
870 * HeapDestroy (KERNEL32.337)
875 BOOL32 WINAPI
HeapDestroy(
876 HANDLE32 heap
/* [in] Handle of heap */
878 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
881 TRACE(heap
, "%08x\n", heap
);
882 if (!heapPtr
) return FALSE
;
884 DeleteCriticalSection( &heapPtr
->critSection
);
885 subheap
= &heapPtr
->subheap
;
888 SUBHEAP
*next
= subheap
->next
;
889 if (subheap
->selector
) FreeSelector( subheap
->selector
);
890 VirtualFree( subheap
, 0, MEM_RELEASE
);
897 /***********************************************************************
898 * HeapAlloc (KERNEL32.334)
900 * Pointer to allocated memory block
903 LPVOID WINAPI
HeapAlloc(
904 HANDLE32 heap
, /* [in] Handle of private heap block */
905 DWORD flags
, /* [in] Heap allocation control flags */
906 DWORD size
/* [in] Number of bytes to allocate */
911 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
913 /* Validate the parameters */
915 if (!heapPtr
) return NULL
;
916 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
917 flags
|= heapPtr
->flags
;
918 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
919 size
= (size
+ 3) & ~3;
920 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
922 /* Locate a suitable free block */
924 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
926 TRACE(heap
, "(%08x,%08lx,%08lx): returning NULL\n",
928 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
929 SetLastError( ERROR_COMMITMENT_LIMIT
);
933 /* Remove the arena from the free list */
935 pArena
->next
->prev
= pArena
->prev
;
936 pArena
->prev
->next
= pArena
->next
;
938 /* Build the in-use arena */
940 pInUse
= (ARENA_INUSE
*)pArena
;
941 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
942 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
943 pInUse
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
944 pInUse
->threadId
= GetCurrentTask();
945 pInUse
->magic
= ARENA_INUSE_MAGIC
;
947 /* Shrink the block */
949 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
951 if (flags
& HEAP_ZERO_MEMORY
) memset( pInUse
+ 1, 0, size
);
952 else if (TRACE_ON(heap
)) memset( pInUse
+ 1, ARENA_INUSE_FILLER
, size
);
954 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
956 TRACE(heap
, "(%08x,%08lx,%08lx): returning %08lx\n",
957 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
958 return (LPVOID
)(pInUse
+ 1);
962 /***********************************************************************
963 * HeapFree (KERNEL32.338)
968 BOOL32 WINAPI
HeapFree(
969 HANDLE32 heap
, /* [in] Handle of heap */
970 DWORD flags
, /* [in] Heap freeing flags */
971 LPVOID ptr
/* [in] Address of memory to free */
975 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
977 /* Validate the parameters */
979 if (!heapPtr
) return FALSE
;
980 flags
&= HEAP_NO_SERIALIZE
;
981 flags
|= heapPtr
->flags
;
982 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
983 if (!ptr
|| !HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
985 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
986 SetLastError( ERROR_INVALID_PARAMETER
);
987 TRACE(heap
, "(%08x,%08lx,%08lx): returning FALSE\n",
988 heap
, flags
, (DWORD
)ptr
);
992 /* Turn the block into a free block */
994 pInUse
= (ARENA_INUSE
*)ptr
- 1;
995 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
996 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
998 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
999 /* SetLastError( 0 ); */
1001 TRACE(heap
, "(%08x,%08lx,%08lx): returning TRUE\n",
1002 heap
, flags
, (DWORD
)ptr
);
1007 /***********************************************************************
1008 * HeapReAlloc (KERNEL32.340)
1010 * Pointer to reallocated memory block
1013 LPVOID WINAPI
HeapReAlloc(
1014 HANDLE32 heap
, /* [in] Handle of heap block */
1015 DWORD flags
, /* [in] Heap reallocation flags */
1016 LPVOID ptr
, /* [in] Address of memory to reallocate */
1017 DWORD size
/* [in] Number of bytes to reallocate */
1019 ARENA_INUSE
*pArena
;
1024 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
1025 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1027 /* Validate the parameters */
1029 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1030 HEAP_REALLOC_IN_PLACE_ONLY
;
1031 flags
|= heapPtr
->flags
;
1032 size
= (size
+ 3) & ~3;
1033 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1035 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1036 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1038 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1039 SetLastError( ERROR_INVALID_PARAMETER
);
1040 TRACE(heap
, "(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1041 heap
, flags
, (DWORD
)ptr
, size
);
1045 /* Check if we need to grow the block */
1047 pArena
= (ARENA_INUSE
*)ptr
- 1;
1048 pArena
->threadId
= GetCurrentTask();
1049 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1050 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1053 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1054 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1055 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1056 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1058 /* The next block is free and large enough */
1059 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1060 pFree
->next
->prev
= pFree
->prev
;
1061 pFree
->prev
->next
= pFree
->next
;
1062 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1063 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1064 + size
+ HEAP_MIN_BLOCK_SIZE
))
1066 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1067 SetLastError( ERROR_OUTOFMEMORY
);
1070 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1072 else /* Do it the hard way */
1075 ARENA_INUSE
*pInUse
;
1076 SUBHEAP
*newsubheap
;
1078 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1079 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1081 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1082 SetLastError( ERROR_OUTOFMEMORY
);
1086 /* Build the in-use arena */
1088 pNew
->next
->prev
= pNew
->prev
;
1089 pNew
->prev
->next
= pNew
->next
;
1090 pInUse
= (ARENA_INUSE
*)pNew
;
1091 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1092 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1093 pInUse
->threadId
= GetCurrentTask();
1094 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1095 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1096 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1098 /* Free the previous block */
1100 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1101 subheap
= newsubheap
;
1105 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1107 /* Clear the extra bytes if needed */
1111 if (flags
& HEAP_ZERO_MEMORY
)
1112 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1113 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1114 else if (TRACE_ON(heap
))
1115 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1116 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1119 /* Return the new arena */
1121 pArena
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
1122 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1124 TRACE(heap
, "(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1125 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1126 return (LPVOID
)(pArena
+ 1);
1130 /***********************************************************************
1131 * HeapCompact (KERNEL32.335)
1133 DWORD WINAPI
HeapCompact( HANDLE32 heap
, DWORD flags
)
1139 /***********************************************************************
1140 * HeapLock (KERNEL32.339)
1141 * Attempts to acquire the critical section object for a specified heap.
1147 BOOL32 WINAPI
HeapLock(
1148 HANDLE32 heap
/* [in] Handle of heap to lock for exclusive access */
1150 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1151 if (!heapPtr
) return FALSE
;
1152 EnterCriticalSection( &heapPtr
->critSection
);
1157 /***********************************************************************
1158 * HeapUnlock (KERNEL32.342)
1159 * Releases ownership of the critical section object.
1165 BOOL32 WINAPI
HeapUnlock(
1166 HANDLE32 heap
/* [in] Handle to the heap to unlock */
1168 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1169 if (!heapPtr
) return FALSE
;
1170 LeaveCriticalSection( &heapPtr
->critSection
);
1175 /***********************************************************************
1176 * HeapSize (KERNEL32.341)
1178 * Size in bytes of allocated memory
1181 DWORD WINAPI
HeapSize(
1182 HANDLE32 heap
, /* [in] Handle of heap */
1183 DWORD flags
, /* [in] Heap size control flags */
1184 LPVOID ptr
/* [in] Address of memory to return size for */
1187 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1189 if (!heapPtr
) return FALSE
;
1190 flags
&= HEAP_NO_SERIALIZE
;
1191 flags
|= heapPtr
->flags
;
1192 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1193 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1195 SetLastError( ERROR_INVALID_PARAMETER
);
1200 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1201 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1203 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1205 TRACE(heap
, "(%08x,%08lx,%08lx): returning %08lx\n",
1206 heap
, flags
, (DWORD
)ptr
, ret
);
1211 /***********************************************************************
1212 * HeapValidate (KERNEL32.343)
1213 * Validates a specified heap.
1222 BOOL32 WINAPI
HeapValidate(
1223 HANDLE32 heap
, /* [in] Handle to the heap */
1224 DWORD flags
, /* [in] Bit flags that control access during operation */
1225 LPCVOID block
/* [in] Optional pointer to memory block to validate */
1228 HEAP
*heapPtr
= (HEAP
*)(heap
);
1230 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
1232 fprintf( stderr
, "Invalid heap %08x!\n", heap
);
1238 /* Only check this single memory block */
1239 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
1240 ((char *)block
< (char *)subheap
+ subheap
->headerSize
1241 + sizeof(ARENA_INUSE
)))
1243 fprintf( stderr
, "Heap %08lx: block %08lx is not inside heap\n",
1244 (DWORD
)heap
, (DWORD
)block
);
1247 return HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1 );
1250 subheap
= &heapPtr
->subheap
;
1253 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1254 while (ptr
< (char *)subheap
+ subheap
->size
)
1256 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1258 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
))
1260 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1264 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
))
1266 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1269 subheap
= subheap
->next
;
1275 /***********************************************************************
1276 * HeapWalk (KERNEL32.344)
1277 * Enumerates the memory blocks in a specified heap.
1283 BOOL32 WINAPI
HeapWalk(
1284 HANDLE32 heap
, /* [in] Handle to heap to enumerate */
1285 LPPROCESS_HEAP_ENTRY
*entry
/* [out] Pointer to structure of enumeration info */
1287 fprintf( stderr
, "HeapWalk(%08x): not implemented\n", heap
);
1292 /***********************************************************************
1295 * Same as HeapAlloc(), but die on failure.
1297 LPVOID
HEAP_xalloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
1299 LPVOID p
= HeapAlloc( heap
, flags
, size
);
1302 fprintf( stderr
, "Virtual memory exhausted.\n" );
1309 /***********************************************************************
1312 LPSTR
HEAP_strdupA( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1314 LPSTR p
= HEAP_xalloc( heap
, flags
, lstrlen32A(str
) + 1 );
1315 lstrcpy32A( p
, str
);
1320 /***********************************************************************
1323 LPWSTR
HEAP_strdupW( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1325 INT32 len
= lstrlen32W(str
) + 1;
1326 LPWSTR p
= HEAP_xalloc( heap
, flags
, len
* sizeof(WCHAR
) );
1327 lstrcpy32W( p
, str
);
1332 /***********************************************************************
1335 LPWSTR
HEAP_strdupAtoW( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1339 if (!str
) return NULL
;
1340 ret
= HEAP_xalloc( heap
, flags
, (lstrlen32A(str
)+1) * sizeof(WCHAR
) );
1341 lstrcpyAtoW( ret
, str
);
1346 /***********************************************************************
1349 LPSTR
HEAP_strdupWtoA( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1353 if (!str
) return NULL
;
1354 ret
= HEAP_xalloc( heap
, flags
, lstrlen32W(str
) + 1 );
1355 lstrcpyWtoA( ret
, str
);