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
27 #include "msvcrt/malloc.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35 #define LOCK_HEAP _mlock( _HEAP_LOCK )
36 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
39 typedef void (*MSVCRT_new_handler_func)(unsigned long size);
41 static MSVCRT_new_handler_func MSVCRT_new_handler;
42 static int MSVCRT_new_mode;
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());
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(_HEAPINFO* next)
172 PROCESS_HEAP_ENTRY phe;
175 phe.lpData = next->_pentry;
176 phe.cbData = next->_size;
177 phe.wFlags = next->_useflag == _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());
189 if (!HeapWalk( GetProcessHeap(), &phe ))
192 if (GetLastError() == ERROR_NO_MORE_ITEMS)
194 MSVCRT__set_errno(GetLastError());
196 return _HEAPBADBEGIN;
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 ? _USEDENTRY : _FREEENTRY;
208 /*********************************************************************
209 * _heapset (MSVCRT.@)
211 int _heapset(unsigned int value)
216 memset( &heap, 0, sizeof(_HEAPINFO) );
218 while ((retval = _heapwalk(&heap)) == _HEAPOK)
220 if (heap._useflag == _FREEENTRY)
221 memset(heap._pentry, value, heap._size);
224 return retval == _HEAPEND? _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 SET_THREAD_VAR(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 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);