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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: Win32 heap operations are MT safe. We only lock the new
21 * handler and non atomic heap operations
25 #include "msvcrt/errno.h"
27 #include "msvcrt/malloc.h"
28 #include "msvcrt/stdlib.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 #define LOCK_HEAP _mlock( _HEAP_LOCK )
37 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
40 typedef void (*MSVCRT_new_handler_func)(unsigned long size);
42 static MSVCRT_new_handler_func MSVCRT_new_handler;
43 static int MSVCRT_new_mode;
46 /*********************************************************************
47 * ??2@YAPAXI@Z (MSVCRT.@)
49 void* MSVCRT_operator_new(unsigned long size)
51 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
52 TRACE("(%ld) returning %p\n", size, retval);
54 if(!retval && MSVCRT_new_handler)
55 (*MSVCRT_new_handler)(size);
60 /*********************************************************************
61 * ??3@YAXPAX@Z (MSVCRT.@)
63 void MSVCRT_operator_delete(void *mem)
66 HeapFree(GetProcessHeap(), 0, mem);
70 /*********************************************************************
71 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
73 MSVCRT_new_handler_func MSVCRT__query_new_handler(void)
75 return MSVCRT_new_handler;
79 /*********************************************************************
80 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
82 int MSVCRT__query_new_mode(void)
84 return MSVCRT_new_mode;
87 /*********************************************************************
88 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
90 MSVCRT_new_handler_func MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
92 MSVCRT_new_handler_func old_handler;
94 old_handler = MSVCRT_new_handler;
95 MSVCRT_new_handler = func;
100 /*********************************************************************
101 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
103 MSVCRT_new_handler_func MSVCRT_set_new_handler(void *func)
105 TRACE("(%p)\n",func);
106 MSVCRT__set_new_handler(NULL);
110 /*********************************************************************
111 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
113 int MSVCRT__set_new_mode(int mode)
117 old_mode = MSVCRT_new_mode;
118 MSVCRT_new_mode = mode;
123 /*********************************************************************
124 * _callnewh (MSVCRT.@)
126 int _callnewh(unsigned long size)
128 if(MSVCRT_new_handler)
129 (*MSVCRT_new_handler)(size);
133 /*********************************************************************
136 void* _expand(void* mem, MSVCRT_size_t size)
138 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
141 /*********************************************************************
142 * _heapchk (MSVCRT.@)
146 if (!HeapValidate( GetProcessHeap(), 0, NULL))
148 MSVCRT__set_errno(GetLastError());
154 /*********************************************************************
155 * _heapmin (MSVCRT.@)
159 if (!HeapCompact( GetProcessHeap(), 0 ))
161 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
162 MSVCRT__set_errno(GetLastError());
168 /*********************************************************************
169 * _heapwalk (MSVCRT.@)
171 int _heapwalk(_HEAPINFO* next)
173 PROCESS_HEAP_ENTRY phe;
176 phe.lpData = next->_pentry;
177 phe.cbData = next->_size;
178 phe.wFlags = next->_useflag == _USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
180 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
181 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
184 MSVCRT__set_errno(GetLastError());
190 if (!HeapWalk( GetProcessHeap(), &phe ))
193 if (GetLastError() == ERROR_NO_MORE_ITEMS)
195 MSVCRT__set_errno(GetLastError());
197 return _HEAPBADBEGIN;
200 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
203 next->_pentry = phe.lpData;
204 next->_size = phe.cbData;
205 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? _USEDENTRY : _FREEENTRY;
209 /*********************************************************************
210 * _heapset (MSVCRT.@)
212 int _heapset(unsigned int value)
217 memset( &heap, 0, sizeof(_HEAPINFO) );
219 while ((retval = _heapwalk(&heap)) == _HEAPOK)
221 if (heap._useflag == _FREEENTRY)
222 memset(heap._pentry, value, heap._size);
225 return retval == _HEAPEND? _HEAPOK : retval;
228 /*********************************************************************
229 * _heapadd (MSVCRT.@)
231 int _heapadd(void* mem, MSVCRT_size_t size)
233 TRACE("(%p,%d) unsupported in Win32\n", mem,size);
234 *MSVCRT__errno() = MSVCRT_ENOSYS;
238 /*********************************************************************
241 MSVCRT_size_t _msize(void* mem)
243 long size = HeapSize(GetProcessHeap(),0,mem);
246 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
247 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
252 /*********************************************************************
255 void* MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
257 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
260 /*********************************************************************
263 void MSVCRT_free(void* ptr)
265 HeapFree(GetProcessHeap(),0,ptr);
268 /*********************************************************************
271 void* MSVCRT_malloc(MSVCRT_size_t size)
273 void *ret = HeapAlloc(GetProcessHeap(),0,size);
275 MSVCRT__set_errno(GetLastError());
279 /*********************************************************************
282 void* MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
284 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);