msvcrt: Fix _Aligned_offset_realloc (move correct block of memory); add a comment...
[wine/wine64.git] / dlls / msvcrt / heap.c
blob06c9d56205bb02a1a97f641adc0906c357feef85
1 /*
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
24 #include "msvcrt.h"
25 #include "mtdll.h"
26 #include "msvcrt/errno.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31 /* MT */
32 #define LOCK_HEAP _mlock( _HEAP_LOCK )
33 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
35 /* _aligned */
36 #define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
37 ~(sizeof(void *) - 1)))
38 #define ALIGN_PTR(ptr, alignment, offset) ((void *) \
39 ((((DWORD_PTR)((char *)ptr + alignment + sizeof(void *) + offset)) & \
40 ~(alignment - 1)) - offset))
43 typedef void (*MSVCRT_new_handler_func)(unsigned long size);
45 static MSVCRT_new_handler_func MSVCRT_new_handler;
46 static int MSVCRT_new_mode;
48 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
49 static unsigned int MSVCRT_amblksiz = 16;
50 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
51 static MSVCRT_size_t MSVCRT_sbh_threshold = 0;
53 /*********************************************************************
54 * ??2@YAPAXI@Z (MSVCRT.@)
56 void* CDECL MSVCRT_operator_new(unsigned long size)
58 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
59 TRACE("(%ld) returning %p\n", size, retval);
60 if(retval) return retval;
61 LOCK_HEAP;
62 if(MSVCRT_new_handler)
63 (*MSVCRT_new_handler)(size);
64 UNLOCK_HEAP;
65 return retval;
68 /*********************************************************************
69 * ??3@YAXPAX@Z (MSVCRT.@)
71 void CDECL MSVCRT_operator_delete(void *mem)
73 TRACE("(%p)\n", mem);
74 HeapFree(GetProcessHeap(), 0, mem);
78 /*********************************************************************
79 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
81 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
83 return MSVCRT_new_handler;
87 /*********************************************************************
88 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
90 int CDECL MSVCRT__query_new_mode(void)
92 return MSVCRT_new_mode;
95 /*********************************************************************
96 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
98 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
100 MSVCRT_new_handler_func old_handler;
101 LOCK_HEAP;
102 old_handler = MSVCRT_new_handler;
103 MSVCRT_new_handler = func;
104 UNLOCK_HEAP;
105 return old_handler;
108 /*********************************************************************
109 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
111 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
113 TRACE("(%p)\n",func);
114 MSVCRT__set_new_handler(NULL);
115 return NULL;
118 /*********************************************************************
119 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
121 int CDECL MSVCRT__set_new_mode(int mode)
123 int old_mode;
124 LOCK_HEAP;
125 old_mode = MSVCRT_new_mode;
126 MSVCRT_new_mode = mode;
127 UNLOCK_HEAP;
128 return old_mode;
131 /*********************************************************************
132 * _callnewh (MSVCRT.@)
134 int CDECL _callnewh(unsigned long size)
136 if(MSVCRT_new_handler)
137 (*MSVCRT_new_handler)(size);
138 return 0;
141 /*********************************************************************
142 * _expand (MSVCRT.@)
144 void* CDECL _expand(void* mem, MSVCRT_size_t size)
146 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
149 /*********************************************************************
150 * _heapchk (MSVCRT.@)
152 int CDECL _heapchk(void)
154 if (!HeapValidate( GetProcessHeap(), 0, NULL))
156 msvcrt_set_errno(GetLastError());
157 return MSVCRT__HEAPBADNODE;
159 return MSVCRT__HEAPOK;
162 /*********************************************************************
163 * _heapmin (MSVCRT.@)
165 int CDECL _heapmin(void)
167 if (!HeapCompact( GetProcessHeap(), 0 ))
169 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
170 msvcrt_set_errno(GetLastError());
171 return -1;
173 return 0;
176 /*********************************************************************
177 * _heapwalk (MSVCRT.@)
179 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
181 PROCESS_HEAP_ENTRY phe;
183 LOCK_HEAP;
184 phe.lpData = next->_pentry;
185 phe.cbData = next->_size;
186 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
188 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
189 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
191 UNLOCK_HEAP;
192 msvcrt_set_errno(GetLastError());
193 return MSVCRT__HEAPBADNODE;
198 if (!HeapWalk( GetProcessHeap(), &phe ))
200 UNLOCK_HEAP;
201 if (GetLastError() == ERROR_NO_MORE_ITEMS)
202 return MSVCRT__HEAPEND;
203 msvcrt_set_errno(GetLastError());
204 if (!phe.lpData)
205 return MSVCRT__HEAPBADBEGIN;
206 return MSVCRT__HEAPBADNODE;
208 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
210 UNLOCK_HEAP;
211 next->_pentry = phe.lpData;
212 next->_size = phe.cbData;
213 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
214 return MSVCRT__HEAPOK;
217 /*********************************************************************
218 * _heapset (MSVCRT.@)
220 int CDECL _heapset(unsigned int value)
222 int retval;
223 struct MSVCRT__heapinfo heap;
225 memset( &heap, 0, sizeof(heap) );
226 LOCK_HEAP;
227 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
229 if (heap._useflag == MSVCRT__FREEENTRY)
230 memset(heap._pentry, value, heap._size);
232 UNLOCK_HEAP;
233 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
236 /*********************************************************************
237 * _heapadd (MSVCRT.@)
239 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
241 TRACE("(%p,%d) unsupported in Win32\n", mem,size);
242 *MSVCRT__errno() = MSVCRT_ENOSYS;
243 return -1;
246 /*********************************************************************
247 * _msize (MSVCRT.@)
249 MSVCRT_size_t CDECL _msize(void* mem)
251 long size = HeapSize(GetProcessHeap(),0,mem);
252 if (size == -1)
254 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
255 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
257 return size;
260 /*********************************************************************
261 * calloc (MSVCRT.@)
263 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
265 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
268 /*********************************************************************
269 * free (MSVCRT.@)
271 void CDECL MSVCRT_free(void* ptr)
273 HeapFree(GetProcessHeap(),0,ptr);
276 /*********************************************************************
277 * malloc (MSVCRT.@)
279 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
281 void *ret = HeapAlloc(GetProcessHeap(),0,size);
282 if (!ret)
283 msvcrt_set_errno(MSVCRT_ENOMEM);
284 return ret;
287 /*********************************************************************
288 * realloc (MSVCRT.@)
290 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
292 if (!ptr) return MSVCRT_malloc(size);
293 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
294 MSVCRT_free(ptr);
295 return NULL;
298 /*********************************************************************
299 * __p__amblksiz (MSVCRT.@)
301 unsigned int* CDECL __p__amblksiz(void)
303 return &MSVCRT_amblksiz;
306 /*********************************************************************
307 * _get_sbh_threshold (MSVCRT.@)
309 MSVCRT_size_t CDECL _get_sbh_threshold(void)
311 return MSVCRT_sbh_threshold;
314 /*********************************************************************
315 * _set_sbh_threshold (MSVCRT.@)
317 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
319 if(threshold > 1016)
320 return 0;
321 else
322 MSVCRT_sbh_threshold = threshold;
323 return 1;
326 /*********************************************************************
327 * _aligned_free (MSVCRT.@)
329 void CDECL _aligned_free(void *memblock)
331 TRACE("(%p)\n", memblock);
333 if (memblock)
335 void **saved = SAVED_PTR(memblock);
336 MSVCRT_free(*saved);
340 /*********************************************************************
341 * _aligned_offset_malloc (MSVCRT.@)
343 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
345 void *memblock, *temp, **saved;
346 TRACE("(%u, %u, %u)\n", size, alignment, offset);
348 /* alignment must be a power of 2 */
349 if ((alignment & (alignment - 1)) != 0)
351 msvcrt_set_errno(EINVAL);
352 return NULL;
355 /* offset must be less than size */
356 if (offset >= size)
358 msvcrt_set_errno(EINVAL);
359 return NULL;
362 /* don't align to less than void pointer size */
363 if (alignment < sizeof(void *))
364 alignment = sizeof(void *);
366 /* allocate enough space for void pointer and alignment */
367 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
369 if (!temp)
370 return NULL;
372 /* adjust pointer for proper alignment and offset */
373 memblock = ALIGN_PTR(temp, alignment, offset);
375 /* Save the real allocation address below returned address */
376 /* so it can be found later to free. */
377 saved = SAVED_PTR(memblock);
378 *saved = temp;
380 return memblock;
383 /*********************************************************************
384 * _aligned_malloc (MSVCRT.@)
386 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
388 TRACE("(%u, %u)\n", size, alignment);
389 return _aligned_offset_malloc(size, alignment, 0);
392 /*********************************************************************
393 * _aligned_offset_realloc (MSVCRT.@)
395 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
396 MSVCRT_size_t alignment, MSVCRT_size_t offset)
398 void * temp, **saved;
399 MSVCRT_size_t old_padding, new_padding, old_size;
400 TRACE("(%p, %u, %u, %u)\n", memblock, size, alignment, offset);
402 if (!memblock)
403 return _aligned_offset_malloc(size, alignment, offset);
405 /* alignment must be a power of 2 */
406 if ((alignment & (alignment - 1)) != 0)
408 msvcrt_set_errno(EINVAL);
409 return NULL;
412 /* offset must be less than size */
413 if (offset >= size)
415 msvcrt_set_errno(EINVAL);
416 return NULL;
419 if (size == 0)
421 _aligned_free(memblock);
422 return NULL;
425 /* don't align to less than void pointer size */
426 if (alignment < sizeof(void *))
427 alignment = sizeof(void *);
429 /* make sure alignment and offset didn't change */
430 saved = SAVED_PTR(memblock);
431 if (memblock != ALIGN_PTR(*saved, alignment, offset))
433 msvcrt_set_errno(EINVAL);
434 return NULL;
437 old_padding = (char *)memblock - (char *)*saved;
439 /* Get previous size of block */
440 old_size = _msize(*saved);
441 if (old_size == -1)
443 /* It seems this function was called with an invalid pointer. Bail out. */
444 return NULL;
446 /* Adjust old_size to get amount of actual data in old block. */
447 old_size -= old_padding;
448 if (old_size < 0)
450 /* Shouldn't happen. Something's weird, so bail out. */
451 return NULL;
454 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
456 if (!temp)
457 return NULL;
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 +-------+---------------------+-+--------------------------+-----------+
473 ^ ^ ^
474 | | |
475 *saved saved memblock
477 Memory layout of new block is as follows:
478 +-------+-----------------------------+-+----------------------+-------+
479 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
480 +-------+-----------------------------+-+----------------------+-------+
481 ^ ^ ^
482 | | |
483 temp saved memblock
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 +-------+---------------------+--------------------------------+-------+
490 ^ ^ ^
491 | | |
492 temp saved memblock
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);
501 *saved = temp;
503 return memblock;
506 /*********************************************************************
507 * _aligned_realloc (MSVCRT.@)
509 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
511 TRACE("(%p, %u, %u)\n", memblock, size, alignment);
512 return _aligned_offset_realloc(memblock, size, alignment, 0);