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
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31 #define LOCK_HEAP _mlock( _HEAP_LOCK )
32 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
35 typedef void (*MSVCRT_new_handler_func)(unsigned long size);
37 static MSVCRT_new_handler_func MSVCRT_new_handler;
38 static int MSVCRT_new_mode;
40 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
41 static unsigned int MSVCRT_amblksiz = 16;
43 /*********************************************************************
44 * ??2@YAPAXI@Z (MSVCRT.@)
46 void* MSVCRT_operator_new(unsigned long size)
48 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
49 TRACE("(%ld) returning %p\n", size, retval);
51 if(!retval && MSVCRT_new_handler)
52 (*MSVCRT_new_handler)(size);
57 /*********************************************************************
58 * ??3@YAXPAX@Z (MSVCRT.@)
60 void MSVCRT_operator_delete(void *mem)
63 HeapFree(GetProcessHeap(), 0, mem);
67 /*********************************************************************
68 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
70 MSVCRT_new_handler_func MSVCRT__query_new_handler(void)
72 return MSVCRT_new_handler;
76 /*********************************************************************
77 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
79 int MSVCRT__query_new_mode(void)
81 return MSVCRT_new_mode;
84 /*********************************************************************
85 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
87 MSVCRT_new_handler_func MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
89 MSVCRT_new_handler_func old_handler;
91 old_handler = MSVCRT_new_handler;
92 MSVCRT_new_handler = func;
97 /*********************************************************************
98 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
100 MSVCRT_new_handler_func MSVCRT_set_new_handler(void *func)
102 TRACE("(%p)\n",func);
103 MSVCRT__set_new_handler(NULL);
107 /*********************************************************************
108 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
110 int MSVCRT__set_new_mode(int mode)
114 old_mode = MSVCRT_new_mode;
115 MSVCRT_new_mode = mode;
120 /*********************************************************************
121 * _callnewh (MSVCRT.@)
123 int _callnewh(unsigned long size)
125 if(MSVCRT_new_handler)
126 (*MSVCRT_new_handler)(size);
130 /*********************************************************************
133 void* _expand(void* mem, MSVCRT_size_t size)
135 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
138 /*********************************************************************
139 * _heapchk (MSVCRT.@)
143 if (!HeapValidate( GetProcessHeap(), 0, NULL))
145 msvcrt_set_errno(GetLastError());
146 return MSVCRT__HEAPBADNODE;
148 return MSVCRT__HEAPOK;
151 /*********************************************************************
152 * _heapmin (MSVCRT.@)
156 if (!HeapCompact( GetProcessHeap(), 0 ))
158 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
159 msvcrt_set_errno(GetLastError());
165 /*********************************************************************
166 * _heapwalk (MSVCRT.@)
168 int _heapwalk(struct MSVCRT__heapinfo* next)
170 PROCESS_HEAP_ENTRY phe;
173 phe.lpData = next->_pentry;
174 phe.cbData = next->_size;
175 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
177 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
178 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
181 msvcrt_set_errno(GetLastError());
182 return MSVCRT__HEAPBADNODE;
187 if (!HeapWalk( GetProcessHeap(), &phe ))
190 if (GetLastError() == ERROR_NO_MORE_ITEMS)
191 return MSVCRT__HEAPEND;
192 msvcrt_set_errno(GetLastError());
194 return MSVCRT__HEAPBADBEGIN;
195 return MSVCRT__HEAPBADNODE;
197 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
200 next->_pentry = phe.lpData;
201 next->_size = phe.cbData;
202 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
203 return MSVCRT__HEAPOK;
206 /*********************************************************************
207 * _heapset (MSVCRT.@)
209 int _heapset(unsigned int value)
212 struct MSVCRT__heapinfo heap;
214 memset( &heap, 0, sizeof(heap) );
216 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
218 if (heap._useflag == MSVCRT__FREEENTRY)
219 memset(heap._pentry, value, heap._size);
222 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
225 /*********************************************************************
226 * _heapadd (MSVCRT.@)
228 int _heapadd(void* mem, MSVCRT_size_t size)
230 TRACE("(%p,%d) unsupported in Win32\n", mem,size);
231 *MSVCRT__errno() = MSVCRT_ENOSYS;
235 /*********************************************************************
238 MSVCRT_size_t _msize(void* mem)
240 long size = HeapSize(GetProcessHeap(),0,mem);
243 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
244 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
249 /*********************************************************************
252 void* MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
254 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
257 /*********************************************************************
260 void MSVCRT_free(void* ptr)
262 HeapFree(GetProcessHeap(),0,ptr);
265 /*********************************************************************
268 void* MSVCRT_malloc(MSVCRT_size_t size)
270 void *ret = HeapAlloc(GetProcessHeap(),0,size);
272 msvcrt_set_errno(GetLastError());
276 /*********************************************************************
279 void* MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
281 if (!ptr) return MSVCRT_malloc(size);
282 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
287 /*********************************************************************
288 * __p__amblksiz (MSVCRT.@)
290 unsigned int* __p__amblksiz(void)
292 return &MSVCRT_amblksiz;