4 * Copyright 1996 Alexandre Julliard
11 #include "selectors.h"
18 /* Note: the heap data structures are based on what Pietrek describes in his
19 * book 'Windows 95 System Programming Secrets'. The layout is not exactly
20 * the same, but could be easily adapted if it turns out some programs
24 typedef struct tagARENA_INUSE
26 DWORD size
; /* Block size; must be the first field */
27 WORD threadId
; /* Allocating thread id */
28 WORD magic
; /* Magic number */
29 DWORD callerEIP
; /* EIP of caller upon allocation */
32 typedef struct tagARENA_FREE
34 DWORD size
; /* Block size; must be the first field */
35 WORD threadId
; /* Freeing thread id */
36 WORD magic
; /* Magic number */
37 struct tagARENA_FREE
*next
; /* Next free arena */
38 struct tagARENA_FREE
*prev
; /* Prev free arena */
41 #define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
42 #define ARENA_FLAG_PREV_FREE 0x00000002
43 #define ARENA_SIZE_MASK 0xfffffffc
44 #define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
45 #define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
47 #define ARENA_INUSE_FILLER 0x55
48 #define ARENA_FREE_FILLER 0xaa
50 #define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
52 /* Max size of the blocks on the free lists */
53 static const DWORD HEAP_freeListSizes
[HEAP_NB_FREE_LISTS
] =
55 0x20, 0x80, 0x200, 0xffffffff
66 typedef struct tagSUBHEAP
68 DWORD size
; /* Size of the whole sub-heap */
69 DWORD commitSize
; /* Committed size of the sub-heap */
70 DWORD headerSize
; /* Size of the heap header */
71 struct tagSUBHEAP
*next
; /* Next sub-heap */
72 struct tagHEAP
*heap
; /* Main heap structure */
73 DWORD magic
; /* Magic number */
74 WORD selector
; /* Selector for HEAP_WINE_SEGPTR heaps */
77 #define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
79 typedef struct tagHEAP
81 SUBHEAP subheap
; /* First sub-heap */
82 struct tagHEAP
*next
; /* Next heap for this process */
83 FREE_LIST_ENTRY freeList
[HEAP_NB_FREE_LISTS
]; /* Free lists */
84 CRITICAL_SECTION critSection
; /* Critical section for serialization */
85 DWORD flags
; /* Heap flags */
86 DWORD magic
; /* Magic number */
89 #define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
91 #define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
92 #define HEAP_MIN_BLOCK_SIZE (8+sizeof(ARENA_FREE)) /* Min. heap block size */
94 HANDLE32 SystemHeap
= 0;
95 HANDLE32 SegptrHeap
= 0;
96 CRITICAL_SECTION
*HEAP_SystemLock
= NULL
;
99 /***********************************************************************
102 void HEAP_Dump( HEAP
*heap
)
108 DUMP( "Heap: %08lx\n", (DWORD
)heap
);
109 DUMP( "Next: %08lx Sub-heaps: %08lx",
110 (DWORD
)heap
->next
, (DWORD
)&heap
->subheap
);
111 subheap
= &heap
->subheap
;
112 while (subheap
->next
)
114 DUMP( " -> %08lx", (DWORD
)subheap
->next
);
115 subheap
= subheap
->next
;
118 DUMP( "\nFree lists:\n Block Stat Size Id\n" );
119 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
120 DUMP( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
121 (DWORD
)&heap
->freeList
[i
].arena
, heap
->freeList
[i
].arena
.size
,
122 heap
->freeList
[i
].arena
.threadId
,
123 (DWORD
)heap
->freeList
[i
].arena
.prev
,
124 (DWORD
)heap
->freeList
[i
].arena
.next
);
126 subheap
= &heap
->subheap
;
129 DWORD freeSize
= 0, usedSize
= 0, arenaSize
= subheap
->headerSize
;
130 DUMP( "\n\nSub-heap %08lx: size=%08lx committed=%08lx\n",
131 (DWORD
)subheap
, subheap
->size
, subheap
->commitSize
);
133 DUMP( "\n Block Stat Size Id\n" );
134 ptr
= (char*)subheap
+ subheap
->headerSize
;
135 while (ptr
< (char *)subheap
+ subheap
->size
)
137 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
139 ARENA_FREE
*pArena
= (ARENA_FREE
*)ptr
;
140 DUMP( "%08lx free %08lx %04x prev=%08lx next=%08lx\n",
141 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
142 pArena
->threadId
, (DWORD
)pArena
->prev
,
143 (DWORD
)pArena
->next
);
144 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
145 arenaSize
+= sizeof(ARENA_FREE
);
146 freeSize
+= pArena
->size
& ARENA_SIZE_MASK
;
148 else if (*(DWORD
*)ptr
& ARENA_FLAG_PREV_FREE
)
150 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
151 DUMP( "%08lx Used %08lx %04x back=%08lx EIP=%08lx\n",
152 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
153 pArena
->threadId
, *((DWORD
*)pArena
- 1),
155 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
156 arenaSize
+= sizeof(ARENA_INUSE
);
157 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
161 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
;
162 DUMP( "%08lx used %08lx %04x EIP=%08lx\n",
163 (DWORD
)pArena
, pArena
->size
& ARENA_SIZE_MASK
,
164 pArena
->threadId
, pArena
->callerEIP
);
165 ptr
+= sizeof(*pArena
) + (pArena
->size
& ARENA_SIZE_MASK
);
166 arenaSize
+= sizeof(ARENA_INUSE
);
167 usedSize
+= pArena
->size
& ARENA_SIZE_MASK
;
170 DUMP( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
171 subheap
->size
, subheap
->commitSize
, freeSize
, usedSize
,
172 arenaSize
, (arenaSize
* 100) / subheap
->size
);
173 subheap
= subheap
->next
;
178 /***********************************************************************
181 * Pointer to the heap
184 static HEAP
*HEAP_GetPtr(
185 HANDLE32 heap
/* [in] Handle to the heap */
187 HEAP
*heapPtr
= (HEAP
*)heap
;
188 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
190 WARN(heap
, "Invalid heap %08x!\n", heap
);
191 SetLastError( ERROR_INVALID_HANDLE
);
194 if (TRACE_ON(heap
) && !HeapValidate( heap
, 0, NULL
))
196 HEAP_Dump( heapPtr
);
198 SetLastError( ERROR_INVALID_HANDLE
);
205 /***********************************************************************
206 * HEAP_InsertFreeBlock
208 * Insert a free block into the free list.
210 static void HEAP_InsertFreeBlock( HEAP
*heap
, ARENA_FREE
*pArena
)
212 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
213 while (pEntry
->size
< pArena
->size
) pEntry
++;
214 pArena
->size
|= ARENA_FLAG_FREE
;
215 pArena
->next
= pEntry
->arena
.next
;
216 pArena
->next
->prev
= pArena
;
217 pArena
->prev
= &pEntry
->arena
;
218 pEntry
->arena
.next
= pArena
;
222 /***********************************************************************
224 * Find the sub-heap containing a given address.
230 static SUBHEAP
*HEAP_FindSubHeap(
231 HEAP
*heap
, /* [in] Heap pointer */
232 LPCVOID ptr
/* [in] Address */
234 SUBHEAP
*sub
= &heap
->subheap
;
237 if (((char *)ptr
>= (char *)sub
) &&
238 ((char *)ptr
< (char *)sub
+ sub
->size
)) return sub
;
245 /***********************************************************************
248 * Make sure the heap storage is committed up to (not including) ptr.
250 static BOOL32
HEAP_Commit( SUBHEAP
*subheap
, void *ptr
)
252 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
253 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
254 if (size
> subheap
->size
) size
= subheap
->size
;
255 if (size
<= subheap
->commitSize
) return TRUE
;
256 if (!VirtualAlloc( (char *)subheap
+ subheap
->commitSize
,
257 size
- subheap
->commitSize
, MEM_COMMIT
,
258 PAGE_EXECUTE_READWRITE
))
260 WARN(heap
, "Could not commit %08lx bytes at %08lx for heap %08lx\n",
261 size
- subheap
->commitSize
,
262 (DWORD
)((char *)subheap
+ subheap
->commitSize
),
263 (DWORD
)subheap
->heap
);
266 subheap
->commitSize
= size
;
271 /***********************************************************************
274 * If possible, decommit the heap storage from (including) 'ptr'.
276 static BOOL32
HEAP_Decommit( SUBHEAP
*subheap
, void *ptr
)
278 DWORD size
= (DWORD
)((char *)ptr
- (char *)subheap
);
279 size
= (size
+ 0xfff) & 0xfffff000; /* Align size on a page boundary */
280 if (size
>= subheap
->commitSize
) return TRUE
;
281 if (!VirtualFree( (char *)subheap
+ size
,
282 subheap
->commitSize
- size
, MEM_DECOMMIT
))
284 WARN(heap
, "Could not decommit %08lx bytes at %08lx for heap %08lx\n",
285 subheap
->commitSize
- size
,
286 (DWORD
)((char *)subheap
+ size
),
287 (DWORD
)subheap
->heap
);
290 subheap
->commitSize
= size
;
295 /***********************************************************************
296 * HEAP_CreateFreeBlock
298 * Create a free block at a specified address. 'size' is the size of the
299 * whole block, including the new arena.
301 static void HEAP_CreateFreeBlock( SUBHEAP
*subheap
, void *ptr
, DWORD size
)
305 /* Create a free arena */
307 pFree
= (ARENA_FREE
*)ptr
;
308 pFree
->threadId
= GetCurrentTask();
309 pFree
->magic
= ARENA_FREE_MAGIC
;
311 /* If debugging, erase the freed block content */
315 char *pEnd
= (char *)ptr
+ size
;
316 if (pEnd
> (char *)subheap
+ subheap
->commitSize
)
317 pEnd
= (char *)subheap
+ subheap
->commitSize
;
318 if (pEnd
> (char *)(pFree
+ 1))
319 memset( pFree
+ 1, ARENA_FREE_FILLER
, pEnd
- (char *)(pFree
+ 1) );
322 /* Check if next block is free also */
324 if (((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
) &&
325 (*(DWORD
*)((char *)ptr
+ size
) & ARENA_FLAG_FREE
))
327 /* Remove the next arena from the free list */
328 ARENA_FREE
*pNext
= (ARENA_FREE
*)((char *)ptr
+ size
);
329 pNext
->next
->prev
= pNext
->prev
;
330 pNext
->prev
->next
= pNext
->next
;
331 size
+= (pNext
->size
& ARENA_SIZE_MASK
) + sizeof(*pNext
);
333 memset( pNext
, ARENA_FREE_FILLER
, sizeof(ARENA_FREE
) );
336 /* Set the next block PREV_FREE flag and pointer */
338 if ((char *)ptr
+ size
< (char *)subheap
+ subheap
->size
)
340 DWORD
*pNext
= (DWORD
*)((char *)ptr
+ size
);
341 *pNext
|= ARENA_FLAG_PREV_FREE
;
342 *(ARENA_FREE
**)(pNext
- 1) = pFree
;
345 /* Last, insert the new block into the free list */
347 pFree
->size
= size
- sizeof(*pFree
);
348 HEAP_InsertFreeBlock( subheap
->heap
, pFree
);
352 /***********************************************************************
353 * HEAP_MakeInUseBlockFree
355 * Turn an in-use block into a free block. Can also decommit the end of
356 * the heap, and possibly even free the sub-heap altogether.
358 static void HEAP_MakeInUseBlockFree( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
361 DWORD size
= (pArena
->size
& ARENA_SIZE_MASK
) + sizeof(*pArena
);
363 /* Check if we can merge with previous block */
365 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
367 pFree
= *((ARENA_FREE
**)pArena
- 1);
368 size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
369 /* Remove it from the free list */
370 pFree
->next
->prev
= pFree
->prev
;
371 pFree
->prev
->next
= pFree
->next
;
373 else pFree
= (ARENA_FREE
*)pArena
;
375 /* Create a free block */
377 HEAP_CreateFreeBlock( subheap
, pFree
, size
);
378 size
= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
);
379 if ((char *)pFree
+ size
< (char *)subheap
+ subheap
->size
)
380 return; /* Not the last block, so nothing more to do */
382 /* Free the whole sub-heap if it's empty and not the original one */
384 if (((char *)pFree
== (char *)subheap
+ subheap
->headerSize
) &&
385 (subheap
!= &subheap
->heap
->subheap
))
387 SUBHEAP
*pPrev
= &subheap
->heap
->subheap
;
388 /* Remove the free block from the list */
389 pFree
->next
->prev
= pFree
->prev
;
390 pFree
->prev
->next
= pFree
->next
;
391 /* Remove the subheap from the list */
392 while (pPrev
&& (pPrev
->next
!= subheap
)) pPrev
= pPrev
->next
;
393 if (pPrev
) pPrev
->next
= subheap
->next
;
394 /* Free the memory */
396 if (subheap
->selector
) FreeSelector( subheap
->selector
);
397 VirtualFree( subheap
, 0, MEM_RELEASE
);
401 /* Decommit the end of the heap */
403 HEAP_Decommit( subheap
, pFree
+ 1 );
407 /***********************************************************************
410 * Shrink an in-use block.
412 static void HEAP_ShrinkBlock(SUBHEAP
*subheap
, ARENA_INUSE
*pArena
, DWORD size
)
414 if ((pArena
->size
& ARENA_SIZE_MASK
) >= size
+ HEAP_MIN_BLOCK_SIZE
)
416 HEAP_CreateFreeBlock( subheap
, (char *)(pArena
+ 1) + size
,
417 (pArena
->size
& ARENA_SIZE_MASK
) - size
);
418 pArena
->size
= (pArena
->size
& ~ARENA_SIZE_MASK
) | size
;
422 /* Turn off PREV_FREE flag in next block */
423 char *pNext
= (char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
);
424 if (pNext
< (char *)subheap
+ subheap
->size
)
425 *(DWORD
*)pNext
&= ~ARENA_FLAG_PREV_FREE
;
430 /***********************************************************************
433 * Create a sub-heap of the given size.
435 static SUBHEAP
*HEAP_CreateSubHeap( DWORD flags
, DWORD commitSize
,
441 /* Round-up sizes on a 64K boundary */
443 if (flags
& HEAP_WINE_SEGPTR
)
445 totalSize
= commitSize
= 0x10000; /* Only 64K at a time for SEGPTRs */
449 totalSize
= (totalSize
+ 0xffff) & 0xffff0000;
450 commitSize
= (commitSize
+ 0xffff) & 0xffff0000;
451 if (!commitSize
) commitSize
= 0x10000;
452 if (totalSize
< commitSize
) totalSize
= commitSize
;
455 /* Allocate the memory block */
457 if (!(subheap
= VirtualAlloc( NULL
, totalSize
,
458 MEM_RESERVE
, PAGE_EXECUTE_READWRITE
)))
460 WARN(heap
, "Could not VirtualAlloc %08lx bytes\n",
464 if (!VirtualAlloc(subheap
, commitSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
))
466 WARN(heap
, "Could not commit %08lx bytes for sub-heap %08lx\n",
467 commitSize
, (DWORD
)subheap
);
468 VirtualFree( subheap
, 0, MEM_RELEASE
);
472 /* Allocate a selector if needed */
474 if (flags
& HEAP_WINE_SEGPTR
)
476 selector
= SELECTOR_AllocBlock( subheap
, totalSize
,
477 (flags
& HEAP_WINE_CODESEG
) ? SEGMENT_CODE
: SEGMENT_DATA
,
478 (flags
& HEAP_WINE_CODESEG
) != 0, FALSE
);
481 WARN(heap
, "Could not allocate selector\n" );
482 VirtualFree( subheap
, 0, MEM_RELEASE
);
487 /* Fill the sub-heap structure */
489 subheap
->size
= totalSize
;
490 subheap
->commitSize
= commitSize
;
491 subheap
->headerSize
= sizeof(*subheap
);
492 subheap
->next
= NULL
;
493 subheap
->heap
= NULL
;
494 subheap
->magic
= SUBHEAP_MAGIC
;
495 subheap
->selector
= selector
;
500 /***********************************************************************
503 * Find a free block at least as large as the requested size, and make sure
504 * the requested size is committed.
506 static ARENA_FREE
*HEAP_FindFreeBlock( HEAP
*heap
, DWORD size
,
507 SUBHEAP
**ppSubHeap
)
511 FREE_LIST_ENTRY
*pEntry
= heap
->freeList
;
513 /* Find a suitable free list, and in it find a block large enough */
515 while (pEntry
->size
< size
) pEntry
++;
516 pArena
= pEntry
->arena
.next
;
517 while (pArena
!= &heap
->freeList
[0].arena
)
519 if (pArena
->size
> size
)
521 subheap
= HEAP_FindSubHeap( heap
, pArena
);
522 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
523 + size
+ HEAP_MIN_BLOCK_SIZE
))
525 *ppSubHeap
= subheap
;
529 pArena
= pArena
->next
;
532 /* If no block was found, attempt to grow the heap */
534 if (!(heap
->flags
& HEAP_GROWABLE
))
536 WARN(heap
, "Not enough space in heap %08lx for %08lx bytes\n",
540 size
+= sizeof(SUBHEAP
) + sizeof(ARENA_FREE
);
541 if (!(subheap
= HEAP_CreateSubHeap( heap
->flags
, size
,
542 MAX( HEAP_DEF_SIZE
, size
) )))
545 /* Insert the new sub-heap in the list */
547 subheap
->heap
= heap
;
548 subheap
->next
= heap
->subheap
.next
;
549 heap
->subheap
.next
= subheap
;
550 size
= subheap
->size
;
551 TRACE(heap
, "created new sub-heap %08lx of %08lx bytes for heap %08lx\n",
552 (DWORD
)subheap
, size
, (DWORD
)heap
);
554 HEAP_CreateFreeBlock( subheap
, subheap
+ 1, size
- sizeof(*subheap
) );
555 *ppSubHeap
= subheap
;
556 return (ARENA_FREE
*)(subheap
+ 1);
560 /***********************************************************************
561 * HEAP_IsValidArenaPtr
563 * Check that the pointer is inside the range possible for arenas.
565 static BOOL32
HEAP_IsValidArenaPtr( HEAP
*heap
, void *ptr
)
568 SUBHEAP
*subheap
= HEAP_FindSubHeap( heap
, ptr
);
569 if (!subheap
) return FALSE
;
570 if ((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
) return TRUE
;
571 if (subheap
!= &heap
->subheap
) return FALSE
;
572 for (i
= 0; i
< HEAP_NB_FREE_LISTS
; i
++)
573 if (ptr
== (void *)&heap
->freeList
[i
].arena
) return TRUE
;
578 /***********************************************************************
579 * HEAP_ValidateFreeArena
581 static BOOL32
HEAP_ValidateFreeArena( SUBHEAP
*subheap
, ARENA_FREE
*pArena
)
583 char *heapEnd
= (char *)subheap
+ subheap
->size
;
585 /* Check magic number */
586 if (pArena
->magic
!= ARENA_FREE_MAGIC
)
588 WARN(heap
, "Heap %08lx: invalid free arena magic for %08lx\n",
589 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
592 /* Check size flags */
593 if (!(pArena
->size
& ARENA_FLAG_FREE
) ||
594 (pArena
->size
& ARENA_FLAG_PREV_FREE
))
596 WARN(heap
, "Heap %08lx: bad flags %lx for free arena %08lx\n",
597 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
599 /* Check arena size */
600 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
602 WARN(heap
, "Heap %08lx: bad size %08lx for free arena %08lx\n",
603 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
606 /* Check that next pointer is valid */
607 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->next
))
609 WARN(heap
, "Heap %08lx: bad next ptr %08lx for arena %08lx\n",
610 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
613 /* Check that next arena is free */
614 if (!(pArena
->next
->size
& ARENA_FLAG_FREE
) ||
615 (pArena
->next
->magic
!= ARENA_FREE_MAGIC
))
617 WARN(heap
, "Heap %08lx: next arena %08lx invalid for %08lx\n",
618 (DWORD
)subheap
->heap
, (DWORD
)pArena
->next
, (DWORD
)pArena
);
621 /* Check that prev pointer is valid */
622 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pArena
->prev
))
624 WARN(heap
, "Heap %08lx: bad prev ptr %08lx for arena %08lx\n",
625 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
628 /* Check that prev arena is free */
629 if (!(pArena
->prev
->size
& ARENA_FLAG_FREE
) ||
630 (pArena
->prev
->magic
!= ARENA_FREE_MAGIC
))
632 WARN(heap
, "Heap %08lx: prev arena %08lx invalid for %08lx\n",
633 (DWORD
)subheap
->heap
, (DWORD
)pArena
->prev
, (DWORD
)pArena
);
636 /* Check that next block has PREV_FREE flag */
637 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
)
639 if (!(*(DWORD
*)((char *)(pArena
+ 1) +
640 (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
642 WARN(heap
, "Heap %08lx: free arena %08lx next block has no PREV_FREE flag\n",
643 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
646 /* Check next block back pointer */
647 if (*((ARENA_FREE
**)((char *)(pArena
+ 1) +
648 (pArena
->size
& ARENA_SIZE_MASK
)) - 1) != pArena
)
650 WARN(heap
, "Heap %08lx: arena %08lx has wrong back ptr %08lx\n",
651 (DWORD
)subheap
->heap
, (DWORD
)pArena
,
652 *((DWORD
*)((char *)(pArena
+1)+ (pArena
->size
& ARENA_SIZE_MASK
)) - 1));
660 /***********************************************************************
661 * HEAP_ValidateInUseArena
663 static BOOL32
HEAP_ValidateInUseArena( SUBHEAP
*subheap
, ARENA_INUSE
*pArena
)
665 char *heapEnd
= (char *)subheap
+ subheap
->size
;
667 /* Check magic number */
668 if (pArena
->magic
!= ARENA_INUSE_MAGIC
)
670 WARN(heap
, "Heap %08lx: invalid in-use arena magic for %08lx\n",
671 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
674 /* Check size flags */
675 if (pArena
->size
& ARENA_FLAG_FREE
)
677 WARN(heap
, "Heap %08lx: bad flags %lx for in-use arena %08lx\n",
678 (DWORD
)subheap
->heap
, pArena
->size
& ~ARENA_SIZE_MASK
, (DWORD
)pArena
);
680 /* Check arena size */
681 if ((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) > heapEnd
)
683 WARN(heap
, "Heap %08lx: bad size %08lx for in-use arena %08lx\n",
684 (DWORD
)subheap
->heap
, (DWORD
)pArena
->size
& ARENA_SIZE_MASK
, (DWORD
)pArena
);
687 /* Check next arena PREV_FREE flag */
688 if (((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
) < heapEnd
) &&
689 (*(DWORD
*)((char *)(pArena
+ 1) + (pArena
->size
& ARENA_SIZE_MASK
)) & ARENA_FLAG_PREV_FREE
))
691 WARN(heap
, "Heap %08lx: in-use arena %08lx next block has PREV_FREE flag\n",
692 (DWORD
)subheap
->heap
, (DWORD
)pArena
);
695 /* Check prev free arena */
696 if (pArena
->size
& ARENA_FLAG_PREV_FREE
)
698 ARENA_FREE
*pPrev
= *((ARENA_FREE
**)pArena
- 1);
699 /* Check prev pointer */
700 if (!HEAP_IsValidArenaPtr( subheap
->heap
, pPrev
))
702 WARN(heap
, "Heap %08lx: bad back ptr %08lx for arena %08lx\n",
703 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
706 /* Check that prev arena is free */
707 if (!(pPrev
->size
& ARENA_FLAG_FREE
) ||
708 (pPrev
->magic
!= ARENA_FREE_MAGIC
))
710 WARN(heap
, "Heap %08lx: prev arena %08lx invalid for in-use %08lx\n",
711 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
714 /* Check that prev arena is really the previous block */
715 if ((char *)(pPrev
+ 1) + (pPrev
->size
& ARENA_SIZE_MASK
) != (char *)pArena
)
717 WARN(heap
, "Heap %08lx: prev arena %08lx is not prev for in-use %08lx\n",
718 (DWORD
)subheap
->heap
, (DWORD
)pPrev
, (DWORD
)pArena
);
726 /***********************************************************************
728 * Checks whether the pointer points to a block inside a given heap.
731 * Should this return BOOL32?
737 int HEAP_IsInsideHeap(
738 HANDLE32 heap
, /* [in] Heap */
739 DWORD flags
, /* [in] Flags */
740 LPCVOID ptr
/* [in] Pointer */
742 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
746 /* Validate the parameters */
748 if (!heapPtr
) return 0;
749 flags
|= heapPtr
->flags
;
750 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
751 ret
= (((subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)) != NULL
) &&
752 (((char *)ptr
>= (char *)subheap
+ subheap
->headerSize
753 + sizeof(ARENA_INUSE
))));
754 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
759 /***********************************************************************
762 * Transform a linear pointer into a SEGPTR. The pointer must have been
763 * allocated from a HEAP_WINE_SEGPTR heap.
765 SEGPTR
HEAP_GetSegptr( HANDLE32 heap
, DWORD flags
, LPCVOID ptr
)
767 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
771 /* Validate the parameters */
773 if (!heapPtr
) return 0;
774 flags
|= heapPtr
->flags
;
775 if (!(flags
& HEAP_WINE_SEGPTR
))
777 WARN(heap
, "Heap %08x is not a SEGPTR heap\n",
781 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
783 /* Get the subheap */
785 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, ptr
)))
787 WARN(heap
, "%p is not inside heap %08x\n",
789 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
793 /* Build the SEGPTR */
795 ret
= PTR_SEG_OFF_TO_SEGPTR(subheap
->selector
, (DWORD
)ptr
-(DWORD
)subheap
);
796 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
801 /***********************************************************************
802 * HeapCreate (KERNEL32.336)
804 * Handle of heap: Success
807 HANDLE32 WINAPI
HeapCreate(
808 DWORD flags
, /* [in] Heap allocation flag */
809 DWORD initialSize
, /* [in] Initial heap size */
810 DWORD maxSize
/* [in] Maximum heap size */
815 FREE_LIST_ENTRY
*pEntry
;
817 /* Allocate the heap block */
821 maxSize
= HEAP_DEF_SIZE
;
822 flags
|= HEAP_GROWABLE
;
824 if (!(subheap
= HEAP_CreateSubHeap( flags
, initialSize
, maxSize
)))
826 SetLastError( ERROR_OUTOFMEMORY
);
830 /* Fill the heap structure */
832 heap
= (HEAP
*)subheap
;
833 subheap
->heap
= heap
;
834 subheap
->headerSize
= sizeof(HEAP
);
837 heap
->magic
= HEAP_MAGIC
;
839 /* Build the free lists */
841 for (i
= 0, pEntry
= heap
->freeList
; i
< HEAP_NB_FREE_LISTS
; i
++, pEntry
++)
843 pEntry
->size
= HEAP_freeListSizes
[i
];
844 pEntry
->arena
.size
= 0 | ARENA_FLAG_FREE
;
845 pEntry
->arena
.next
= i
< HEAP_NB_FREE_LISTS
-1 ?
846 &heap
->freeList
[i
+1].arena
: &heap
->freeList
[0].arena
;
847 pEntry
->arena
.prev
= i
? &heap
->freeList
[i
-1].arena
:
848 &heap
->freeList
[HEAP_NB_FREE_LISTS
-1].arena
;
849 pEntry
->arena
.threadId
= 0;
850 pEntry
->arena
.magic
= ARENA_FREE_MAGIC
;
853 /* Create the first free block */
855 HEAP_CreateFreeBlock( subheap
, heap
+ 1, subheap
->size
- sizeof(*heap
) );
857 /* Initialize critical section */
859 InitializeCriticalSection( &heap
->critSection
);
860 if (!SystemHeap
) HEAP_SystemLock
= &heap
->critSection
;
864 return (HANDLE32
)heap
;
868 /***********************************************************************
869 * HeapDestroy (KERNEL32.337)
874 BOOL32 WINAPI
HeapDestroy(
875 HANDLE32 heap
/* [in] Handle of heap */
877 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
880 TRACE(heap
, "%08x\n", heap
);
881 if (!heapPtr
) return FALSE
;
883 DeleteCriticalSection( &heapPtr
->critSection
);
884 subheap
= &heapPtr
->subheap
;
887 SUBHEAP
*next
= subheap
->next
;
888 if (subheap
->selector
) FreeSelector( subheap
->selector
);
889 VirtualFree( subheap
, 0, MEM_RELEASE
);
896 /***********************************************************************
897 * HeapAlloc (KERNEL32.334)
899 * Pointer to allocated memory block
902 LPVOID WINAPI
HeapAlloc(
903 HANDLE32 heap
, /* [in] Handle of private heap block */
904 DWORD flags
, /* [in] Heap allocation control flags */
905 DWORD size
/* [in] Number of bytes to allocate */
910 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
912 /* Validate the parameters */
914 if (!heapPtr
) return NULL
;
915 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
;
916 flags
|= heapPtr
->flags
;
917 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
918 size
= (size
+ 3) & ~3;
919 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
921 /* Locate a suitable free block */
923 if (!(pArena
= HEAP_FindFreeBlock( heapPtr
, size
, &subheap
)))
925 TRACE(heap
, "(%08x,%08lx,%08lx): returning NULL\n",
927 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
928 SetLastError( ERROR_COMMITMENT_LIMIT
);
932 /* Remove the arena from the free list */
934 pArena
->next
->prev
= pArena
->prev
;
935 pArena
->prev
->next
= pArena
->next
;
937 /* Build the in-use arena */
939 pInUse
= (ARENA_INUSE
*)pArena
;
940 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
941 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
942 pInUse
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
943 pInUse
->threadId
= GetCurrentTask();
944 pInUse
->magic
= ARENA_INUSE_MAGIC
;
946 /* Shrink the block */
948 HEAP_ShrinkBlock( subheap
, pInUse
, size
);
950 if (flags
& HEAP_ZERO_MEMORY
) memset( pInUse
+ 1, 0, size
);
951 else if (TRACE_ON(heap
)) memset( pInUse
+ 1, ARENA_INUSE_FILLER
, size
);
953 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
955 TRACE(heap
, "(%08x,%08lx,%08lx): returning %08lx\n",
956 heap
, flags
, size
, (DWORD
)(pInUse
+ 1) );
957 return (LPVOID
)(pInUse
+ 1);
961 /***********************************************************************
962 * HeapFree (KERNEL32.338)
967 BOOL32 WINAPI
HeapFree(
968 HANDLE32 heap
, /* [in] Handle of heap */
969 DWORD flags
, /* [in] Heap freeing flags */
970 LPVOID ptr
/* [in] Address of memory to free */
974 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
976 /* Validate the parameters */
978 if (!heapPtr
) return FALSE
;
979 flags
&= HEAP_NO_SERIALIZE
;
980 flags
|= heapPtr
->flags
;
981 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
982 if (!ptr
|| !HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
984 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
985 SetLastError( ERROR_INVALID_PARAMETER
);
986 TRACE(heap
, "(%08x,%08lx,%08lx): returning FALSE\n",
987 heap
, flags
, (DWORD
)ptr
);
991 /* Turn the block into a free block */
993 pInUse
= (ARENA_INUSE
*)ptr
- 1;
994 subheap
= HEAP_FindSubHeap( heapPtr
, pInUse
);
995 HEAP_MakeInUseBlockFree( subheap
, pInUse
);
997 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
998 /* SetLastError( 0 ); */
1000 TRACE(heap
, "(%08x,%08lx,%08lx): returning TRUE\n",
1001 heap
, flags
, (DWORD
)ptr
);
1006 /***********************************************************************
1007 * HeapReAlloc (KERNEL32.340)
1009 * Pointer to reallocated memory block
1012 LPVOID WINAPI
HeapReAlloc(
1013 HANDLE32 heap
, /* [in] Handle of heap block */
1014 DWORD flags
, /* [in] Heap reallocation flags */
1015 LPVOID ptr
, /* [in] Address of memory to reallocate */
1016 DWORD size
/* [in] Number of bytes to reallocate */
1018 ARENA_INUSE
*pArena
;
1023 if (!ptr
) return HeapAlloc( heap
, flags
, size
); /* FIXME: correct? */
1024 if (!(heapPtr
= HEAP_GetPtr( heap
))) return FALSE
;
1026 /* Validate the parameters */
1028 flags
&= HEAP_GENERATE_EXCEPTIONS
| HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
|
1029 HEAP_REALLOC_IN_PLACE_ONLY
;
1030 flags
|= heapPtr
->flags
;
1031 size
= (size
+ 3) & ~3;
1032 if (size
< HEAP_MIN_BLOCK_SIZE
) size
= HEAP_MIN_BLOCK_SIZE
;
1034 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1035 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1037 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1038 SetLastError( ERROR_INVALID_PARAMETER
);
1039 TRACE(heap
, "(%08x,%08lx,%08lx,%08lx): returning NULL\n",
1040 heap
, flags
, (DWORD
)ptr
, size
);
1044 /* Check if we need to grow the block */
1046 pArena
= (ARENA_INUSE
*)ptr
- 1;
1047 pArena
->threadId
= GetCurrentTask();
1048 subheap
= HEAP_FindSubHeap( heapPtr
, pArena
);
1049 oldSize
= (pArena
->size
& ARENA_SIZE_MASK
);
1052 char *pNext
= (char *)(pArena
+ 1) + oldSize
;
1053 if ((pNext
< (char *)subheap
+ subheap
->size
) &&
1054 (*(DWORD
*)pNext
& ARENA_FLAG_FREE
) &&
1055 (oldSize
+ (*(DWORD
*)pNext
& ARENA_SIZE_MASK
) + sizeof(ARENA_FREE
) >= size
))
1057 /* The next block is free and large enough */
1058 ARENA_FREE
*pFree
= (ARENA_FREE
*)pNext
;
1059 pFree
->next
->prev
= pFree
->prev
;
1060 pFree
->prev
->next
= pFree
->next
;
1061 pArena
->size
+= (pFree
->size
& ARENA_SIZE_MASK
) + sizeof(*pFree
);
1062 if (!HEAP_Commit( subheap
, (char *)pArena
+ sizeof(ARENA_INUSE
)
1063 + size
+ HEAP_MIN_BLOCK_SIZE
))
1065 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1066 SetLastError( ERROR_OUTOFMEMORY
);
1069 HEAP_ShrinkBlock( subheap
, pArena
, size
);
1071 else /* Do it the hard way */
1074 ARENA_INUSE
*pInUse
;
1075 SUBHEAP
*newsubheap
;
1077 if ((flags
& HEAP_REALLOC_IN_PLACE_ONLY
) ||
1078 !(pNew
= HEAP_FindFreeBlock( heapPtr
, size
, &newsubheap
)))
1080 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1081 SetLastError( ERROR_OUTOFMEMORY
);
1085 /* Build the in-use arena */
1087 pNew
->next
->prev
= pNew
->prev
;
1088 pNew
->prev
->next
= pNew
->next
;
1089 pInUse
= (ARENA_INUSE
*)pNew
;
1090 pInUse
->size
= (pInUse
->size
& ~ARENA_FLAG_FREE
)
1091 + sizeof(ARENA_FREE
) - sizeof(ARENA_INUSE
);
1092 pInUse
->threadId
= GetCurrentTask();
1093 pInUse
->magic
= ARENA_INUSE_MAGIC
;
1094 HEAP_ShrinkBlock( newsubheap
, pInUse
, size
);
1095 memcpy( pInUse
+ 1, pArena
+ 1, oldSize
);
1097 /* Free the previous block */
1099 HEAP_MakeInUseBlockFree( subheap
, pArena
);
1100 subheap
= newsubheap
;
1104 else HEAP_ShrinkBlock( subheap
, pArena
, size
); /* Shrink the block */
1106 /* Clear the extra bytes if needed */
1110 if (flags
& HEAP_ZERO_MEMORY
)
1111 memset( (char *)(pArena
+ 1) + oldSize
, 0,
1112 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1113 else if (TRACE_ON(heap
))
1114 memset( (char *)(pArena
+ 1) + oldSize
, ARENA_INUSE_FILLER
,
1115 (pArena
->size
& ARENA_SIZE_MASK
) - oldSize
);
1118 /* Return the new arena */
1120 pArena
->callerEIP
= *((DWORD
*)&heap
- 1); /* hack hack */
1121 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1123 TRACE(heap
, "(%08x,%08lx,%08lx,%08lx): returning %08lx\n",
1124 heap
, flags
, (DWORD
)ptr
, size
, (DWORD
)(pArena
+ 1) );
1125 return (LPVOID
)(pArena
+ 1);
1129 /***********************************************************************
1130 * HeapCompact (KERNEL32.335)
1132 DWORD WINAPI
HeapCompact( HANDLE32 heap
, DWORD flags
)
1138 /***********************************************************************
1139 * HeapLock (KERNEL32.339)
1140 * Attempts to acquire the critical section object for a specified heap.
1146 BOOL32 WINAPI
HeapLock(
1147 HANDLE32 heap
/* [in] Handle of heap to lock for exclusive access */
1149 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1150 if (!heapPtr
) return FALSE
;
1151 EnterCriticalSection( &heapPtr
->critSection
);
1156 /***********************************************************************
1157 * HeapUnlock (KERNEL32.342)
1158 * Releases ownership of the critical section object.
1164 BOOL32 WINAPI
HeapUnlock(
1165 HANDLE32 heap
/* [in] Handle to the heap to unlock */
1167 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1168 if (!heapPtr
) return FALSE
;
1169 LeaveCriticalSection( &heapPtr
->critSection
);
1174 /***********************************************************************
1175 * HeapSize (KERNEL32.341)
1177 * Size in bytes of allocated memory
1180 DWORD WINAPI
HeapSize(
1181 HANDLE32 heap
, /* [in] Handle of heap */
1182 DWORD flags
, /* [in] Heap size control flags */
1183 LPVOID ptr
/* [in] Address of memory to return size for */
1186 HEAP
*heapPtr
= HEAP_GetPtr( heap
);
1188 if (!heapPtr
) return FALSE
;
1189 flags
&= HEAP_NO_SERIALIZE
;
1190 flags
|= heapPtr
->flags
;
1191 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapLock( heap
);
1192 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
1194 SetLastError( ERROR_INVALID_PARAMETER
);
1199 ARENA_INUSE
*pArena
= (ARENA_INUSE
*)ptr
- 1;
1200 ret
= pArena
->size
& ARENA_SIZE_MASK
;
1202 if (!(flags
& HEAP_NO_SERIALIZE
)) HeapUnlock( heap
);
1204 TRACE(heap
, "(%08x,%08lx,%08lx): returning %08lx\n",
1205 heap
, flags
, (DWORD
)ptr
, ret
);
1210 /***********************************************************************
1211 * HeapValidate (KERNEL32.343)
1212 * Validates a specified heap.
1221 BOOL32 WINAPI
HeapValidate(
1222 HANDLE32 heap
, /* [in] Handle to the heap */
1223 DWORD flags
, /* [in] Bit flags that control access during operation */
1224 LPCVOID block
/* [in] Optional pointer to memory block to validate */
1227 HEAP
*heapPtr
= (HEAP
*)(heap
);
1229 if (!heapPtr
|| (heapPtr
->magic
!= HEAP_MAGIC
))
1231 WARN(heap
, "Invalid heap %08x!\n", heap
);
1237 /* Only check this single memory block */
1238 if (!(subheap
= HEAP_FindSubHeap( heapPtr
, block
)) ||
1239 ((char *)block
< (char *)subheap
+ subheap
->headerSize
1240 + sizeof(ARENA_INUSE
)))
1242 WARN(heap
, "Heap %08lx: block %08lx is not inside heap\n",
1243 (DWORD
)heap
, (DWORD
)block
);
1246 return HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)block
- 1 );
1249 subheap
= &heapPtr
->subheap
;
1252 char *ptr
= (char *)subheap
+ subheap
->headerSize
;
1253 while (ptr
< (char *)subheap
+ subheap
->size
)
1255 if (*(DWORD
*)ptr
& ARENA_FLAG_FREE
)
1257 if (!HEAP_ValidateFreeArena( subheap
, (ARENA_FREE
*)ptr
))
1259 ptr
+= sizeof(ARENA_FREE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1263 if (!HEAP_ValidateInUseArena( subheap
, (ARENA_INUSE
*)ptr
))
1265 ptr
+= sizeof(ARENA_INUSE
) + (*(DWORD
*)ptr
& ARENA_SIZE_MASK
);
1268 subheap
= subheap
->next
;
1274 /***********************************************************************
1275 * HeapWalk (KERNEL32.344)
1276 * Enumerates the memory blocks in a specified heap.
1282 BOOL32 WINAPI
HeapWalk(
1283 HANDLE32 heap
, /* [in] Handle to heap to enumerate */
1284 LPPROCESS_HEAP_ENTRY
*entry
/* [out] Pointer to structure of enumeration info */
1286 FIXME(heap
, "(%08x): stub.\n", heap
);
1291 /***********************************************************************
1294 * Same as HeapAlloc(), but die on failure.
1296 LPVOID
HEAP_xalloc( HANDLE32 heap
, DWORD flags
, DWORD size
)
1298 LPVOID p
= HeapAlloc( heap
, flags
, size
);
1301 MSG("Virtual memory exhausted.\n" );
1308 /***********************************************************************
1311 LPSTR
HEAP_strdupA( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1313 LPSTR p
= HEAP_xalloc( heap
, flags
, strlen(str
) + 1 );
1319 /***********************************************************************
1322 LPWSTR
HEAP_strdupW( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1324 INT32 len
= lstrlen32W(str
) + 1;
1325 LPWSTR p
= HEAP_xalloc( heap
, flags
, len
* sizeof(WCHAR
) );
1326 lstrcpy32W( p
, str
);
1331 /***********************************************************************
1334 LPWSTR
HEAP_strdupAtoW( HANDLE32 heap
, DWORD flags
, LPCSTR str
)
1338 if (!str
) return NULL
;
1339 ret
= HEAP_xalloc( heap
, flags
, (strlen(str
)+1) * sizeof(WCHAR
) );
1340 lstrcpyAtoW( ret
, str
);
1345 /***********************************************************************
1348 LPSTR
HEAP_strdupWtoA( HANDLE32 heap
, DWORD flags
, LPCWSTR str
)
1352 if (!str
) return NULL
;
1353 ret
= HEAP_xalloc( heap
, flags
, lstrlen32W(str
) + 1 );
1354 lstrcpyWtoA( ret
, str
);