2 * msvcrt.dll heap functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Note: Win32 heap operations are MT safe. We only lock the new
21 * handler and non atomic heap operations
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
31 #define LOCK_HEAP _mlock( _HEAP_LOCK )
32 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
35 #define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
36 ~(sizeof(void *) - 1)))
37 #define ALIGN_PTR(ptr, alignment, offset) ((void *) \
38 ((((DWORD_PTR)((char *)ptr + alignment + sizeof(void *) + offset)) & \
39 ~(alignment - 1)) - offset))
41 #define SB_HEAP_ALIGN 16
43 static HANDLE heap
, sb_heap
;
45 typedef int (CDECL
*MSVCRT_new_handler_func
)(MSVCRT_size_t size
);
47 static MSVCRT_new_handler_func MSVCRT_new_handler
;
48 static int MSVCRT_new_mode
;
50 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
51 static unsigned int MSVCRT_amblksiz
= 16;
52 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
53 static MSVCRT_size_t MSVCRT_sbh_threshold
= 0;
55 static void* msvcrt_heap_alloc(DWORD flags
, MSVCRT_size_t size
)
57 if(size
< MSVCRT_sbh_threshold
)
59 void *memblock
, *temp
, **saved
;
61 temp
= HeapAlloc(sb_heap
, flags
, size
+sizeof(void*)+SB_HEAP_ALIGN
);
62 if(!temp
) return NULL
;
64 memblock
= ALIGN_PTR(temp
, SB_HEAP_ALIGN
, 0);
65 saved
= SAVED_PTR(memblock
);
70 return HeapAlloc(heap
, flags
, size
);
73 static void* msvcrt_heap_realloc(DWORD flags
, void *ptr
, MSVCRT_size_t size
)
75 if(sb_heap
&& ptr
&& !HeapValidate(heap
, 0, ptr
))
77 /* TODO: move data to normal heap if it exceeds sbh_threshold limit */
78 void *memblock
, *temp
, **saved
;
79 MSVCRT_size_t old_padding
, new_padding
, old_size
;
81 saved
= SAVED_PTR(ptr
);
82 old_padding
= (char*)ptr
- (char*)*saved
;
83 old_size
= HeapSize(sb_heap
, 0, *saved
);
86 old_size
-= old_padding
;
88 temp
= HeapReAlloc(sb_heap
, flags
, *saved
, size
+sizeof(void*)+SB_HEAP_ALIGN
);
89 if(!temp
) return NULL
;
91 memblock
= ALIGN_PTR(temp
, SB_HEAP_ALIGN
, 0);
92 saved
= SAVED_PTR(memblock
);
93 new_padding
= (char*)memblock
- (char*)temp
;
95 if(new_padding
!= old_padding
)
96 memmove(memblock
, (char*)temp
+old_padding
, old_size
>size
? size
: old_size
);
102 return HeapReAlloc(heap
, flags
, ptr
, size
);
105 static BOOL
msvcrt_heap_free(void *ptr
)
107 if(sb_heap
&& ptr
&& !HeapValidate(heap
, 0, ptr
))
109 void **saved
= SAVED_PTR(ptr
);
110 return HeapFree(sb_heap
, 0, *saved
);
113 return HeapFree(heap
, 0, ptr
);
116 static MSVCRT_size_t
msvcrt_heap_size(void *ptr
)
118 if(sb_heap
&& ptr
&& !HeapValidate(heap
, 0, ptr
))
120 void **saved
= SAVED_PTR(ptr
);
121 return HeapSize(sb_heap
, 0, *saved
);
124 return HeapSize(heap
, 0, ptr
);
127 /*********************************************************************
128 * ??2@YAPAXI@Z (MSVCRT.@)
130 void* CDECL
MSVCRT_operator_new(MSVCRT_size_t size
)
137 retval
= msvcrt_heap_alloc(0, size
);
140 TRACE("(%ld) returning %p\n", size
, retval
);
145 if(MSVCRT_new_handler
)
146 freed
= (*MSVCRT_new_handler
)(size
);
152 TRACE("(%ld) out of memory\n", size
);
154 throw_bad_alloc("bad allocation");
160 /*********************************************************************
161 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
163 void* CDECL
MSVCRT_operator_new_dbg(MSVCRT_size_t size
, int type
, const char *file
, int line
)
165 return MSVCRT_operator_new( size
);
169 /*********************************************************************
170 * ??3@YAXPAX@Z (MSVCRT.@)
172 void CDECL
MSVCRT_operator_delete(void *mem
)
174 TRACE("(%p)\n", mem
);
175 msvcrt_heap_free(mem
);
179 /*********************************************************************
180 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
182 MSVCRT_new_handler_func CDECL
MSVCRT__query_new_handler(void)
184 return MSVCRT_new_handler
;
188 /*********************************************************************
189 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
191 int CDECL
MSVCRT__query_new_mode(void)
193 return MSVCRT_new_mode
;
196 /*********************************************************************
197 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
199 MSVCRT_new_handler_func CDECL
MSVCRT__set_new_handler(MSVCRT_new_handler_func func
)
201 MSVCRT_new_handler_func old_handler
;
203 old_handler
= MSVCRT_new_handler
;
204 MSVCRT_new_handler
= func
;
209 /*********************************************************************
210 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
212 MSVCRT_new_handler_func CDECL
MSVCRT_set_new_handler(void *func
)
214 TRACE("(%p)\n",func
);
215 MSVCRT__set_new_handler(NULL
);
219 /*********************************************************************
220 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
222 int CDECL
MSVCRT__set_new_mode(int mode
)
226 old_mode
= MSVCRT_new_mode
;
227 MSVCRT_new_mode
= mode
;
232 /*********************************************************************
233 * _callnewh (MSVCRT.@)
235 int CDECL
_callnewh(MSVCRT_size_t size
)
237 if(MSVCRT_new_handler
)
238 (*MSVCRT_new_handler
)(size
);
242 /*********************************************************************
245 void* CDECL
_expand(void* mem
, MSVCRT_size_t size
)
247 return msvcrt_heap_realloc(HEAP_REALLOC_IN_PLACE_ONLY
, mem
, size
);
250 /*********************************************************************
251 * _heapchk (MSVCRT.@)
253 int CDECL
_heapchk(void)
255 if (!HeapValidate(heap
, 0, NULL
) ||
256 (sb_heap
&& !HeapValidate(sb_heap
, 0, NULL
)))
258 msvcrt_set_errno(GetLastError());
259 return MSVCRT__HEAPBADNODE
;
261 return MSVCRT__HEAPOK
;
264 /*********************************************************************
265 * _heapmin (MSVCRT.@)
267 int CDECL
_heapmin(void)
269 if (!HeapCompact( heap
, 0 ) ||
270 (sb_heap
&& !HeapCompact( sb_heap
, 0 )))
272 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
273 msvcrt_set_errno(GetLastError());
279 /*********************************************************************
280 * _heapwalk (MSVCRT.@)
282 int CDECL
_heapwalk(struct MSVCRT__heapinfo
* next
)
284 PROCESS_HEAP_ENTRY phe
;
287 FIXME("small blocks heap not supported\n");
290 phe
.lpData
= next
->_pentry
;
291 phe
.cbData
= next
->_size
;
292 phe
.wFlags
= next
->_useflag
== MSVCRT__USEDENTRY
? PROCESS_HEAP_ENTRY_BUSY
: 0;
294 if (phe
.lpData
&& phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
&&
295 !HeapValidate( heap
, 0, phe
.lpData
))
298 msvcrt_set_errno(GetLastError());
299 return MSVCRT__HEAPBADNODE
;
304 if (!HeapWalk( heap
, &phe
))
307 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
308 return MSVCRT__HEAPEND
;
309 msvcrt_set_errno(GetLastError());
311 return MSVCRT__HEAPBADBEGIN
;
312 return MSVCRT__HEAPBADNODE
;
314 } while (phe
.wFlags
& (PROCESS_HEAP_REGION
|PROCESS_HEAP_UNCOMMITTED_RANGE
));
317 next
->_pentry
= phe
.lpData
;
318 next
->_size
= phe
.cbData
;
319 next
->_useflag
= phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
? MSVCRT__USEDENTRY
: MSVCRT__FREEENTRY
;
320 return MSVCRT__HEAPOK
;
323 /*********************************************************************
324 * _heapset (MSVCRT.@)
326 int CDECL
_heapset(unsigned int value
)
329 struct MSVCRT__heapinfo heap
;
331 memset( &heap
, 0, sizeof(heap
) );
333 while ((retval
= _heapwalk(&heap
)) == MSVCRT__HEAPOK
)
335 if (heap
._useflag
== MSVCRT__FREEENTRY
)
336 memset(heap
._pentry
, value
, heap
._size
);
339 return retval
== MSVCRT__HEAPEND
? MSVCRT__HEAPOK
: retval
;
342 /*********************************************************************
343 * _heapadd (MSVCRT.@)
345 int CDECL
_heapadd(void* mem
, MSVCRT_size_t size
)
347 TRACE("(%p,%ld) unsupported in Win32\n", mem
,size
);
348 *MSVCRT__errno() = MSVCRT_ENOSYS
;
352 /*********************************************************************
353 * _get_heap_handle (MSVCRT.@)
355 MSVCRT_intptr_t CDECL
_get_heap_handle(void)
357 return (MSVCRT_intptr_t
)heap
;
360 /*********************************************************************
363 MSVCRT_size_t CDECL
_msize(void* mem
)
365 MSVCRT_size_t size
= msvcrt_heap_size(mem
);
366 if (size
== ~(MSVCRT_size_t
)0)
368 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
369 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
374 /*********************************************************************
375 * _aligned_msize (MSVCR100.@)
377 size_t CDECL
_aligned_msize(void *p
, MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
381 if(!MSVCRT_CHECK_PMT(p
)) return -1;
383 if(alignment
< sizeof(void*))
384 alignment
= sizeof(void*);
386 alloc_ptr
= SAVED_PTR(p
);
387 return _msize(*alloc_ptr
)-alignment
-sizeof(void*);
390 /*********************************************************************
393 void* CDECL
MSVCRT_calloc(MSVCRT_size_t size
, MSVCRT_size_t count
)
395 return msvcrt_heap_alloc(HEAP_ZERO_MEMORY
, size
*count
);
398 /*********************************************************************
401 void CDECL
MSVCRT_free(void* ptr
)
403 msvcrt_heap_free(ptr
);
406 /*********************************************************************
409 void* CDECL
MSVCRT_malloc(MSVCRT_size_t size
)
411 void *ret
= msvcrt_heap_alloc(0, size
);
413 *MSVCRT__errno() = MSVCRT_ENOMEM
;
417 /*********************************************************************
420 void* CDECL
MSVCRT_realloc(void* ptr
, MSVCRT_size_t size
)
422 if (!ptr
) return MSVCRT_malloc(size
);
423 if (size
) return msvcrt_heap_realloc(0, ptr
, size
);
428 /*********************************************************************
429 * _recalloc (MSVCR100.@)
431 void* CDECL
_recalloc(void *mem
, MSVCRT_size_t num
, MSVCRT_size_t size
)
433 MSVCRT_size_t old_size
;
437 return MSVCRT_calloc(num
, size
);
440 old_size
= _msize(mem
);
442 ret
= MSVCRT_realloc(mem
, size
);
444 *MSVCRT__errno() = MSVCRT_ENOMEM
;
449 memset((BYTE
*)ret
+old_size
, 0, size
-old_size
);
453 /*********************************************************************
454 * __p__amblksiz (MSVCRT.@)
456 unsigned int* CDECL
__p__amblksiz(void)
458 return &MSVCRT_amblksiz
;
461 /*********************************************************************
462 * _get_sbh_threshold (MSVCRT.@)
464 MSVCRT_size_t CDECL
_get_sbh_threshold(void)
466 return MSVCRT_sbh_threshold
;
469 /*********************************************************************
470 * _set_sbh_threshold (MSVCRT.@)
472 int CDECL
_set_sbh_threshold(MSVCRT_size_t threshold
)
482 sb_heap
= HeapCreate(0, 0, 0);
487 MSVCRT_sbh_threshold
= (threshold
+0xf) & ~0xf;
492 /*********************************************************************
493 * _aligned_free (MSVCRT.@)
495 void CDECL
_aligned_free(void *memblock
)
497 TRACE("(%p)\n", memblock
);
501 void **saved
= SAVED_PTR(memblock
);
506 /*********************************************************************
507 * _aligned_offset_malloc (MSVCRT.@)
509 void * CDECL
_aligned_offset_malloc(MSVCRT_size_t size
, MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
511 void *memblock
, *temp
, **saved
;
512 TRACE("(%lu, %lu, %lu)\n", size
, alignment
, offset
);
514 /* alignment must be a power of 2 */
515 if ((alignment
& (alignment
- 1)) != 0)
517 *MSVCRT__errno() = MSVCRT_EINVAL
;
521 /* offset must be less than size */
522 if (offset
&& offset
>= size
)
524 *MSVCRT__errno() = MSVCRT_EINVAL
;
528 /* don't align to less than void pointer size */
529 if (alignment
< sizeof(void *))
530 alignment
= sizeof(void *);
532 /* allocate enough space for void pointer and alignment */
533 temp
= MSVCRT_malloc(size
+ alignment
+ sizeof(void *));
538 /* adjust pointer for proper alignment and offset */
539 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
541 /* Save the real allocation address below returned address */
542 /* so it can be found later to free. */
543 saved
= SAVED_PTR(memblock
);
549 /*********************************************************************
550 * _aligned_malloc (MSVCRT.@)
552 void * CDECL
_aligned_malloc(MSVCRT_size_t size
, MSVCRT_size_t alignment
)
554 TRACE("(%lu, %lu)\n", size
, alignment
);
555 return _aligned_offset_malloc(size
, alignment
, 0);
558 /*********************************************************************
559 * _aligned_offset_realloc (MSVCRT.@)
561 void * CDECL
_aligned_offset_realloc(void *memblock
, MSVCRT_size_t size
,
562 MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
564 void * temp
, **saved
;
565 MSVCRT_size_t old_padding
, new_padding
, old_size
;
566 TRACE("(%p, %lu, %lu, %lu)\n", memblock
, size
, alignment
, offset
);
569 return _aligned_offset_malloc(size
, alignment
, offset
);
571 /* alignment must be a power of 2 */
572 if ((alignment
& (alignment
- 1)) != 0)
574 *MSVCRT__errno() = MSVCRT_EINVAL
;
578 /* offset must be less than size */
581 *MSVCRT__errno() = MSVCRT_EINVAL
;
587 _aligned_free(memblock
);
591 /* don't align to less than void pointer size */
592 if (alignment
< sizeof(void *))
593 alignment
= sizeof(void *);
595 /* make sure alignment and offset didn't change */
596 saved
= SAVED_PTR(memblock
);
597 if (memblock
!= ALIGN_PTR(*saved
, alignment
, offset
))
599 *MSVCRT__errno() = MSVCRT_EINVAL
;
603 old_padding
= (char *)memblock
- (char *)*saved
;
605 /* Get previous size of block */
606 old_size
= _msize(*saved
);
609 /* It seems this function was called with an invalid pointer. Bail out. */
613 /* Adjust old_size to get amount of actual data in old block. */
614 if (old_size
< old_padding
)
616 /* Shouldn't happen. Something's weird, so bail out. */
619 old_size
-= old_padding
;
621 temp
= MSVCRT_realloc(*saved
, size
+ alignment
+ sizeof(void *));
626 /* adjust pointer for proper alignment and offset */
627 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
629 /* Save the real allocation address below returned address */
630 /* so it can be found later to free. */
631 saved
= SAVED_PTR(memblock
);
633 new_padding
= (char *)memblock
- (char *)temp
;
636 Memory layout of old block is as follows:
637 +-------+---------------------+-+--------------------------+-----------+
638 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
639 +-------+---------------------+-+--------------------------+-----------+
642 *saved saved memblock
644 Memory layout of new block is as follows:
645 +-------+-----------------------------+-+----------------------+-------+
646 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
647 +-------+-----------------------------+-+----------------------+-------+
652 However, in the new block, actual data is still written as follows
653 (because it was copied by MSVCRT_realloc):
654 +-------+---------------------+--------------------------------+-------+
655 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
656 +-------+---------------------+--------------------------------+-------+
661 Therefore, min(old_size,size) bytes of actual data have to be moved
662 from the offset they were at in the old block (temp + old_padding),
663 to the offset they have to be in the new block (temp + new_padding == memblock).
665 if (new_padding
!= old_padding
)
666 memmove((char *)memblock
, (char *)temp
+ old_padding
, (old_size
< size
) ? old_size
: size
);
673 /*********************************************************************
674 * _aligned_realloc (MSVCRT.@)
676 void * CDECL
_aligned_realloc(void *memblock
, MSVCRT_size_t size
, MSVCRT_size_t alignment
)
678 TRACE("(%p, %lu, %lu)\n", memblock
, size
, alignment
);
679 return _aligned_offset_realloc(memblock
, size
, alignment
, 0);
682 /*********************************************************************
683 * memmove_s (MSVCRT.@)
685 int CDECL
MSVCRT_memmove_s(void *dest
, MSVCRT_size_t numberOfElements
, const void *src
, MSVCRT_size_t count
)
687 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
692 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
693 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
694 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
696 memmove(dest
, src
, count
);
700 /*********************************************************************
701 * wmemmove_s (MSVCR100.@)
703 int CDECL
wmemmove_s(MSVCRT_wchar_t
*dest
, MSVCRT_size_t numberOfElements
,
704 const MSVCRT_wchar_t
*src
, MSVCRT_size_t count
)
706 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
711 /* Native does not seem to conform to 6.7.1.2.3 in
712 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
713 * in that it does not zero the output buffer on constraint violation.
715 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
716 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
717 if (!MSVCRT_CHECK_PMT_ERR(count
<= numberOfElements
, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
719 memmove(dest
, src
, sizeof(MSVCRT_wchar_t
)*count
);
723 /*********************************************************************
724 * memcpy_s (MSVCRT.@)
726 int CDECL
MSVCRT_memcpy_s(void *dest
, MSVCRT_size_t numberOfElements
, const void *src
, MSVCRT_size_t count
)
728 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
733 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
734 if (!MSVCRT_CHECK_PMT(src
!= NULL
))
736 memset(dest
, 0, numberOfElements
);
737 return MSVCRT_EINVAL
;
739 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, MSVCRT_ERANGE
))
741 memset(dest
, 0, numberOfElements
);
742 return MSVCRT_ERANGE
;
745 memcpy(dest
, src
, count
);
749 /*********************************************************************
750 * wmemcpy_s (MSVCR100.@)
752 int CDECL
wmemcpy_s(MSVCRT_wchar_t
*dest
, MSVCRT_size_t numberOfElements
,
753 const MSVCRT_wchar_t
*src
, MSVCRT_size_t count
)
755 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
760 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
762 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) {
763 memset(dest
, 0, numberOfElements
*sizeof(MSVCRT_wchar_t
));
764 return MSVCRT_EINVAL
;
766 if (!MSVCRT_CHECK_PMT_ERR(count
<= numberOfElements
, MSVCRT_ERANGE
)) {
767 memset(dest
, 0, numberOfElements
*sizeof(MSVCRT_wchar_t
));
768 return MSVCRT_ERANGE
;
771 memcpy(dest
, src
, sizeof(MSVCRT_wchar_t
)*count
);
775 /*********************************************************************
776 * strncpy_s (MSVCRT.@)
778 int CDECL
MSVCRT_strncpy_s(char *dest
, MSVCRT_size_t numberOfElements
,
779 const char *src
, MSVCRT_size_t count
)
781 MSVCRT_size_t i
, end
;
783 TRACE("(%p %lu %s %lu)\n", dest
, numberOfElements
, debugstr_a(src
), count
);
786 if(dest
&& numberOfElements
)
791 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
792 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
793 if (!MSVCRT_CHECK_PMT(numberOfElements
!= 0)) return MSVCRT_EINVAL
;
795 if(count
!=MSVCRT__TRUNCATE
&& count
<numberOfElements
)
798 end
= numberOfElements
-1;
800 for(i
=0; i
<end
&& src
[i
]; i
++)
803 if(!src
[i
] || end
==count
|| count
==MSVCRT__TRUNCATE
) {
808 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL
);
810 return MSVCRT_EINVAL
;
813 BOOL
msvcrt_init_heap(void)
815 heap
= HeapCreate(0, 0, 0);
819 void msvcrt_destroy_heap(void)
823 HeapDestroy(sb_heap
);