2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll heap functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
2001-01-11 00:59:25 +01:00
|
|
|
* Note: Win32 heap operations are MT safe. We only lock the new
|
|
|
|
* handler and non atomic heap operations
|
|
|
|
*/
|
|
|
|
|
2020-12-02 00:53:24 +01:00
|
|
|
#include <malloc.h>
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
2002-02-21 21:22:00 +01:00
|
|
|
#include "mtdll.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* MT */
|
2020-11-19 11:33:51 +01:00
|
|
|
#define LOCK_HEAP _lock( _HEAP_LOCK )
|
|
|
|
#define UNLOCK_HEAP _unlock( _HEAP_LOCK )
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2007-07-28 02:02:23 +02:00
|
|
|
/* _aligned */
|
|
|
|
#define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
|
|
|
|
~(sizeof(void *) - 1)))
|
|
|
|
#define ALIGN_PTR(ptr, alignment, offset) ((void *) \
|
|
|
|
((((DWORD_PTR)((char *)ptr + alignment + sizeof(void *) + offset)) & \
|
|
|
|
~(alignment - 1)) - offset))
|
|
|
|
|
2014-06-18 11:35:19 +02:00
|
|
|
#define SB_HEAP_ALIGN 16
|
|
|
|
|
|
|
|
static HANDLE heap, sb_heap;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
typedef int (CDECL *MSVCRT_new_handler_func)(size_t size);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
static MSVCRT_new_handler_func MSVCRT_new_handler;
|
|
|
|
static int MSVCRT_new_mode;
|
|
|
|
|
2005-09-19 16:41:25 +02:00
|
|
|
/* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
|
|
|
|
static unsigned int MSVCRT_amblksiz = 16;
|
2005-09-23 12:07:05 +02:00
|
|
|
/* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
|
2020-11-27 14:17:05 +01:00
|
|
|
static size_t MSVCRT_sbh_threshold = 0;
|
2001-04-23 20:20:55 +02:00
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
static void* msvcrt_heap_alloc(DWORD flags, size_t size)
|
2014-06-18 11:35:19 +02:00
|
|
|
{
|
|
|
|
if(size < MSVCRT_sbh_threshold)
|
|
|
|
{
|
|
|
|
void *memblock, *temp, **saved;
|
|
|
|
|
|
|
|
temp = HeapAlloc(sb_heap, flags, size+sizeof(void*)+SB_HEAP_ALIGN);
|
|
|
|
if(!temp) return NULL;
|
|
|
|
|
|
|
|
memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
|
|
|
|
saved = SAVED_PTR(memblock);
|
|
|
|
*saved = temp;
|
|
|
|
return memblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HeapAlloc(heap, flags, size);
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
static void* msvcrt_heap_realloc(DWORD flags, void *ptr, size_t size)
|
2014-06-18 11:35:19 +02:00
|
|
|
{
|
|
|
|
if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
|
|
|
|
{
|
|
|
|
/* TODO: move data to normal heap if it exceeds sbh_threshold limit */
|
|
|
|
void *memblock, *temp, **saved;
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t old_padding, new_padding, old_size;
|
2014-06-18 11:35:19 +02:00
|
|
|
|
|
|
|
saved = SAVED_PTR(ptr);
|
|
|
|
old_padding = (char*)ptr - (char*)*saved;
|
|
|
|
old_size = HeapSize(sb_heap, 0, *saved);
|
|
|
|
if(old_size == -1)
|
|
|
|
return NULL;
|
|
|
|
old_size -= old_padding;
|
|
|
|
|
|
|
|
temp = HeapReAlloc(sb_heap, flags, *saved, size+sizeof(void*)+SB_HEAP_ALIGN);
|
|
|
|
if(!temp) return NULL;
|
|
|
|
|
|
|
|
memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
|
|
|
|
saved = SAVED_PTR(memblock);
|
|
|
|
new_padding = (char*)memblock - (char*)temp;
|
|
|
|
|
|
|
|
if(new_padding != old_padding)
|
|
|
|
memmove(memblock, (char*)temp+old_padding, old_size>size ? size : old_size);
|
|
|
|
|
|
|
|
*saved = temp;
|
|
|
|
return memblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HeapReAlloc(heap, flags, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL msvcrt_heap_free(void *ptr)
|
|
|
|
{
|
|
|
|
if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
|
|
|
|
{
|
|
|
|
void **saved = SAVED_PTR(ptr);
|
|
|
|
return HeapFree(sb_heap, 0, *saved);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HeapFree(heap, 0, ptr);
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
static size_t msvcrt_heap_size(void *ptr)
|
2014-06-18 11:35:19 +02:00
|
|
|
{
|
|
|
|
if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
|
|
|
|
{
|
|
|
|
void **saved = SAVED_PTR(ptr);
|
|
|
|
return HeapSize(sb_heap, 0, *saved);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HeapSize(heap, 0, ptr);
|
|
|
|
}
|
|
|
|
|
2020-01-29 14:27:29 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _callnewh (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL _callnewh(size_t size)
|
2020-01-29 14:27:29 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
MSVCRT_new_handler_func handler = MSVCRT_new_handler;
|
|
|
|
if(handler)
|
|
|
|
ret = (*handler)(size) ? 1 : 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2001-07-02 21:59:40 +02:00
|
|
|
* ??2@YAPAXI@Z (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
void* CDECL DECLSPEC_HOTPATCH operator_new(size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2011-09-13 10:30:17 +02:00
|
|
|
void *retval;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-06-18 11:35:19 +02:00
|
|
|
retval = msvcrt_heap_alloc(0, size);
|
2011-09-13 10:30:17 +02:00
|
|
|
if(retval)
|
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%Iu) returning %p\n", size, retval);
|
2011-09-13 10:30:17 +02:00
|
|
|
return retval;
|
|
|
|
}
|
2020-01-29 14:27:29 +01:00
|
|
|
} while(_callnewh(size));
|
2011-09-13 10:30:17 +02:00
|
|
|
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%Iu) out of memory\n", size);
|
2014-09-02 17:06:06 +02:00
|
|
|
#if _MSVCR_VER >= 80
|
2017-03-27 10:26:54 +02:00
|
|
|
throw_exception(EXCEPTION_BAD_ALLOC, 0, "bad allocation");
|
2014-09-02 17:06:06 +02:00
|
|
|
#endif
|
2011-09-13 10:30:17 +02:00
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2010-04-13 17:19:27 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ??2@YAPAXIHPBDH@Z (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
void* CDECL operator_new_dbg(size_t size, int type, const char *file, int line)
|
2010-04-13 17:19:27 +02:00
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
return operator_new( size );
|
2010-04-13 17:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
2001-07-02 21:59:40 +02:00
|
|
|
* ??3@YAXPAX@Z (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
void CDECL DECLSPEC_HOTPATCH operator_delete(void *mem)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%p)\n", mem);
|
2014-06-18 11:35:19 +02:00
|
|
|
msvcrt_heap_free(mem);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
MSVCRT_new_handler_func CDECL _query_new_handler(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_new_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ?_query_new_mode@@YAHXZ (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
int CDECL _query_new_mode(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_new_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
MSVCRT_new_handler_func CDECL _set_new_handler(MSVCRT_new_handler_func func)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
MSVCRT_new_handler_func old_handler;
|
|
|
|
LOCK_HEAP;
|
|
|
|
old_handler = MSVCRT_new_handler;
|
|
|
|
MSVCRT_new_handler = func;
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
return old_handler;
|
|
|
|
}
|
|
|
|
|
2001-12-21 21:27:39 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
MSVCRT_new_handler_func CDECL set_new_handler(void *func)
|
2001-12-21 21:27:39 +01:00
|
|
|
{
|
|
|
|
TRACE("(%p)\n",func);
|
2020-12-01 13:51:32 +01:00
|
|
|
_set_new_handler(NULL);
|
2001-12-21 21:27:39 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* ?_set_new_mode@@YAHH@Z (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
int CDECL _set_new_mode(int mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-01-29 14:27:35 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(mode == 0 || mode == 1)) return -1;
|
|
|
|
return InterlockedExchange(&MSVCRT_new_mode, mode);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _expand (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void* CDECL _expand(void* mem, size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-06-18 11:35:19 +02:00
|
|
|
return msvcrt_heap_realloc(HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapchk (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _heapchk(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-06-18 11:35:19 +02:00
|
|
|
if (!HeapValidate(heap, 0, NULL) ||
|
|
|
|
(sb_heap && !HeapValidate(sb_heap, 0, NULL)))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-12-02 00:53:24 +01:00
|
|
|
return _HEAPBADNODE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2020-12-02 00:53:24 +01:00
|
|
|
return _HEAPOK;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapmin (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _heapmin(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-06-18 11:35:19 +02:00
|
|
|
if (!HeapCompact( heap, 0 ) ||
|
|
|
|
(sb_heap && !HeapCompact( sb_heap, 0 )))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapwalk (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:53:24 +01:00
|
|
|
int CDECL _heapwalk(_HEAPINFO *next)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
PROCESS_HEAP_ENTRY phe;
|
|
|
|
|
2014-06-18 11:35:19 +02:00
|
|
|
if (sb_heap)
|
|
|
|
FIXME("small blocks heap not supported\n");
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
LOCK_HEAP;
|
|
|
|
phe.lpData = next->_pentry;
|
|
|
|
phe.cbData = next->_size;
|
2020-12-02 00:53:24 +01:00
|
|
|
phe.wFlags = next->_useflag == _USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
|
2014-04-22 14:38:47 +02:00
|
|
|
!HeapValidate( heap, 0, phe.lpData ))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
UNLOCK_HEAP;
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2020-12-02 00:53:24 +01:00
|
|
|
return _HEAPBADNODE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-04-22 14:38:47 +02:00
|
|
|
if (!HeapWalk( heap, &phe ))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_ITEMS)
|
2020-12-02 00:53:24 +01:00
|
|
|
return _HEAPEND;
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
if (!phe.lpData)
|
2020-12-02 00:53:24 +01:00
|
|
|
return _HEAPBADBEGIN;
|
|
|
|
return _HEAPBADNODE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
} while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
|
|
|
|
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
next->_pentry = phe.lpData;
|
|
|
|
next->_size = phe.cbData;
|
2020-12-02 00:53:24 +01:00
|
|
|
next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? _USEDENTRY : _FREEENTRY;
|
|
|
|
return _HEAPOK;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapset (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _heapset(unsigned int value)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval;
|
2020-12-02 00:53:24 +01:00
|
|
|
_HEAPINFO heap;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
memset( &heap, 0, sizeof(heap) );
|
2001-01-11 00:59:25 +01:00
|
|
|
LOCK_HEAP;
|
2020-12-02 00:53:24 +01:00
|
|
|
while ((retval = _heapwalk(&heap)) == _HEAPOK)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-02 00:53:24 +01:00
|
|
|
if (heap._useflag == _FREEENTRY)
|
2001-01-11 00:59:25 +01:00
|
|
|
memset(heap._pentry, value, heap._size);
|
|
|
|
}
|
|
|
|
UNLOCK_HEAP;
|
2020-12-02 00:53:24 +01:00
|
|
|
return retval == _HEAPEND ? _HEAPOK : retval;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-12-21 21:27:39 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _heapadd (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL _heapadd(void* mem, size_t size)
|
2001-12-21 21:27:39 +01:00
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p,%Iu) unsupported in Win32\n", mem,size);
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOSYS;
|
2001-12-21 21:27:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-20 21:39:22 +02:00
|
|
|
/*********************************************************************
|
2014-04-22 14:38:47 +02:00
|
|
|
* _get_heap_handle (MSVCRT.@)
|
2010-04-20 21:39:22 +02:00
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
intptr_t CDECL _get_heap_handle(void)
|
2010-04-20 21:39:22 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
return (intptr_t)heap;
|
2010-04-20 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _msize (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t CDECL _msize(void* mem)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t size = msvcrt_heap_size(mem);
|
|
|
|
if (size == ~(size_t)0)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
WARN(":Probably called with non wine-allocated memory, ret = -1\n");
|
|
|
|
/* At least the Win32 crtdll/msvcrt also return -1 in this case */
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=80
|
2014-04-16 16:15:21 +02:00
|
|
|
/*********************************************************************
|
2017-12-28 18:46:53 +01:00
|
|
|
* _aligned_msize (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t CDECL _aligned_msize(void *p, size_t alignment, size_t offset)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
|
|
|
void **alloc_ptr;
|
|
|
|
|
|
|
|
if(!MSVCRT_CHECK_PMT(p)) return -1;
|
|
|
|
|
|
|
|
if(alignment < sizeof(void*))
|
|
|
|
alignment = sizeof(void*);
|
|
|
|
|
|
|
|
alloc_ptr = SAVED_PTR(p);
|
|
|
|
return _msize(*alloc_ptr)-alignment-sizeof(void*);
|
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* calloc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
void* CDECL DECLSPEC_HOTPATCH calloc(size_t count, size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t bytes = count*size;
|
2016-04-27 10:11:49 +02:00
|
|
|
|
|
|
|
if (size && bytes / size != count)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOMEM;
|
2016-04-27 10:11:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msvcrt_heap_alloc(HEAP_ZERO_MEMORY, bytes);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=140
|
2016-06-17 02:02:23 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _calloc_base (UCRTBASE.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void* CDECL _calloc_base(size_t count, size_t size)
|
2016-06-17 02:02:23 +02:00
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
return calloc(count, size);
|
2016-06-17 02:02:23 +02:00
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2016-06-17 02:02:23 +02:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* free (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
void CDECL DECLSPEC_HOTPATCH free(void* ptr)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2014-06-18 11:35:19 +02:00
|
|
|
msvcrt_heap_free(ptr);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=140
|
2016-06-17 02:02:25 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _free_base (UCRTBASE.@)
|
|
|
|
*/
|
|
|
|
void CDECL _free_base(void* ptr)
|
|
|
|
{
|
|
|
|
msvcrt_heap_free(ptr);
|
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2016-06-17 02:02:25 +02:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* malloc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
void* CDECL malloc(size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-01-29 14:27:25 +01:00
|
|
|
void *ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = msvcrt_heap_alloc(0, size);
|
|
|
|
if (ret || !MSVCRT_new_mode)
|
|
|
|
break;
|
|
|
|
} while(_callnewh(size));
|
|
|
|
|
|
|
|
if (!ret)
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOMEM;
|
2020-01-29 14:27:25 +01:00
|
|
|
return ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=140
|
2016-06-17 02:02:24 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _malloc_base (UCRTBASE.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void* CDECL _malloc_base(size_t size)
|
2016-06-17 02:02:24 +02:00
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
return malloc(size);
|
2016-06-17 02:02:24 +02:00
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2016-06-17 02:02:24 +02:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* realloc (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
void* CDECL DECLSPEC_HOTPATCH realloc(void* ptr, size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
if (!ptr) return malloc(size);
|
2014-06-18 11:35:19 +02:00
|
|
|
if (size) return msvcrt_heap_realloc(0, ptr, size);
|
2020-12-01 13:51:32 +01:00
|
|
|
free(ptr);
|
2003-11-21 00:41:13 +01:00
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2005-09-19 16:41:25 +02:00
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=140
|
2016-06-17 02:02:26 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _realloc_base (UCRTBASE.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void* CDECL _realloc_base(void* ptr, size_t size)
|
2016-06-17 02:02:26 +02:00
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
return realloc(ptr, size);
|
2016-06-17 02:02:26 +02:00
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2016-06-17 02:02:26 +02:00
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=80
|
2014-04-16 16:15:21 +02:00
|
|
|
/*********************************************************************
|
2017-12-28 18:46:53 +01:00
|
|
|
* _recalloc (MSVCR80.@)
|
2014-04-16 16:15:21 +02:00
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void* CDECL _recalloc(void *mem, size_t num, size_t size)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t old_size;
|
2014-04-16 16:15:21 +02:00
|
|
|
void *ret;
|
|
|
|
|
|
|
|
if(!mem)
|
2020-12-01 13:51:32 +01:00
|
|
|
return calloc(num, size);
|
2014-04-16 16:15:21 +02:00
|
|
|
|
|
|
|
size = num*size;
|
|
|
|
old_size = _msize(mem);
|
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
ret = realloc(mem, size);
|
2014-04-16 16:15:21 +02:00
|
|
|
if(!ret) {
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ENOMEM;
|
2014-04-16 16:15:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(size>old_size)
|
|
|
|
memset((BYTE*)ret+old_size, 0, size-old_size);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2005-09-19 16:41:25 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* __p__amblksiz (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
unsigned int* CDECL __p__amblksiz(void)
|
2005-09-19 16:41:25 +02:00
|
|
|
{
|
|
|
|
return &MSVCRT_amblksiz;
|
|
|
|
}
|
2005-09-23 12:07:05 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _get_sbh_threshold (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t CDECL _get_sbh_threshold(void)
|
2005-09-23 12:07:05 +02:00
|
|
|
{
|
|
|
|
return MSVCRT_sbh_threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _set_sbh_threshold (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL _set_sbh_threshold(size_t threshold)
|
2005-09-23 12:07:05 +02:00
|
|
|
{
|
2014-06-18 11:35:19 +02:00
|
|
|
#ifdef _WIN64
|
|
|
|
return 0;
|
|
|
|
#else
|
2005-09-23 12:07:05 +02:00
|
|
|
if(threshold > 1016)
|
|
|
|
return 0;
|
2014-06-18 11:35:19 +02:00
|
|
|
|
|
|
|
if(!sb_heap)
|
|
|
|
{
|
|
|
|
sb_heap = HeapCreate(0, 0, 0);
|
|
|
|
if(!sb_heap)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
|
2005-09-23 12:07:05 +02:00
|
|
|
return 1;
|
2014-06-18 11:35:19 +02:00
|
|
|
#endif
|
2005-09-23 12:07:05 +02:00
|
|
|
}
|
2007-07-28 02:02:23 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _aligned_free (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void CDECL _aligned_free(void *memblock)
|
|
|
|
{
|
|
|
|
TRACE("(%p)\n", memblock);
|
|
|
|
|
|
|
|
if (memblock)
|
|
|
|
{
|
|
|
|
void **saved = SAVED_PTR(memblock);
|
2020-12-01 13:51:32 +01:00
|
|
|
free(*saved);
|
2007-07-28 02:02:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _aligned_offset_malloc (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void * CDECL _aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
|
|
|
void *memblock, *temp, **saved;
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%Iu, %Iu, %Iu)\n", size, alignment, offset);
|
2007-07-28 02:02:23 +02:00
|
|
|
|
|
|
|
/* alignment must be a power of 2 */
|
|
|
|
if ((alignment & (alignment - 1)) != 0)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2007-07-28 02:02:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* offset must be less than size */
|
2012-12-19 10:16:45 +01:00
|
|
|
if (offset && offset >= size)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2007-07-28 02:02:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't align to less than void pointer size */
|
|
|
|
if (alignment < sizeof(void *))
|
|
|
|
alignment = sizeof(void *);
|
|
|
|
|
|
|
|
/* allocate enough space for void pointer and alignment */
|
2020-12-01 13:51:32 +01:00
|
|
|
temp = malloc(size + alignment + sizeof(void *));
|
2007-07-28 02:02:23 +02:00
|
|
|
|
|
|
|
if (!temp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* adjust pointer for proper alignment and offset */
|
|
|
|
memblock = ALIGN_PTR(temp, alignment, offset);
|
|
|
|
|
|
|
|
/* Save the real allocation address below returned address */
|
|
|
|
/* so it can be found later to free. */
|
|
|
|
saved = SAVED_PTR(memblock);
|
|
|
|
*saved = temp;
|
|
|
|
|
|
|
|
return memblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _aligned_malloc (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void * CDECL _aligned_malloc(size_t size, size_t alignment)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%Iu, %Iu)\n", size, alignment);
|
2007-07-28 02:02:23 +02:00
|
|
|
return _aligned_offset_malloc(size, alignment, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _aligned_offset_realloc (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void * CDECL _aligned_offset_realloc(void *memblock, size_t size,
|
|
|
|
size_t alignment, size_t offset)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
|
|
|
void * temp, **saved;
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t old_padding, new_padding, old_size;
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p, %Iu, %Iu, %Iu)\n", memblock, size, alignment, offset);
|
2007-07-28 02:02:23 +02:00
|
|
|
|
|
|
|
if (!memblock)
|
|
|
|
return _aligned_offset_malloc(size, alignment, offset);
|
|
|
|
|
|
|
|
/* alignment must be a power of 2 */
|
|
|
|
if ((alignment & (alignment - 1)) != 0)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2007-07-28 02:02:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* offset must be less than size */
|
|
|
|
if (offset >= size)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2007-07-28 02:02:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
_aligned_free(memblock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't align to less than void pointer size */
|
|
|
|
if (alignment < sizeof(void *))
|
|
|
|
alignment = sizeof(void *);
|
|
|
|
|
|
|
|
/* make sure alignment and offset didn't change */
|
|
|
|
saved = SAVED_PTR(memblock);
|
|
|
|
if (memblock != ALIGN_PTR(*saved, alignment, offset))
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2007-07-28 02:02:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-28 11:47:18 +01:00
|
|
|
old_padding = (char *)memblock - (char *)*saved;
|
|
|
|
|
|
|
|
/* Get previous size of block */
|
|
|
|
old_size = _msize(*saved);
|
|
|
|
if (old_size == -1)
|
|
|
|
{
|
|
|
|
/* It seems this function was called with an invalid pointer. Bail out. */
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-03 22:56:37 +01:00
|
|
|
|
2007-11-28 11:47:18 +01:00
|
|
|
/* Adjust old_size to get amount of actual data in old block. */
|
2007-12-03 22:56:37 +01:00
|
|
|
if (old_size < old_padding)
|
2007-11-28 11:47:18 +01:00
|
|
|
{
|
|
|
|
/* Shouldn't happen. Something's weird, so bail out. */
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-03 22:56:37 +01:00
|
|
|
old_size -= old_padding;
|
2007-07-28 02:02:23 +02:00
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
temp = realloc(*saved, size + alignment + sizeof(void *));
|
2007-07-28 02:02:23 +02:00
|
|
|
|
|
|
|
if (!temp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* adjust pointer for proper alignment and offset */
|
|
|
|
memblock = ALIGN_PTR(temp, alignment, offset);
|
|
|
|
|
|
|
|
/* Save the real allocation address below returned address */
|
|
|
|
/* so it can be found later to free. */
|
|
|
|
saved = SAVED_PTR(memblock);
|
|
|
|
|
2007-11-28 11:47:18 +01:00
|
|
|
new_padding = (char *)memblock - (char *)temp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Memory layout of old block is as follows:
|
|
|
|
+-------+---------------------+-+--------------------------+-----------+
|
|
|
|
| ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
|
|
|
|
+-------+---------------------+-+--------------------------+-----------+
|
|
|
|
^ ^ ^
|
|
|
|
| | |
|
|
|
|
*saved saved memblock
|
|
|
|
|
|
|
|
Memory layout of new block is as follows:
|
|
|
|
+-------+-----------------------------+-+----------------------+-------+
|
|
|
|
| ... | "new_padding" bytes | | ... "size" bytes ... | ... |
|
|
|
|
+-------+-----------------------------+-+----------------------+-------+
|
|
|
|
^ ^ ^
|
|
|
|
| | |
|
|
|
|
temp saved memblock
|
|
|
|
|
|
|
|
However, in the new block, actual data is still written as follows
|
2020-12-01 13:51:32 +01:00
|
|
|
(because it was copied by realloc):
|
2007-11-28 11:47:18 +01:00
|
|
|
+-------+---------------------+--------------------------------+-------+
|
|
|
|
| ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
|
|
|
|
+-------+---------------------+--------------------------------+-------+
|
|
|
|
^ ^ ^
|
|
|
|
| | |
|
|
|
|
temp saved memblock
|
|
|
|
|
|
|
|
Therefore, min(old_size,size) bytes of actual data have to be moved
|
|
|
|
from the offset they were at in the old block (temp + old_padding),
|
|
|
|
to the offset they have to be in the new block (temp + new_padding == memblock).
|
|
|
|
*/
|
2007-07-28 02:02:23 +02:00
|
|
|
if (new_padding != old_padding)
|
2007-11-28 11:47:18 +01:00
|
|
|
memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
|
2007-07-28 02:02:23 +02:00
|
|
|
|
|
|
|
*saved = temp;
|
|
|
|
|
|
|
|
return memblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _aligned_realloc (MSVCRT.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
void * CDECL _aligned_realloc(void *memblock, size_t size, size_t alignment)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p, %Iu, %Iu)\n", memblock, size, alignment);
|
2007-07-28 02:02:23 +02:00
|
|
|
return _aligned_offset_realloc(memblock, size, alignment, 0);
|
|
|
|
}
|
2010-04-12 15:35:31 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* memmove_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
int CDECL memmove_s(void *dest, size_t numberOfElements, const void *src, size_t count)
|
2010-04-12 15:35:31 +02:00
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
|
2010-04-12 15:35:31 +02:00
|
|
|
|
|
|
|
if(!count)
|
|
|
|
return 0;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, ERANGE )) return ERANGE;
|
2010-04-12 15:35:31 +02:00
|
|
|
|
2012-07-28 18:43:43 +02:00
|
|
|
memmove(dest, src, count);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=100
|
2014-04-16 16:15:21 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* wmemmove_s (MSVCR100.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL wmemmove_s(wchar_t *dest, size_t numberOfElements,
|
|
|
|
const wchar_t *src, size_t count)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
|
2014-04-16 16:15:21 +02:00
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Native does not seem to conform to 6.7.1.2.3 in
|
|
|
|
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
|
|
|
|
* in that it does not zero the output buffer on constraint violation.
|
|
|
|
*/
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, ERANGE)) return ERANGE;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
memmove(dest, src, sizeof(wchar_t)*count);
|
2014-04-16 16:15:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2012-07-28 18:43:43 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* memcpy_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
int CDECL memcpy_s(void *dest, size_t numberOfElements, const void *src, size_t count)
|
2012-07-28 18:43:43 +02:00
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
|
2012-07-28 18:43:43 +02:00
|
|
|
|
|
|
|
if(!count)
|
|
|
|
return 0;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
|
2012-07-28 18:43:43 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(src != NULL))
|
|
|
|
{
|
|
|
|
memset(dest, 0, numberOfElements);
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-04-12 15:35:31 +02:00
|
|
|
}
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT_ERR( count <= numberOfElements, ERANGE ))
|
2012-07-28 18:43:43 +02:00
|
|
|
{
|
2010-04-12 15:35:31 +02:00
|
|
|
memset(dest, 0, numberOfElements);
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2010-04-12 15:35:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:38:54 +02:00
|
|
|
memmove(dest, src, count);
|
2010-04-12 15:35:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-04-20 21:39:11 +02:00
|
|
|
|
2017-12-28 18:46:53 +01:00
|
|
|
#if _MSVCR_VER>=100
|
2014-04-16 16:15:21 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* wmemcpy_s (MSVCR100.@)
|
|
|
|
*/
|
2020-11-27 14:17:05 +01:00
|
|
|
int CDECL wmemcpy_s(wchar_t *dest, size_t numberOfElements,
|
|
|
|
const wchar_t *src, size_t count)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
|
2014-04-16 16:15:21 +02:00
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
|
2014-04-16 16:15:21 +02:00
|
|
|
|
|
|
|
if (!MSVCRT_CHECK_PMT(src != NULL)) {
|
2020-11-27 14:16:49 +01:00
|
|
|
memset(dest, 0, numberOfElements*sizeof(wchar_t));
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2014-04-16 16:15:21 +02:00
|
|
|
}
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT_ERR(count <= numberOfElements, ERANGE)) {
|
2020-11-27 14:16:49 +01:00
|
|
|
memset(dest, 0, numberOfElements*sizeof(wchar_t));
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2014-04-16 16:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
memmove(dest, src, sizeof(wchar_t)*count);
|
2014-04-16 16:15:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-28 18:46:53 +01:00
|
|
|
#endif
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2010-04-20 21:39:11 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strncpy_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-01 13:51:32 +01:00
|
|
|
int CDECL strncpy_s(char *dest, size_t numberOfElements,
|
2020-11-27 14:17:05 +01:00
|
|
|
const char *src, size_t count)
|
2010-04-20 21:39:11 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i, end;
|
2010-04-20 21:39:11 +02:00
|
|
|
|
2020-11-20 12:35:52 +01:00
|
|
|
TRACE("(%p %Iu %s %Iu)\n", dest, numberOfElements, debugstr_a(src), count);
|
2010-04-20 21:39:11 +02:00
|
|
|
|
2012-09-07 16:58:23 +02:00
|
|
|
if(!count) {
|
|
|
|
if(dest && numberOfElements)
|
|
|
|
*dest = 0;
|
2010-04-20 21:39:11 +02:00
|
|
|
return 0;
|
2012-09-07 16:58:23 +02:00
|
|
|
}
|
2010-04-20 21:39:11 +02:00
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(numberOfElements != 0)) return EINVAL;
|
2010-04-20 21:39:11 +02:00
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if(count!=_TRUNCATE && count<numberOfElements)
|
2010-04-20 21:39:11 +02:00
|
|
|
end = count;
|
|
|
|
else
|
|
|
|
end = numberOfElements-1;
|
|
|
|
|
|
|
|
for(i=0; i<end && src[i]; i++)
|
|
|
|
dest[i] = src[i];
|
|
|
|
|
2020-12-04 15:50:17 +01:00
|
|
|
if(!src[i] || end==count || count==_TRUNCATE) {
|
2010-04-20 21:39:11 +02:00
|
|
|
dest[i] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", EINVAL);
|
2010-04-20 21:39:11 +02:00
|
|
|
dest[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-04-20 21:39:11 +02:00
|
|
|
}
|
2014-04-22 14:38:47 +02:00
|
|
|
|
|
|
|
BOOL msvcrt_init_heap(void)
|
|
|
|
{
|
|
|
|
heap = HeapCreate(0, 0, 0);
|
|
|
|
return heap != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void msvcrt_destroy_heap(void)
|
|
|
|
{
|
|
|
|
HeapDestroy(heap);
|
2014-06-18 11:35:19 +02:00
|
|
|
if(sb_heap)
|
|
|
|
HeapDestroy(sb_heap);
|
2014-04-22 14:38:47 +02:00
|
|
|
}
|