dmime: Implement band track IDirectMusicTrack_Play.
[wine.git] / dlls / msvcrt / heap.c
blobbf06c37e2c59e462c8a095e2b05689754a0a7945
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 <malloc.h>
25 #include "msvcrt.h"
26 #include "mtdll.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31 /* MT */
32 #define LOCK_HEAP _lock( _HEAP_LOCK )
33 #define UNLOCK_HEAP _unlock( _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))
42 #define SB_HEAP_ALIGN 16
44 static HANDLE heap, sb_heap;
46 typedef int (CDECL *MSVCRT_new_handler_func)(size_t size);
48 static MSVCRT_new_handler_func MSVCRT_new_handler;
49 static LONG MSVCRT_new_mode;
51 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
52 static unsigned int MSVCRT_amblksiz = 16;
53 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
54 static size_t MSVCRT_sbh_threshold = 0;
56 static void* msvcrt_heap_alloc(DWORD flags, size_t size)
58 if(size < MSVCRT_sbh_threshold)
60 void *memblock, *temp, **saved;
62 temp = HeapAlloc(sb_heap, flags, size+sizeof(void*)+SB_HEAP_ALIGN);
63 if(!temp) return NULL;
65 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
66 saved = SAVED_PTR(memblock);
67 *saved = temp;
68 return memblock;
71 return HeapAlloc(heap, flags, size);
74 static void* msvcrt_heap_realloc(DWORD flags, void *ptr, size_t size)
76 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
78 /* TODO: move data to normal heap if it exceeds sbh_threshold limit */
79 void *memblock, *temp, **saved;
80 size_t old_padding, new_padding, old_size;
82 saved = SAVED_PTR(ptr);
83 old_padding = (char*)ptr - (char*)*saved;
84 old_size = HeapSize(sb_heap, 0, *saved);
85 if(old_size == -1)
86 return NULL;
87 old_size -= old_padding;
89 temp = HeapReAlloc(sb_heap, flags, *saved, size+sizeof(void*)+SB_HEAP_ALIGN);
90 if(!temp) return NULL;
92 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
93 saved = SAVED_PTR(memblock);
94 new_padding = (char*)memblock - (char*)temp;
96 if(new_padding != old_padding)
97 memmove(memblock, (char*)temp+old_padding, old_size>size ? size : old_size);
99 *saved = temp;
100 return memblock;
103 return HeapReAlloc(heap, flags, ptr, size);
106 static BOOL msvcrt_heap_free(void *ptr)
108 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
110 void **saved = SAVED_PTR(ptr);
111 return HeapFree(sb_heap, 0, *saved);
114 return HeapFree(heap, 0, ptr);
117 static size_t msvcrt_heap_size(void *ptr)
119 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
121 void **saved = SAVED_PTR(ptr);
122 return HeapSize(sb_heap, 0, *saved);
125 return HeapSize(heap, 0, ptr);
128 /*********************************************************************
129 * _callnewh (MSVCRT.@)
131 int CDECL _callnewh(size_t size)
133 int ret = 0;
134 MSVCRT_new_handler_func handler = MSVCRT_new_handler;
135 if(handler)
136 ret = (*handler)(size) ? 1 : 0;
137 return ret;
140 /*********************************************************************
141 * ??2@YAPAXI@Z (MSVCRT.@)
143 void* CDECL DECLSPEC_HOTPATCH operator_new(size_t size)
145 void *retval;
149 retval = msvcrt_heap_alloc(0, size);
150 if(retval)
152 TRACE("(%Iu) returning %p\n", size, retval);
153 return retval;
155 } while(_callnewh(size));
157 TRACE("(%Iu) out of memory\n", size);
158 #if _MSVCR_VER >= 80
159 throw_bad_alloc();
160 #endif
161 return NULL;
165 /*********************************************************************
166 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
168 void* CDECL operator_new_dbg(size_t size, int type, const char *file, int line)
170 return operator_new( size );
174 /*********************************************************************
175 * ??3@YAXPAX@Z (MSVCRT.@)
177 void CDECL DECLSPEC_HOTPATCH operator_delete(void *mem)
179 TRACE("(%p)\n", mem);
180 msvcrt_heap_free(mem);
184 /*********************************************************************
185 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
187 MSVCRT_new_handler_func CDECL _query_new_handler(void)
189 return MSVCRT_new_handler;
193 /*********************************************************************
194 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
196 int CDECL _query_new_mode(void)
198 return MSVCRT_new_mode;
201 /*********************************************************************
202 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
204 MSVCRT_new_handler_func CDECL _set_new_handler(MSVCRT_new_handler_func func)
206 MSVCRT_new_handler_func old_handler;
207 LOCK_HEAP;
208 old_handler = MSVCRT_new_handler;
209 MSVCRT_new_handler = func;
210 UNLOCK_HEAP;
211 return old_handler;
214 /*********************************************************************
215 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
217 MSVCRT_new_handler_func CDECL set_new_handler(void *func)
219 TRACE("(%p)\n",func);
220 _set_new_handler(NULL);
221 return NULL;
224 /*********************************************************************
225 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
227 int CDECL _set_new_mode(int mode)
229 if(!MSVCRT_CHECK_PMT(mode == 0 || mode == 1)) return -1;
230 return InterlockedExchange(&MSVCRT_new_mode, mode);
233 /*********************************************************************
234 * _expand (MSVCRT.@)
236 void* CDECL _expand(void* mem, size_t size)
238 return msvcrt_heap_realloc(HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
241 /*********************************************************************
242 * _heapchk (MSVCRT.@)
244 int CDECL _heapchk(void)
246 if (!HeapValidate(heap, 0, NULL) ||
247 (sb_heap && !HeapValidate(sb_heap, 0, NULL)))
249 msvcrt_set_errno(GetLastError());
250 return _HEAPBADNODE;
252 return _HEAPOK;
255 /*********************************************************************
256 * _heapmin (MSVCRT.@)
258 int CDECL _heapmin(void)
260 if (!HeapCompact( heap, 0 ) ||
261 (sb_heap && !HeapCompact( sb_heap, 0 )))
263 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
264 msvcrt_set_errno(GetLastError());
265 return -1;
267 return 0;
270 /*********************************************************************
271 * _heapwalk (MSVCRT.@)
273 int CDECL _heapwalk(_HEAPINFO *next)
275 PROCESS_HEAP_ENTRY phe;
277 if (sb_heap)
278 FIXME("small blocks heap not supported\n");
280 LOCK_HEAP;
281 phe.lpData = next->_pentry;
282 phe.cbData = next->_size;
283 phe.wFlags = next->_useflag == _USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
285 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
286 !HeapValidate( heap, 0, phe.lpData ))
288 UNLOCK_HEAP;
289 msvcrt_set_errno(GetLastError());
290 return _HEAPBADNODE;
295 if (!HeapWalk( heap, &phe ))
297 UNLOCK_HEAP;
298 if (GetLastError() == ERROR_NO_MORE_ITEMS)
299 return _HEAPEND;
300 msvcrt_set_errno(GetLastError());
301 if (!phe.lpData)
302 return _HEAPBADBEGIN;
303 return _HEAPBADNODE;
305 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
307 UNLOCK_HEAP;
308 next->_pentry = phe.lpData;
309 next->_size = phe.cbData;
310 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? _USEDENTRY : _FREEENTRY;
311 return _HEAPOK;
314 /*********************************************************************
315 * _heapset (MSVCRT.@)
317 int CDECL _heapset(unsigned int value)
319 int retval;
320 _HEAPINFO heap;
322 memset( &heap, 0, sizeof(heap) );
323 LOCK_HEAP;
324 while ((retval = _heapwalk(&heap)) == _HEAPOK)
326 if (heap._useflag == _FREEENTRY)
327 memset(heap._pentry, value, heap._size);
329 UNLOCK_HEAP;
330 return retval == _HEAPEND ? _HEAPOK : retval;
333 /*********************************************************************
334 * _heapadd (MSVCRT.@)
336 int CDECL _heapadd(void* mem, size_t size)
338 TRACE("(%p,%Iu) unsupported in Win32\n", mem,size);
339 *_errno() = ENOSYS;
340 return -1;
343 /*********************************************************************
344 * _get_heap_handle (MSVCRT.@)
346 intptr_t CDECL _get_heap_handle(void)
348 return (intptr_t)heap;
351 /*********************************************************************
352 * _msize (MSVCRT.@)
354 size_t CDECL _msize(void* mem)
356 size_t size = msvcrt_heap_size(mem);
357 if (size == ~(size_t)0)
359 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
360 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
362 return size;
365 #if _MSVCR_VER>=80
366 /*********************************************************************
367 * _aligned_msize (MSVCR80.@)
369 size_t CDECL _aligned_msize(void *p, size_t alignment, size_t offset)
371 void **alloc_ptr;
373 if(!MSVCRT_CHECK_PMT(p)) return -1;
375 if(alignment < sizeof(void*))
376 alignment = sizeof(void*);
378 alloc_ptr = SAVED_PTR(p);
379 return _msize(*alloc_ptr)-alignment-sizeof(void*);
381 #endif
383 /*********************************************************************
384 * calloc (MSVCRT.@)
386 void* CDECL DECLSPEC_HOTPATCH calloc(size_t count, size_t size)
388 size_t bytes = count*size;
390 if (size && bytes / size != count)
392 *_errno() = ENOMEM;
393 return NULL;
396 return msvcrt_heap_alloc(HEAP_ZERO_MEMORY, bytes);
399 #if _MSVCR_VER>=140
400 /*********************************************************************
401 * _calloc_base (UCRTBASE.@)
403 void* CDECL _calloc_base(size_t count, size_t size)
405 return calloc(count, size);
407 #endif
409 /*********************************************************************
410 * free (MSVCRT.@)
412 void CDECL DECLSPEC_HOTPATCH free(void* ptr)
414 msvcrt_heap_free(ptr);
417 #if _MSVCR_VER>=140
418 /*********************************************************************
419 * _free_base (UCRTBASE.@)
421 void CDECL _free_base(void* ptr)
423 msvcrt_heap_free(ptr);
425 #endif
427 /*********************************************************************
428 * malloc (MSVCRT.@)
430 void* CDECL malloc(size_t size)
432 void *ret;
436 ret = msvcrt_heap_alloc(0, size);
437 if (ret || !MSVCRT_new_mode)
438 break;
439 } while(_callnewh(size));
441 if (!ret)
442 *_errno() = ENOMEM;
443 return ret;
446 #if _MSVCR_VER>=140
447 /*********************************************************************
448 * _malloc_base (UCRTBASE.@)
450 void* CDECL _malloc_base(size_t size)
452 return malloc(size);
454 #endif
456 /*********************************************************************
457 * realloc (MSVCRT.@)
459 void* CDECL DECLSPEC_HOTPATCH realloc(void* ptr, size_t size)
461 if (!ptr) return malloc(size);
462 if (size) return msvcrt_heap_realloc(0, ptr, size);
463 free(ptr);
464 return NULL;
467 #if _MSVCR_VER>=140
468 /*********************************************************************
469 * _realloc_base (UCRTBASE.@)
471 void* CDECL _realloc_base(void* ptr, size_t size)
473 return realloc(ptr, size);
475 #endif
477 #if _MSVCR_VER>=80
478 /*********************************************************************
479 * _recalloc (MSVCR80.@)
481 void* CDECL _recalloc(void *mem, size_t num, size_t size)
483 size_t old_size;
484 void *ret;
486 if(!mem)
487 return calloc(num, size);
489 size = num*size;
490 old_size = _msize(mem);
492 ret = realloc(mem, size);
493 if(!ret) {
494 *_errno() = ENOMEM;
495 return NULL;
498 if(size>old_size)
499 memset((BYTE*)ret+old_size, 0, size-old_size);
500 return ret;
502 #endif
504 /*********************************************************************
505 * __p__amblksiz (MSVCRT.@)
507 unsigned int* CDECL __p__amblksiz(void)
509 return &MSVCRT_amblksiz;
512 /*********************************************************************
513 * _get_sbh_threshold (MSVCRT.@)
515 size_t CDECL _get_sbh_threshold(void)
517 return MSVCRT_sbh_threshold;
520 /*********************************************************************
521 * _set_sbh_threshold (MSVCRT.@)
523 int CDECL _set_sbh_threshold(size_t threshold)
525 #ifdef _WIN64
526 return 0;
527 #else
528 if(threshold > 1016)
529 return 0;
531 if(!sb_heap)
533 sb_heap = HeapCreate(0, 0, 0);
534 if(!sb_heap)
535 return 0;
538 MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
539 return 1;
540 #endif
543 /*********************************************************************
544 * _aligned_free (MSVCRT.@)
546 void CDECL _aligned_free(void *memblock)
548 TRACE("(%p)\n", memblock);
550 if (memblock)
552 void **saved = SAVED_PTR(memblock);
553 free(*saved);
557 /*********************************************************************
558 * _aligned_offset_malloc (MSVCRT.@)
560 void * CDECL _aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
562 void *memblock, *temp, **saved;
563 TRACE("(%Iu, %Iu, %Iu)\n", size, alignment, offset);
565 /* alignment must be a power of 2 */
566 if ((alignment & (alignment - 1)) != 0)
568 *_errno() = EINVAL;
569 return NULL;
572 /* offset must be less than size */
573 if (offset && offset >= size)
575 *_errno() = EINVAL;
576 return NULL;
579 /* don't align to less than void pointer size */
580 if (alignment < sizeof(void *))
581 alignment = sizeof(void *);
583 /* allocate enough space for void pointer and alignment */
584 temp = malloc(size + alignment + sizeof(void *));
586 if (!temp)
587 return NULL;
589 /* adjust pointer for proper alignment and offset */
590 memblock = ALIGN_PTR(temp, alignment, offset);
592 /* Save the real allocation address below returned address */
593 /* so it can be found later to free. */
594 saved = SAVED_PTR(memblock);
595 *saved = temp;
597 return memblock;
600 /*********************************************************************
601 * _aligned_malloc (MSVCRT.@)
603 void * CDECL _aligned_malloc(size_t size, size_t alignment)
605 TRACE("(%Iu, %Iu)\n", size, alignment);
606 return _aligned_offset_malloc(size, alignment, 0);
609 /*********************************************************************
610 * _aligned_offset_realloc (MSVCRT.@)
612 void * CDECL _aligned_offset_realloc(void *memblock, size_t size,
613 size_t alignment, size_t offset)
615 void * temp, **saved;
616 size_t old_padding, new_padding, old_size;
617 TRACE("(%p, %Iu, %Iu, %Iu)\n", memblock, size, alignment, offset);
619 if (!memblock)
620 return _aligned_offset_malloc(size, alignment, offset);
622 /* alignment must be a power of 2 */
623 if ((alignment & (alignment - 1)) != 0)
625 *_errno() = EINVAL;
626 return NULL;
629 /* offset must be less than size */
630 if (offset >= size)
632 *_errno() = EINVAL;
633 return NULL;
636 if (size == 0)
638 _aligned_free(memblock);
639 return NULL;
642 /* don't align to less than void pointer size */
643 if (alignment < sizeof(void *))
644 alignment = sizeof(void *);
646 /* make sure alignment and offset didn't change */
647 saved = SAVED_PTR(memblock);
648 if (memblock != ALIGN_PTR(*saved, alignment, offset))
650 *_errno() = EINVAL;
651 return NULL;
654 old_padding = (char *)memblock - (char *)*saved;
656 /* Get previous size of block */
657 old_size = _msize(*saved);
658 if (old_size == -1)
660 /* It seems this function was called with an invalid pointer. Bail out. */
661 return NULL;
664 /* Adjust old_size to get amount of actual data in old block. */
665 if (old_size < old_padding)
667 /* Shouldn't happen. Something's weird, so bail out. */
668 return NULL;
670 old_size -= old_padding;
672 temp = realloc(*saved, size + alignment + sizeof(void *));
674 if (!temp)
675 return NULL;
677 /* adjust pointer for proper alignment and offset */
678 memblock = ALIGN_PTR(temp, alignment, offset);
680 /* Save the real allocation address below returned address */
681 /* so it can be found later to free. */
682 saved = SAVED_PTR(memblock);
684 new_padding = (char *)memblock - (char *)temp;
687 Memory layout of old block is as follows:
688 +-------+---------------------+-+--------------------------+-----------+
689 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
690 +-------+---------------------+-+--------------------------+-----------+
691 ^ ^ ^
692 | | |
693 *saved saved memblock
695 Memory layout of new block is as follows:
696 +-------+-----------------------------+-+----------------------+-------+
697 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
698 +-------+-----------------------------+-+----------------------+-------+
699 ^ ^ ^
700 | | |
701 temp saved memblock
703 However, in the new block, actual data is still written as follows
704 (because it was copied by realloc):
705 +-------+---------------------+--------------------------------+-------+
706 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
707 +-------+---------------------+--------------------------------+-------+
708 ^ ^ ^
709 | | |
710 temp saved memblock
712 Therefore, min(old_size,size) bytes of actual data have to be moved
713 from the offset they were at in the old block (temp + old_padding),
714 to the offset they have to be in the new block (temp + new_padding == memblock).
716 if (new_padding != old_padding)
717 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
719 *saved = temp;
721 return memblock;
724 /*********************************************************************
725 * _aligned_realloc (MSVCRT.@)
727 void * CDECL _aligned_realloc(void *memblock, size_t size, size_t alignment)
729 TRACE("(%p, %Iu, %Iu)\n", memblock, size, alignment);
730 return _aligned_offset_realloc(memblock, size, alignment, 0);
733 /*********************************************************************
734 * memmove_s (MSVCRT.@)
736 int CDECL memmove_s(void *dest, size_t numberOfElements, const void *src, size_t count)
738 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
740 if(!count)
741 return 0;
743 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
744 if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
745 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, ERANGE )) return ERANGE;
747 memmove(dest, src, count);
748 return 0;
751 #if _MSVCR_VER>=100
752 /*********************************************************************
753 * wmemmove_s (MSVCR100.@)
755 int CDECL wmemmove_s(wchar_t *dest, size_t numberOfElements,
756 const wchar_t *src, size_t count)
758 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
760 if (!count)
761 return 0;
763 /* Native does not seem to conform to 6.7.1.2.3 in
764 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
765 * in that it does not zero the output buffer on constraint violation.
767 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
768 if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
769 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, ERANGE)) return ERANGE;
771 memmove(dest, src, sizeof(wchar_t)*count);
772 return 0;
774 #endif
776 /*********************************************************************
777 * memcpy_s (MSVCRT.@)
779 int CDECL memcpy_s(void *dest, size_t numberOfElements, const void *src, size_t count)
781 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
783 if(!count)
784 return 0;
786 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
787 if (!MSVCRT_CHECK_PMT(src != NULL))
789 memset(dest, 0, numberOfElements);
790 return EINVAL;
792 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, ERANGE ))
794 memset(dest, 0, numberOfElements);
795 return ERANGE;
798 memmove(dest, src, count);
799 return 0;
802 #if _MSVCR_VER>=100
803 /*********************************************************************
804 * wmemcpy_s (MSVCR100.@)
806 int CDECL wmemcpy_s(wchar_t *dest, size_t numberOfElements,
807 const wchar_t *src, size_t count)
809 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
811 if (!count)
812 return 0;
814 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
816 if (!MSVCRT_CHECK_PMT(src != NULL)) {
817 memset(dest, 0, numberOfElements*sizeof(wchar_t));
818 return EINVAL;
820 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, ERANGE)) {
821 memset(dest, 0, numberOfElements*sizeof(wchar_t));
822 return ERANGE;
825 memmove(dest, src, sizeof(wchar_t)*count);
826 return 0;
828 #endif
830 BOOL msvcrt_init_heap(void)
832 heap = HeapCreate(0, 0, 0);
833 return heap != NULL;
836 void msvcrt_destroy_heap(void)
838 HeapDestroy(heap);
839 if(sb_heap)
840 HeapDestroy(sb_heap);