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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
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"
|
2001-12-21 21:27:39 +01:00
|
|
|
#include "ms_errno.h"
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-04-23 20:20:55 +02:00
|
|
|
#include "msvcrt/malloc.h"
|
2002-02-21 21:22:00 +01:00
|
|
|
#include "mtdll.h"
|
2001-04-11 01:25:25 +02:00
|
|
|
|
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 )
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-12-21 21:27:39 +01:00
|
|
|
typedef void (*MSVCRT_new_handler_func)(unsigned long size);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
static MSVCRT_new_handler_func MSVCRT_new_handler;
|
|
|
|
static int MSVCRT_new_mode;
|
|
|
|
|
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
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void* MSVCRT_operator_new(unsigned long size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
void *retval = HeapAlloc(GetProcessHeap(), 0, size);
|
|
|
|
TRACE("(%ld) returning %p\n", size, retval);
|
|
|
|
LOCK_HEAP;
|
|
|
|
if(retval && MSVCRT_new_handler)
|
2001-12-21 21:27:39 +01:00
|
|
|
(*MSVCRT_new_handler)(size);
|
2001-01-11 00:59:25 +01:00
|
|
|
UNLOCK_HEAP;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2001-07-02 21:59:40 +02:00
|
|
|
* ??3@YAXPAX@Z (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void 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.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_new_handler_func MSVCRT__query_new_handler(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return MSVCRT_new_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* ?_query_new_mode@@YAHXZ (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int 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.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
MSVCRT_new_handler_func 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.@)
|
|
|
|
*/
|
|
|
|
MSVCRT_new_handler_func MSVCRT_set_new_handler(void *func)
|
|
|
|
{
|
|
|
|
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.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int 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.@)
|
|
|
|
*/
|
|
|
|
int _callnewh(unsigned long size)
|
|
|
|
{
|
|
|
|
if(MSVCRT_new_handler)
|
|
|
|
(*MSVCRT_new_handler)(size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _expand (MSVCRT.@)
|
|
|
|
*/
|
2001-04-23 20:20:55 +02:00
|
|
|
void* _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.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _heapchk(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (!HeapValidate( GetProcessHeap(), 0, NULL))
|
|
|
|
{
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
2001-04-23 20:20:55 +02:00
|
|
|
return _HEAPBADNODE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2001-04-23 20:20:55 +02:00
|
|
|
return _HEAPOK;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapmin (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _heapmin(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (!HeapCompact( GetProcessHeap(), 0 ))
|
|
|
|
{
|
|
|
|
if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapwalk (MSVCRT.@)
|
|
|
|
*/
|
2001-04-23 20:20:55 +02:00
|
|
|
int _heapwalk(_HEAPINFO* next)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
PROCESS_HEAP_ENTRY phe;
|
|
|
|
|
|
|
|
LOCK_HEAP;
|
|
|
|
phe.lpData = next->_pentry;
|
|
|
|
phe.cbData = next->_size;
|
2001-04-23 20:20:55 +02: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 &&
|
|
|
|
!HeapValidate( GetProcessHeap(), 0, phe.lpData ))
|
|
|
|
{
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
2001-04-23 20:20:55 +02:00
|
|
|
return _HEAPBADNODE;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!HeapWalk( GetProcessHeap(), &phe ))
|
|
|
|
{
|
|
|
|
UNLOCK_HEAP;
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_ITEMS)
|
2001-04-23 20:20:55 +02:00
|
|
|
return _HEAPEND;
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
if (!phe.lpData)
|
2001-04-23 20:20:55 +02: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;
|
2001-04-23 20:20:55 +02:00
|
|
|
next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? _USEDENTRY : _FREEENTRY;
|
|
|
|
return _HEAPOK;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _heapset (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
int _heapset(unsigned int value)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
int retval;
|
2001-04-23 20:20:55 +02:00
|
|
|
_HEAPINFO heap;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-04-23 20:20:55 +02:00
|
|
|
memset( &heap, 0, sizeof(_HEAPINFO) );
|
2001-01-11 00:59:25 +01:00
|
|
|
LOCK_HEAP;
|
2001-04-23 20:20:55 +02:00
|
|
|
while ((retval = _heapwalk(&heap)) == _HEAPOK)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-23 20:20:55 +02:00
|
|
|
if (heap._useflag == _FREEENTRY)
|
2001-01-11 00:59:25 +01:00
|
|
|
memset(heap._pentry, value, heap._size);
|
|
|
|
}
|
|
|
|
UNLOCK_HEAP;
|
2001-04-23 20:20:55 +02:00
|
|
|
return retval == _HEAPEND? _HEAPOK : retval;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-12-21 21:27:39 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _heapadd (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int _heapadd(void* mem, MSVCRT_size_t size)
|
|
|
|
{
|
|
|
|
TRACE("(%p,%d) unsupported in Win32\n", mem,size);
|
|
|
|
SET_THREAD_VAR(errno,MSVCRT_ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _msize (MSVCRT.@)
|
|
|
|
*/
|
2001-04-23 20:20:55 +02:00
|
|
|
MSVCRT_size_t _msize(void* mem)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
long size = HeapSize(GetProcessHeap(),0,mem);
|
|
|
|
if (size == -1)
|
|
|
|
{
|
|
|
|
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.@)
|
|
|
|
*/
|
2001-04-23 20:20:55 +02:00
|
|
|
void* 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.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
void MSVCRT_free(void* ptr)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(),0,ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* malloc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-23 20:20:55 +02:00
|
|
|
void* MSVCRT_malloc(MSVCRT_size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
void *ret = HeapAlloc(GetProcessHeap(),0,size);
|
|
|
|
if (!ret)
|
|
|
|
MSVCRT__set_errno(GetLastError());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* realloc (MSVCRT.@)
|
|
|
|
*/
|
2001-04-23 20:20:55 +02:00
|
|
|
void* MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
|
|
|
|
}
|