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))
42 typedef int (CDECL
*MSVCRT_new_handler_func
)(MSVCRT_size_t size
);
44 static MSVCRT_new_handler_func MSVCRT_new_handler
;
45 static int MSVCRT_new_mode
;
47 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
48 static unsigned int MSVCRT_amblksiz
= 16;
49 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
50 static MSVCRT_size_t MSVCRT_sbh_threshold
= 0;
52 /*********************************************************************
53 * ??2@YAPAXI@Z (MSVCRT.@)
55 void* CDECL
MSVCRT_operator_new(MSVCRT_size_t size
)
62 retval
= HeapAlloc(GetProcessHeap(), 0, size
);
65 TRACE("(%ld) returning %p\n", size
, retval
);
70 if(MSVCRT_new_handler
)
71 freed
= (*MSVCRT_new_handler
)(size
);
77 TRACE("(%ld) out of memory\n", size
);
82 /*********************************************************************
83 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
85 void* CDECL
MSVCRT_operator_new_dbg(MSVCRT_size_t size
, int type
, const char *file
, int line
)
87 return MSVCRT_operator_new( size
);
91 /*********************************************************************
92 * ??3@YAXPAX@Z (MSVCRT.@)
94 void CDECL
MSVCRT_operator_delete(void *mem
)
97 HeapFree(GetProcessHeap(), 0, mem
);
101 /*********************************************************************
102 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
104 MSVCRT_new_handler_func CDECL
MSVCRT__query_new_handler(void)
106 return MSVCRT_new_handler
;
110 /*********************************************************************
111 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
113 int CDECL
MSVCRT__query_new_mode(void)
115 return MSVCRT_new_mode
;
118 /*********************************************************************
119 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
121 MSVCRT_new_handler_func CDECL
MSVCRT__set_new_handler(MSVCRT_new_handler_func func
)
123 MSVCRT_new_handler_func old_handler
;
125 old_handler
= MSVCRT_new_handler
;
126 MSVCRT_new_handler
= func
;
131 /*********************************************************************
132 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
134 MSVCRT_new_handler_func CDECL
MSVCRT_set_new_handler(void *func
)
136 TRACE("(%p)\n",func
);
137 MSVCRT__set_new_handler(NULL
);
141 /*********************************************************************
142 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
144 int CDECL
MSVCRT__set_new_mode(int mode
)
148 old_mode
= MSVCRT_new_mode
;
149 MSVCRT_new_mode
= mode
;
154 /*********************************************************************
155 * _callnewh (MSVCRT.@)
157 int CDECL
_callnewh(MSVCRT_size_t size
)
159 if(MSVCRT_new_handler
)
160 (*MSVCRT_new_handler
)(size
);
164 /*********************************************************************
167 void* CDECL
_expand(void* mem
, MSVCRT_size_t size
)
169 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, mem
, size
);
172 /*********************************************************************
173 * _heapchk (MSVCRT.@)
175 int CDECL
_heapchk(void)
177 if (!HeapValidate( GetProcessHeap(), 0, NULL
))
179 msvcrt_set_errno(GetLastError());
180 return MSVCRT__HEAPBADNODE
;
182 return MSVCRT__HEAPOK
;
185 /*********************************************************************
186 * _heapmin (MSVCRT.@)
188 int CDECL
_heapmin(void)
190 if (!HeapCompact( GetProcessHeap(), 0 ))
192 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
193 msvcrt_set_errno(GetLastError());
199 /*********************************************************************
200 * _heapwalk (MSVCRT.@)
202 int CDECL
_heapwalk(struct MSVCRT__heapinfo
* next
)
204 PROCESS_HEAP_ENTRY phe
;
207 phe
.lpData
= next
->_pentry
;
208 phe
.cbData
= next
->_size
;
209 phe
.wFlags
= next
->_useflag
== MSVCRT__USEDENTRY
? PROCESS_HEAP_ENTRY_BUSY
: 0;
211 if (phe
.lpData
&& phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
&&
212 !HeapValidate( GetProcessHeap(), 0, phe
.lpData
))
215 msvcrt_set_errno(GetLastError());
216 return MSVCRT__HEAPBADNODE
;
221 if (!HeapWalk( GetProcessHeap(), &phe
))
224 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
225 return MSVCRT__HEAPEND
;
226 msvcrt_set_errno(GetLastError());
228 return MSVCRT__HEAPBADBEGIN
;
229 return MSVCRT__HEAPBADNODE
;
231 } while (phe
.wFlags
& (PROCESS_HEAP_REGION
|PROCESS_HEAP_UNCOMMITTED_RANGE
));
234 next
->_pentry
= phe
.lpData
;
235 next
->_size
= phe
.cbData
;
236 next
->_useflag
= phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
? MSVCRT__USEDENTRY
: MSVCRT__FREEENTRY
;
237 return MSVCRT__HEAPOK
;
240 /*********************************************************************
241 * _heapset (MSVCRT.@)
243 int CDECL
_heapset(unsigned int value
)
246 struct MSVCRT__heapinfo heap
;
248 memset( &heap
, 0, sizeof(heap
) );
250 while ((retval
= _heapwalk(&heap
)) == MSVCRT__HEAPOK
)
252 if (heap
._useflag
== MSVCRT__FREEENTRY
)
253 memset(heap
._pentry
, value
, heap
._size
);
256 return retval
== MSVCRT__HEAPEND
? MSVCRT__HEAPOK
: retval
;
259 /*********************************************************************
260 * _heapadd (MSVCRT.@)
262 int CDECL
_heapadd(void* mem
, MSVCRT_size_t size
)
264 TRACE("(%p,%ld) unsupported in Win32\n", mem
,size
);
265 *MSVCRT__errno() = MSVCRT_ENOSYS
;
269 /*********************************************************************
270 * _heapadd (MSVCRT.@)
272 MSVCRT_intptr_t CDECL
_get_heap_handle(void)
274 return (MSVCRT_intptr_t
)GetProcessHeap();
277 /*********************************************************************
280 MSVCRT_size_t CDECL
_msize(void* mem
)
282 MSVCRT_size_t size
= HeapSize(GetProcessHeap(),0,mem
);
283 if (size
== ~(MSVCRT_size_t
)0)
285 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
286 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
291 /*********************************************************************
294 void* CDECL
MSVCRT_calloc(MSVCRT_size_t size
, MSVCRT_size_t count
)
296 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
299 /*********************************************************************
302 void CDECL
MSVCRT_free(void* ptr
)
304 HeapFree(GetProcessHeap(),0,ptr
);
307 /*********************************************************************
310 void* CDECL
MSVCRT_malloc(MSVCRT_size_t size
)
312 void *ret
= HeapAlloc(GetProcessHeap(),0,size
);
314 *MSVCRT__errno() = MSVCRT_ENOMEM
;
318 /*********************************************************************
321 void* CDECL
MSVCRT_realloc(void* ptr
, MSVCRT_size_t size
)
323 if (!ptr
) return MSVCRT_malloc(size
);
324 if (size
) return HeapReAlloc(GetProcessHeap(), 0, ptr
, size
);
329 /*********************************************************************
330 * __p__amblksiz (MSVCRT.@)
332 unsigned int* CDECL
__p__amblksiz(void)
334 return &MSVCRT_amblksiz
;
337 /*********************************************************************
338 * _get_sbh_threshold (MSVCRT.@)
340 MSVCRT_size_t CDECL
_get_sbh_threshold(void)
342 return MSVCRT_sbh_threshold
;
345 /*********************************************************************
346 * _set_sbh_threshold (MSVCRT.@)
348 int CDECL
_set_sbh_threshold(MSVCRT_size_t threshold
)
353 MSVCRT_sbh_threshold
= threshold
;
357 /*********************************************************************
358 * _aligned_free (MSVCRT.@)
360 void CDECL
_aligned_free(void *memblock
)
362 TRACE("(%p)\n", memblock
);
366 void **saved
= SAVED_PTR(memblock
);
371 /*********************************************************************
372 * _aligned_offset_malloc (MSVCRT.@)
374 void * CDECL
_aligned_offset_malloc(MSVCRT_size_t size
, MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
376 void *memblock
, *temp
, **saved
;
377 TRACE("(%lu, %lu, %lu)\n", size
, alignment
, offset
);
379 /* alignment must be a power of 2 */
380 if ((alignment
& (alignment
- 1)) != 0)
382 *MSVCRT__errno() = MSVCRT_EINVAL
;
386 /* offset must be less than size */
387 if (offset
&& offset
>= size
)
389 *MSVCRT__errno() = MSVCRT_EINVAL
;
393 /* don't align to less than void pointer size */
394 if (alignment
< sizeof(void *))
395 alignment
= sizeof(void *);
397 /* allocate enough space for void pointer and alignment */
398 temp
= MSVCRT_malloc(size
+ alignment
+ sizeof(void *));
403 /* adjust pointer for proper alignment and offset */
404 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
406 /* Save the real allocation address below returned address */
407 /* so it can be found later to free. */
408 saved
= SAVED_PTR(memblock
);
414 /*********************************************************************
415 * _aligned_malloc (MSVCRT.@)
417 void * CDECL
_aligned_malloc(MSVCRT_size_t size
, MSVCRT_size_t alignment
)
419 TRACE("(%lu, %lu)\n", size
, alignment
);
420 return _aligned_offset_malloc(size
, alignment
, 0);
423 /*********************************************************************
424 * _aligned_offset_realloc (MSVCRT.@)
426 void * CDECL
_aligned_offset_realloc(void *memblock
, MSVCRT_size_t size
,
427 MSVCRT_size_t alignment
, MSVCRT_size_t offset
)
429 void * temp
, **saved
;
430 MSVCRT_size_t old_padding
, new_padding
, old_size
;
431 TRACE("(%p, %lu, %lu, %lu)\n", memblock
, size
, alignment
, offset
);
434 return _aligned_offset_malloc(size
, alignment
, offset
);
436 /* alignment must be a power of 2 */
437 if ((alignment
& (alignment
- 1)) != 0)
439 *MSVCRT__errno() = MSVCRT_EINVAL
;
443 /* offset must be less than size */
446 *MSVCRT__errno() = MSVCRT_EINVAL
;
452 _aligned_free(memblock
);
456 /* don't align to less than void pointer size */
457 if (alignment
< sizeof(void *))
458 alignment
= sizeof(void *);
460 /* make sure alignment and offset didn't change */
461 saved
= SAVED_PTR(memblock
);
462 if (memblock
!= ALIGN_PTR(*saved
, alignment
, offset
))
464 *MSVCRT__errno() = MSVCRT_EINVAL
;
468 old_padding
= (char *)memblock
- (char *)*saved
;
470 /* Get previous size of block */
471 old_size
= _msize(*saved
);
474 /* It seems this function was called with an invalid pointer. Bail out. */
478 /* Adjust old_size to get amount of actual data in old block. */
479 if (old_size
< old_padding
)
481 /* Shouldn't happen. Something's weird, so bail out. */
484 old_size
-= old_padding
;
486 temp
= MSVCRT_realloc(*saved
, size
+ alignment
+ sizeof(void *));
491 /* adjust pointer for proper alignment and offset */
492 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
494 /* Save the real allocation address below returned address */
495 /* so it can be found later to free. */
496 saved
= SAVED_PTR(memblock
);
498 new_padding
= (char *)memblock
- (char *)temp
;
501 Memory layout of old block is as follows:
502 +-------+---------------------+-+--------------------------+-----------+
503 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
504 +-------+---------------------+-+--------------------------+-----------+
507 *saved saved memblock
509 Memory layout of new block is as follows:
510 +-------+-----------------------------+-+----------------------+-------+
511 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
512 +-------+-----------------------------+-+----------------------+-------+
517 However, in the new block, actual data is still written as follows
518 (because it was copied by MSVCRT_realloc):
519 +-------+---------------------+--------------------------------+-------+
520 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
521 +-------+---------------------+--------------------------------+-------+
526 Therefore, min(old_size,size) bytes of actual data have to be moved
527 from the offset they were at in the old block (temp + old_padding),
528 to the offset they have to be in the new block (temp + new_padding == memblock).
530 if (new_padding
!= old_padding
)
531 memmove((char *)memblock
, (char *)temp
+ old_padding
, (old_size
< size
) ? old_size
: size
);
538 /*********************************************************************
539 * _aligned_realloc (MSVCRT.@)
541 void * CDECL
_aligned_realloc(void *memblock
, MSVCRT_size_t size
, MSVCRT_size_t alignment
)
543 TRACE("(%p, %lu, %lu)\n", memblock
, size
, alignment
);
544 return _aligned_offset_realloc(memblock
, size
, alignment
, 0);
547 /*********************************************************************
548 * memmove_s (MSVCRT.@)
550 int CDECL
memmove_s(void *dest
, MSVCRT_size_t numberOfElements
, const void *src
, MSVCRT_size_t count
)
552 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
557 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
558 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
559 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
561 memmove(dest
, src
, count
);
565 /*********************************************************************
566 * memcpy_s (MSVCRT.@)
568 int CDECL
memcpy_s(void *dest
, MSVCRT_size_t numberOfElements
, const void *src
, MSVCRT_size_t count
)
570 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
575 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
576 if (!MSVCRT_CHECK_PMT(src
!= NULL
))
578 memset(dest
, 0, numberOfElements
);
579 return MSVCRT_EINVAL
;
581 if (!MSVCRT_CHECK_PMT_ERR( count
<= numberOfElements
, MSVCRT_ERANGE
))
583 memset(dest
, 0, numberOfElements
);
584 return MSVCRT_ERANGE
;
587 memcpy(dest
, src
, count
);
591 /*********************************************************************
592 * strncpy_s (MSVCRT.@)
594 int CDECL
strncpy_s(char *dest
, MSVCRT_size_t numberOfElements
,
595 const char *src
, MSVCRT_size_t count
)
597 MSVCRT_size_t i
, end
;
599 TRACE("(%p %lu %s %lu)\n", dest
, numberOfElements
, debugstr_a(src
), count
);
602 if(dest
&& numberOfElements
)
607 if (!MSVCRT_CHECK_PMT(dest
!= NULL
)) return MSVCRT_EINVAL
;
608 if (!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
609 if (!MSVCRT_CHECK_PMT(numberOfElements
!= 0)) return MSVCRT_EINVAL
;
611 if(count
!=MSVCRT__TRUNCATE
&& count
<numberOfElements
)
614 end
= numberOfElements
-1;
616 for(i
=0; i
<end
&& src
[i
]; i
++)
619 if(!src
[i
] || end
==count
|| count
==MSVCRT__TRUNCATE
) {
624 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL
);
626 return MSVCRT_EINVAL
;