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 (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)
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 /*********************************************************************
256 * _heapadd (MSVCRT.@)
258 MSVCRT_intptr_t CDECL _get_heap_handle(void)
260 return (MSVCRT_intptr_t)GetProcessHeap();
263 /*********************************************************************
266 MSVCRT_size_t CDECL _msize(void* mem)
268 MSVCRT_size_t size = HeapSize(GetProcessHeap(),0,mem);
269 if (size == ~(MSVCRT_size_t)0)
271 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
272 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
277 /*********************************************************************
280 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
282 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
285 /*********************************************************************
288 void CDECL MSVCRT_free(void* ptr)
290 HeapFree(GetProcessHeap(),0,ptr);
293 /*********************************************************************
296 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
298 void *ret = HeapAlloc(GetProcessHeap(),0,size);
300 *MSVCRT__errno() = MSVCRT_ENOMEM;
304 /*********************************************************************
307 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
309 if (!ptr) return MSVCRT_malloc(size);
310 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
315 /*********************************************************************
316 * __p__amblksiz (MSVCRT.@)
318 unsigned int* CDECL __p__amblksiz(void)
320 return &MSVCRT_amblksiz;
323 /*********************************************************************
324 * _get_sbh_threshold (MSVCRT.@)
326 MSVCRT_size_t CDECL _get_sbh_threshold(void)
328 return MSVCRT_sbh_threshold;
331 /*********************************************************************
332 * _set_sbh_threshold (MSVCRT.@)
334 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
339 MSVCRT_sbh_threshold = threshold;
343 /*********************************************************************
344 * _aligned_free (MSVCRT.@)
346 void CDECL _aligned_free(void *memblock)
348 TRACE("(%p)\n", memblock);
352 void **saved = SAVED_PTR(memblock);
357 /*********************************************************************
358 * _aligned_offset_malloc (MSVCRT.@)
360 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
362 void *memblock, *temp, **saved;
363 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
365 /* alignment must be a power of 2 */
366 if ((alignment & (alignment - 1)) != 0)
368 *MSVCRT__errno() = MSVCRT_EINVAL;
372 /* offset must be less than size */
375 *MSVCRT__errno() = MSVCRT_EINVAL;
379 /* don't align to less than void pointer size */
380 if (alignment < sizeof(void *))
381 alignment = sizeof(void *);
383 /* allocate enough space for void pointer and alignment */
384 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
389 /* adjust pointer for proper alignment and offset */
390 memblock = ALIGN_PTR(temp, alignment, offset);
392 /* Save the real allocation address below returned address */
393 /* so it can be found later to free. */
394 saved = SAVED_PTR(memblock);
400 /*********************************************************************
401 * _aligned_malloc (MSVCRT.@)
403 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
405 TRACE("(%lu, %lu)\n", size, alignment);
406 return _aligned_offset_malloc(size, alignment, 0);
409 /*********************************************************************
410 * _aligned_offset_realloc (MSVCRT.@)
412 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
413 MSVCRT_size_t alignment, MSVCRT_size_t offset)
415 void * temp, **saved;
416 MSVCRT_size_t old_padding, new_padding, old_size;
417 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
420 return _aligned_offset_malloc(size, alignment, offset);
422 /* alignment must be a power of 2 */
423 if ((alignment & (alignment - 1)) != 0)
425 *MSVCRT__errno() = MSVCRT_EINVAL;
429 /* offset must be less than size */
432 *MSVCRT__errno() = MSVCRT_EINVAL;
438 _aligned_free(memblock);
442 /* don't align to less than void pointer size */
443 if (alignment < sizeof(void *))
444 alignment = sizeof(void *);
446 /* make sure alignment and offset didn't change */
447 saved = SAVED_PTR(memblock);
448 if (memblock != ALIGN_PTR(*saved, alignment, offset))
450 *MSVCRT__errno() = MSVCRT_EINVAL;
454 old_padding = (char *)memblock - (char *)*saved;
456 /* Get previous size of block */
457 old_size = _msize(*saved);
460 /* It seems this function was called with an invalid pointer. Bail out. */
464 /* Adjust old_size to get amount of actual data in old block. */
465 if (old_size < old_padding)
467 /* Shouldn't happen. Something's weird, so bail out. */
470 old_size -= old_padding;
472 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
477 /* adjust pointer for proper alignment and offset */
478 memblock = ALIGN_PTR(temp, alignment, offset);
480 /* Save the real allocation address below returned address */
481 /* so it can be found later to free. */
482 saved = SAVED_PTR(memblock);
484 new_padding = (char *)memblock - (char *)temp;
487 Memory layout of old block is as follows:
488 +-------+---------------------+-+--------------------------+-----------+
489 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
490 +-------+---------------------+-+--------------------------+-----------+
493 *saved saved memblock
495 Memory layout of new block is as follows:
496 +-------+-----------------------------+-+----------------------+-------+
497 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
498 +-------+-----------------------------+-+----------------------+-------+
503 However, in the new block, actual data is still written as follows
504 (because it was copied by MSVCRT_realloc):
505 +-------+---------------------+--------------------------------+-------+
506 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
507 +-------+---------------------+--------------------------------+-------+
512 Therefore, min(old_size,size) bytes of actual data have to be moved
513 from the offset they were at in the old block (temp + old_padding),
514 to the offset they have to be in the new block (temp + new_padding == memblock).
516 if (new_padding != old_padding)
517 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
524 /*********************************************************************
525 * _aligned_realloc (MSVCRT.@)
527 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
529 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
530 return _aligned_offset_realloc(memblock, size, alignment, 0);
533 /*********************************************************************
534 * memmove_s (MSVCRT.@)
536 int CDECL memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
538 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
545 memset(dest, 0, numberOfElements);
547 *MSVCRT__errno() = MSVCRT_EINVAL;
548 return MSVCRT_EINVAL;
551 if(count > numberOfElements) {
552 memset(dest, 0, numberOfElements);
554 *MSVCRT__errno() = MSVCRT_ERANGE;
555 return MSVCRT_ERANGE;
558 memmove(dest, src, count);
562 /*********************************************************************
563 * strncpy_s (MSVCRT.@)
565 int CDECL strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
566 const char *src, MSVCRT_size_t count)
568 MSVCRT_size_t i, end;
570 TRACE("(%s %lu %s %lu)\n", dest, numberOfElements, src, count);
575 if (!MSVCRT_CHECK_PMT(dest != NULL) || !MSVCRT_CHECK_PMT(src != NULL) || !MSVCRT_CHECK_PMT(numberOfElements != 0)) {
576 *MSVCRT__errno() = MSVCRT_EINVAL;
577 return MSVCRT_EINVAL;
580 if(count!=MSVCRT__TRUNCATE && count<numberOfElements)
583 end = numberOfElements-1;
585 for(i=0; i<end && src[i]; i++)
588 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
593 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small");
595 *MSVCRT__errno() = MSVCRT_EINVAL;
596 return MSVCRT_EINVAL;