d3dx9: Use parameter data for the first constant only in d3dx_set_shader_const_state().
[wine.git] / dlls / msvcrt / heap.c
blob0d6aee07f3643f3c24e8408e0415be71b4416af1
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 "config.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 _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))
42 #define SB_HEAP_ALIGN 16
44 static HANDLE heap, sb_heap;
46 typedef int (CDECL *MSVCRT_new_handler_func)(MSVCRT_size_t size);
48 static MSVCRT_new_handler_func MSVCRT_new_handler;
49 static int 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 MSVCRT_size_t MSVCRT_sbh_threshold = 0;
56 static void* msvcrt_heap_alloc(DWORD flags, MSVCRT_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, MSVCRT_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 MSVCRT_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 MSVCRT_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 * ??2@YAPAXI@Z (MSVCRT.@)
131 void* CDECL DECLSPEC_HOTPATCH MSVCRT_operator_new(MSVCRT_size_t size)
133 void *retval;
134 int freed;
135 MSVCRT_new_handler_func handler;
139 retval = msvcrt_heap_alloc(0, size);
140 if(retval)
142 TRACE("(%ld) returning %p\n", size, retval);
143 return retval;
146 handler = MSVCRT_new_handler;
147 if(handler)
148 freed = (*handler)(size);
149 else
150 freed = 0;
151 } while(freed);
153 TRACE("(%ld) out of memory\n", size);
154 #if _MSVCR_VER >= 80
155 throw_exception(EXCEPTION_BAD_ALLOC, 0, "bad allocation");
156 #endif
157 return NULL;
161 /*********************************************************************
162 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
164 void* CDECL MSVCRT_operator_new_dbg(MSVCRT_size_t size, int type, const char *file, int line)
166 return MSVCRT_operator_new( size );
170 /*********************************************************************
171 * ??3@YAXPAX@Z (MSVCRT.@)
173 void CDECL DECLSPEC_HOTPATCH MSVCRT_operator_delete(void *mem)
175 TRACE("(%p)\n", mem);
176 msvcrt_heap_free(mem);
180 /*********************************************************************
181 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
183 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
185 return MSVCRT_new_handler;
189 /*********************************************************************
190 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
192 int CDECL MSVCRT__query_new_mode(void)
194 return MSVCRT_new_mode;
197 /*********************************************************************
198 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
200 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
202 MSVCRT_new_handler_func old_handler;
203 LOCK_HEAP;
204 old_handler = MSVCRT_new_handler;
205 MSVCRT_new_handler = func;
206 UNLOCK_HEAP;
207 return old_handler;
210 /*********************************************************************
211 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
213 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
215 TRACE("(%p)\n",func);
216 MSVCRT__set_new_handler(NULL);
217 return NULL;
220 /*********************************************************************
221 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
223 int CDECL MSVCRT__set_new_mode(int mode)
225 int old_mode;
226 LOCK_HEAP;
227 old_mode = MSVCRT_new_mode;
228 MSVCRT_new_mode = mode;
229 UNLOCK_HEAP;
230 return old_mode;
233 /*********************************************************************
234 * _callnewh (MSVCRT.@)
236 int CDECL _callnewh(MSVCRT_size_t size)
238 int ret = 0;
239 MSVCRT_new_handler_func handler = MSVCRT_new_handler;
240 if(handler)
241 ret = (*handler)(size) ? 1 : 0;
242 return ret;
245 /*********************************************************************
246 * _expand (MSVCRT.@)
248 void* CDECL _expand(void* mem, MSVCRT_size_t size)
250 return msvcrt_heap_realloc(HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
253 /*********************************************************************
254 * _heapchk (MSVCRT.@)
256 int CDECL _heapchk(void)
258 if (!HeapValidate(heap, 0, NULL) ||
259 (sb_heap && !HeapValidate(sb_heap, 0, NULL)))
261 msvcrt_set_errno(GetLastError());
262 return MSVCRT__HEAPBADNODE;
264 return MSVCRT__HEAPOK;
267 /*********************************************************************
268 * _heapmin (MSVCRT.@)
270 int CDECL _heapmin(void)
272 if (!HeapCompact( heap, 0 ) ||
273 (sb_heap && !HeapCompact( sb_heap, 0 )))
275 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
276 msvcrt_set_errno(GetLastError());
277 return -1;
279 return 0;
282 /*********************************************************************
283 * _heapwalk (MSVCRT.@)
285 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
287 PROCESS_HEAP_ENTRY phe;
289 if (sb_heap)
290 FIXME("small blocks heap not supported\n");
292 LOCK_HEAP;
293 phe.lpData = next->_pentry;
294 phe.cbData = next->_size;
295 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
297 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
298 !HeapValidate( heap, 0, phe.lpData ))
300 UNLOCK_HEAP;
301 msvcrt_set_errno(GetLastError());
302 return MSVCRT__HEAPBADNODE;
307 if (!HeapWalk( heap, &phe ))
309 UNLOCK_HEAP;
310 if (GetLastError() == ERROR_NO_MORE_ITEMS)
311 return MSVCRT__HEAPEND;
312 msvcrt_set_errno(GetLastError());
313 if (!phe.lpData)
314 return MSVCRT__HEAPBADBEGIN;
315 return MSVCRT__HEAPBADNODE;
317 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
319 UNLOCK_HEAP;
320 next->_pentry = phe.lpData;
321 next->_size = phe.cbData;
322 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
323 return MSVCRT__HEAPOK;
326 /*********************************************************************
327 * _heapset (MSVCRT.@)
329 int CDECL _heapset(unsigned int value)
331 int retval;
332 struct MSVCRT__heapinfo heap;
334 memset( &heap, 0, sizeof(heap) );
335 LOCK_HEAP;
336 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
338 if (heap._useflag == MSVCRT__FREEENTRY)
339 memset(heap._pentry, value, heap._size);
341 UNLOCK_HEAP;
342 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
345 /*********************************************************************
346 * _heapadd (MSVCRT.@)
348 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
350 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
351 *MSVCRT__errno() = MSVCRT_ENOSYS;
352 return -1;
355 /*********************************************************************
356 * _get_heap_handle (MSVCRT.@)
358 MSVCRT_intptr_t CDECL _get_heap_handle(void)
360 return (MSVCRT_intptr_t)heap;
363 /*********************************************************************
364 * _msize (MSVCRT.@)
366 MSVCRT_size_t CDECL _msize(void* mem)
368 MSVCRT_size_t size = msvcrt_heap_size(mem);
369 if (size == ~(MSVCRT_size_t)0)
371 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
372 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
374 return size;
377 #if _MSVCR_VER>=80
378 /*********************************************************************
379 * _aligned_msize (MSVCR80.@)
381 size_t CDECL _aligned_msize(void *p, MSVCRT_size_t alignment, MSVCRT_size_t offset)
383 void **alloc_ptr;
385 if(!MSVCRT_CHECK_PMT(p)) return -1;
387 if(alignment < sizeof(void*))
388 alignment = sizeof(void*);
390 alloc_ptr = SAVED_PTR(p);
391 return _msize(*alloc_ptr)-alignment-sizeof(void*);
393 #endif
395 /*********************************************************************
396 * calloc (MSVCRT.@)
398 void* CDECL DECLSPEC_HOTPATCH MSVCRT_calloc(MSVCRT_size_t count, MSVCRT_size_t size)
400 MSVCRT_size_t bytes = count*size;
402 if (size && bytes / size != count)
404 *MSVCRT__errno() = MSVCRT_ENOMEM;
405 return NULL;
408 return msvcrt_heap_alloc(HEAP_ZERO_MEMORY, bytes);
411 #if _MSVCR_VER>=140
412 /*********************************************************************
413 * _calloc_base (UCRTBASE.@)
415 void* CDECL _calloc_base(MSVCRT_size_t count, MSVCRT_size_t size)
417 return MSVCRT_calloc(count, size);
419 #endif
421 /*********************************************************************
422 * free (MSVCRT.@)
424 void CDECL DECLSPEC_HOTPATCH MSVCRT_free(void* ptr)
426 msvcrt_heap_free(ptr);
429 #if _MSVCR_VER>=140
430 /*********************************************************************
431 * _free_base (UCRTBASE.@)
433 void CDECL _free_base(void* ptr)
435 msvcrt_heap_free(ptr);
437 #endif
439 /*********************************************************************
440 * malloc (MSVCRT.@)
442 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
444 void *ret = msvcrt_heap_alloc(0, size);
445 if (!ret)
446 *MSVCRT__errno() = MSVCRT_ENOMEM;
447 return ret;
450 #if _MSVCR_VER>=140
451 /*********************************************************************
452 * _malloc_base (UCRTBASE.@)
454 void* CDECL _malloc_base(MSVCRT_size_t size)
456 return MSVCRT_malloc(size);
458 #endif
460 /*********************************************************************
461 * realloc (MSVCRT.@)
463 void* CDECL DECLSPEC_HOTPATCH MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
465 if (!ptr) return MSVCRT_malloc(size);
466 if (size) return msvcrt_heap_realloc(0, ptr, size);
467 MSVCRT_free(ptr);
468 return NULL;
471 #if _MSVCR_VER>=140
472 /*********************************************************************
473 * _realloc_base (UCRTBASE.@)
475 void* CDECL _realloc_base(void* ptr, MSVCRT_size_t size)
477 return MSVCRT_realloc(ptr, size);
479 #endif
481 #if _MSVCR_VER>=80
482 /*********************************************************************
483 * _recalloc (MSVCR80.@)
485 void* CDECL _recalloc(void *mem, MSVCRT_size_t num, MSVCRT_size_t size)
487 MSVCRT_size_t old_size;
488 void *ret;
490 if(!mem)
491 return MSVCRT_calloc(num, size);
493 size = num*size;
494 old_size = _msize(mem);
496 ret = MSVCRT_realloc(mem, size);
497 if(!ret) {
498 *MSVCRT__errno() = MSVCRT_ENOMEM;
499 return NULL;
502 if(size>old_size)
503 memset((BYTE*)ret+old_size, 0, size-old_size);
504 return ret;
506 #endif
508 /*********************************************************************
509 * __p__amblksiz (MSVCRT.@)
511 unsigned int* CDECL __p__amblksiz(void)
513 return &MSVCRT_amblksiz;
516 /*********************************************************************
517 * _get_sbh_threshold (MSVCRT.@)
519 MSVCRT_size_t CDECL _get_sbh_threshold(void)
521 return MSVCRT_sbh_threshold;
524 /*********************************************************************
525 * _set_sbh_threshold (MSVCRT.@)
527 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
529 #ifdef _WIN64
530 return 0;
531 #else
532 if(threshold > 1016)
533 return 0;
535 if(!sb_heap)
537 sb_heap = HeapCreate(0, 0, 0);
538 if(!sb_heap)
539 return 0;
542 MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
543 return 1;
544 #endif
547 /*********************************************************************
548 * _aligned_free (MSVCRT.@)
550 void CDECL _aligned_free(void *memblock)
552 TRACE("(%p)\n", memblock);
554 if (memblock)
556 void **saved = SAVED_PTR(memblock);
557 MSVCRT_free(*saved);
561 /*********************************************************************
562 * _aligned_offset_malloc (MSVCRT.@)
564 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
566 void *memblock, *temp, **saved;
567 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
569 /* alignment must be a power of 2 */
570 if ((alignment & (alignment - 1)) != 0)
572 *MSVCRT__errno() = MSVCRT_EINVAL;
573 return NULL;
576 /* offset must be less than size */
577 if (offset && offset >= size)
579 *MSVCRT__errno() = MSVCRT_EINVAL;
580 return NULL;
583 /* don't align to less than void pointer size */
584 if (alignment < sizeof(void *))
585 alignment = sizeof(void *);
587 /* allocate enough space for void pointer and alignment */
588 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
590 if (!temp)
591 return NULL;
593 /* adjust pointer for proper alignment and offset */
594 memblock = ALIGN_PTR(temp, alignment, offset);
596 /* Save the real allocation address below returned address */
597 /* so it can be found later to free. */
598 saved = SAVED_PTR(memblock);
599 *saved = temp;
601 return memblock;
604 /*********************************************************************
605 * _aligned_malloc (MSVCRT.@)
607 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
609 TRACE("(%lu, %lu)\n", size, alignment);
610 return _aligned_offset_malloc(size, alignment, 0);
613 /*********************************************************************
614 * _aligned_offset_realloc (MSVCRT.@)
616 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
617 MSVCRT_size_t alignment, MSVCRT_size_t offset)
619 void * temp, **saved;
620 MSVCRT_size_t old_padding, new_padding, old_size;
621 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
623 if (!memblock)
624 return _aligned_offset_malloc(size, alignment, offset);
626 /* alignment must be a power of 2 */
627 if ((alignment & (alignment - 1)) != 0)
629 *MSVCRT__errno() = MSVCRT_EINVAL;
630 return NULL;
633 /* offset must be less than size */
634 if (offset >= size)
636 *MSVCRT__errno() = MSVCRT_EINVAL;
637 return NULL;
640 if (size == 0)
642 _aligned_free(memblock);
643 return NULL;
646 /* don't align to less than void pointer size */
647 if (alignment < sizeof(void *))
648 alignment = sizeof(void *);
650 /* make sure alignment and offset didn't change */
651 saved = SAVED_PTR(memblock);
652 if (memblock != ALIGN_PTR(*saved, alignment, offset))
654 *MSVCRT__errno() = MSVCRT_EINVAL;
655 return NULL;
658 old_padding = (char *)memblock - (char *)*saved;
660 /* Get previous size of block */
661 old_size = _msize(*saved);
662 if (old_size == -1)
664 /* It seems this function was called with an invalid pointer. Bail out. */
665 return NULL;
668 /* Adjust old_size to get amount of actual data in old block. */
669 if (old_size < old_padding)
671 /* Shouldn't happen. Something's weird, so bail out. */
672 return NULL;
674 old_size -= old_padding;
676 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
678 if (!temp)
679 return NULL;
681 /* adjust pointer for proper alignment and offset */
682 memblock = ALIGN_PTR(temp, alignment, offset);
684 /* Save the real allocation address below returned address */
685 /* so it can be found later to free. */
686 saved = SAVED_PTR(memblock);
688 new_padding = (char *)memblock - (char *)temp;
691 Memory layout of old block is as follows:
692 +-------+---------------------+-+--------------------------+-----------+
693 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
694 +-------+---------------------+-+--------------------------+-----------+
695 ^ ^ ^
696 | | |
697 *saved saved memblock
699 Memory layout of new block is as follows:
700 +-------+-----------------------------+-+----------------------+-------+
701 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
702 +-------+-----------------------------+-+----------------------+-------+
703 ^ ^ ^
704 | | |
705 temp saved memblock
707 However, in the new block, actual data is still written as follows
708 (because it was copied by MSVCRT_realloc):
709 +-------+---------------------+--------------------------------+-------+
710 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
711 +-------+---------------------+--------------------------------+-------+
712 ^ ^ ^
713 | | |
714 temp saved memblock
716 Therefore, min(old_size,size) bytes of actual data have to be moved
717 from the offset they were at in the old block (temp + old_padding),
718 to the offset they have to be in the new block (temp + new_padding == memblock).
720 if (new_padding != old_padding)
721 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
723 *saved = temp;
725 return memblock;
728 /*********************************************************************
729 * _aligned_realloc (MSVCRT.@)
731 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
733 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
734 return _aligned_offset_realloc(memblock, size, alignment, 0);
737 /*********************************************************************
738 * memmove_s (MSVCRT.@)
740 int CDECL MSVCRT_memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
742 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
744 if(!count)
745 return 0;
747 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
748 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
749 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
751 memmove(dest, src, count);
752 return 0;
755 #if _MSVCR_VER>=100
756 /*********************************************************************
757 * wmemmove_s (MSVCR100.@)
759 int CDECL wmemmove_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
760 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
762 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
764 if (!count)
765 return 0;
767 /* Native does not seem to conform to 6.7.1.2.3 in
768 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
769 * in that it does not zero the output buffer on constraint violation.
771 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
772 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
773 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) return MSVCRT_ERANGE;
775 memmove(dest, src, sizeof(MSVCRT_wchar_t)*count);
776 return 0;
778 #endif
780 /*********************************************************************
781 * memcpy_s (MSVCRT.@)
783 int CDECL MSVCRT_memcpy_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
785 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
787 if(!count)
788 return 0;
790 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
791 if (!MSVCRT_CHECK_PMT(src != NULL))
793 memset(dest, 0, numberOfElements);
794 return MSVCRT_EINVAL;
796 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE ))
798 memset(dest, 0, numberOfElements);
799 return MSVCRT_ERANGE;
802 memmove(dest, src, count);
803 return 0;
806 #if _MSVCR_VER>=100
807 /*********************************************************************
808 * wmemcpy_s (MSVCR100.@)
810 int CDECL wmemcpy_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
811 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
813 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
815 if (!count)
816 return 0;
818 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
820 if (!MSVCRT_CHECK_PMT(src != NULL)) {
821 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
822 return MSVCRT_EINVAL;
824 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) {
825 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
826 return MSVCRT_ERANGE;
829 memmove(dest, src, sizeof(MSVCRT_wchar_t)*count);
830 return 0;
832 #endif
834 /*********************************************************************
835 * strncpy_s (MSVCRT.@)
837 int CDECL MSVCRT_strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
838 const char *src, MSVCRT_size_t count)
840 MSVCRT_size_t i, end;
842 TRACE("(%p %lu %s %lu)\n", dest, numberOfElements, debugstr_a(src), count);
844 if(!count) {
845 if(dest && numberOfElements)
846 *dest = 0;
847 return 0;
850 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
851 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
852 if (!MSVCRT_CHECK_PMT(numberOfElements != 0)) return MSVCRT_EINVAL;
854 if(count!=MSVCRT__TRUNCATE && count<numberOfElements)
855 end = count;
856 else
857 end = numberOfElements-1;
859 for(i=0; i<end && src[i]; i++)
860 dest[i] = src[i];
862 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
863 dest[i] = '\0';
864 return 0;
867 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL);
868 dest[0] = '\0';
869 return MSVCRT_EINVAL;
872 BOOL msvcrt_init_heap(void)
874 heap = HeapCreate(0, 0, 0);
875 return heap != NULL;
878 void msvcrt_destroy_heap(void)
880 HeapDestroy(heap);
881 if(sb_heap)
882 HeapDestroy(sb_heap);