msvcrt: Add small blocks heap tests.
[wine.git] / dlls / msvcrt / heap.c
blob432df86e1a27583d3eb03e55292169388e7b0d5f
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;
137 retval = msvcrt_heap_alloc(0, size);
138 if(retval)
140 TRACE("(%ld) returning %p\n", size, retval);
141 return retval;
144 LOCK_HEAP;
145 if(MSVCRT_new_handler)
146 freed = (*MSVCRT_new_handler)(size);
147 else
148 freed = 0;
149 UNLOCK_HEAP;
150 } while(freed);
152 TRACE("(%ld) out of memory\n", size);
153 return NULL;
157 /*********************************************************************
158 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
160 void* CDECL MSVCRT_operator_new_dbg(MSVCRT_size_t size, int type, const char *file, int line)
162 return MSVCRT_operator_new( size );
166 /*********************************************************************
167 * ??3@YAXPAX@Z (MSVCRT.@)
169 void CDECL MSVCRT_operator_delete(void *mem)
171 TRACE("(%p)\n", mem);
172 msvcrt_heap_free(mem);
176 /*********************************************************************
177 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
179 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
181 return MSVCRT_new_handler;
185 /*********************************************************************
186 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
188 int CDECL MSVCRT__query_new_mode(void)
190 return MSVCRT_new_mode;
193 /*********************************************************************
194 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
196 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
198 MSVCRT_new_handler_func old_handler;
199 LOCK_HEAP;
200 old_handler = MSVCRT_new_handler;
201 MSVCRT_new_handler = func;
202 UNLOCK_HEAP;
203 return old_handler;
206 /*********************************************************************
207 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
209 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
211 TRACE("(%p)\n",func);
212 MSVCRT__set_new_handler(NULL);
213 return NULL;
216 /*********************************************************************
217 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
219 int CDECL MSVCRT__set_new_mode(int mode)
221 int old_mode;
222 LOCK_HEAP;
223 old_mode = MSVCRT_new_mode;
224 MSVCRT_new_mode = mode;
225 UNLOCK_HEAP;
226 return old_mode;
229 /*********************************************************************
230 * _callnewh (MSVCRT.@)
232 int CDECL _callnewh(MSVCRT_size_t size)
234 if(MSVCRT_new_handler)
235 (*MSVCRT_new_handler)(size);
236 return 0;
239 /*********************************************************************
240 * _expand (MSVCRT.@)
242 void* CDECL _expand(void* mem, MSVCRT_size_t size)
244 return msvcrt_heap_realloc(HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
247 /*********************************************************************
248 * _heapchk (MSVCRT.@)
250 int CDECL _heapchk(void)
252 if (!HeapValidate(heap, 0, NULL) ||
253 (sb_heap && !HeapValidate(sb_heap, 0, NULL)))
255 msvcrt_set_errno(GetLastError());
256 return MSVCRT__HEAPBADNODE;
258 return MSVCRT__HEAPOK;
261 /*********************************************************************
262 * _heapmin (MSVCRT.@)
264 int CDECL _heapmin(void)
266 if (!HeapCompact( heap, 0 ) ||
267 (sb_heap && !HeapCompact( sb_heap, 0 )))
269 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
270 msvcrt_set_errno(GetLastError());
271 return -1;
273 return 0;
276 /*********************************************************************
277 * _heapwalk (MSVCRT.@)
279 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
281 PROCESS_HEAP_ENTRY phe;
283 if (sb_heap)
284 FIXME("small blocks heap not supported\n");
286 LOCK_HEAP;
287 phe.lpData = next->_pentry;
288 phe.cbData = next->_size;
289 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
291 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
292 !HeapValidate( heap, 0, phe.lpData ))
294 UNLOCK_HEAP;
295 msvcrt_set_errno(GetLastError());
296 return MSVCRT__HEAPBADNODE;
301 if (!HeapWalk( heap, &phe ))
303 UNLOCK_HEAP;
304 if (GetLastError() == ERROR_NO_MORE_ITEMS)
305 return MSVCRT__HEAPEND;
306 msvcrt_set_errno(GetLastError());
307 if (!phe.lpData)
308 return MSVCRT__HEAPBADBEGIN;
309 return MSVCRT__HEAPBADNODE;
311 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
313 UNLOCK_HEAP;
314 next->_pentry = phe.lpData;
315 next->_size = phe.cbData;
316 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
317 return MSVCRT__HEAPOK;
320 /*********************************************************************
321 * _heapset (MSVCRT.@)
323 int CDECL _heapset(unsigned int value)
325 int retval;
326 struct MSVCRT__heapinfo heap;
328 memset( &heap, 0, sizeof(heap) );
329 LOCK_HEAP;
330 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
332 if (heap._useflag == MSVCRT__FREEENTRY)
333 memset(heap._pentry, value, heap._size);
335 UNLOCK_HEAP;
336 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
339 /*********************************************************************
340 * _heapadd (MSVCRT.@)
342 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
344 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
345 *MSVCRT__errno() = MSVCRT_ENOSYS;
346 return -1;
349 /*********************************************************************
350 * _get_heap_handle (MSVCRT.@)
352 MSVCRT_intptr_t CDECL _get_heap_handle(void)
354 return (MSVCRT_intptr_t)heap;
357 /*********************************************************************
358 * _msize (MSVCRT.@)
360 MSVCRT_size_t CDECL _msize(void* mem)
362 MSVCRT_size_t size = msvcrt_heap_size(mem);
363 if (size == ~(MSVCRT_size_t)0)
365 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
366 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
368 return size;
371 /*********************************************************************
372 * _aligned_msize (MSVCR100.@)
374 size_t CDECL _aligned_msize(void *p, MSVCRT_size_t alignment, MSVCRT_size_t offset)
376 void **alloc_ptr;
378 if(!MSVCRT_CHECK_PMT(p)) return -1;
380 if(alignment < sizeof(void*))
381 alignment = sizeof(void*);
383 alloc_ptr = SAVED_PTR(p);
384 return _msize(*alloc_ptr)-alignment-sizeof(void*);
387 /*********************************************************************
388 * calloc (MSVCRT.@)
390 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
392 return msvcrt_heap_alloc(HEAP_ZERO_MEMORY, size*count);
395 /*********************************************************************
396 * free (MSVCRT.@)
398 void CDECL MSVCRT_free(void* ptr)
400 msvcrt_heap_free(ptr);
403 /*********************************************************************
404 * malloc (MSVCRT.@)
406 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
408 void *ret = msvcrt_heap_alloc(0, size);
409 if (!ret)
410 *MSVCRT__errno() = MSVCRT_ENOMEM;
411 return ret;
414 /*********************************************************************
415 * realloc (MSVCRT.@)
417 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
419 if (!ptr) return MSVCRT_malloc(size);
420 if (size) return msvcrt_heap_realloc(0, ptr, size);
421 MSVCRT_free(ptr);
422 return NULL;
425 /*********************************************************************
426 * _recalloc (MSVCR100.@)
428 void* CDECL _recalloc(void *mem, MSVCRT_size_t num, MSVCRT_size_t size)
430 MSVCRT_size_t old_size;
431 void *ret;
433 if(!mem)
434 return MSVCRT_calloc(num, size);
436 size = num*size;
437 old_size = _msize(mem);
439 ret = MSVCRT_realloc(mem, size);
440 if(!ret) {
441 *MSVCRT__errno() = MSVCRT_ENOMEM;
442 return NULL;
445 if(size>old_size)
446 memset((BYTE*)ret+old_size, 0, size-old_size);
447 return ret;
450 /*********************************************************************
451 * __p__amblksiz (MSVCRT.@)
453 unsigned int* CDECL __p__amblksiz(void)
455 return &MSVCRT_amblksiz;
458 /*********************************************************************
459 * _get_sbh_threshold (MSVCRT.@)
461 MSVCRT_size_t CDECL _get_sbh_threshold(void)
463 return MSVCRT_sbh_threshold;
466 /*********************************************************************
467 * _set_sbh_threshold (MSVCRT.@)
469 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
471 #ifdef _WIN64
472 return 0;
473 #else
474 if(threshold > 1016)
475 return 0;
477 if(!sb_heap)
479 sb_heap = HeapCreate(0, 0, 0);
480 if(!sb_heap)
481 return 0;
484 MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
485 return 1;
486 #endif
489 /*********************************************************************
490 * _aligned_free (MSVCRT.@)
492 void CDECL _aligned_free(void *memblock)
494 TRACE("(%p)\n", memblock);
496 if (memblock)
498 void **saved = SAVED_PTR(memblock);
499 MSVCRT_free(*saved);
503 /*********************************************************************
504 * _aligned_offset_malloc (MSVCRT.@)
506 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
508 void *memblock, *temp, **saved;
509 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
511 /* alignment must be a power of 2 */
512 if ((alignment & (alignment - 1)) != 0)
514 *MSVCRT__errno() = MSVCRT_EINVAL;
515 return NULL;
518 /* offset must be less than size */
519 if (offset && offset >= size)
521 *MSVCRT__errno() = MSVCRT_EINVAL;
522 return NULL;
525 /* don't align to less than void pointer size */
526 if (alignment < sizeof(void *))
527 alignment = sizeof(void *);
529 /* allocate enough space for void pointer and alignment */
530 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
532 if (!temp)
533 return NULL;
535 /* adjust pointer for proper alignment and offset */
536 memblock = ALIGN_PTR(temp, alignment, offset);
538 /* Save the real allocation address below returned address */
539 /* so it can be found later to free. */
540 saved = SAVED_PTR(memblock);
541 *saved = temp;
543 return memblock;
546 /*********************************************************************
547 * _aligned_malloc (MSVCRT.@)
549 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
551 TRACE("(%lu, %lu)\n", size, alignment);
552 return _aligned_offset_malloc(size, alignment, 0);
555 /*********************************************************************
556 * _aligned_offset_realloc (MSVCRT.@)
558 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
559 MSVCRT_size_t alignment, MSVCRT_size_t offset)
561 void * temp, **saved;
562 MSVCRT_size_t old_padding, new_padding, old_size;
563 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
565 if (!memblock)
566 return _aligned_offset_malloc(size, alignment, offset);
568 /* alignment must be a power of 2 */
569 if ((alignment & (alignment - 1)) != 0)
571 *MSVCRT__errno() = MSVCRT_EINVAL;
572 return NULL;
575 /* offset must be less than size */
576 if (offset >= size)
578 *MSVCRT__errno() = MSVCRT_EINVAL;
579 return NULL;
582 if (size == 0)
584 _aligned_free(memblock);
585 return NULL;
588 /* don't align to less than void pointer size */
589 if (alignment < sizeof(void *))
590 alignment = sizeof(void *);
592 /* make sure alignment and offset didn't change */
593 saved = SAVED_PTR(memblock);
594 if (memblock != ALIGN_PTR(*saved, alignment, offset))
596 *MSVCRT__errno() = MSVCRT_EINVAL;
597 return NULL;
600 old_padding = (char *)memblock - (char *)*saved;
602 /* Get previous size of block */
603 old_size = _msize(*saved);
604 if (old_size == -1)
606 /* It seems this function was called with an invalid pointer. Bail out. */
607 return NULL;
610 /* Adjust old_size to get amount of actual data in old block. */
611 if (old_size < old_padding)
613 /* Shouldn't happen. Something's weird, so bail out. */
614 return NULL;
616 old_size -= old_padding;
618 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
620 if (!temp)
621 return NULL;
623 /* adjust pointer for proper alignment and offset */
624 memblock = ALIGN_PTR(temp, alignment, offset);
626 /* Save the real allocation address below returned address */
627 /* so it can be found later to free. */
628 saved = SAVED_PTR(memblock);
630 new_padding = (char *)memblock - (char *)temp;
633 Memory layout of old block is as follows:
634 +-------+---------------------+-+--------------------------+-----------+
635 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
636 +-------+---------------------+-+--------------------------+-----------+
637 ^ ^ ^
638 | | |
639 *saved saved memblock
641 Memory layout of new block is as follows:
642 +-------+-----------------------------+-+----------------------+-------+
643 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
644 +-------+-----------------------------+-+----------------------+-------+
645 ^ ^ ^
646 | | |
647 temp saved memblock
649 However, in the new block, actual data is still written as follows
650 (because it was copied by MSVCRT_realloc):
651 +-------+---------------------+--------------------------------+-------+
652 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
653 +-------+---------------------+--------------------------------+-------+
654 ^ ^ ^
655 | | |
656 temp saved memblock
658 Therefore, min(old_size,size) bytes of actual data have to be moved
659 from the offset they were at in the old block (temp + old_padding),
660 to the offset they have to be in the new block (temp + new_padding == memblock).
662 if (new_padding != old_padding)
663 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
665 *saved = temp;
667 return memblock;
670 /*********************************************************************
671 * _aligned_realloc (MSVCRT.@)
673 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
675 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
676 return _aligned_offset_realloc(memblock, size, alignment, 0);
679 /*********************************************************************
680 * memmove_s (MSVCRT.@)
682 int CDECL MSVCRT_memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
684 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
686 if(!count)
687 return 0;
689 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
690 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
691 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
693 memmove(dest, src, count);
694 return 0;
697 /*********************************************************************
698 * wmemmove_s (MSVCR100.@)
700 int CDECL wmemmove_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
701 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
703 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
705 if (!count)
706 return 0;
708 /* Native does not seem to conform to 6.7.1.2.3 in
709 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
710 * in that it does not zero the output buffer on constraint violation.
712 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
713 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
714 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) return MSVCRT_ERANGE;
716 memmove(dest, src, sizeof(MSVCRT_wchar_t)*count);
717 return 0;
720 /*********************************************************************
721 * memcpy_s (MSVCRT.@)
723 int CDECL MSVCRT_memcpy_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
725 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
727 if(!count)
728 return 0;
730 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
731 if (!MSVCRT_CHECK_PMT(src != NULL))
733 memset(dest, 0, numberOfElements);
734 return MSVCRT_EINVAL;
736 if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, MSVCRT_ERANGE ))
738 memset(dest, 0, numberOfElements);
739 return MSVCRT_ERANGE;
742 memcpy(dest, src, count);
743 return 0;
746 /*********************************************************************
747 * wmemcpy_s (MSVCR100.@)
749 int CDECL wmemcpy_s(MSVCRT_wchar_t *dest, MSVCRT_size_t numberOfElements,
750 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
752 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
754 if (!count)
755 return 0;
757 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
759 if (!MSVCRT_CHECK_PMT(src != NULL)) {
760 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
761 return MSVCRT_EINVAL;
763 if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, MSVCRT_ERANGE)) {
764 memset(dest, 0, numberOfElements*sizeof(MSVCRT_wchar_t));
765 return MSVCRT_ERANGE;
768 memcpy(dest, src, sizeof(MSVCRT_wchar_t)*count);
769 return 0;
772 /*********************************************************************
773 * strncpy_s (MSVCRT.@)
775 int CDECL MSVCRT_strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
776 const char *src, MSVCRT_size_t count)
778 MSVCRT_size_t i, end;
780 TRACE("(%p %lu %s %lu)\n", dest, numberOfElements, debugstr_a(src), count);
782 if(!count) {
783 if(dest && numberOfElements)
784 *dest = 0;
785 return 0;
788 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
789 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
790 if (!MSVCRT_CHECK_PMT(numberOfElements != 0)) return MSVCRT_EINVAL;
792 if(count!=MSVCRT__TRUNCATE && count<numberOfElements)
793 end = count;
794 else
795 end = numberOfElements-1;
797 for(i=0; i<end && src[i]; i++)
798 dest[i] = src[i];
800 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
801 dest[i] = '\0';
802 return 0;
805 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL);
806 dest[0] = '\0';
807 return MSVCRT_EINVAL;
810 BOOL msvcrt_init_heap(void)
812 heap = HeapCreate(0, 0, 0);
813 return heap != NULL;
816 void msvcrt_destroy_heap(void)
818 HeapDestroy(heap);
819 if(sb_heap)
820 HeapDestroy(sb_heap);