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
28 #define LOCK_HEAP _mlock( _HEAP_LOCK )
29 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
32 #define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
33 ~(sizeof(void *) - 1)))
34 #define ALIGN_PTR(ptr, alignment, offset) ((void *) \
35 ((((DWORD_PTR)((char *)ptr + alignment + sizeof(void *) + offset)) & \
36 ~(alignment - 1)) - offset))
39 typedef int (CDECL
*MSVCRT_new_handler_func
)(size_t size
);
41 static MSVCRT_new_handler_func MSVCRT_new_handler
;
42 static int MSVCRT_new_mode
;
44 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
45 static unsigned int MSVCRT_amblksiz
= 16;
46 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
47 static size_t MSVCRT_sbh_threshold
= 0;
49 /*********************************************************************
50 * ??2@YAPAXI@Z (MSVCRT.@)
52 void* CDECL
MSVCRT_operator_new(size_t size
)
59 retval
= HeapAlloc(GetProcessHeap(), 0, size
);
62 TRACE("(%ld) returning %p\n", size
, retval
);
67 if(MSVCRT_new_handler
)
68 freed
= (*MSVCRT_new_handler
)(size
);
74 TRACE("(%ld) out of memory\n", size
);
79 /*********************************************************************
80 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
82 void* CDECL
MSVCRT_operator_new_dbg(size_t size
, int type
, const char *file
, int line
)
84 return MSVCRT_operator_new( size
);
88 /*********************************************************************
89 * ??3@YAXPAX@Z (MSVCRT.@)
91 void CDECL
MSVCRT_operator_delete(void *mem
)
94 HeapFree(GetProcessHeap(), 0, mem
);
98 /*********************************************************************
99 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
101 MSVCRT_new_handler_func CDECL
MSVCRT__query_new_handler(void)
103 return MSVCRT_new_handler
;
107 /*********************************************************************
108 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
110 int CDECL
MSVCRT__query_new_mode(void)
112 return MSVCRT_new_mode
;
115 /*********************************************************************
116 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
118 MSVCRT_new_handler_func CDECL
MSVCRT__set_new_handler(MSVCRT_new_handler_func func
)
120 MSVCRT_new_handler_func old_handler
;
122 old_handler
= MSVCRT_new_handler
;
123 MSVCRT_new_handler
= func
;
128 /*********************************************************************
129 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
131 MSVCRT_new_handler_func CDECL
MSVCRT_set_new_handler(void *func
)
133 TRACE("(%p)\n",func
);
134 MSVCRT__set_new_handler(NULL
);
138 /*********************************************************************
139 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
141 int CDECL
MSVCRT__set_new_mode(int mode
)
145 old_mode
= MSVCRT_new_mode
;
146 MSVCRT_new_mode
= mode
;
151 /*********************************************************************
152 * _callnewh (MSVCRT.@)
154 int CDECL
_callnewh(size_t size
)
156 if(MSVCRT_new_handler
)
157 (*MSVCRT_new_handler
)(size
);
161 /*********************************************************************
164 void* CDECL
_expand(void* mem
, size_t size
)
166 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, mem
, size
);
169 /*********************************************************************
170 * _heapchk (MSVCRT.@)
172 int CDECL
_heapchk(void)
174 if (!HeapValidate( GetProcessHeap(), 0, NULL
))
176 _dosmaperr(GetLastError());
182 /*********************************************************************
183 * _heapmin (MSVCRT.@)
185 int CDECL
_heapmin(void)
187 if (!HeapCompact( GetProcessHeap(), 0 ))
189 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
190 _dosmaperr(GetLastError());
196 /*********************************************************************
197 * _heapwalk (MSVCRT.@)
199 int CDECL
_heapwalk(_HEAPINFO
* next
)
201 PROCESS_HEAP_ENTRY phe
;
204 phe
.lpData
= next
->_pentry
;
205 phe
.cbData
= (DWORD
)next
->_size
;
206 phe
.wFlags
= next
->_useflag
== _USEDENTRY
? PROCESS_HEAP_ENTRY_BUSY
: 0;
208 if (phe
.lpData
&& phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
&&
209 !HeapValidate( GetProcessHeap(), 0, phe
.lpData
))
212 _dosmaperr(GetLastError());
218 if (!HeapWalk( GetProcessHeap(), &phe
))
221 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
223 _dosmaperr(GetLastError());
225 return _HEAPBADBEGIN
;
228 } while (phe
.wFlags
& (PROCESS_HEAP_REGION
|PROCESS_HEAP_UNCOMMITTED_RANGE
));
231 next
->_pentry
= phe
.lpData
;
232 next
->_size
= phe
.cbData
;
233 next
->_useflag
= phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
? _USEDENTRY
: _FREEENTRY
;
237 /*********************************************************************
238 * _heapset (MSVCRT.@)
240 int CDECL
_heapset(unsigned int value
)
245 memset( &heap
, 0, sizeof(heap
) );
247 while ((retval
= _heapwalk(&heap
)) == _HEAPOK
)
249 if (heap
._useflag
== _FREEENTRY
)
250 memset(heap
._pentry
, value
, heap
._size
);
253 return retval
== _HEAPEND
? _HEAPOK
: retval
;
256 /*********************************************************************
257 * _heapadd (MSVCRT.@)
259 int CDECL
_heapadd(void* mem
, size_t size
)
261 TRACE("(%p,%ld) unsupported in Win32\n", mem
,size
);
266 /*********************************************************************
267 * _heapadd (MSVCRT.@)
269 intptr_t CDECL
_get_heap_handle(void)
271 return (intptr_t)GetProcessHeap();
274 /*********************************************************************
277 size_t CDECL
_msize(void* mem
)
279 size_t size
= HeapSize(GetProcessHeap(),0,mem
);
280 if (size
== ~(size_t)0)
282 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
283 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
288 /*********************************************************************
291 void* CDECL
calloc(size_t size
, size_t count
)
293 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
296 /*********************************************************************
299 void CDECL
free(void* ptr
)
301 if(ptr
== NULL
) return;
302 HeapFree(GetProcessHeap(),0,ptr
);
305 /*********************************************************************
308 void* CDECL
malloc(size_t size
)
310 void *ret
= HeapAlloc(GetProcessHeap(),0,size
);
316 /*********************************************************************
319 void* CDECL
realloc(void* ptr
, size_t size
)
321 if (!ptr
) return malloc(size
);
322 if (size
) return HeapReAlloc(GetProcessHeap(), 0, ptr
, size
);
327 /*********************************************************************
328 * __p__amblksiz (MSVCRT.@)
330 unsigned int* CDECL
__p__amblksiz(void)
332 return &MSVCRT_amblksiz
;
335 /*********************************************************************
336 * _get_sbh_threshold (MSVCRT.@)
338 size_t CDECL
_get_sbh_threshold(void)
340 return MSVCRT_sbh_threshold
;
343 /*********************************************************************
344 * _set_sbh_threshold (MSVCRT.@)
346 int CDECL
_set_sbh_threshold(size_t threshold
)
351 MSVCRT_sbh_threshold
= threshold
;
355 /*********************************************************************
356 * _aligned_free (MSVCRT.@)
358 void CDECL
_aligned_free(void *memblock
)
360 TRACE("(%p)\n", memblock
);
364 void **saved
= SAVED_PTR(memblock
);
369 /*********************************************************************
370 * _aligned_offset_malloc (MSVCRT.@)
372 void * CDECL
_aligned_offset_malloc(size_t size
, size_t alignment
, size_t offset
)
374 void *memblock
, *temp
, **saved
;
375 TRACE("(%lu, %lu, %lu)\n", size
, alignment
, offset
);
377 /* alignment must be a power of 2 */
378 if ((alignment
& (alignment
- 1)) != 0)
384 /* offset must be less than size */
391 /* don't align to less than void pointer size */
392 if (alignment
< sizeof(void *))
393 alignment
= sizeof(void *);
395 /* allocate enough space for void pointer and alignment */
396 temp
= malloc(size
+ alignment
+ sizeof(void *));
401 /* adjust pointer for proper alignment and offset */
402 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
404 /* Save the real allocation address below returned address */
405 /* so it can be found later to free. */
406 saved
= SAVED_PTR(memblock
);
412 /*********************************************************************
413 * _aligned_malloc (MSVCRT.@)
415 void * CDECL
_aligned_malloc(size_t size
, size_t alignment
)
417 TRACE("(%lu, %lu)\n", size
, alignment
);
418 return _aligned_offset_malloc(size
, alignment
, 0);
421 /*********************************************************************
422 * _aligned_offset_realloc (MSVCRT.@)
424 void * CDECL
_aligned_offset_realloc(void *memblock
, size_t size
,
425 size_t alignment
, size_t offset
)
427 void * temp
, **saved
;
428 size_t old_padding
, new_padding
, old_size
;
429 TRACE("(%p, %lu, %lu, %lu)\n", memblock
, size
, alignment
, offset
);
432 return _aligned_offset_malloc(size
, alignment
, offset
);
434 /* alignment must be a power of 2 */
435 if ((alignment
& (alignment
- 1)) != 0)
441 /* offset must be less than size */
450 _aligned_free(memblock
);
454 /* don't align to less than void pointer size */
455 if (alignment
< sizeof(void *))
456 alignment
= sizeof(void *);
458 /* make sure alignment and offset didn't change */
459 saved
= SAVED_PTR(memblock
);
460 if (memblock
!= ALIGN_PTR(*saved
, alignment
, offset
))
466 old_padding
= (char *)memblock
- (char *)*saved
;
468 /* Get previous size of block */
469 old_size
= _msize(*saved
);
472 /* It seems this function was called with an invalid pointer. Bail out. */
476 /* Adjust old_size to get amount of actual data in old block. */
477 if (old_size
< old_padding
)
479 /* Shouldn't happen. Something's weird, so bail out. */
482 old_size
-= old_padding
;
484 temp
= realloc(*saved
, size
+ alignment
+ sizeof(void *));
489 /* adjust pointer for proper alignment and offset */
490 memblock
= ALIGN_PTR(temp
, alignment
, offset
);
492 /* Save the real allocation address below returned address */
493 /* so it can be found later to free. */
494 saved
= SAVED_PTR(memblock
);
496 new_padding
= (char *)memblock
- (char *)temp
;
499 Memory layout of old block is as follows:
500 +-------+---------------------+-+--------------------------+-----------+
501 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
502 +-------+---------------------+-+--------------------------+-----------+
505 *saved saved memblock
507 Memory layout of new block is as follows:
508 +-------+-----------------------------+-+----------------------+-------+
509 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
510 +-------+-----------------------------+-+----------------------+-------+
515 However, in the new block, actual data is still written as follows
516 (because it was copied by MSVCRT_realloc):
517 +-------+---------------------+--------------------------------+-------+
518 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
519 +-------+---------------------+--------------------------------+-------+
524 Therefore, min(old_size,size) bytes of actual data have to be moved
525 from the offset they were at in the old block (temp + old_padding),
526 to the offset they have to be in the new block (temp + new_padding == memblock).
528 if (new_padding
!= old_padding
)
529 memmove((char *)memblock
, (char *)temp
+ old_padding
, (old_size
< size
) ? old_size
: size
);
536 /*********************************************************************
537 * _aligned_realloc (MSVCRT.@)
539 void * CDECL
_aligned_realloc(void *memblock
, size_t size
, size_t alignment
)
541 TRACE("(%p, %lu, %lu)\n", memblock
, size
, alignment
);
542 return _aligned_offset_realloc(memblock
, size
, alignment
, 0);
545 /*********************************************************************
546 * memmove_s (MSVCRT.@)
548 int CDECL
memmove_s(void *dest
, size_t numberOfElements
, const void *src
, size_t count
)
550 TRACE("(%p %lu %p %lu)\n", dest
, numberOfElements
, src
, count
);
557 memset(dest
, 0, numberOfElements
);
563 if(count
> numberOfElements
) {
564 memset(dest
, 0, numberOfElements
);
570 memmove(dest
, src
, count
);
574 /*********************************************************************
575 * strncpy_s (MSVCRT.@)
577 int CDECL
strncpy_s(char *dest
, size_t numberOfElements
,
578 const char *src
, size_t count
)
582 TRACE("(%s %lu %s %lu)\n", dest
, numberOfElements
, src
, count
);
587 if (!MSVCRT_CHECK_PMT(dest
!= NULL
) || !MSVCRT_CHECK_PMT(src
!= NULL
) ||
588 !MSVCRT_CHECK_PMT(numberOfElements
!= 0)) {
593 if(count
!=_TRUNCATE
&& count
<numberOfElements
)
596 end
= numberOfElements
-1;
598 for(i
=0; i
<end
&& src
[i
]; i
++)
601 if(!src
[i
] || end
==count
|| count
==_TRUNCATE
) {
606 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", EINVAL
);