2 * msvcrt.dll heap functions
4 * Copyright 2000 Jon Griffiths
6 * Note: Win32 heap operations are MT safe. We only lock the new
7 * handler and non atomic heap operations
12 #include "msvcrt/malloc.h"
15 DEFAULT_DEBUG_CHANNEL(msvcrt);
18 extern CRITICAL_SECTION MSVCRT_heap_cs;
19 #define LOCK_HEAP EnterCriticalSection(&MSVCRT_heap_cs)
20 #define UNLOCK_HEAP LeaveCriticalSection(&MSVCRT_heap_cs)
22 typedef void (*MSVCRT_new_handler_func)(void);
24 static MSVCRT_new_handler_func MSVCRT_new_handler;
25 static int MSVCRT_new_mode;
28 /*********************************************************************
29 * ??2@YAPAXI@Z (MSVCRT.@)
31 void* MSVCRT_operator_new(unsigned long size)
33 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
34 TRACE("(%ld) returning %p\n", size, retval);
36 if(retval && MSVCRT_new_handler)
37 (*MSVCRT_new_handler)();
42 /*********************************************************************
43 * ??3@YAXPAX@Z (MSVCRT.@)
45 void MSVCRT_operator_delete(void *mem)
48 HeapFree(GetProcessHeap(), 0, mem);
52 /*********************************************************************
53 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
55 MSVCRT_new_handler_func MSVCRT__query_new_handler(void)
57 return MSVCRT_new_handler;
61 /*********************************************************************
62 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
64 int MSVCRT__query_new_mode(void)
66 return MSVCRT_new_mode;
69 /*********************************************************************
70 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
72 MSVCRT_new_handler_func MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
74 MSVCRT_new_handler_func old_handler;
76 old_handler = MSVCRT_new_handler;
77 MSVCRT_new_handler = func;
82 /*********************************************************************
83 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
85 int MSVCRT__set_new_mode(int mode)
89 old_mode = MSVCRT_new_mode;
90 MSVCRT_new_mode = mode;
95 /*********************************************************************
98 void* _expand(void* mem, MSVCRT_size_t size)
100 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
103 /*********************************************************************
104 * _heapchk (MSVCRT.@)
108 if (!HeapValidate( GetProcessHeap(), 0, NULL))
110 MSVCRT__set_errno(GetLastError());
116 /*********************************************************************
117 * _heapmin (MSVCRT.@)
121 if (!HeapCompact( GetProcessHeap(), 0 ))
123 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
124 MSVCRT__set_errno(GetLastError());
130 /*********************************************************************
131 * _heapwalk (MSVCRT.@)
133 int _heapwalk(_HEAPINFO* next)
135 PROCESS_HEAP_ENTRY phe;
138 phe.lpData = next->_pentry;
139 phe.cbData = next->_size;
140 phe.wFlags = next->_useflag == _USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
142 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
143 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
146 MSVCRT__set_errno(GetLastError());
152 if (!HeapWalk( GetProcessHeap(), &phe ))
155 if (GetLastError() == ERROR_NO_MORE_ITEMS)
157 MSVCRT__set_errno(GetLastError());
159 return _HEAPBADBEGIN;
162 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
165 next->_pentry = phe.lpData;
166 next->_size = phe.cbData;
167 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? _USEDENTRY : _FREEENTRY;
171 /*********************************************************************
172 * _heapset (MSVCRT.@)
174 int _heapset(unsigned int value)
179 memset( &heap, 0, sizeof(_HEAPINFO) );
181 while ((retval = _heapwalk(&heap)) == _HEAPOK)
183 if (heap._useflag == _FREEENTRY)
184 memset(heap._pentry, value, heap._size);
187 return retval == _HEAPEND? _HEAPOK : retval;
190 /*********************************************************************
193 MSVCRT_size_t _msize(void* mem)
195 long size = HeapSize(GetProcessHeap(),0,mem);
198 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
199 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
204 /*********************************************************************
207 void* MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
209 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
212 /*********************************************************************
215 void MSVCRT_free(void* ptr)
217 HeapFree(GetProcessHeap(),0,ptr);
220 /*********************************************************************
223 void* MSVCRT_malloc(MSVCRT_size_t size)
225 void *ret = HeapAlloc(GetProcessHeap(),0,size);
227 MSVCRT__set_errno(GetLastError());
231 /*********************************************************************
234 void* MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
236 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);