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))
43 typedef int (CDECL
*MSVCRT_new_handler_func
)(MSVCRT_size_t size
);
45 static MSVCRT_new_handler_func MSVCRT_new_handler
;
46 static int MSVCRT_new_mode
;
48 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
49 static unsigned int MSVCRT_amblksiz
= 16;
50 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
51 static MSVCRT_size_t MSVCRT_sbh_threshold
= 0;
53 /*********************************************************************
54 * ??2@YAPAXI@Z (MSVCRT.@)
56 void* CDECL
MSVCRT_operator_new(MSVCRT_size_t size
)
63 retval
= HeapAlloc(heap
, 0, size
);
66 TRACE("(%ld) returning %p\n", size
, retval
);
71 if(MSVCRT_new_handler
)
72 freed
= (*MSVCRT_new_handler
)(size
);
78 TRACE("(%ld) out of memory\n", size
);
83 /*********************************************************************
84 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
86 void* CDECL
MSVCRT_operator_new_dbg(MSVCRT_size_t size
, int type
, const char *file
, int line
)
88 return MSVCRT_operator_new( size
);
92 /*********************************************************************
93 * ??3@YAXPAX@Z (MSVCRT.@)
95 void CDECL
MSVCRT_operator_delete(void *mem
)
98 HeapFree(heap
, 0, mem
);
102 /*********************************************************************
103 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
105 MSVCRT_new_handler_func CDECL
MSVCRT__query_new_handler(void)
107 return MSVCRT_new_handler
;
111 /*********************************************************************
112 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
114 int CDECL
MSVCRT__query_new_mode(void)
116 return MSVCRT_new_mode
;
119 /*********************************************************************
120 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
122 MSVCRT_new_handler_func CDECL
MSVCRT__set_new_handler(MSVCRT_new_handler_func func
)
124 MSVCRT_new_handler_func old_handler
;
126 old_handler
= MSVCRT_new_handler
;
127 MSVCRT_new_handler
= func
;
132 /*********************************************************************
133 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
135 MSVCRT_new_handler_func CDECL
MSVCRT_set_new_handler(void *func
)
137 TRACE("(%p)\n",func
);
138 MSVCRT__set_new_handler(NULL
);
142 /*********************************************************************
143 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
145 int CDECL
MSVCRT__set_new_mode(int mode
)
149 old_mode
= MSVCRT_new_mode
;
150 MSVCRT_new_mode
= mode
;
155 /*********************************************************************
156 * _callnewh (MSVCRT.@)
158 int CDECL
_callnewh(MSVCRT_size_t size
)
160 if(MSVCRT_new_handler
)
161 (*MSVCRT_new_handler
)(size
);
165 /*********************************************************************
168 void* CDECL
_expand(void* mem
, MSVCRT_size_t size
)
170 return HeapReAlloc(heap
, HEAP_REALLOC_IN_PLACE_ONLY
, mem
, size
);
173 /*********************************************************************
174 * _heapchk (MSVCRT.@)
176 int CDECL
_heapchk(void)
178 if (!HeapValidate( heap
, 0, NULL
))
180 msvcrt_set_errno(GetLastError());
181 return MSVCRT__HEAPBADNODE
;
183 return MSVCRT__HEAPOK
;
186 /*********************************************************************
187 * _heapmin (MSVCRT.@)
189 int CDECL
_heapmin(void)
191 if (!HeapCompact( heap
, 0 ))
193 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
194 msvcrt_set_errno(GetLastError());
200 /*********************************************************************
201 * _heapwalk (MSVCRT.@)
203 int CDECL
_heapwalk(struct MSVCRT__heapinfo
* next
)
205 PROCESS_HEAP_ENTRY phe
;
208 phe
.lpData
= next
->_pentry
;
209 phe
.cbData
= next
->_size
;
210 phe
.wFlags
= next
->_useflag
== MSVCRT__USEDENTRY
? PROCESS_HEAP_ENTRY_BUSY
: 0;
212 if (phe
.lpData
&& phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
&&
213 !HeapValidate( heap
, 0, phe
.lpData
))
216 msvcrt_set_errno(GetLastError());
217 return MSVCRT__HEAPBADNODE
;
222 if (!HeapWalk( heap
, &phe
))
225 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
226 return MSVCRT__HEAPEND
;
227 msvcrt_set_errno(GetLastError());
229 return MSVCRT__HEAPBADBEGIN
;
230 return MSVCRT__HEAPBADNODE
;
232 } while (phe
.wFlags
& (PROCESS_HEAP_REGION
|PROCESS_HEAP_UNCOMMITTED_RANGE
));
235 next
->_pentry
= phe
.lpData
;
236 next
->_size
= phe
.cbData
;
237 next
->_useflag
= phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
? MSVCRT__USEDENTRY
: MSVCRT__FREEENTRY
;
238 return MSVCRT__HEAPOK
;
241 /*********************************************************************
242 * _heapset (MSVCRT.@)
244 int CDECL
_heapset(unsigned int value
)
247 struct MSVCRT__heapinfo heap
;
249 memset( &heap
, 0, sizeof(heap
) );
251 while ((retval
= _heapwalk(&heap
)) == MSVCRT__HEAPOK
)
253 if (heap
._useflag
== MSVCRT__FREEENTRY
)
254 memset(heap
._pentry
, value
, heap
._size
);
257 return retval
== MSVCRT__HEAPEND
? MSVCRT__HEAPOK
: retval
;
260 /*********************************************************************
261 * _heapadd (MSVCRT.@)
263 int CDECL
_heapadd(void* mem
, MSVCRT_size_t size
)
265 TRACE("(%p,%ld) unsupported in Win32\n", mem
,size
);
266 *MSVCRT__errno() = MSVCRT_ENOSYS
;
270 /*********************************************************************
271 * _get_heap_handle (MSVCRT.@)
273 MSVCRT_intptr_t CDECL
_get_heap_handle(void)
275 return (MSVCRT_intptr_t
)heap
;
278 /*********************************************************************
281 MSVCRT_size_t CDECL
_msize(void* mem
)
283 MSVCRT_size_t size
= HeapSize(heap
,0,mem
);
284 if (size
== ~(MSVCRT_size_t
)0)
286 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
287 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
292 /*********************************************************************
293 * _aligned_msize (MSVCR100.@)
295 size_t CDECL
_aligned_msize(void *p
, MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
299 if(!MSVCRT_CHECK_PMT(p
)) return -1;
301 if(alignment
< sizeof(void*))
302 alignment
= sizeof(void*);
304 alloc_ptr
= SAVED_PTR(p
);
305 return _msize(*alloc_ptr
)-alignment
-sizeof(void*);
308 /*********************************************************************
311 void* CDECL
MSVCRT_calloc(MSVCRT_size_t size
, MSVCRT_size_t count
)
313 return HeapAlloc( heap
, HEAP_ZERO_MEMORY
, size
* count
);
316 /*********************************************************************
319 void CDECL
MSVCRT_free(void* ptr
)
321 HeapFree(heap
,0,ptr
);
324 /*********************************************************************
327 void* CDECL
MSVCRT_malloc(MSVCRT_size_t size
)
329 void *ret
= HeapAlloc(heap
,0,size
);
331 *MSVCRT__errno() = MSVCRT_ENOMEM
;
335 /*********************************************************************
338 void* CDECL
MSVCRT_realloc(void* ptr
, MSVCRT_size_t size
)
340 if (!ptr
) return MSVCRT_malloc(size
);
341 if (size
) return HeapReAlloc(heap
, 0, ptr
, size
);
346 /*********************************************************************
347 * _recalloc (MSVCR100.@)
349 void* CDECL
_recalloc(void *mem
, MSVCRT_size_t num
, MSVCRT_size_t size
)
351 MSVCRT_size_t old_size
;
355 return MSVCRT_calloc(num
, size
);
358 old_size
= _msize(mem
);
360 ret
= MSVCRT_realloc(mem
, size
);
362 *MSVCRT__errno() = MSVCRT_ENOMEM
;
367 memset((BYTE
*)ret
+old_size
, 0, size
-old_size
);
371 /*********************************************************************
372 * __p__amblksiz (MSVCRT.@)
374 unsigned int* CDECL
__p__amblksiz(void)
376 return &MSVCRT_amblksiz
;
379 /*********************************************************************
380 * _get_sbh_threshold (MSVCRT.@)
382 MSVCRT_size_t CDECL
_get_sbh_threshold(void)
384 return MSVCRT_sbh_threshold
;
387 /*********************************************************************
388 * _set_sbh_threshold (MSVCRT.@)
390 int CDECL
_set_sbh_threshold(MSVCRT_size_t threshold
)
395 MSVCRT_sbh_threshold
= threshold
;
399 /*********************************************************************
400 * _aligned_free (MSVCRT.@)
402 void CDECL
_aligned_free(void *memblock
)
404 TRACE("(%p)\n", memblock
);
408 void **saved
= SAVED_PTR(memblock
);
413 /*********************************************************************
414 * _aligned_offset_malloc (MSVCRT.@)
416 void * CDECL
_aligned_offset_malloc(MSVCRT_size_t size
, MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
418 void *memblock
, *temp
, **saved
;
419 TRACE("(%lu, %lu, %lu)\n", size
, alignment
, offset
);
421 /* alignment must be a power of 2 */
422 if ((alignment
& (alignment
- 1)) != 0)
424 *MSVCRT__errno() = MSVCRT_EINVAL
;
428 /* offset must be less than size */
429 if (offset
&& offset
>= size
)
431 *MSVCRT__errno() = MSVCRT_EINVAL
;
435 /* don't align to less than void pointer size */
436 if (alignment
< sizeof(void *))
437 alignment
= sizeof(void *);
439 /* allocate enough space for void pointer and alignment */
440 temp
= MSVCRT_malloc(size
+ alignment
+ sizeof(void *));
445 /* adjust pointer for proper alignment and offset */
446 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
448 /* Save the real allocation address below returned address */
449 /* so it can be found later to free. */
450 saved
= SAVED_PTR(memblock
);
456 /*********************************************************************
457 * _aligned_malloc (MSVCRT.@)
459 void * CDECL
_aligned_malloc(MSVCRT_size_t size
, MSVCRT_size_t alignment
)
461 TRACE("(%lu, %lu)\n", size
, alignment
);
462 return _aligned_offset_malloc(size
, alignment
, 0);
465 /*********************************************************************
466 * _aligned_offset_realloc (MSVCRT.@)
468 void * CDECL
_aligned_offset_realloc(void *memblock
, MSVCRT_size_t size
,
469 MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
471 void * temp
, **saved
;
472 MSVCRT_size_t old_padding
, new_padding
, old_size
;
473 TRACE("(%p, %lu, %lu, %lu)\n", memblock
, size
, alignment
, offset
);
476 return _aligned_offset_malloc(size
, alignment
, offset
);
478 /* alignment must be a power of 2 */
479 if ((alignment
& (alignment
- 1)) != 0)
481 *MSVCRT__errno() = MSVCRT_EINVAL
;
485 /* offset must be less than size */
488 *MSVCRT__errno() = MSVCRT_EINVAL
;
494 _aligned_free(memblock
);
498 /* don't align to less than void pointer size */
499 if (alignment
< sizeof(void *))
500 alignment
= sizeof(void *);
502 /* make sure alignment and offset didn't change */
503 saved
= SAVED_PTR(memblock
);
504 if (memblock
!= ALIGN_PTR(*saved
, alignment
, offset
))
506 *MSVCRT__errno() = MSVCRT_EINVAL
;
510 old_padding
= (char *)memblock
- (char *)*saved
;
512 /* Get previous size of block */
513 old_size
= _msize(*saved
);
516 /* It seems this function was called with an invalid pointer. Bail out. */
520 /* Adjust old_size to get amount of actual data in old block. */
521 if (old_size
< old_padding
)
523 /* Shouldn't happen. Something's weird, so bail out. */
526 old_size
-= old_padding
;
528 temp
= MSVCRT_realloc(*saved
, size
+ alignment
+ sizeof(void *));
533 /* adjust pointer for proper alignment and offset */
534 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
536 /* Save the real allocation address below returned address */
537 /* so it can be found later to free. */
538 saved
= SAVED_PTR(memblock
);
540 new_padding
= (char *)memblock
- (char *)temp
;
543 Memory layout of old block is as follows:
544 +-------+---------------------+-+--------------------------+-----------+
545 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
546 +-------+---------------------+-+--------------------------+-----------+
549 *saved saved memblock
551 Memory layout of new block is as follows:
552 +-------+-----------------------------+-+----------------------+-------+
553 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
554 +-------+-----------------------------+-+----------------------+-------+
559 However, in the new block, actual data is still written as follows
560 (because it was copied by MSVCRT_realloc):
561 +-------+---------------------+--------------------------------+-------+
562 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
563 +-------+---------------------+--------------------------------+-------+
568 Therefore, min(old_size,size) bytes of actual data have to be moved
569 from the offset they were at in the old block (temp + old_padding),
570 to the offset they have to be in the new block (temp + new_padding == memblock).
572 if (new_padding
!= old_padding
)
573 memmove((char *)memblock
, (char *)temp
+ old_padding
, (old_size
< size
) ? old_size
: size
);
580 /*********************************************************************
581 * _aligned_realloc (MSVCRT.@)
583 void * CDECL
_aligned_realloc(void *memblock
, MSVCRT_size_t size
, MSVCRT_size_t alignment
)
585 TRACE("(%p, %lu, %lu)\n", memblock
, size
, alignment
);
586 return _aligned_offset_realloc(memblock
, size
, alignment
, 0);
589 /*********************************************************************
590 * memmove_s (MSVCRT.@)
592 int CDECL
MSVCRT_memmove_s(void *dest
, MSVCRT_size_t numberOfElements
, const void *src
, MSVCRT_size_t count
)
594 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
599 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
600 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
601 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
603 memmove(dest
, src
, count
);
607 /*********************************************************************
608 * wmemmove_s (MSVCR100.@)
610 int CDECL
wmemmove_s(MSVCRT_wchar_t
*dest
, MSVCRT_size_t numberOfElements
,
611 const MSVCRT_wchar_t
*src
, MSVCRT_size_t count
)
613 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
618 /* Native does not seem to conform to 6.7.1.2.3 in
619 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
620 * in that it does not zero the output buffer on constraint violation.
622 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
623 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
624 if (!MSVCRT_CHECK_PMT_ERR(count
<= numberOfElements
, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
626 memmove(dest
, src
, sizeof(MSVCRT_wchar_t
)*count
);
630 /*********************************************************************
631 * memcpy_s (MSVCRT.@)
633 int CDECL
MSVCRT_memcpy_s(void *dest
, MSVCRT_size_t numberOfElements
, const void *src
, MSVCRT_size_t count
)
635 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
640 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
641 if (!MSVCRT_CHECK_PMT(src
!= NULL
))
643 memset(dest
, 0, numberOfElements
);
644 return MSVCRT_EINVAL
;
646 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, MSVCRT_ERANGE
))
648 memset(dest
, 0, numberOfElements
);
649 return MSVCRT_ERANGE
;
652 memcpy(dest
, src
, count
);
656 /*********************************************************************
657 * wmemcpy_s (MSVCR100.@)
659 int CDECL
wmemcpy_s(MSVCRT_wchar_t
*dest
, MSVCRT_size_t numberOfElements
,
660 const MSVCRT_wchar_t
*src
, MSVCRT_size_t count
)
662 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
667 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
669 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) {
670 memset(dest
, 0, numberOfElements
*sizeof(MSVCRT_wchar_t
));
671 return MSVCRT_EINVAL
;
673 if (!MSVCRT_CHECK_PMT_ERR(count
<= numberOfElements
, MSVCRT_ERANGE
)) {
674 memset(dest
, 0, numberOfElements
*sizeof(MSVCRT_wchar_t
));
675 return MSVCRT_ERANGE
;
678 memcpy(dest
, src
, sizeof(MSVCRT_wchar_t
)*count
);
682 /*********************************************************************
683 * strncpy_s (MSVCRT.@)
685 int CDECL
MSVCRT_strncpy_s(char *dest
, MSVCRT_size_t numberOfElements
,
686 const char *src
, MSVCRT_size_t count
)
688 MSVCRT_size_t i
, end
;
690 TRACE("(%p %lu %s %lu)\n", dest
, numberOfElements
, debugstr_a(src
), count
);
693 if(dest
&& numberOfElements
)
698 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
699 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
700 if (!MSVCRT_CHECK_PMT(numberOfElements
!= 0)) return MSVCRT_EINVAL
;
702 if(count
!=MSVCRT__TRUNCATE
&& count
<numberOfElements
)
705 end
= numberOfElements
-1;
707 for(i
=0; i
<end
&& src
[i
]; i
++)
710 if(!src
[i
] || end
==count
|| count
==MSVCRT__TRUNCATE
) {
715 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL
);
717 return MSVCRT_EINVAL
;
720 BOOL
msvcrt_init_heap(void)
722 heap
= HeapCreate(0, 0, 0);
726 void msvcrt_destroy_heap(void)