webservices/tests: Add a test to show that the reader converts text to UTF-8.
[wine.git] / dlls / msvcrt / heap.c
blobed412533046502733658102357a9481094319442
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 #define SB_HEAP_ALIGN 16
43 static HANDLE heap, sb_heap;
45 typedef int (CDECL *MSVCRT_new_handler_func)(MSVCRT_size_t size);
47 static MSVCRT_new_handler_func MSVCRT_new_handler;
48 static int MSVCRT_new_mode;
50 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
51 static unsigned int MSVCRT_amblksiz = 16;
52 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
53 static MSVCRT_size_t MSVCRT_sbh_threshold = 0;
55 static void* msvcrt_heap_alloc(DWORD flags, MSVCRT_size_t size)
57 if(size < MSVCRT_sbh_threshold)
59 void *memblock, *temp, **saved;
61 temp = HeapAlloc(sb_heap, flags, size+sizeof(void*)+SB_HEAP_ALIGN);
62 if(!temp) return NULL;
64 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
65 saved = SAVED_PTR(memblock);
66 *saved = temp;
67 return memblock;
70 return HeapAlloc(heap, flags, size);
73 static void* msvcrt_heap_realloc(DWORD flags, void *ptr, MSVCRT_size_t size)
75 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
77 /* TODO: move data to normal heap if it exceeds sbh_threshold limit */
78 void *memblock, *temp, **saved;
79 MSVCRT_size_t old_padding, new_padding, old_size;
81 saved = SAVED_PTR(ptr);
82 old_padding = (char*)ptr - (char*)*saved;
83 old_size = HeapSize(sb_heap, 0, *saved);
84 if(old_size == -1)
85 return NULL;
86 old_size -= old_padding;
88 temp = HeapReAlloc(sb_heap, flags, *saved, size+sizeof(void*)+SB_HEAP_ALIGN);
89 if(!temp) return NULL;
91 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
92 saved = SAVED_PTR(memblock);
93 new_padding = (char*)memblock - (char*)temp;
95 if(new_padding != old_padding)
96 memmove(memblock, (char*)temp+old_padding, old_size>size ? size : old_size);
98 *saved = temp;
99 return memblock;
102 return HeapReAlloc(heap, flags, ptr, size);
105 static BOOL msvcrt_heap_free(void *ptr)
107 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
109 void **saved = SAVED_PTR(ptr);
110 return HeapFree(sb_heap, 0, *saved);
113 return HeapFree(heap, 0, ptr);
116 static MSVCRT_size_t msvcrt_heap_size(void *ptr)
118 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
120 void **saved = SAVED_PTR(ptr);
121 return HeapSize(sb_heap, 0, *saved);
124 return HeapSize(heap, 0, ptr);
127 /*********************************************************************
128 * ??2@YAPAXI@Z (MSVCRT.@)
130 void* CDECL MSVCRT_operator_new(MSVCRT_size_t size)
132 void *retval;
133 int freed;
134 MSVCRT_new_handler_func handler;
138 retval = msvcrt_heap_alloc(0, size);
139 if(retval)
141 TRACE("(%ld) returning %p\n", size, retval);
142 return retval;
145 handler = MSVCRT_new_handler;
146 if(handler)
147 freed = (*handler)(size);
148 else
149 freed = 0;
150 } while(freed);
152 TRACE("(%ld) out of memory\n", size);
153 #if _MSVCR_VER >= 80
154 throw_exception(EXCEPTION_BAD_ALLOC, 0, "bad allocation");
155 #endif
156 return NULL;
160 /*********************************************************************
161 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
163 void* CDECL MSVCRT_operator_new_dbg(MSVCRT_size_t size, int type, const char *file, int line)
165 return MSVCRT_operator_new( size );
169 /*********************************************************************
170 * ??3@YAXPAX@Z (MSVCRT.@)
172 void CDECL MSVCRT_operator_delete(void *mem)
174 TRACE("(%p)\n", mem);
175 msvcrt_heap_free(mem);
179 /*********************************************************************
180 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
182 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
184 return MSVCRT_new_handler;
188 /*********************************************************************
189 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
191 int CDECL MSVCRT__query_new_mode(void)
193 return MSVCRT_new_mode;
196 /*********************************************************************
197 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
199 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
201 MSVCRT_new_handler_func old_handler;
202 LOCK_HEAP;
203 old_handler = MSVCRT_new_handler;
204 MSVCRT_new_handler = func;
205 UNLOCK_HEAP;
206 return old_handler;
209 /*********************************************************************
210 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
212 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
214 TRACE("(%p)\n",func);
215 MSVCRT__set_new_handler(NULL);
216 return NULL;
219 /*********************************************************************
220 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
222 int CDECL MSVCRT__set_new_mode(int mode)
224 int old_mode;
225 LOCK_HEAP;
226 old_mode = MSVCRT_new_mode;
227 MSVCRT_new_mode = mode;
228 UNLOCK_HEAP;
229 return old_mode;
232 /*********************************************************************
233 * _callnewh (MSVCRT.@)
235 int CDECL _callnewh(MSVCRT_size_t size)
237 int ret = 0;
238 MSVCRT_new_handler_func handler = MSVCRT_new_handler;
239 if(handler)
240 ret = (*handler)(size) ? 1 : 0;
241 return ret;
244 /*********************************************************************
245 * _expand (MSVCRT.@)
247 void* CDECL _expand(void* mem, MSVCRT_size_t size)
249 return msvcrt_heap_realloc(HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
252 /*********************************************************************
253 * _heapchk (MSVCRT.@)
255 int CDECL _heapchk(void)
257 if (!HeapValidate(heap, 0, NULL) ||
258 (sb_heap && !HeapValidate(sb_heap, 0, NULL)))
260 msvcrt_set_errno(GetLastError());
261 return MSVCRT__HEAPBADNODE;
263 return MSVCRT__HEAPOK;
266 /*********************************************************************
267 * _heapmin (MSVCRT.@)
269 int CDECL _heapmin(void)
271 if (!HeapCompact( heap, 0 ) ||
272 (sb_heap && !HeapCompact( sb_heap, 0 )))
274 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
275 msvcrt_set_errno(GetLastError());
276 return -1;
278 return 0;
281 /*********************************************************************
282 * _heapwalk (MSVCRT.@)
284 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
286 PROCESS_HEAP_ENTRY phe;
288 if (sb_heap)
289 FIXME("small blocks heap not supported\n");
291 LOCK_HEAP;
292 phe.lpData = next->_pentry;
293 phe.cbData = next->_size;
294 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
296 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
297 !HeapValidate( heap, 0, phe.lpData ))
299 UNLOCK_HEAP;
300 msvcrt_set_errno(GetLastError());
301 return MSVCRT__HEAPBADNODE;
306 if (!HeapWalk( heap, &phe ))
308 UNLOCK_HEAP;
309 if (GetLastError() == ERROR_NO_MORE_ITEMS)
310 return MSVCRT__HEAPEND;
311 msvcrt_set_errno(GetLastError());
312 if (!phe.lpData)
313 return MSVCRT__HEAPBADBEGIN;
314 return MSVCRT__HEAPBADNODE;
316 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
318 UNLOCK_HEAP;
319 next->_pentry = phe.lpData;
320 next->_size = phe.cbData;
321 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
322 return MSVCRT__HEAPOK;
325 /*********************************************************************
326 * _heapset (MSVCRT.@)
328 int CDECL _heapset(unsigned int value)
330 int retval;
331 struct MSVCRT__heapinfo heap;
333 memset( &heap, 0, sizeof(heap) );
334 LOCK_HEAP;
335 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
337 if (heap._useflag == MSVCRT__FREEENTRY)
338 memset(heap._pentry, value, heap._size);
340 UNLOCK_HEAP;
341 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
344 /*********************************************************************
345 * _heapadd (MSVCRT.@)
347 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
349 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
350 *MSVCRT__errno() = MSVCRT_ENOSYS;
351 return -1;
354 /*********************************************************************
355 * _get_heap_handle (MSVCRT.@)
357 MSVCRT_intptr_t CDECL _get_heap_handle(void)
359 return (MSVCRT_intptr_t)heap;
362 /*********************************************************************
363 * _msize (MSVCRT.@)
365 MSVCRT_size_t CDECL _msize(void* mem)
367 MSVCRT_size_t size = msvcrt_heap_size(mem);
368 if (size == ~(MSVCRT_size_t)0)
370 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
371 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
373 return size;
376 /*********************************************************************
377 * _aligned_msize (MSVCR100.@)
379 size_t CDECL _aligned_msize(void *p, MSVCRT_size_t alignment, MSVCRT_size_t offset)
381 void **alloc_ptr;
383 if(!MSVCRT_CHECK_PMT(p)) return -1;
385 if(alignment < sizeof(void*))
386 alignment = sizeof(void*);
388 alloc_ptr = SAVED_PTR(p);
389 return _msize(*alloc_ptr)-alignment-sizeof(void*);
392 /*********************************************************************
393 * calloc (MSVCRT.@)
395 void* CDECL MSVCRT_calloc(MSVCRT_size_t count, MSVCRT_size_t size)
397 MSVCRT_size_t bytes = count*size;
399 if (size && bytes / size != count)
401 *MSVCRT__errno() = MSVCRT_ENOMEM;
402 return NULL;
405 return msvcrt_heap_alloc(HEAP_ZERO_MEMORY, bytes);
408 /*********************************************************************
409 * _calloc_base (UCRTBASE.@)
411 void* CDECL _calloc_base(MSVCRT_size_t count, MSVCRT_size_t size)
413 return MSVCRT_calloc(count, size);
416 /*********************************************************************
417 * free (MSVCRT.@)
419 void CDECL MSVCRT_free(void* ptr)
421 msvcrt_heap_free(ptr);
424 /*********************************************************************
425 * _free_base (UCRTBASE.@)
427 void CDECL _free_base(void* ptr)
429 msvcrt_heap_free(ptr);
432 /*********************************************************************
433 * malloc (MSVCRT.@)
435 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
437 void *ret = msvcrt_heap_alloc(0, size);
438 if (!ret)
439 *MSVCRT__errno() = MSVCRT_ENOMEM;
440 return ret;
443 /*********************************************************************
444 * _malloc_base (UCRTBASE.@)
446 void* CDECL _malloc_base(MSVCRT_size_t size)
448 return MSVCRT_malloc(size);
451 /*********************************************************************
452 * realloc (MSVCRT.@)
454 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
456 if (!ptr) return MSVCRT_malloc(size);
457 if (size) return msvcrt_heap_realloc(0, ptr, size);
458 MSVCRT_free(ptr);
459 return NULL;
462 /*********************************************************************
463 * _realloc_base (UCRTBASE.@)
465 void* CDECL _realloc_base(void* ptr, MSVCRT_size_t size)
467 return MSVCRT_realloc(ptr, size);
470 /*********************************************************************
471 * _recalloc (MSVCR100.@)
473 void* CDECL _recalloc(void *mem, MSVCRT_size_t num, MSVCRT_size_t size)
475 MSVCRT_size_t old_size;
476 void *ret;
478 if(!mem)
479 return MSVCRT_calloc(num, size);
481 size = num*size;
482 old_size = _msize(mem);
484 ret = MSVCRT_realloc(mem, size);
485 if(!ret) {
486 *MSVCRT__errno() = MSVCRT_ENOMEM;
487 return NULL;
490 if(size>old_size)
491 memset((BYTE*)ret+old_size, 0, size-old_size);
492 return ret;
495 /*********************************************************************
496 * __p__amblksiz (MSVCRT.@)
498 unsigned int* CDECL __p__amblksiz(void)
500 return &MSVCRT_amblksiz;
503 /*********************************************************************
504 * _get_sbh_threshold (MSVCRT.@)
506 MSVCRT_size_t CDECL _get_sbh_threshold(void)
508 return MSVCRT_sbh_threshold;
511 /*********************************************************************
512 * _set_sbh_threshold (MSVCRT.@)
514 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
516 #ifdef _WIN64
517 return 0;
518 #else
519 if(threshold > 1016)
520 return 0;
522 if(!sb_heap)
524 sb_heap = HeapCreate(0, 0, 0);
525 if(!sb_heap)
526 return 0;
529 MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
530 return 1;
531 #endif
534 /*********************************************************************
535 * _aligned_free (MSVCRT.@)
537 void CDECL _aligned_free(void *memblock)
539 TRACE("(%p)\n", memblock);
541 if (memblock)
543 void **saved = SAVED_PTR(memblock);
544 MSVCRT_free(*saved);
548 /*********************************************************************
549 * _aligned_offset_malloc (MSVCRT.@)
551 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
553 void *memblock, *temp, **saved;
554 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
556 /* alignment must be a power of 2 */
557 if ((alignment & (alignment - 1)) != 0)
559 *MSVCRT__errno() = MSVCRT_EINVAL;
560 return NULL;
563 /* offset must be less than size */
564 if (offset && offset >= size)
566 *MSVCRT__errno() = MSVCRT_EINVAL;
567 return NULL;
570 /* don't align to less than void pointer size */
571 if (alignment < sizeof(void *))
572 alignment = sizeof(void *);
574 /* allocate enough space for void pointer and alignment */
575 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
577 if (!temp)
578 return NULL;
580 /* adjust pointer for proper alignment and offset */
581 memblock = ALIGN_PTR(temp, alignment, offset);
583 /* Save the real allocation address below returned address */
584 /* so it can be found later to free. */
585 saved = SAVED_PTR(memblock);
586 *saved = temp;
588 return memblock;
591 /*********************************************************************
592 * _aligned_malloc (MSVCRT.@)
594 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
596 TRACE("(%lu, %lu)\n", size, alignment);
597 return _aligned_offset_malloc(size, alignment, 0);
600 /*********************************************************************
601 * _aligned_offset_realloc (MSVCRT.@)
603 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
604 MSVCRT_size_t alignment, MSVCRT_size_t offset)
606 void * temp, **saved;
607 MSVCRT_size_t old_padding, new_padding, old_size;
608 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
610 if (!memblock)
611 return _aligned_offset_malloc(size, alignment, offset);
613 /* alignment must be a power of 2 */
614 if ((alignment & (alignment - 1)) != 0)
616 *MSVCRT__errno() = MSVCRT_EINVAL;
617 return NULL;
620 /* offset must be less than size */
621 if (offset >= size)
623 *MSVCRT__errno() = MSVCRT_EINVAL;
624 return NULL;
627 if (size == 0)
629 _aligned_free(memblock);
630 return NULL;
633 /* don't align to less than void pointer size */
634 if (alignment < sizeof(void *))
635 alignment = sizeof(void *);
637 /* make sure alignment and offset didn't change */
638 saved = SAVED_PTR(memblock);
639 if (memblock != ALIGN_PTR(*saved, alignment, offset))
641 *MSVCRT__errno() = MSVCRT_EINVAL;
642 return NULL;
645 old_padding = (char *)memblock - (char *)*saved;
647 /* Get previous size of block */
648 old_size = _msize(*saved);
649 if (old_size == -1)
651 /* It seems this function was called with an invalid pointer. Bail out. */
652 return NULL;
655 /* Adjust old_size to get amount of actual data in old block. */
656 if (old_size < old_padding)
658 /* Shouldn't happen. Something's weird, so bail out. */
659 return NULL;
661 old_size -= old_padding;
663 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
665 if (!temp)
666 return NULL;
668 /* adjust pointer for proper alignment and offset */
669 memblock = ALIGN_PTR(temp, alignment, offset);
671 /* Save the real allocation address below returned address */
672 /* so it can be found later to free. */
673 saved = SAVED_PTR(memblock);
675 new_padding = (char *)memblock - (char *)temp;
678 Memory layout of old block is as follows:
679 +-------+---------------------+-+--------------------------+-----------+
680 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
681 +-------+---------------------+-+--------------------------+-----------+
682 ^ ^ ^
683 | | |
684 *saved saved memblock
686 Memory layout of new block is as follows:
687 +-------+-----------------------------+-+----------------------+-------+
688 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
689 +-------+-----------------------------+-+----------------------+-------+
690 ^ ^ ^
691 | | |
692 temp saved memblock
694 However, in the new block, actual data is still written as follows
695 (because it was copied by MSVCRT_realloc):
696 +-------+---------------------+--------------------------------+-------+
697 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
698 +-------+---------------------+--------------------------------+-------+
699 ^ ^ ^
700 | | |
701 temp saved memblock
703 Therefore, min(old_size,size) bytes of actual data have to be moved
704 from the offset they were at in the old block (temp + old_padding),
705 to the offset they have to be in the new block (temp + new_padding == memblock).
707 if (new_padding != old_padding)
708 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
710 *saved = temp;
712 return memblock;
715 /*********************************************************************
716 * _aligned_realloc (MSVCRT.@)
718 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
720 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
721 return _aligned_offset_realloc(memblock, size, alignment, 0);
724 /*********************************************************************
725 * memmove_s (MSVCRT.@)
727 int CDECL MSVCRT_memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
729 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
731 if(!count)
732 return 0;
734 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
735 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
736 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
738 memmove(dest, src, count);
739 return 0;
742 /*********************************************************************
743 * wmemmove_s (MSVCR100.@)
745 int CDECL wmemmove_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
746 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
748 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
750 if (!count)
751 return 0;
753 /* Native does not seem to conform to 6.7.1.2.3 in
754 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
755 * in that it does not zero the output buffer on constraint violation.
757 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
758 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
759 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) return MSVCRT_ERANGE;
761 memmove(dest, src, sizeof(MSVCRT_wchar_t)*count);
762 return 0;
765 /*********************************************************************
766 * memcpy_s (MSVCRT.@)
768 int CDECL MSVCRT_memcpy_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
770 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
772 if(!count)
773 return 0;
775 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
776 if (!MSVCRT_CHECK_PMT(src != NULL))
778 memset(dest, 0, numberOfElements);
779 return MSVCRT_EINVAL;
781 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE ))
783 memset(dest, 0, numberOfElements);
784 return MSVCRT_ERANGE;
787 memcpy(dest, src, count);
788 return 0;
791 /*********************************************************************
792 * wmemcpy_s (MSVCR100.@)
794 int CDECL wmemcpy_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
795 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
797 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
799 if (!count)
800 return 0;
802 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
804 if (!MSVCRT_CHECK_PMT(src != NULL)) {
805 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
806 return MSVCRT_EINVAL;
808 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) {
809 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
810 return MSVCRT_ERANGE;
813 memcpy(dest, src, sizeof(MSVCRT_wchar_t)*count);
814 return 0;
817 /*********************************************************************
818 * strncpy_s (MSVCRT.@)
820 int CDECL MSVCRT_strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
821 const char *src, MSVCRT_size_t count)
823 MSVCRT_size_t i, end;
825 TRACE("(%p %lu %s %lu)\n", dest, numberOfElements, debugstr_a(src), count);
827 if(!count) {
828 if(dest && numberOfElements)
829 *dest = 0;
830 return 0;
833 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
834 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
835 if (!MSVCRT_CHECK_PMT(numberOfElements != 0)) return MSVCRT_EINVAL;
837 if(count!=MSVCRT__TRUNCATE && count<numberOfElements)
838 end = count;
839 else
840 end = numberOfElements-1;
842 for(i=0; i<end && src[i]; i++)
843 dest[i] = src[i];
845 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
846 dest[i] = '\0';
847 return 0;
850 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL);
851 dest[0] = '\0';
852 return MSVCRT_EINVAL;
855 BOOL msvcrt_init_heap(void)
857 heap = HeapCreate(0, 0, 0);
858 return heap != NULL;
861 void msvcrt_destroy_heap(void)
863 HeapDestroy(heap);
864 if(sb_heap)
865 HeapDestroy(sb_heap);