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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 */
|
2002-02-21 21:22:00 +01:00
|
|
|
#define LOCK_HEAP _mlock( _HEAP_LOCK )
|
|
|
|
#define UNLOCK_HEAP _munlock( _HEAP_LOCK )
|
|
|
|
|
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))
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2011-09-13 10:30:17 +02:00
|
|
|
typedef int (CDECL *MSVCRT_new_handler_func)(MSVCRT_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 */
|
2007-08-08 14:07:57 +02:00
|
|
|
static MSVCRT_size_t MSVCRT_sbh_threshold = 0;
|
2001-04-23 20:20:55 +02:00
|
|
|
|
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
|
|
|
*/
|
2008-12-11 21:01:38 +01:00
|
|
|
void* CDECL MSVCRT_operator_new(MSVCRT_size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2011-09-13 10:30:17 +02:00
|
|
|
void *retval;
|
|
|
|
int freed;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
retval = HeapAlloc(GetProcessHeap(), 0, size);
|
|
|
|
if(retval)
|
|
|
|
{
|
|
|
|
TRACE("(%ld) returning %p\n", size, retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_HEAP;
|
|
|
|
if(MSVCRT_new_handler)
|
|
|
|
freed = (*MSVCRT_new_handler)(size);
|
|
|
|
else
|
|
|
|
freed = 0;
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
} while(freed);
|
|
|
|
|
|
|
|
TRACE("(%ld) out of memory\n", size);
|
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2010-04-13 17:19:27 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ??2@YAPAXIHPBDH@Z (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void* CDECL MSVCRT_operator_new_dbg(MSVCRT_size_t size, int type, const char *file, int line)
|
|
|
|
{
|
|
|
|
return MSVCRT_operator_new( size );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void CDECL MSVCRT_operator_delete(void *mem)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
TRACE("(%p)\n", mem);
|
|
|
|
HeapFree(GetProcessHeap(), 0, mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_new_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ?_query_new_mode@@YAHXZ (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL MSVCRT__query_new_mode(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_new_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_new_handler_func CDECL MSVCRT__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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
|
2001-12-21 21:27:39 +01:00
|
|
|
{
|
|
|
|
TRACE("(%p)\n",func);
|
|
|
|
MSVCRT__set_new_handler(NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* ?_set_new_mode@@YAHH@Z (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL MSVCRT__set_new_mode(int mode)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int old_mode;
|
|
|
|
LOCK_HEAP;
|
|
|
|
old_mode = MSVCRT_new_mode;
|
|
|
|
MSVCRT_new_mode = mode;
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
return old_mode;
|
|
|
|
}
|
|
|
|
|
2001-12-21 21:27:39 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _callnewh (MSVCRT.@)
|
|
|
|
*/
|
2008-12-11 21:01:38 +01:00
|
|
|
int CDECL _callnewh(MSVCRT_size_t size)
|
2001-12-21 21:27:39 +01:00
|
|
|
{
|
|
|
|
if(MSVCRT_new_handler)
|
|
|
|
(*MSVCRT_new_handler)(size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _expand (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void* CDECL _expand(void* mem, MSVCRT_size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapchk (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _heapchk(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (!HeapValidate( GetProcessHeap(), 0, NULL))
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
return MSVCRT__HEAPBADNODE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2004-06-25 03:19:15 +02:00
|
|
|
return MSVCRT__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
|
|
|
{
|
|
|
|
if (!HeapCompact( GetProcessHeap(), 0 ))
|
|
|
|
{
|
|
|
|
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.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
PROCESS_HEAP_ENTRY phe;
|
|
|
|
|
|
|
|
LOCK_HEAP;
|
|
|
|
phe.lpData = next->_pentry;
|
|
|
|
phe.cbData = next->_size;
|
2004-06-25 03:19:15 +02:00
|
|
|
phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
|
|
|
|
!HeapValidate( GetProcessHeap(), 0, phe.lpData ))
|
|
|
|
{
|
|
|
|
UNLOCK_HEAP;
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
|
|
|
return MSVCRT__HEAPBADNODE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!HeapWalk( GetProcessHeap(), &phe ))
|
|
|
|
{
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_ITEMS)
|
2004-06-25 03:19:15 +02:00
|
|
|
return MSVCRT__HEAPEND;
|
|
|
|
msvcrt_set_errno(GetLastError());
|
2001-01-11 00:59:25 +01:00
|
|
|
if (!phe.lpData)
|
2004-06-25 03:19:15 +02:00
|
|
|
return MSVCRT__HEAPBADBEGIN;
|
|
|
|
return MSVCRT__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;
|
2004-06-25 03:19:15 +02:00
|
|
|
next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
|
|
|
|
return MSVCRT__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;
|
2004-06-25 03:19:15 +02:00
|
|
|
struct MSVCRT__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;
|
2004-06-25 03:19:15 +02:00
|
|
|
while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
if (heap._useflag == MSVCRT__FREEENTRY)
|
2001-01-11 00:59:25 +01:00
|
|
|
memset(heap._pentry, value, heap._size);
|
|
|
|
}
|
|
|
|
UNLOCK_HEAP;
|
2004-06-25 03:19:15 +02:00
|
|
|
return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-12-21 21:27:39 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _heapadd (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _heapadd(void* mem, MSVCRT_size_t size)
|
2001-12-21 21:27:39 +01:00
|
|
|
{
|
2008-12-11 21:01:38 +01:00
|
|
|
TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_ENOSYS;
|
2001-12-21 21:27:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-20 21:39:22 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _heapadd (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
MSVCRT_intptr_t CDECL _get_heap_handle(void)
|
|
|
|
{
|
|
|
|
return (MSVCRT_intptr_t)GetProcessHeap();
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _msize (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_size_t CDECL _msize(void* mem)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2008-12-11 21:01:38 +01:00
|
|
|
MSVCRT_size_t size = HeapSize(GetProcessHeap(),0,mem);
|
|
|
|
if (size == ~(MSVCRT_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* calloc (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* free (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void CDECL MSVCRT_free(void* ptr)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(),0,ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* malloc (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
void *ret = HeapAlloc(GetProcessHeap(),0,size);
|
|
|
|
if (!ret)
|
2008-02-27 12:24:09 +01:00
|
|
|
*MSVCRT__errno() = MSVCRT_ENOMEM;
|
2001-01-11 00:59:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* realloc (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2003-11-21 00:41:13 +01:00
|
|
|
if (!ptr) return MSVCRT_malloc(size);
|
|
|
|
if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
|
|
|
|
MSVCRT_free(ptr);
|
|
|
|
return NULL;
|
2001-01-11 00:59:25 +01: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.@)
|
|
|
|
*/
|
2007-08-08 14:07:57 +02:00
|
|
|
MSVCRT_size_t CDECL _get_sbh_threshold(void)
|
2005-09-23 12:07:05 +02:00
|
|
|
{
|
|
|
|
return MSVCRT_sbh_threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _set_sbh_threshold (MSVCRT.@)
|
|
|
|
*/
|
2007-08-08 14:07:57 +02:00
|
|
|
int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
|
2005-09-23 12:07:05 +02:00
|
|
|
{
|
|
|
|
if(threshold > 1016)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
MSVCRT_sbh_threshold = threshold;
|
|
|
|
return 1;
|
|
|
|
}
|
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);
|
|
|
|
MSVCRT_free(*saved);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _aligned_offset_malloc (MSVCRT.@)
|
|
|
|
*/
|
2007-08-08 14:07:57 +02:00
|
|
|
void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
|
|
|
void *memblock, *temp, **saved;
|
2008-12-11 21:01:38 +01:00
|
|
|
TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
|
2007-07-28 02:02:23 +02:00
|
|
|
|
|
|
|
/* alignment must be a power of 2 */
|
|
|
|
if ((alignment & (alignment - 1)) != 0)
|
|
|
|
{
|
2008-02-27 12:24:09 +01:00
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
2007-07-28 02:02:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* offset must be less than size */
|
|
|
|
if (offset >= size)
|
|
|
|
{
|
2008-02-27 12:24:09 +01:00
|
|
|
*MSVCRT__errno() = MSVCRT_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 */
|
|
|
|
temp = MSVCRT_malloc(size + alignment + sizeof(void *));
|
|
|
|
|
|
|
|
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.@)
|
|
|
|
*/
|
2007-08-08 14:07:57 +02:00
|
|
|
void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
2008-12-11 21:01:38 +01:00
|
|
|
TRACE("(%lu, %lu)\n", size, alignment);
|
2007-07-28 02:02:23 +02:00
|
|
|
return _aligned_offset_malloc(size, alignment, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _aligned_offset_realloc (MSVCRT.@)
|
|
|
|
*/
|
2007-08-08 14:07:57 +02:00
|
|
|
void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
|
|
|
|
MSVCRT_size_t alignment, MSVCRT_size_t offset)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
|
|
|
void * temp, **saved;
|
2007-11-28 11:47:18 +01:00
|
|
|
MSVCRT_size_t old_padding, new_padding, old_size;
|
2008-12-11 21:01:38 +01:00
|
|
|
TRACE("(%p, %lu, %lu, %lu)\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)
|
|
|
|
{
|
2008-02-27 12:24:09 +01:00
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
2007-07-28 02:02:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* offset must be less than size */
|
|
|
|
if (offset >= size)
|
|
|
|
{
|
2008-02-27 12:24:09 +01:00
|
|
|
*MSVCRT__errno() = MSVCRT_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))
|
|
|
|
{
|
2008-02-27 12:24:09 +01:00
|
|
|
*MSVCRT__errno() = MSVCRT_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
|
|
|
|
|
|
|
temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
|
|
|
|
|
|
|
|
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
|
|
|
|
(because it was copied by MSVCRT_realloc):
|
|
|
|
+-------+---------------------+--------------------------------+-------+
|
|
|
|
| ... | "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.@)
|
|
|
|
*/
|
2007-08-08 14:07:57 +02:00
|
|
|
void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
|
2007-07-28 02:02:23 +02:00
|
|
|
{
|
2008-12-11 21:01:38 +01:00
|
|
|
TRACE("(%p, %lu, %lu)\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.@)
|
|
|
|
*/
|
|
|
|
int CDECL memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
|
|
|
|
{
|
|
|
|
TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
|
|
|
|
|
|
|
|
if(!count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(!dest || !src) {
|
|
|
|
if(dest)
|
|
|
|
memset(dest, 0, numberOfElements);
|
|
|
|
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return MSVCRT_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count > numberOfElements) {
|
|
|
|
memset(dest, 0, numberOfElements);
|
|
|
|
|
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
|
|
|
return MSVCRT_ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(dest, src, count);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-04-20 21:39:11 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* strncpy_s (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
|
|
|
|
const char *src, MSVCRT_size_t count)
|
|
|
|
{
|
|
|
|
MSVCRT_size_t i, end;
|
|
|
|
|
|
|
|
TRACE("(%s %lu %s %lu)\n", dest, numberOfElements, src, count);
|
|
|
|
|
|
|
|
if(!count)
|
|
|
|
return 0;
|
|
|
|
|
2010-10-29 15:39:03 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(dest != NULL) || !MSVCRT_CHECK_PMT(src != NULL) || !MSVCRT_CHECK_PMT(numberOfElements != 0)) {
|
2010-04-20 21:39:11 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return MSVCRT_EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-06-06 17:11:09 +02:00
|
|
|
if(count!=MSVCRT__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];
|
|
|
|
|
2010-06-06 17:11:09 +02:00
|
|
|
if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
|
2010-04-20 21:39:11 +02:00
|
|
|
dest[i] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-29 15:39:03 +02:00
|
|
|
MSVCRT_INVALID_PMT("dest[numberOfElements] is too small");
|
2010-04-20 21:39:11 +02:00
|
|
|
dest[0] = '\0';
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return MSVCRT_EINVAL;
|
|
|
|
}
|