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 void (*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)
57 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
58 TRACE("(%ld) returning %p\n", size, retval);
59 if(retval) return retval;
61 if(MSVCRT_new_handler)
62 (*MSVCRT_new_handler)(size);
68 /*********************************************************************
69 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
71 void* CDECL MSVCRT_operator_new_dbg(MSVCRT_size_t size, int type, const char *file, int line)
73 return MSVCRT_operator_new( size );
77 /*********************************************************************
78 * ??3@YAXPAX@Z (MSVCRT.@)
80 void CDECL MSVCRT_operator_delete(void *mem)
83 HeapFree(GetProcessHeap(), 0, mem);
87 /*********************************************************************
88 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
90 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
92 return MSVCRT_new_handler;
96 /*********************************************************************
97 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
99 int CDECL MSVCRT__query_new_mode(void)
101 return MSVCRT_new_mode;
104 /*********************************************************************
105 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
107 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
109 MSVCRT_new_handler_func old_handler;
111 old_handler = MSVCRT_new_handler;
112 MSVCRT_new_handler = func;
117 /*********************************************************************
118 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
120 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
122 TRACE("(%p)\n",func);
123 MSVCRT__set_new_handler(NULL);
127 /*********************************************************************
128 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
130 int CDECL MSVCRT__set_new_mode(int mode)
134 old_mode = MSVCRT_new_mode;
135 MSVCRT_new_mode = mode;
140 /*********************************************************************
141 * _callnewh (MSVCRT.@)
143 int CDECL _callnewh(MSVCRT_size_t size)
145 if(MSVCRT_new_handler)
146 (*MSVCRT_new_handler)(size);
150 /*********************************************************************
153 void* CDECL _expand(void* mem, MSVCRT_size_t size)
155 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
158 /*********************************************************************
159 * _heapchk (MSVCRT.@)
161 int CDECL _heapchk(void)
163 if (!HeapValidate( GetProcessHeap(), 0, NULL))
165 msvcrt_set_errno(GetLastError());
166 return MSVCRT__HEAPBADNODE;
168 return MSVCRT__HEAPOK;
171 /*********************************************************************
172 * _heapmin (MSVCRT.@)
174 int CDECL _heapmin(void)
176 if (!HeapCompact( GetProcessHeap(), 0 ))
178 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
179 msvcrt_set_errno(GetLastError());
185 /*********************************************************************
186 * _heapwalk (MSVCRT.@)
188 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
190 PROCESS_HEAP_ENTRY phe;
193 phe.lpData = next->_pentry;
194 phe.cbData = next->_size;
195 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
197 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
198 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
201 msvcrt_set_errno(GetLastError());
202 return MSVCRT__HEAPBADNODE;
207 if (!HeapWalk( GetProcessHeap(), &phe ))
210 if (GetLastError() == ERROR_NO_MORE_ITEMS)
211 return MSVCRT__HEAPEND;
212 msvcrt_set_errno(GetLastError());
214 return MSVCRT__HEAPBADBEGIN;
215 return MSVCRT__HEAPBADNODE;
217 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
220 next->_pentry = phe.lpData;
221 next->_size = phe.cbData;
222 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
223 return MSVCRT__HEAPOK;
226 /*********************************************************************
227 * _heapset (MSVCRT.@)
229 int CDECL _heapset(unsigned int value)
232 struct MSVCRT__heapinfo heap;
234 memset( &heap, 0, sizeof(heap) );
236 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
238 if (heap._useflag == MSVCRT__FREEENTRY)
239 memset(heap._pentry, value, heap._size);
242 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
245 /*********************************************************************
246 * _heapadd (MSVCRT.@)
248 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
250 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
251 *MSVCRT__errno() = MSVCRT_ENOSYS;
255 /*********************************************************************
258 MSVCRT_size_t CDECL _msize(void* mem)
260 MSVCRT_size_t size = HeapSize(GetProcessHeap(),0,mem);
261 if (size == ~(MSVCRT_size_t)0)
263 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
264 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
269 /*********************************************************************
272 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
274 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
277 /*********************************************************************
280 void CDECL MSVCRT_free(void* ptr)
282 HeapFree(GetProcessHeap(),0,ptr);
285 /*********************************************************************
288 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
290 void *ret = HeapAlloc(GetProcessHeap(),0,size);
292 *MSVCRT__errno() = MSVCRT_ENOMEM;
296 /*********************************************************************
299 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
301 if (!ptr) return MSVCRT_malloc(size);
302 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
307 /*********************************************************************
308 * __p__amblksiz (MSVCRT.@)
310 unsigned int* CDECL __p__amblksiz(void)
312 return &MSVCRT_amblksiz;
315 /*********************************************************************
316 * _get_sbh_threshold (MSVCRT.@)
318 MSVCRT_size_t CDECL _get_sbh_threshold(void)
320 return MSVCRT_sbh_threshold;
323 /*********************************************************************
324 * _set_sbh_threshold (MSVCRT.@)
326 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
331 MSVCRT_sbh_threshold = threshold;
335 /*********************************************************************
336 * _aligned_free (MSVCRT.@)
338 void CDECL _aligned_free(void *memblock)
340 TRACE("(%p)\n", memblock);
344 void **saved = SAVED_PTR(memblock);
349 /*********************************************************************
350 * _aligned_offset_malloc (MSVCRT.@)
352 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
354 void *memblock, *temp, **saved;
355 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
357 /* alignment must be a power of 2 */
358 if ((alignment & (alignment - 1)) != 0)
360 *MSVCRT__errno() = MSVCRT_EINVAL;
364 /* offset must be less than size */
367 *MSVCRT__errno() = MSVCRT_EINVAL;
371 /* don't align to less than void pointer size */
372 if (alignment < sizeof(void *))
373 alignment = sizeof(void *);
375 /* allocate enough space for void pointer and alignment */
376 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
381 /* adjust pointer for proper alignment and offset */
382 memblock = ALIGN_PTR(temp, alignment, offset);
384 /* Save the real allocation address below returned address */
385 /* so it can be found later to free. */
386 saved = SAVED_PTR(memblock);
392 /*********************************************************************
393 * _aligned_malloc (MSVCRT.@)
395 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
397 TRACE("(%lu, %lu)\n", size, alignment);
398 return _aligned_offset_malloc(size, alignment, 0);
401 /*********************************************************************
402 * _aligned_offset_realloc (MSVCRT.@)
404 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
405 MSVCRT_size_t alignment, MSVCRT_size_t offset)
407 void * temp, **saved;
408 MSVCRT_size_t old_padding, new_padding, old_size;
409 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
412 return _aligned_offset_malloc(size, alignment, offset);
414 /* alignment must be a power of 2 */
415 if ((alignment & (alignment - 1)) != 0)
417 *MSVCRT__errno() = MSVCRT_EINVAL;
421 /* offset must be less than size */
424 *MSVCRT__errno() = MSVCRT_EINVAL;
430 _aligned_free(memblock);
434 /* don't align to less than void pointer size */
435 if (alignment < sizeof(void *))
436 alignment = sizeof(void *);
438 /* make sure alignment and offset didn't change */
439 saved = SAVED_PTR(memblock);
440 if (memblock != ALIGN_PTR(*saved, alignment, offset))
442 *MSVCRT__errno() = MSVCRT_EINVAL;
446 old_padding = (char *)memblock - (char *)*saved;
448 /* Get previous size of block */
449 old_size = _msize(*saved);
452 /* It seems this function was called with an invalid pointer. Bail out. */
456 /* Adjust old_size to get amount of actual data in old block. */
457 if (old_size < old_padding)
459 /* Shouldn't happen. Something's weird, so bail out. */
462 old_size -= old_padding;
464 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
469 /* adjust pointer for proper alignment and offset */
470 memblock = ALIGN_PTR(temp, alignment, offset);
472 /* Save the real allocation address below returned address */
473 /* so it can be found later to free. */
474 saved = SAVED_PTR(memblock);
476 new_padding = (char *)memblock - (char *)temp;
479 Memory layout of old block is as follows:
480 +-------+---------------------+-+--------------------------+-----------+
481 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
482 +-------+---------------------+-+--------------------------+-----------+
485 *saved saved memblock
487 Memory layout of new block is as follows:
488 +-------+-----------------------------+-+----------------------+-------+
489 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
490 +-------+-----------------------------+-+----------------------+-------+
495 However, in the new block, actual data is still written as follows
496 (because it was copied by MSVCRT_realloc):
497 +-------+---------------------+--------------------------------+-------+
498 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
499 +-------+---------------------+--------------------------------+-------+
504 Therefore, min(old_size,size) bytes of actual data have to be moved
505 from the offset they were at in the old block (temp + old_padding),
506 to the offset they have to be in the new block (temp + new_padding == memblock).
508 if (new_padding != old_padding)
509 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
516 /*********************************************************************
517 * _aligned_realloc (MSVCRT.@)
519 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
521 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
522 return _aligned_offset_realloc(memblock, size, alignment, 0);
525 /*********************************************************************
526 * memmove_s (MSVCRT.@)
528 int CDECL memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
530 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
537 memset(dest, 0, numberOfElements);
539 *MSVCRT__errno() = MSVCRT_EINVAL;
540 return MSVCRT_EINVAL;
543 if(count > numberOfElements) {
544 memset(dest, 0, numberOfElements);
546 *MSVCRT__errno() = MSVCRT_ERANGE;
547 return MSVCRT_ERANGE;
550 memmove(dest, src, count);
554 /*********************************************************************
555 * strncpy_s (MSVCRT.@)
557 int CDECL strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
558 const char *src, MSVCRT_size_t count)
560 MSVCRT_size_t i, end;
562 TRACE("(%s %lu %s %lu)\n", dest, numberOfElements, src, count);
567 if(!dest || !src || !numberOfElements) {
568 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
569 *MSVCRT__errno() = MSVCRT_EINVAL;
570 return MSVCRT_EINVAL;
573 if(count!=_TRUNCATE && count<numberOfElements)
576 end = numberOfElements-1;
578 for(i=0; i<end && src[i]; i++)
581 if(!src[i] || end==count || count==_TRUNCATE) {
586 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
588 *MSVCRT__errno() = MSVCRT_EINVAL;
589 return MSVCRT_EINVAL;