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);
67 /*********************************************************************
68 * ??3@YAXPAX@Z (MSVCRT.@)
70 void CDECL MSVCRT_operator_delete(void *mem)
73 HeapFree(GetProcessHeap(), 0, mem);
77 /*********************************************************************
78 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
80 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
82 return MSVCRT_new_handler;
86 /*********************************************************************
87 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
89 int CDECL MSVCRT__query_new_mode(void)
91 return MSVCRT_new_mode;
94 /*********************************************************************
95 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
97 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
99 MSVCRT_new_handler_func old_handler;
101 old_handler = MSVCRT_new_handler;
102 MSVCRT_new_handler = func;
107 /*********************************************************************
108 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
110 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
112 TRACE("(%p)\n",func);
113 MSVCRT__set_new_handler(NULL);
117 /*********************************************************************
118 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
120 int CDECL MSVCRT__set_new_mode(int mode)
124 old_mode = MSVCRT_new_mode;
125 MSVCRT_new_mode = mode;
130 /*********************************************************************
131 * _callnewh (MSVCRT.@)
133 int CDECL _callnewh(MSVCRT_size_t size)
135 if(MSVCRT_new_handler)
136 (*MSVCRT_new_handler)(size);
140 /*********************************************************************
143 void* CDECL _expand(void* mem, MSVCRT_size_t size)
145 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
148 /*********************************************************************
149 * _heapchk (MSVCRT.@)
151 int CDECL _heapchk(void)
153 if (!HeapValidate( GetProcessHeap(), 0, NULL))
155 msvcrt_set_errno(GetLastError());
156 return MSVCRT__HEAPBADNODE;
158 return MSVCRT__HEAPOK;
161 /*********************************************************************
162 * _heapmin (MSVCRT.@)
164 int CDECL _heapmin(void)
166 if (!HeapCompact( GetProcessHeap(), 0 ))
168 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
169 msvcrt_set_errno(GetLastError());
175 /*********************************************************************
176 * _heapwalk (MSVCRT.@)
178 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
180 PROCESS_HEAP_ENTRY phe;
183 phe.lpData = next->_pentry;
184 phe.cbData = next->_size;
185 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
187 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
188 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
191 msvcrt_set_errno(GetLastError());
192 return MSVCRT__HEAPBADNODE;
197 if (!HeapWalk( GetProcessHeap(), &phe ))
200 if (GetLastError() == ERROR_NO_MORE_ITEMS)
201 return MSVCRT__HEAPEND;
202 msvcrt_set_errno(GetLastError());
204 return MSVCRT__HEAPBADBEGIN;
205 return MSVCRT__HEAPBADNODE;
207 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
210 next->_pentry = phe.lpData;
211 next->_size = phe.cbData;
212 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
213 return MSVCRT__HEAPOK;
216 /*********************************************************************
217 * _heapset (MSVCRT.@)
219 int CDECL _heapset(unsigned int value)
222 struct MSVCRT__heapinfo heap;
224 memset( &heap, 0, sizeof(heap) );
226 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
228 if (heap._useflag == MSVCRT__FREEENTRY)
229 memset(heap._pentry, value, heap._size);
232 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
235 /*********************************************************************
236 * _heapadd (MSVCRT.@)
238 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
240 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
241 *MSVCRT__errno() = MSVCRT_ENOSYS;
245 /*********************************************************************
248 MSVCRT_size_t CDECL _msize(void* mem)
250 MSVCRT_size_t size = HeapSize(GetProcessHeap(),0,mem);
251 if (size == ~(MSVCRT_size_t)0)
253 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
254 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
259 /*********************************************************************
262 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
264 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
267 /*********************************************************************
270 void CDECL MSVCRT_free(void* ptr)
272 HeapFree(GetProcessHeap(),0,ptr);
275 /*********************************************************************
278 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
280 void *ret = HeapAlloc(GetProcessHeap(),0,size);
282 *MSVCRT__errno() = MSVCRT_ENOMEM;
286 /*********************************************************************
289 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
291 if (!ptr) return MSVCRT_malloc(size);
292 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
297 /*********************************************************************
298 * __p__amblksiz (MSVCRT.@)
300 unsigned int* CDECL __p__amblksiz(void)
302 return &MSVCRT_amblksiz;
305 /*********************************************************************
306 * _get_sbh_threshold (MSVCRT.@)
308 MSVCRT_size_t CDECL _get_sbh_threshold(void)
310 return MSVCRT_sbh_threshold;
313 /*********************************************************************
314 * _set_sbh_threshold (MSVCRT.@)
316 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
321 MSVCRT_sbh_threshold = threshold;
325 /*********************************************************************
326 * _aligned_free (MSVCRT.@)
328 void CDECL _aligned_free(void *memblock)
330 TRACE("(%p)\n", memblock);
334 void **saved = SAVED_PTR(memblock);
339 /*********************************************************************
340 * _aligned_offset_malloc (MSVCRT.@)
342 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
344 void *memblock, *temp, **saved;
345 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
347 /* alignment must be a power of 2 */
348 if ((alignment & (alignment - 1)) != 0)
350 *MSVCRT__errno() = MSVCRT_EINVAL;
354 /* offset must be less than size */
357 *MSVCRT__errno() = MSVCRT_EINVAL;
361 /* don't align to less than void pointer size */
362 if (alignment < sizeof(void *))
363 alignment = sizeof(void *);
365 /* allocate enough space for void pointer and alignment */
366 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
371 /* adjust pointer for proper alignment and offset */
372 memblock = ALIGN_PTR(temp, alignment, offset);
374 /* Save the real allocation address below returned address */
375 /* so it can be found later to free. */
376 saved = SAVED_PTR(memblock);
382 /*********************************************************************
383 * _aligned_malloc (MSVCRT.@)
385 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
387 TRACE("(%lu, %lu)\n", size, alignment);
388 return _aligned_offset_malloc(size, alignment, 0);
391 /*********************************************************************
392 * _aligned_offset_realloc (MSVCRT.@)
394 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
395 MSVCRT_size_t alignment, MSVCRT_size_t offset)
397 void * temp, **saved;
398 MSVCRT_size_t old_padding, new_padding, old_size;
399 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
402 return _aligned_offset_malloc(size, alignment, offset);
404 /* alignment must be a power of 2 */
405 if ((alignment & (alignment - 1)) != 0)
407 *MSVCRT__errno() = MSVCRT_EINVAL;
411 /* offset must be less than size */
414 *MSVCRT__errno() = MSVCRT_EINVAL;
420 _aligned_free(memblock);
424 /* don't align to less than void pointer size */
425 if (alignment < sizeof(void *))
426 alignment = sizeof(void *);
428 /* make sure alignment and offset didn't change */
429 saved = SAVED_PTR(memblock);
430 if (memblock != ALIGN_PTR(*saved, alignment, offset))
432 *MSVCRT__errno() = MSVCRT_EINVAL;
436 old_padding = (char *)memblock - (char *)*saved;
438 /* Get previous size of block */
439 old_size = _msize(*saved);
442 /* It seems this function was called with an invalid pointer. Bail out. */
446 /* Adjust old_size to get amount of actual data in old block. */
447 if (old_size < old_padding)
449 /* Shouldn't happen. Something's weird, so bail out. */
452 old_size -= old_padding;
454 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
459 /* adjust pointer for proper alignment and offset */
460 memblock = ALIGN_PTR(temp, alignment, offset);
462 /* Save the real allocation address below returned address */
463 /* so it can be found later to free. */
464 saved = SAVED_PTR(memblock);
466 new_padding = (char *)memblock - (char *)temp;
469 Memory layout of old block is as follows:
470 +-------+---------------------+-+--------------------------+-----------+
471 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
472 +-------+---------------------+-+--------------------------+-----------+
475 *saved saved memblock
477 Memory layout of new block is as follows:
478 +-------+-----------------------------+-+----------------------+-------+
479 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
480 +-------+-----------------------------+-+----------------------+-------+
485 However, in the new block, actual data is still written as follows
486 (because it was copied by MSVCRT_realloc):
487 +-------+---------------------+--------------------------------+-------+
488 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
489 +-------+---------------------+--------------------------------+-------+
494 Therefore, min(old_size,size) bytes of actual data have to be moved
495 from the offset they were at in the old block (temp + old_padding),
496 to the offset they have to be in the new block (temp + new_padding == memblock).
498 if (new_padding != old_padding)
499 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
506 /*********************************************************************
507 * _aligned_realloc (MSVCRT.@)
509 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
511 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
512 return _aligned_offset_realloc(memblock, size, alignment, 0);
515 /*********************************************************************
516 * memmove_s (MSVCRT.@)
518 int CDECL memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
520 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
527 memset(dest, 0, numberOfElements);
529 *MSVCRT__errno() = MSVCRT_EINVAL;
530 return MSVCRT_EINVAL;
533 if(count > numberOfElements) {
534 memset(dest, 0, numberOfElements);
536 *MSVCRT__errno() = MSVCRT_ERANGE;
537 return MSVCRT_ERANGE;
540 memmove(dest, src, count);