msvcrt/tests: Make qsort_comp() static.
[wine/multimedia.git] / dlls / msvcrt / heap.c
blob0cac3fe91723910a02b2fa8a38a91305cc43738f
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))
41 static HANDLE heap;
43 typedef int (CDECL *MSVCRT_new_handler_func)(MSVCRT_size_t 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(MSVCRT_size_t size)
58 void *retval;
59 int freed;
63 retval = HeapAlloc(heap, 0, size);
64 if(retval)
66 TRACE("(%ld) returning %p\n", size, retval);
67 return retval;
70 LOCK_HEAP;
71 if(MSVCRT_new_handler)
72 freed = (*MSVCRT_new_handler)(size);
73 else
74 freed = 0;
75 UNLOCK_HEAP;
76 } while(freed);
78 TRACE("(%ld) out of memory\n", size);
79 return NULL;
83 /*********************************************************************
84 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
86 void* CDECL MSVCRT_operator_new_dbg(MSVCRT_size_t size, int type, const char *file, int line)
88 return MSVCRT_operator_new( size );
92 /*********************************************************************
93 * ??3@YAXPAX@Z (MSVCRT.@)
95 void CDECL MSVCRT_operator_delete(void *mem)
97 TRACE("(%p)\n", mem);
98 HeapFree(heap, 0, mem);
102 /*********************************************************************
103 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
105 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
107 return MSVCRT_new_handler;
111 /*********************************************************************
112 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
114 int CDECL MSVCRT__query_new_mode(void)
116 return MSVCRT_new_mode;
119 /*********************************************************************
120 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
122 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
124 MSVCRT_new_handler_func old_handler;
125 LOCK_HEAP;
126 old_handler = MSVCRT_new_handler;
127 MSVCRT_new_handler = func;
128 UNLOCK_HEAP;
129 return old_handler;
132 /*********************************************************************
133 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
135 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
137 TRACE("(%p)\n",func);
138 MSVCRT__set_new_handler(NULL);
139 return NULL;
142 /*********************************************************************
143 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
145 int CDECL MSVCRT__set_new_mode(int mode)
147 int old_mode;
148 LOCK_HEAP;
149 old_mode = MSVCRT_new_mode;
150 MSVCRT_new_mode = mode;
151 UNLOCK_HEAP;
152 return old_mode;
155 /*********************************************************************
156 * _callnewh (MSVCRT.@)
158 int CDECL _callnewh(MSVCRT_size_t size)
160 if(MSVCRT_new_handler)
161 (*MSVCRT_new_handler)(size);
162 return 0;
165 /*********************************************************************
166 * _expand (MSVCRT.@)
168 void* CDECL _expand(void* mem, MSVCRT_size_t size)
170 return HeapReAlloc(heap, HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
173 /*********************************************************************
174 * _heapchk (MSVCRT.@)
176 int CDECL _heapchk(void)
178 if (!HeapValidate( heap, 0, NULL))
180 msvcrt_set_errno(GetLastError());
181 return MSVCRT__HEAPBADNODE;
183 return MSVCRT__HEAPOK;
186 /*********************************************************************
187 * _heapmin (MSVCRT.@)
189 int CDECL _heapmin(void)
191 if (!HeapCompact( heap, 0 ))
193 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
194 msvcrt_set_errno(GetLastError());
195 return -1;
197 return 0;
200 /*********************************************************************
201 * _heapwalk (MSVCRT.@)
203 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
205 PROCESS_HEAP_ENTRY phe;
207 LOCK_HEAP;
208 phe.lpData = next->_pentry;
209 phe.cbData = next->_size;
210 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
212 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
213 !HeapValidate( heap, 0, phe.lpData ))
215 UNLOCK_HEAP;
216 msvcrt_set_errno(GetLastError());
217 return MSVCRT__HEAPBADNODE;
222 if (!HeapWalk( heap, &phe ))
224 UNLOCK_HEAP;
225 if (GetLastError() == ERROR_NO_MORE_ITEMS)
226 return MSVCRT__HEAPEND;
227 msvcrt_set_errno(GetLastError());
228 if (!phe.lpData)
229 return MSVCRT__HEAPBADBEGIN;
230 return MSVCRT__HEAPBADNODE;
232 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
234 UNLOCK_HEAP;
235 next->_pentry = phe.lpData;
236 next->_size = phe.cbData;
237 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
238 return MSVCRT__HEAPOK;
241 /*********************************************************************
242 * _heapset (MSVCRT.@)
244 int CDECL _heapset(unsigned int value)
246 int retval;
247 struct MSVCRT__heapinfo heap;
249 memset( &heap, 0, sizeof(heap) );
250 LOCK_HEAP;
251 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
253 if (heap._useflag == MSVCRT__FREEENTRY)
254 memset(heap._pentry, value, heap._size);
256 UNLOCK_HEAP;
257 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
260 /*********************************************************************
261 * _heapadd (MSVCRT.@)
263 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
265 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
266 *MSVCRT__errno() = MSVCRT_ENOSYS;
267 return -1;
270 /*********************************************************************
271 * _get_heap_handle (MSVCRT.@)
273 MSVCRT_intptr_t CDECL _get_heap_handle(void)
275 return (MSVCRT_intptr_t)heap;
278 /*********************************************************************
279 * _msize (MSVCRT.@)
281 MSVCRT_size_t CDECL _msize(void* mem)
283 MSVCRT_size_t size = HeapSize(heap,0,mem);
284 if (size == ~(MSVCRT_size_t)0)
286 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
287 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
289 return size;
292 /*********************************************************************
293 * _aligned_msize (MSVCR100.@)
295 size_t CDECL _aligned_msize(void *p, MSVCRT_size_t alignment, MSVCRT_size_t offset)
297 void **alloc_ptr;
299 if(!MSVCRT_CHECK_PMT(p)) return -1;
301 if(alignment < sizeof(void*))
302 alignment = sizeof(void*);
304 alloc_ptr = SAVED_PTR(p);
305 return _msize(*alloc_ptr)-alignment-sizeof(void*);
308 /*********************************************************************
309 * calloc (MSVCRT.@)
311 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
313 return HeapAlloc( heap, HEAP_ZERO_MEMORY, size * count );
316 /*********************************************************************
317 * free (MSVCRT.@)
319 void CDECL MSVCRT_free(void* ptr)
321 HeapFree(heap,0,ptr);
324 /*********************************************************************
325 * malloc (MSVCRT.@)
327 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
329 void *ret = HeapAlloc(heap,0,size);
330 if (!ret)
331 *MSVCRT__errno() = MSVCRT_ENOMEM;
332 return ret;
335 /*********************************************************************
336 * realloc (MSVCRT.@)
338 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
340 if (!ptr) return MSVCRT_malloc(size);
341 if (size) return HeapReAlloc(heap, 0, ptr, size);
342 MSVCRT_free(ptr);
343 return NULL;
346 /*********************************************************************
347 * _recalloc (MSVCR100.@)
349 void* CDECL _recalloc(void *mem, MSVCRT_size_t num, MSVCRT_size_t size)
351 MSVCRT_size_t old_size;
352 void *ret;
354 if(!mem)
355 return MSVCRT_calloc(num, size);
357 size = num*size;
358 old_size = _msize(mem);
360 ret = MSVCRT_realloc(mem, size);
361 if(!ret) {
362 *MSVCRT__errno() = MSVCRT_ENOMEM;
363 return NULL;
366 if(size>old_size)
367 memset((BYTE*)ret+old_size, 0, size-old_size);
368 return ret;
371 /*********************************************************************
372 * __p__amblksiz (MSVCRT.@)
374 unsigned int* CDECL __p__amblksiz(void)
376 return &MSVCRT_amblksiz;
379 /*********************************************************************
380 * _get_sbh_threshold (MSVCRT.@)
382 MSVCRT_size_t CDECL _get_sbh_threshold(void)
384 return MSVCRT_sbh_threshold;
387 /*********************************************************************
388 * _set_sbh_threshold (MSVCRT.@)
390 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
392 if(threshold > 1016)
393 return 0;
394 else
395 MSVCRT_sbh_threshold = threshold;
396 return 1;
399 /*********************************************************************
400 * _aligned_free (MSVCRT.@)
402 void CDECL _aligned_free(void *memblock)
404 TRACE("(%p)\n", memblock);
406 if (memblock)
408 void **saved = SAVED_PTR(memblock);
409 MSVCRT_free(*saved);
413 /*********************************************************************
414 * _aligned_offset_malloc (MSVCRT.@)
416 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
418 void *memblock, *temp, **saved;
419 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
421 /* alignment must be a power of 2 */
422 if ((alignment & (alignment - 1)) != 0)
424 *MSVCRT__errno() = MSVCRT_EINVAL;
425 return NULL;
428 /* offset must be less than size */
429 if (offset && offset >= size)
431 *MSVCRT__errno() = MSVCRT_EINVAL;
432 return NULL;
435 /* don't align to less than void pointer size */
436 if (alignment < sizeof(void *))
437 alignment = sizeof(void *);
439 /* allocate enough space for void pointer and alignment */
440 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
442 if (!temp)
443 return NULL;
445 /* adjust pointer for proper alignment and offset */
446 memblock = ALIGN_PTR(temp, alignment, offset);
448 /* Save the real allocation address below returned address */
449 /* so it can be found later to free. */
450 saved = SAVED_PTR(memblock);
451 *saved = temp;
453 return memblock;
456 /*********************************************************************
457 * _aligned_malloc (MSVCRT.@)
459 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
461 TRACE("(%lu, %lu)\n", size, alignment);
462 return _aligned_offset_malloc(size, alignment, 0);
465 /*********************************************************************
466 * _aligned_offset_realloc (MSVCRT.@)
468 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
469 MSVCRT_size_t alignment, MSVCRT_size_t offset)
471 void * temp, **saved;
472 MSVCRT_size_t old_padding, new_padding, old_size;
473 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
475 if (!memblock)
476 return _aligned_offset_malloc(size, alignment, offset);
478 /* alignment must be a power of 2 */
479 if ((alignment & (alignment - 1)) != 0)
481 *MSVCRT__errno() = MSVCRT_EINVAL;
482 return NULL;
485 /* offset must be less than size */
486 if (offset >= size)
488 *MSVCRT__errno() = MSVCRT_EINVAL;
489 return NULL;
492 if (size == 0)
494 _aligned_free(memblock);
495 return NULL;
498 /* don't align to less than void pointer size */
499 if (alignment < sizeof(void *))
500 alignment = sizeof(void *);
502 /* make sure alignment and offset didn't change */
503 saved = SAVED_PTR(memblock);
504 if (memblock != ALIGN_PTR(*saved, alignment, offset))
506 *MSVCRT__errno() = MSVCRT_EINVAL;
507 return NULL;
510 old_padding = (char *)memblock - (char *)*saved;
512 /* Get previous size of block */
513 old_size = _msize(*saved);
514 if (old_size == -1)
516 /* It seems this function was called with an invalid pointer. Bail out. */
517 return NULL;
520 /* Adjust old_size to get amount of actual data in old block. */
521 if (old_size < old_padding)
523 /* Shouldn't happen. Something's weird, so bail out. */
524 return NULL;
526 old_size -= old_padding;
528 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
530 if (!temp)
531 return NULL;
533 /* adjust pointer for proper alignment and offset */
534 memblock = ALIGN_PTR(temp, alignment, offset);
536 /* Save the real allocation address below returned address */
537 /* so it can be found later to free. */
538 saved = SAVED_PTR(memblock);
540 new_padding = (char *)memblock - (char *)temp;
543 Memory layout of old block is as follows:
544 +-------+---------------------+-+--------------------------+-----------+
545 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
546 +-------+---------------------+-+--------------------------+-----------+
547 ^ ^ ^
548 | | |
549 *saved saved memblock
551 Memory layout of new block is as follows:
552 +-------+-----------------------------+-+----------------------+-------+
553 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
554 +-------+-----------------------------+-+----------------------+-------+
555 ^ ^ ^
556 | | |
557 temp saved memblock
559 However, in the new block, actual data is still written as follows
560 (because it was copied by MSVCRT_realloc):
561 +-------+---------------------+--------------------------------+-------+
562 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
563 +-------+---------------------+--------------------------------+-------+
564 ^ ^ ^
565 | | |
566 temp saved memblock
568 Therefore, min(old_size,size) bytes of actual data have to be moved
569 from the offset they were at in the old block (temp + old_padding),
570 to the offset they have to be in the new block (temp + new_padding == memblock).
572 if (new_padding != old_padding)
573 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
575 *saved = temp;
577 return memblock;
580 /*********************************************************************
581 * _aligned_realloc (MSVCRT.@)
583 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
585 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
586 return _aligned_offset_realloc(memblock, size, alignment, 0);
589 /*********************************************************************
590 * memmove_s (MSVCRT.@)
592 int CDECL MSVCRT_memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
594 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
596 if(!count)
597 return 0;
599 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
600 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
601 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
603 memmove(dest, src, count);
604 return 0;
607 /*********************************************************************
608 * wmemmove_s (MSVCR100.@)
610 int CDECL wmemmove_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
611 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
613 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
615 if (!count)
616 return 0;
618 /* Native does not seem to conform to 6.7.1.2.3 in
619 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
620 * in that it does not zero the output buffer on constraint violation.
622 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
623 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
624 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) return MSVCRT_ERANGE;
626 memmove(dest, src, sizeof(MSVCRT_wchar_t)*count);
627 return 0;
630 /*********************************************************************
631 * memcpy_s (MSVCRT.@)
633 int CDECL MSVCRT_memcpy_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
635 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
637 if(!count)
638 return 0;
640 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
641 if (!MSVCRT_CHECK_PMT(src != NULL))
643 memset(dest, 0, numberOfElements);
644 return MSVCRT_EINVAL;
646 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE ))
648 memset(dest, 0, numberOfElements);
649 return MSVCRT_ERANGE;
652 memcpy(dest, src, count);
653 return 0;
656 /*********************************************************************
657 * wmemcpy_s (MSVCR100.@)
659 int CDECL wmemcpy_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
660 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
662 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
664 if (!count)
665 return 0;
667 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
669 if (!MSVCRT_CHECK_PMT(src != NULL)) {
670 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
671 return MSVCRT_EINVAL;
673 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) {
674 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
675 return MSVCRT_ERANGE;
678 memcpy(dest, src, sizeof(MSVCRT_wchar_t)*count);
679 return 0;
682 /*********************************************************************
683 * strncpy_s (MSVCRT.@)
685 int CDECL MSVCRT_strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
686 const char *src, MSVCRT_size_t count)
688 MSVCRT_size_t i, end;
690 TRACE("(%p %lu %s %lu)\n", dest, numberOfElements, debugstr_a(src), count);
692 if(!count) {
693 if(dest && numberOfElements)
694 *dest = 0;
695 return 0;
698 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
699 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
700 if (!MSVCRT_CHECK_PMT(numberOfElements != 0)) return MSVCRT_EINVAL;
702 if(count!=MSVCRT__TRUNCATE && count<numberOfElements)
703 end = count;
704 else
705 end = numberOfElements-1;
707 for(i=0; i<end && src[i]; i++)
708 dest[i] = src[i];
710 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
711 dest[i] = '\0';
712 return 0;
715 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL);
716 dest[0] = '\0';
717 return MSVCRT_EINVAL;
720 BOOL msvcrt_init_heap(void)
722 heap = HeapCreate(0, 0, 0);
723 return heap != NULL;
726 void msvcrt_destroy_heap(void)
728 HeapDestroy(heap);