Implement _popen and _pclose, and use debugstr_a to avoid a crash
[wine/wine-kai.git] / dlls / msvcrt / heap.c
blob74e7b371aa2aeb7a086e5b25e478a8e02824d0f9
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;
41 /*********************************************************************
42 * ??2@YAPAXI@Z (MSVCRT.@)
44 void* MSVCRT_operator_new(unsigned long size)
46 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
47 TRACE("(%ld) returning %p\n", size, retval);
48 LOCK_HEAP;
49 if(!retval && MSVCRT_new_handler)
50 (*MSVCRT_new_handler)(size);
51 UNLOCK_HEAP;
52 return retval;
55 /*********************************************************************
56 * ??3@YAXPAX@Z (MSVCRT.@)
58 void MSVCRT_operator_delete(void *mem)
60 TRACE("(%p)\n", mem);
61 HeapFree(GetProcessHeap(), 0, mem);
65 /*********************************************************************
66 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
68 MSVCRT_new_handler_func MSVCRT__query_new_handler(void)
70 return MSVCRT_new_handler;
74 /*********************************************************************
75 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
77 int MSVCRT__query_new_mode(void)
79 return MSVCRT_new_mode;
82 /*********************************************************************
83 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
85 MSVCRT_new_handler_func MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
87 MSVCRT_new_handler_func old_handler;
88 LOCK_HEAP;
89 old_handler = MSVCRT_new_handler;
90 MSVCRT_new_handler = func;
91 UNLOCK_HEAP;
92 return old_handler;
95 /*********************************************************************
96 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
98 MSVCRT_new_handler_func MSVCRT_set_new_handler(void *func)
100 TRACE("(%p)\n",func);
101 MSVCRT__set_new_handler(NULL);
102 return NULL;
105 /*********************************************************************
106 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
108 int MSVCRT__set_new_mode(int mode)
110 int old_mode;
111 LOCK_HEAP;
112 old_mode = MSVCRT_new_mode;
113 MSVCRT_new_mode = mode;
114 UNLOCK_HEAP;
115 return old_mode;
118 /*********************************************************************
119 * _callnewh (MSVCRT.@)
121 int _callnewh(unsigned long size)
123 if(MSVCRT_new_handler)
124 (*MSVCRT_new_handler)(size);
125 return 0;
128 /*********************************************************************
129 * _expand (MSVCRT.@)
131 void* _expand(void* mem, MSVCRT_size_t size)
133 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
136 /*********************************************************************
137 * _heapchk (MSVCRT.@)
139 int _heapchk(void)
141 if (!HeapValidate( GetProcessHeap(), 0, NULL))
143 msvcrt_set_errno(GetLastError());
144 return MSVCRT__HEAPBADNODE;
146 return MSVCRT__HEAPOK;
149 /*********************************************************************
150 * _heapmin (MSVCRT.@)
152 int _heapmin(void)
154 if (!HeapCompact( GetProcessHeap(), 0 ))
156 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
157 msvcrt_set_errno(GetLastError());
158 return -1;
160 return 0;
163 /*********************************************************************
164 * _heapwalk (MSVCRT.@)
166 int _heapwalk(struct MSVCRT__heapinfo* next)
168 PROCESS_HEAP_ENTRY phe;
170 LOCK_HEAP;
171 phe.lpData = next->_pentry;
172 phe.cbData = next->_size;
173 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
175 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
176 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
178 UNLOCK_HEAP;
179 msvcrt_set_errno(GetLastError());
180 return MSVCRT__HEAPBADNODE;
185 if (!HeapWalk( GetProcessHeap(), &phe ))
187 UNLOCK_HEAP;
188 if (GetLastError() == ERROR_NO_MORE_ITEMS)
189 return MSVCRT__HEAPEND;
190 msvcrt_set_errno(GetLastError());
191 if (!phe.lpData)
192 return MSVCRT__HEAPBADBEGIN;
193 return MSVCRT__HEAPBADNODE;
195 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
197 UNLOCK_HEAP;
198 next->_pentry = phe.lpData;
199 next->_size = phe.cbData;
200 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
201 return MSVCRT__HEAPOK;
204 /*********************************************************************
205 * _heapset (MSVCRT.@)
207 int _heapset(unsigned int value)
209 int retval;
210 struct MSVCRT__heapinfo heap;
212 memset( &heap, 0, sizeof(heap) );
213 LOCK_HEAP;
214 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
216 if (heap._useflag == MSVCRT__FREEENTRY)
217 memset(heap._pentry, value, heap._size);
219 UNLOCK_HEAP;
220 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
223 /*********************************************************************
224 * _heapadd (MSVCRT.@)
226 int _heapadd(void* mem, MSVCRT_size_t size)
228 TRACE("(%p,%d) unsupported in Win32\n", mem,size);
229 *MSVCRT__errno() = MSVCRT_ENOSYS;
230 return -1;
233 /*********************************************************************
234 * _msize (MSVCRT.@)
236 MSVCRT_size_t _msize(void* mem)
238 long size = HeapSize(GetProcessHeap(),0,mem);
239 if (size == -1)
241 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
242 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
244 return size;
247 /*********************************************************************
248 * calloc (MSVCRT.@)
250 void* MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
252 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
255 /*********************************************************************
256 * free (MSVCRT.@)
258 void MSVCRT_free(void* ptr)
260 HeapFree(GetProcessHeap(),0,ptr);
263 /*********************************************************************
264 * malloc (MSVCRT.@)
266 void* MSVCRT_malloc(MSVCRT_size_t size)
268 void *ret = HeapAlloc(GetProcessHeap(),0,size);
269 if (!ret)
270 msvcrt_set_errno(GetLastError());
271 return ret;
274 /*********************************************************************
275 * realloc (MSVCRT.@)
277 void* MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
279 if (!ptr) return MSVCRT_malloc(size);
280 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
281 MSVCRT_free(ptr);
282 return NULL;