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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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;
42 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
43 static size_t MSVCRT_sbh_threshold = 0;
45 /*********************************************************************
46 * ??2@YAPAXI@Z (MSVCRT.@)
48 void* MSVCRT_operator_new(unsigned long size)
50 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
51 TRACE("(%ld) returning %p\n", size, retval);
53 if(!retval && MSVCRT_new_handler)
54 (*MSVCRT_new_handler)(size);
59 /*********************************************************************
60 * ??3@YAXPAX@Z (MSVCRT.@)
62 void MSVCRT_operator_delete(void *mem)
65 HeapFree(GetProcessHeap(), 0, mem);
69 /*********************************************************************
70 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
72 MSVCRT_new_handler_func MSVCRT__query_new_handler(void)
74 return MSVCRT_new_handler;
78 /*********************************************************************
79 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
81 int MSVCRT__query_new_mode(void)
83 return MSVCRT_new_mode;
86 /*********************************************************************
87 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
89 MSVCRT_new_handler_func MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
91 MSVCRT_new_handler_func old_handler;
93 old_handler = MSVCRT_new_handler;
94 MSVCRT_new_handler = func;
99 /*********************************************************************
100 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
102 MSVCRT_new_handler_func MSVCRT_set_new_handler(void *func)
104 TRACE("(%p)\n",func);
105 MSVCRT__set_new_handler(NULL);
109 /*********************************************************************
110 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
112 int MSVCRT__set_new_mode(int mode)
116 old_mode = MSVCRT_new_mode;
117 MSVCRT_new_mode = mode;
122 /*********************************************************************
123 * _callnewh (MSVCRT.@)
125 int _callnewh(unsigned long size)
127 if(MSVCRT_new_handler)
128 (*MSVCRT_new_handler)(size);
132 /*********************************************************************
135 void* _expand(void* mem, MSVCRT_size_t size)
137 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
140 /*********************************************************************
141 * _heapchk (MSVCRT.@)
145 if (!HeapValidate( GetProcessHeap(), 0, NULL))
147 msvcrt_set_errno(GetLastError());
148 return MSVCRT__HEAPBADNODE;
150 return MSVCRT__HEAPOK;
153 /*********************************************************************
154 * _heapmin (MSVCRT.@)
158 if (!HeapCompact( GetProcessHeap(), 0 ))
160 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
161 msvcrt_set_errno(GetLastError());
167 /*********************************************************************
168 * _heapwalk (MSVCRT.@)
170 int _heapwalk(struct MSVCRT__heapinfo* next)
172 PROCESS_HEAP_ENTRY phe;
175 phe.lpData = next->_pentry;
176 phe.cbData = next->_size;
177 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
179 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
180 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
183 msvcrt_set_errno(GetLastError());
184 return MSVCRT__HEAPBADNODE;
189 if (!HeapWalk( GetProcessHeap(), &phe ))
192 if (GetLastError() == ERROR_NO_MORE_ITEMS)
193 return MSVCRT__HEAPEND;
194 msvcrt_set_errno(GetLastError());
196 return MSVCRT__HEAPBADBEGIN;
197 return MSVCRT__HEAPBADNODE;
199 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
202 next->_pentry = phe.lpData;
203 next->_size = phe.cbData;
204 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
205 return MSVCRT__HEAPOK;
208 /*********************************************************************
209 * _heapset (MSVCRT.@)
211 int _heapset(unsigned int value)
214 struct MSVCRT__heapinfo heap;
216 memset( &heap, 0, sizeof(heap) );
218 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
220 if (heap._useflag == MSVCRT__FREEENTRY)
221 memset(heap._pentry, value, heap._size);
224 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
227 /*********************************************************************
228 * _heapadd (MSVCRT.@)
230 int _heapadd(void* mem, MSVCRT_size_t size)
232 TRACE("(%p,%d) unsupported in Win32\n", mem,size);
233 *MSVCRT__errno() = MSVCRT_ENOSYS;
237 /*********************************************************************
240 MSVCRT_size_t _msize(void* mem)
242 long size = HeapSize(GetProcessHeap(),0,mem);
245 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
246 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
251 /*********************************************************************
254 void* MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
256 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
259 /*********************************************************************
262 void MSVCRT_free(void* ptr)
264 HeapFree(GetProcessHeap(),0,ptr);
267 /*********************************************************************
270 void* MSVCRT_malloc(MSVCRT_size_t size)
272 void *ret = HeapAlloc(GetProcessHeap(),0,size);
274 msvcrt_set_errno(GetLastError());
278 /*********************************************************************
281 void* MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
283 if (!ptr) return MSVCRT_malloc(size);
284 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
289 /*********************************************************************
290 * __p__amblksiz (MSVCRT.@)
292 unsigned int* __p__amblksiz(void)
294 return &MSVCRT_amblksiz;
297 /*********************************************************************
298 * _get_sbh_threshold (MSVCRT.@)
300 size_t _get_sbh_threshold(void)
302 return MSVCRT_sbh_threshold;
305 /*********************************************************************
306 * _set_sbh_threshold (MSVCRT.@)
308 int _set_sbh_threshold(size_t threshold)
313 MSVCRT_sbh_threshold = threshold;