Fixed HeapRealloc typo.
[wine/wine64.git] / dlls / msvcrt / heap.c
blob35994cdad1bfa1da1fe74ca5fa9d6b76adada494
1 /*
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
24 #include "msvcrt.h"
25 #include "msvcrt/errno.h"
27 #include "msvcrt/malloc.h"
28 #include "msvcrt/stdlib.h"
29 #include "mtdll.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35 /* MT */
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);
53 LOCK_HEAP;
54 if(!retval && MSVCRT_new_handler)
55 (*MSVCRT_new_handler)(size);
56 UNLOCK_HEAP;
57 return retval;
60 /*********************************************************************
61 * ??3@YAXPAX@Z (MSVCRT.@)
63 void MSVCRT_operator_delete(void *mem)
65 TRACE("(%p)\n", 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;
93 LOCK_HEAP;
94 old_handler = MSVCRT_new_handler;
95 MSVCRT_new_handler = func;
96 UNLOCK_HEAP;
97 return old_handler;
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);
107 return NULL;
110 /*********************************************************************
111 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
113 int MSVCRT__set_new_mode(int mode)
115 int old_mode;
116 LOCK_HEAP;
117 old_mode = MSVCRT_new_mode;
118 MSVCRT_new_mode = mode;
119 UNLOCK_HEAP;
120 return old_mode;
123 /*********************************************************************
124 * _callnewh (MSVCRT.@)
126 int _callnewh(unsigned long size)
128 if(MSVCRT_new_handler)
129 (*MSVCRT_new_handler)(size);
130 return 0;
133 /*********************************************************************
134 * _expand (MSVCRT.@)
136 void* _expand(void* mem, MSVCRT_size_t size)
138 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
141 /*********************************************************************
142 * _heapchk (MSVCRT.@)
144 int _heapchk(void)
146 if (!HeapValidate( GetProcessHeap(), 0, NULL))
148 MSVCRT__set_errno(GetLastError());
149 return _HEAPBADNODE;
151 return _HEAPOK;
154 /*********************************************************************
155 * _heapmin (MSVCRT.@)
157 int _heapmin(void)
159 if (!HeapCompact( GetProcessHeap(), 0 ))
161 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
162 MSVCRT__set_errno(GetLastError());
163 return -1;
165 return 0;
168 /*********************************************************************
169 * _heapwalk (MSVCRT.@)
171 int _heapwalk(_HEAPINFO* next)
173 PROCESS_HEAP_ENTRY phe;
175 LOCK_HEAP;
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 ))
183 UNLOCK_HEAP;
184 MSVCRT__set_errno(GetLastError());
185 return _HEAPBADNODE;
190 if (!HeapWalk( GetProcessHeap(), &phe ))
192 UNLOCK_HEAP;
193 if (GetLastError() == ERROR_NO_MORE_ITEMS)
194 return _HEAPEND;
195 MSVCRT__set_errno(GetLastError());
196 if (!phe.lpData)
197 return _HEAPBADBEGIN;
198 return _HEAPBADNODE;
200 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
202 UNLOCK_HEAP;
203 next->_pentry = phe.lpData;
204 next->_size = phe.cbData;
205 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? _USEDENTRY : _FREEENTRY;
206 return _HEAPOK;
209 /*********************************************************************
210 * _heapset (MSVCRT.@)
212 int _heapset(unsigned int value)
214 int retval;
215 _HEAPINFO heap;
217 memset( &heap, 0, sizeof(_HEAPINFO) );
218 LOCK_HEAP;
219 while ((retval = _heapwalk(&heap)) == _HEAPOK)
221 if (heap._useflag == _FREEENTRY)
222 memset(heap._pentry, value, heap._size);
224 UNLOCK_HEAP;
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;
235 return -1;
238 /*********************************************************************
239 * _msize (MSVCRT.@)
241 MSVCRT_size_t _msize(void* mem)
243 long size = HeapSize(GetProcessHeap(),0,mem);
244 if (size == -1)
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 */
249 return size;
252 /*********************************************************************
253 * calloc (MSVCRT.@)
255 void* MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
257 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
260 /*********************************************************************
261 * free (MSVCRT.@)
263 void MSVCRT_free(void* ptr)
265 HeapFree(GetProcessHeap(),0,ptr);
268 /*********************************************************************
269 * malloc (MSVCRT.@)
271 void* MSVCRT_malloc(MSVCRT_size_t size)
273 void *ret = HeapAlloc(GetProcessHeap(),0,size);
274 if (!ret)
275 MSVCRT__set_errno(GetLastError());
276 return ret;
279 /*********************************************************************
280 * realloc (MSVCRT.@)
282 void* MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
284 if (!ptr) return MSVCRT_malloc(size);
285 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
286 MSVCRT_free(ptr);
287 return NULL;