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* CDECL
MSVCRT_operator_new(unsigned long size
)
50 void *retval
= HeapAlloc(GetProcessHeap(), 0, size
);
51 TRACE("(%ld) returning %p\n", size
, retval
);
52 if(retval
) return retval
;
54 if(MSVCRT_new_handler
)
55 (*MSVCRT_new_handler
)(size
);
60 /*********************************************************************
61 * ??3@YAXPAX@Z (MSVCRT.@)
63 void CDECL
MSVCRT_operator_delete(void *mem
)
66 HeapFree(GetProcessHeap(), 0, mem
);
70 /*********************************************************************
71 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
73 MSVCRT_new_handler_func CDECL
MSVCRT__query_new_handler(void)
75 return MSVCRT_new_handler
;
79 /*********************************************************************
80 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
82 int CDECL
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 CDECL
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 CDECL
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 CDECL
MSVCRT__set_new_mode(int mode
)
117 old_mode
= MSVCRT_new_mode
;
118 MSVCRT_new_mode
= mode
;
123 /*********************************************************************
124 * _callnewh (MSVCRT.@)
126 int CDECL
_callnewh(unsigned long size
)
128 if(MSVCRT_new_handler
)
129 (*MSVCRT_new_handler
)(size
);
133 /*********************************************************************
136 void* CDECL
_expand(void* mem
, MSVCRT_size_t size
)
138 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, mem
, size
);
141 /*********************************************************************
142 * _heapchk (MSVCRT.@)
144 int CDECL
_heapchk(void)
146 if (!HeapValidate( GetProcessHeap(), 0, NULL
))
148 msvcrt_set_errno(GetLastError());
149 return MSVCRT__HEAPBADNODE
;
151 return MSVCRT__HEAPOK
;
154 /*********************************************************************
155 * _heapmin (MSVCRT.@)
157 int CDECL
_heapmin(void)
159 if (!HeapCompact( GetProcessHeap(), 0 ))
161 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
162 msvcrt_set_errno(GetLastError());
168 /*********************************************************************
169 * _heapwalk (MSVCRT.@)
171 int CDECL
_heapwalk(struct MSVCRT__heapinfo
* next
)
173 PROCESS_HEAP_ENTRY phe
;
176 phe
.lpData
= next
->_pentry
;
177 phe
.cbData
= next
->_size
;
178 phe
.wFlags
= next
->_useflag
== MSVCRT__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());
185 return MSVCRT__HEAPBADNODE
;
190 if (!HeapWalk( GetProcessHeap(), &phe
))
193 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
194 return MSVCRT__HEAPEND
;
195 msvcrt_set_errno(GetLastError());
197 return MSVCRT__HEAPBADBEGIN
;
198 return MSVCRT__HEAPBADNODE
;
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
? MSVCRT__USEDENTRY
: MSVCRT__FREEENTRY
;
206 return MSVCRT__HEAPOK
;
209 /*********************************************************************
210 * _heapset (MSVCRT.@)
212 int CDECL
_heapset(unsigned int value
)
215 struct MSVCRT__heapinfo heap
;
217 memset( &heap
, 0, sizeof(heap
) );
219 while ((retval
= _heapwalk(&heap
)) == MSVCRT__HEAPOK
)
221 if (heap
._useflag
== MSVCRT__FREEENTRY
)
222 memset(heap
._pentry
, value
, heap
._size
);
225 return retval
== MSVCRT__HEAPEND
? MSVCRT__HEAPOK
: retval
;
228 /*********************************************************************
229 * _heapadd (MSVCRT.@)
231 int CDECL
_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 CDECL
_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* CDECL
MSVCRT_calloc(MSVCRT_size_t size
, MSVCRT_size_t count
)
257 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
260 /*********************************************************************
263 void CDECL
MSVCRT_free(void* ptr
)
265 HeapFree(GetProcessHeap(),0,ptr
);
268 /*********************************************************************
271 void* CDECL
MSVCRT_malloc(MSVCRT_size_t size
)
273 void *ret
= HeapAlloc(GetProcessHeap(),0,size
);
275 msvcrt_set_errno(GetLastError());
279 /*********************************************************************
282 void* CDECL
MSVCRT_realloc(void* ptr
, MSVCRT_size_t size
)
284 if (!ptr
) return MSVCRT_malloc(size
);
285 if (size
) return HeapReAlloc(GetProcessHeap(), 0, ptr
, size
);
290 /*********************************************************************
291 * __p__amblksiz (MSVCRT.@)
293 unsigned int* CDECL
__p__amblksiz(void)
295 return &MSVCRT_amblksiz
;
298 /*********************************************************************
299 * _get_sbh_threshold (MSVCRT.@)
301 size_t CDECL
_get_sbh_threshold(void)
303 return MSVCRT_sbh_threshold
;
306 /*********************************************************************
307 * _set_sbh_threshold (MSVCRT.@)
309 int CDECL
_set_sbh_threshold(size_t threshold
)
314 MSVCRT_sbh_threshold
= threshold
;