Implementation of _get_sbh_threshold and _set_sbh_threshold
[wine/multimedia.git] / dlls / msvcrt / heap.c
blob38715e5814c562b6eb6508880088a3fa69c12676
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 "mtdll.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30 /* MT */
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);
52 LOCK_HEAP;
53 if(!retval && MSVCRT_new_handler)
54 (*MSVCRT_new_handler)(size);
55 UNLOCK_HEAP;
56 return retval;
59 /*********************************************************************
60 * ??3@YAXPAX@Z (MSVCRT.@)
62 void MSVCRT_operator_delete(void *mem)
64 TRACE("(%p)\n", 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;
92 LOCK_HEAP;
93 old_handler = MSVCRT_new_handler;
94 MSVCRT_new_handler = func;
95 UNLOCK_HEAP;
96 return old_handler;
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);
106 return NULL;
109 /*********************************************************************
110 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
112 int MSVCRT__set_new_mode(int mode)
114 int old_mode;
115 LOCK_HEAP;
116 old_mode = MSVCRT_new_mode;
117 MSVCRT_new_mode = mode;
118 UNLOCK_HEAP;
119 return old_mode;
122 /*********************************************************************
123 * _callnewh (MSVCRT.@)
125 int _callnewh(unsigned long size)
127 if(MSVCRT_new_handler)
128 (*MSVCRT_new_handler)(size);
129 return 0;
132 /*********************************************************************
133 * _expand (MSVCRT.@)
135 void* _expand(void* mem, MSVCRT_size_t size)
137 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
140 /*********************************************************************
141 * _heapchk (MSVCRT.@)
143 int _heapchk(void)
145 if (!HeapValidate( GetProcessHeap(), 0, NULL))
147 msvcrt_set_errno(GetLastError());
148 return MSVCRT__HEAPBADNODE;
150 return MSVCRT__HEAPOK;
153 /*********************************************************************
154 * _heapmin (MSVCRT.@)
156 int _heapmin(void)
158 if (!HeapCompact( GetProcessHeap(), 0 ))
160 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
161 msvcrt_set_errno(GetLastError());
162 return -1;
164 return 0;
167 /*********************************************************************
168 * _heapwalk (MSVCRT.@)
170 int _heapwalk(struct MSVCRT__heapinfo* next)
172 PROCESS_HEAP_ENTRY phe;
174 LOCK_HEAP;
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 ))
182 UNLOCK_HEAP;
183 msvcrt_set_errno(GetLastError());
184 return MSVCRT__HEAPBADNODE;
189 if (!HeapWalk( GetProcessHeap(), &phe ))
191 UNLOCK_HEAP;
192 if (GetLastError() == ERROR_NO_MORE_ITEMS)
193 return MSVCRT__HEAPEND;
194 msvcrt_set_errno(GetLastError());
195 if (!phe.lpData)
196 return MSVCRT__HEAPBADBEGIN;
197 return MSVCRT__HEAPBADNODE;
199 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
201 UNLOCK_HEAP;
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)
213 int retval;
214 struct MSVCRT__heapinfo heap;
216 memset( &heap, 0, sizeof(heap) );
217 LOCK_HEAP;
218 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
220 if (heap._useflag == MSVCRT__FREEENTRY)
221 memset(heap._pentry, value, heap._size);
223 UNLOCK_HEAP;
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;
234 return -1;
237 /*********************************************************************
238 * _msize (MSVCRT.@)
240 MSVCRT_size_t _msize(void* mem)
242 long size = HeapSize(GetProcessHeap(),0,mem);
243 if (size == -1)
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 */
248 return size;
251 /*********************************************************************
252 * calloc (MSVCRT.@)
254 void* MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
256 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
259 /*********************************************************************
260 * free (MSVCRT.@)
262 void MSVCRT_free(void* ptr)
264 HeapFree(GetProcessHeap(),0,ptr);
267 /*********************************************************************
268 * malloc (MSVCRT.@)
270 void* MSVCRT_malloc(MSVCRT_size_t size)
272 void *ret = HeapAlloc(GetProcessHeap(),0,size);
273 if (!ret)
274 msvcrt_set_errno(GetLastError());
275 return ret;
278 /*********************************************************************
279 * realloc (MSVCRT.@)
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);
285 MSVCRT_free(ptr);
286 return NULL;
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)
310 if(threshold > 1016)
311 return 0;
312 else
313 MSVCRT_sbh_threshold = threshold;
314 return 1;