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
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
32 #define LOCK_HEAP _lock( _HEAP_LOCK )
33 #define UNLOCK_HEAP _unlock( _HEAP_LOCK )
36 #define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
37 ~(sizeof(void *) - 1)))
38 #define ALIGN_PTR(ptr, alignment, offset) ((void *) \
39 ((((DWORD_PTR)((char *)ptr + alignment + sizeof(void *) + offset)) & \
40 ~(alignment - 1)) - offset))
42 #define SB_HEAP_ALIGN 16
44 static HANDLE heap
, sb_heap
;
46 typedef int (CDECL
*MSVCRT_new_handler_func
)(size_t size
);
48 static MSVCRT_new_handler_func MSVCRT_new_handler
;
49 static int MSVCRT_new_mode
;
51 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
52 static unsigned int MSVCRT_amblksiz
= 16;
53 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
54 static size_t MSVCRT_sbh_threshold
= 0;
56 static void* msvcrt_heap_alloc(DWORD flags
, size_t size
)
58 if(size
< MSVCRT_sbh_threshold
)
60 void *memblock
, *temp
, **saved
;
62 temp
= HeapAlloc(sb_heap
, flags
, size
+sizeof(void*)+SB_HEAP_ALIGN
);
63 if(!temp
) return NULL
;
65 memblock
= ALIGN_PTR(temp
, SB_HEAP_ALIGN
, 0);
66 saved
= SAVED_PTR(memblock
);
71 return HeapAlloc(heap
, flags
, size
);
74 static void* msvcrt_heap_realloc(DWORD flags
, void *ptr
, size_t size
)
76 if(sb_heap
&& ptr
&& !HeapValidate(heap
, 0, ptr
))
78 /* TODO: move data to normal heap if it exceeds sbh_threshold limit */
79 void *memblock
, *temp
, **saved
;
80 size_t old_padding
, new_padding
, old_size
;
82 saved
= SAVED_PTR(ptr
);
83 old_padding
= (char*)ptr
- (char*)*saved
;
84 old_size
= HeapSize(sb_heap
, 0, *saved
);
87 old_size
-= old_padding
;
89 temp
= HeapReAlloc(sb_heap
, flags
, *saved
, size
+sizeof(void*)+SB_HEAP_ALIGN
);
90 if(!temp
) return NULL
;
92 memblock
= ALIGN_PTR(temp
, SB_HEAP_ALIGN
, 0);
93 saved
= SAVED_PTR(memblock
);
94 new_padding
= (char*)memblock
- (char*)temp
;
96 if(new_padding
!= old_padding
)
97 memmove(memblock
, (char*)temp
+old_padding
, old_size
>size
? size
: old_size
);
103 return HeapReAlloc(heap
, flags
, ptr
, size
);
106 static BOOL
msvcrt_heap_free(void *ptr
)
108 if(sb_heap
&& ptr
&& !HeapValidate(heap
, 0, ptr
))
110 void **saved
= SAVED_PTR(ptr
);
111 return HeapFree(sb_heap
, 0, *saved
);
114 return HeapFree(heap
, 0, ptr
);
117 static size_t msvcrt_heap_size(void *ptr
)
119 if(sb_heap
&& ptr
&& !HeapValidate(heap
, 0, ptr
))
121 void **saved
= SAVED_PTR(ptr
);
122 return HeapSize(sb_heap
, 0, *saved
);
125 return HeapSize(heap
, 0, ptr
);
128 /*********************************************************************
129 * _callnewh (MSVCRT.@)
131 int CDECL
_callnewh(size_t size
)
134 MSVCRT_new_handler_func handler
= MSVCRT_new_handler
;
136 ret
= (*handler
)(size
) ? 1 : 0;
140 /*********************************************************************
141 * ??2@YAPAXI@Z (MSVCRT.@)
143 void* CDECL DECLSPEC_HOTPATCH
operator_new(size_t size
)
149 retval
= msvcrt_heap_alloc(0, size
);
152 TRACE("(%Iu) returning %p\n", size
, retval
);
155 } while(_callnewh(size
));
157 TRACE("(%Iu) out of memory\n", size
);
159 throw_exception(EXCEPTION_BAD_ALLOC
, 0, "bad allocation");
165 /*********************************************************************
166 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
168 void* CDECL
operator_new_dbg(size_t size
, int type
, const char *file
, int line
)
170 return operator_new( size
);
174 /*********************************************************************
175 * ??3@YAXPAX@Z (MSVCRT.@)
177 void CDECL DECLSPEC_HOTPATCH
operator_delete(void *mem
)
179 TRACE("(%p)\n", mem
);
180 msvcrt_heap_free(mem
);
184 /*********************************************************************
185 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
187 MSVCRT_new_handler_func CDECL
_query_new_handler(void)
189 return MSVCRT_new_handler
;
193 /*********************************************************************
194 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
196 int CDECL
_query_new_mode(void)
198 return MSVCRT_new_mode
;
201 /*********************************************************************
202 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
204 MSVCRT_new_handler_func CDECL
_set_new_handler(MSVCRT_new_handler_func func
)
206 MSVCRT_new_handler_func old_handler
;
208 old_handler
= MSVCRT_new_handler
;
209 MSVCRT_new_handler
= func
;
214 /*********************************************************************
215 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
217 MSVCRT_new_handler_func CDECL
set_new_handler(void *func
)
219 TRACE("(%p)\n",func
);
220 _set_new_handler(NULL
);
224 /*********************************************************************
225 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
227 int CDECL
_set_new_mode(int mode
)
229 if(!MSVCRT_CHECK_PMT(mode
== 0 || mode
== 1)) return -1;
230 return InterlockedExchange(&MSVCRT_new_mode
, mode
);
233 /*********************************************************************
236 void* CDECL
_expand(void* mem
, size_t size
)
238 return msvcrt_heap_realloc(HEAP_REALLOC_IN_PLACE_ONLY
, mem
, size
);
241 /*********************************************************************
242 * _heapchk (MSVCRT.@)
244 int CDECL
_heapchk(void)
246 if (!HeapValidate(heap
, 0, NULL
) ||
247 (sb_heap
&& !HeapValidate(sb_heap
, 0, NULL
)))
249 msvcrt_set_errno(GetLastError());
255 /*********************************************************************
256 * _heapmin (MSVCRT.@)
258 int CDECL
_heapmin(void)
260 if (!HeapCompact( heap
, 0 ) ||
261 (sb_heap
&& !HeapCompact( sb_heap
, 0 )))
263 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
264 msvcrt_set_errno(GetLastError());
270 /*********************************************************************
271 * _heapwalk (MSVCRT.@)
273 int CDECL
_heapwalk(_HEAPINFO
*next
)
275 PROCESS_HEAP_ENTRY phe
;
278 FIXME("small blocks heap not supported\n");
281 phe
.lpData
= next
->_pentry
;
282 phe
.cbData
= next
->_size
;
283 phe
.wFlags
= next
->_useflag
== _USEDENTRY
? PROCESS_HEAP_ENTRY_BUSY
: 0;
285 if (phe
.lpData
&& phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
&&
286 !HeapValidate( heap
, 0, phe
.lpData
))
289 msvcrt_set_errno(GetLastError());
295 if (!HeapWalk( heap
, &phe
))
298 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
300 msvcrt_set_errno(GetLastError());
302 return _HEAPBADBEGIN
;
305 } while (phe
.wFlags
& (PROCESS_HEAP_REGION
|PROCESS_HEAP_UNCOMMITTED_RANGE
));
308 next
->_pentry
= phe
.lpData
;
309 next
->_size
= phe
.cbData
;
310 next
->_useflag
= phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
? _USEDENTRY
: _FREEENTRY
;
314 /*********************************************************************
315 * _heapset (MSVCRT.@)
317 int CDECL
_heapset(unsigned int value
)
322 memset( &heap
, 0, sizeof(heap
) );
324 while ((retval
= _heapwalk(&heap
)) == _HEAPOK
)
326 if (heap
._useflag
== _FREEENTRY
)
327 memset(heap
._pentry
, value
, heap
._size
);
330 return retval
== _HEAPEND
? _HEAPOK
: retval
;
333 /*********************************************************************
334 * _heapadd (MSVCRT.@)
336 int CDECL
_heapadd(void* mem
, size_t size
)
338 TRACE("(%p,%Iu) unsupported in Win32\n", mem
,size
);
343 /*********************************************************************
344 * _get_heap_handle (MSVCRT.@)
346 intptr_t CDECL
_get_heap_handle(void)
348 return (intptr_t)heap
;
351 /*********************************************************************
354 size_t CDECL
_msize(void* mem
)
356 size_t size
= msvcrt_heap_size(mem
);
357 if (size
== ~(size_t)0)
359 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
360 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
366 /*********************************************************************
367 * _aligned_msize (MSVCR80.@)
369 size_t CDECL
_aligned_msize(void *p
, size_t alignment
, size_t offset
)
373 if(!MSVCRT_CHECK_PMT(p
)) return -1;
375 if(alignment
< sizeof(void*))
376 alignment
= sizeof(void*);
378 alloc_ptr
= SAVED_PTR(p
);
379 return _msize(*alloc_ptr
)-alignment
-sizeof(void*);
383 /*********************************************************************
386 void* CDECL DECLSPEC_HOTPATCH
calloc(size_t count
, size_t size
)
388 size_t bytes
= count
*size
;
390 if (size
&& bytes
/ size
!= count
)
396 return msvcrt_heap_alloc(HEAP_ZERO_MEMORY
, bytes
);
400 /*********************************************************************
401 * _calloc_base (UCRTBASE.@)
403 void* CDECL
_calloc_base(size_t count
, size_t size
)
405 return calloc(count
, size
);
409 /*********************************************************************
412 void CDECL DECLSPEC_HOTPATCH
free(void* ptr
)
414 msvcrt_heap_free(ptr
);
418 /*********************************************************************
419 * _free_base (UCRTBASE.@)
421 void CDECL
_free_base(void* ptr
)
423 msvcrt_heap_free(ptr
);
427 /*********************************************************************
430 void* CDECL
malloc(size_t size
)
436 ret
= msvcrt_heap_alloc(0, size
);
437 if (ret
|| !MSVCRT_new_mode
)
439 } while(_callnewh(size
));
447 /*********************************************************************
448 * _malloc_base (UCRTBASE.@)
450 void* CDECL
_malloc_base(size_t size
)
456 /*********************************************************************
459 void* CDECL DECLSPEC_HOTPATCH
realloc(void* ptr
, size_t size
)
461 if (!ptr
) return malloc(size
);
462 if (size
) return msvcrt_heap_realloc(0, ptr
, size
);
468 /*********************************************************************
469 * _realloc_base (UCRTBASE.@)
471 void* CDECL
_realloc_base(void* ptr
, size_t size
)
473 return realloc(ptr
, size
);
478 /*********************************************************************
479 * _recalloc (MSVCR80.@)
481 void* CDECL
_recalloc(void *mem
, size_t num
, size_t size
)
487 return calloc(num
, size
);
490 old_size
= _msize(mem
);
492 ret
= realloc(mem
, size
);
499 memset((BYTE
*)ret
+old_size
, 0, size
-old_size
);
504 /*********************************************************************
505 * __p__amblksiz (MSVCRT.@)
507 unsigned int* CDECL
__p__amblksiz(void)
509 return &MSVCRT_amblksiz
;
512 /*********************************************************************
513 * _get_sbh_threshold (MSVCRT.@)
515 size_t CDECL
_get_sbh_threshold(void)
517 return MSVCRT_sbh_threshold
;
520 /*********************************************************************
521 * _set_sbh_threshold (MSVCRT.@)
523 int CDECL
_set_sbh_threshold(size_t threshold
)
533 sb_heap
= HeapCreate(0, 0, 0);
538 MSVCRT_sbh_threshold
= (threshold
+0xf) & ~0xf;
543 /*********************************************************************
544 * _aligned_free (MSVCRT.@)
546 void CDECL
_aligned_free(void *memblock
)
548 TRACE("(%p)\n", memblock
);
552 void **saved
= SAVED_PTR(memblock
);
557 /*********************************************************************
558 * _aligned_offset_malloc (MSVCRT.@)
560 void * CDECL
_aligned_offset_malloc(size_t size
, size_t alignment
, size_t offset
)
562 void *memblock
, *temp
, **saved
;
563 TRACE("(%Iu, %Iu, %Iu)\n", size
, alignment
, offset
);
565 /* alignment must be a power of 2 */
566 if ((alignment
& (alignment
- 1)) != 0)
572 /* offset must be less than size */
573 if (offset
&& offset
>= size
)
579 /* don't align to less than void pointer size */
580 if (alignment
< sizeof(void *))
581 alignment
= sizeof(void *);
583 /* allocate enough space for void pointer and alignment */
584 temp
= malloc(size
+ alignment
+ sizeof(void *));
589 /* adjust pointer for proper alignment and offset */
590 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
592 /* Save the real allocation address below returned address */
593 /* so it can be found later to free. */
594 saved
= SAVED_PTR(memblock
);
600 /*********************************************************************
601 * _aligned_malloc (MSVCRT.@)
603 void * CDECL
_aligned_malloc(size_t size
, size_t alignment
)
605 TRACE("(%Iu, %Iu)\n", size
, alignment
);
606 return _aligned_offset_malloc(size
, alignment
, 0);
609 /*********************************************************************
610 * _aligned_offset_realloc (MSVCRT.@)
612 void * CDECL
_aligned_offset_realloc(void *memblock
, size_t size
,
613 size_t alignment
, size_t offset
)
615 void * temp
, **saved
;
616 size_t old_padding
, new_padding
, old_size
;
617 TRACE("(%p, %Iu, %Iu, %Iu)\n", memblock
, size
, alignment
, offset
);
620 return _aligned_offset_malloc(size
, alignment
, offset
);
622 /* alignment must be a power of 2 */
623 if ((alignment
& (alignment
- 1)) != 0)
629 /* offset must be less than size */
638 _aligned_free(memblock
);
642 /* don't align to less than void pointer size */
643 if (alignment
< sizeof(void *))
644 alignment
= sizeof(void *);
646 /* make sure alignment and offset didn't change */
647 saved
= SAVED_PTR(memblock
);
648 if (memblock
!= ALIGN_PTR(*saved
, alignment
, offset
))
654 old_padding
= (char *)memblock
- (char *)*saved
;
656 /* Get previous size of block */
657 old_size
= _msize(*saved
);
660 /* It seems this function was called with an invalid pointer. Bail out. */
664 /* Adjust old_size to get amount of actual data in old block. */
665 if (old_size
< old_padding
)
667 /* Shouldn't happen. Something's weird, so bail out. */
670 old_size
-= old_padding
;
672 temp
= realloc(*saved
, size
+ alignment
+ sizeof(void *));
677 /* adjust pointer for proper alignment and offset */
678 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
680 /* Save the real allocation address below returned address */
681 /* so it can be found later to free. */
682 saved
= SAVED_PTR(memblock
);
684 new_padding
= (char *)memblock
- (char *)temp
;
687 Memory layout of old block is as follows:
688 +-------+---------------------+-+--------------------------+-----------+
689 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
690 +-------+---------------------+-+--------------------------+-----------+
693 *saved saved memblock
695 Memory layout of new block is as follows:
696 +-------+-----------------------------+-+----------------------+-------+
697 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
698 +-------+-----------------------------+-+----------------------+-------+
703 However, in the new block, actual data is still written as follows
704 (because it was copied by realloc):
705 +-------+---------------------+--------------------------------+-------+
706 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
707 +-------+---------------------+--------------------------------+-------+
712 Therefore, min(old_size,size) bytes of actual data have to be moved
713 from the offset they were at in the old block (temp + old_padding),
714 to the offset they have to be in the new block (temp + new_padding == memblock).
716 if (new_padding
!= old_padding
)
717 memmove((char *)memblock
, (char *)temp
+ old_padding
, (old_size
< size
) ? old_size
: size
);
724 /*********************************************************************
725 * _aligned_realloc (MSVCRT.@)
727 void * CDECL
_aligned_realloc(void *memblock
, size_t size
, size_t alignment
)
729 TRACE("(%p, %Iu, %Iu)\n", memblock
, size
, alignment
);
730 return _aligned_offset_realloc(memblock
, size
, alignment
, 0);
733 /*********************************************************************
734 * memmove_s (MSVCRT.@)
736 int CDECL
memmove_s(void *dest
, size_t numberOfElements
, const void *src
, size_t count
)
738 TRACE("(%p %Iu %p %Iu)\n", dest
, numberOfElements
, src
, count
);
743 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return EINVAL
;
744 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return EINVAL
;
745 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, ERANGE
)) return ERANGE
;
747 memmove(dest
, src
, count
);
752 /*********************************************************************
753 * wmemmove_s (MSVCR100.@)
755 int CDECL
wmemmove_s(wchar_t *dest
, size_t numberOfElements
,
756 const wchar_t *src
, size_t count
)
758 TRACE("(%p %Iu %p %Iu)\n", dest
, numberOfElements
, src
, count
);
763 /* Native does not seem to conform to 6.7.1.2.3 in
764 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
765 * in that it does not zero the output buffer on constraint violation.
767 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return EINVAL
;
768 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return EINVAL
;
769 if (!MSVCRT_CHECK_PMT_ERR(count
<= numberOfElements
, ERANGE
)) return ERANGE
;
771 memmove(dest
, src
, sizeof(wchar_t)*count
);
776 /*********************************************************************
777 * memcpy_s (MSVCRT.@)
779 int CDECL
memcpy_s(void *dest
, size_t numberOfElements
, const void *src
, size_t count
)
781 TRACE("(%p %Iu %p %Iu)\n", dest
, numberOfElements
, src
, count
);
786 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return EINVAL
;
787 if (!MSVCRT_CHECK_PMT(src
!= NULL
))
789 memset(dest
, 0, numberOfElements
);
792 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, ERANGE
))
794 memset(dest
, 0, numberOfElements
);
798 memmove(dest
, src
, count
);
803 /*********************************************************************
804 * wmemcpy_s (MSVCR100.@)
806 int CDECL
wmemcpy_s(wchar_t *dest
, size_t numberOfElements
,
807 const wchar_t *src
, size_t count
)
809 TRACE("(%p %Iu %p %Iu)\n", dest
, numberOfElements
, src
, count
);
814 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return EINVAL
;
816 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) {
817 memset(dest
, 0, numberOfElements
*sizeof(wchar_t));
820 if (!MSVCRT_CHECK_PMT_ERR(count
<= numberOfElements
, ERANGE
)) {
821 memset(dest
, 0, numberOfElements
*sizeof(wchar_t));
825 memmove(dest
, src
, sizeof(wchar_t)*count
);
830 /*********************************************************************
831 * strncpy_s (MSVCRT.@)
833 int CDECL
strncpy_s(char *dest
, size_t numberOfElements
,
834 const char *src
, size_t count
)
838 TRACE("(%p %Iu %s %Iu)\n", dest
, numberOfElements
, debugstr_a(src
), count
);
841 if(dest
&& numberOfElements
)
846 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return EINVAL
;
847 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return EINVAL
;
848 if (!MSVCRT_CHECK_PMT(numberOfElements
!= 0)) return EINVAL
;
850 if(count
!=_TRUNCATE
&& count
<numberOfElements
)
853 end
= numberOfElements
-1;
855 for(i
=0; i
<end
&& src
[i
]; i
++)
858 if(!src
[i
] || end
==count
|| count
==_TRUNCATE
) {
863 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", EINVAL
);
868 BOOL
msvcrt_init_heap(void)
870 heap
= HeapCreate(0, 0, 0);
874 void msvcrt_destroy_heap(void)
878 HeapDestroy(sb_heap
);