pop e6a83a1bd42c871764373a338ab59a0e9b0b678c
[wine/hacks.git] / dlls / msvcrt / heap.c
blobb1d4076252bf64a0070e1e788d2ed25e34c75ca2
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 "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30 /* MT */
31 #define LOCK_HEAP _mlock( _HEAP_LOCK )
32 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
34 /* _aligned */
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;
60 LOCK_HEAP;
61 if(MSVCRT_new_handler)
62 (*MSVCRT_new_handler)(size);
63 UNLOCK_HEAP;
64 return retval;
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)
82 TRACE("(%p)\n", 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;
110 LOCK_HEAP;
111 old_handler = MSVCRT_new_handler;
112 MSVCRT_new_handler = func;
113 UNLOCK_HEAP;
114 return old_handler;
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);
124 return NULL;
127 /*********************************************************************
128 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
130 int CDECL MSVCRT__set_new_mode(int mode)
132 int old_mode;
133 LOCK_HEAP;
134 old_mode = MSVCRT_new_mode;
135 MSVCRT_new_mode = mode;
136 UNLOCK_HEAP;
137 return old_mode;
140 /*********************************************************************
141 * _callnewh (MSVCRT.@)
143 int CDECL _callnewh(MSVCRT_size_t size)
145 if(MSVCRT_new_handler)
146 (*MSVCRT_new_handler)(size);
147 return 0;
150 /*********************************************************************
151 * _expand (MSVCRT.@)
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());
180 return -1;
182 return 0;
185 /*********************************************************************
186 * _heapwalk (MSVCRT.@)
188 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
190 PROCESS_HEAP_ENTRY phe;
192 LOCK_HEAP;
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 ))
200 UNLOCK_HEAP;
201 msvcrt_set_errno(GetLastError());
202 return MSVCRT__HEAPBADNODE;
207 if (!HeapWalk( GetProcessHeap(), &phe ))
209 UNLOCK_HEAP;
210 if (GetLastError() == ERROR_NO_MORE_ITEMS)
211 return MSVCRT__HEAPEND;
212 msvcrt_set_errno(GetLastError());
213 if (!phe.lpData)
214 return MSVCRT__HEAPBADBEGIN;
215 return MSVCRT__HEAPBADNODE;
217 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
219 UNLOCK_HEAP;
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)
231 int retval;
232 struct MSVCRT__heapinfo heap;
234 memset( &heap, 0, sizeof(heap) );
235 LOCK_HEAP;
236 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
238 if (heap._useflag == MSVCRT__FREEENTRY)
239 memset(heap._pentry, value, heap._size);
241 UNLOCK_HEAP;
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;
252 return -1;
255 /*********************************************************************
256 * _heapadd (MSVCRT.@)
258 MSVCRT_intptr_t CDECL _get_heap_handle(void)
260 return (MSVCRT_intptr_t)GetProcessHeap();
263 /*********************************************************************
264 * _msize (MSVCRT.@)
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 */
274 return size;
277 /*********************************************************************
278 * calloc (MSVCRT.@)
280 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
282 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
285 /*********************************************************************
286 * free (MSVCRT.@)
288 void CDECL MSVCRT_free(void* ptr)
290 HeapFree(GetProcessHeap(),0,ptr);
293 /*********************************************************************
294 * malloc (MSVCRT.@)
296 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
298 void *ret = HeapAlloc(GetProcessHeap(),0,size);
299 if (!ret)
300 *MSVCRT__errno() = MSVCRT_ENOMEM;
301 return ret;
304 /*********************************************************************
305 * realloc (MSVCRT.@)
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);
311 MSVCRT_free(ptr);
312 return NULL;
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)
336 if(threshold > 1016)
337 return 0;
338 else
339 MSVCRT_sbh_threshold = threshold;
340 return 1;
343 /*********************************************************************
344 * _aligned_free (MSVCRT.@)
346 void CDECL _aligned_free(void *memblock)
348 TRACE("(%p)\n", memblock);
350 if (memblock)
352 void **saved = SAVED_PTR(memblock);
353 MSVCRT_free(*saved);
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;
369 return NULL;
372 /* offset must be less than size */
373 if (offset >= size)
375 *MSVCRT__errno() = MSVCRT_EINVAL;
376 return NULL;
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 *));
386 if (!temp)
387 return NULL;
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);
395 *saved = temp;
397 return 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);
419 if (!memblock)
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;
426 return NULL;
429 /* offset must be less than size */
430 if (offset >= size)
432 *MSVCRT__errno() = MSVCRT_EINVAL;
433 return NULL;
436 if (size == 0)
438 _aligned_free(memblock);
439 return NULL;
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;
451 return NULL;
454 old_padding = (char *)memblock - (char *)*saved;
456 /* Get previous size of block */
457 old_size = _msize(*saved);
458 if (old_size == -1)
460 /* It seems this function was called with an invalid pointer. Bail out. */
461 return NULL;
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. */
468 return NULL;
470 old_size -= old_padding;
472 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
474 if (!temp)
475 return NULL;
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 +-------+---------------------+-+--------------------------+-----------+
491 ^ ^ ^
492 | | |
493 *saved saved memblock
495 Memory layout of new block is as follows:
496 +-------+-----------------------------+-+----------------------+-------+
497 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
498 +-------+-----------------------------+-+----------------------+-------+
499 ^ ^ ^
500 | | |
501 temp saved memblock
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 +-------+---------------------+--------------------------------+-------+
508 ^ ^ ^
509 | | |
510 temp saved memblock
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);
519 *saved = temp;
521 return memblock;
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);
540 if(!count)
541 return 0;
543 if(!dest || !src) {
544 if(dest)
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);
559 return 0;
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);
572 if(!count)
573 return 0;
575 if(!dest || !src || !numberOfElements) {
576 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
577 *MSVCRT__errno() = MSVCRT_EINVAL;
578 return MSVCRT_EINVAL;
581 if(count!=MSVCRT__TRUNCATE && count<numberOfElements)
582 end = count;
583 else
584 end = numberOfElements-1;
586 for(i=0; i<end && src[i]; i++)
587 dest[i] = src[i];
589 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
590 dest[i] = '\0';
591 return 0;
594 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
595 dest[0] = '\0';
596 *MSVCRT__errno() = MSVCRT_EINVAL;
597 return MSVCRT_EINVAL;