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
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
;
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
);
49 if(!retval
&& MSVCRT_new_handler
)
50 (*MSVCRT_new_handler
)(size
);
55 /*********************************************************************
56 * ??3@YAXPAX@Z (MSVCRT.@)
58 void MSVCRT_operator_delete(void *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
;
89 old_handler
= MSVCRT_new_handler
;
90 MSVCRT_new_handler
= func
;
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
);
105 /*********************************************************************
106 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
108 int MSVCRT__set_new_mode(int mode
)
112 old_mode
= MSVCRT_new_mode
;
113 MSVCRT_new_mode
= mode
;
118 /*********************************************************************
119 * _callnewh (MSVCRT.@)
121 int _callnewh(unsigned long size
)
123 if(MSVCRT_new_handler
)
124 (*MSVCRT_new_handler
)(size
);
128 /*********************************************************************
131 void* _expand(void* mem
, MSVCRT_size_t size
)
133 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, mem
, size
);
136 /*********************************************************************
137 * _heapchk (MSVCRT.@)
141 if (!HeapValidate( GetProcessHeap(), 0, NULL
))
143 msvcrt_set_errno(GetLastError());
144 return MSVCRT__HEAPBADNODE
;
146 return MSVCRT__HEAPOK
;
149 /*********************************************************************
150 * _heapmin (MSVCRT.@)
154 if (!HeapCompact( GetProcessHeap(), 0 ))
156 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
157 msvcrt_set_errno(GetLastError());
163 /*********************************************************************
164 * _heapwalk (MSVCRT.@)
166 int _heapwalk(struct MSVCRT__heapinfo
* next
)
168 PROCESS_HEAP_ENTRY phe
;
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
))
179 msvcrt_set_errno(GetLastError());
180 return MSVCRT__HEAPBADNODE
;
185 if (!HeapWalk( GetProcessHeap(), &phe
))
188 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
189 return MSVCRT__HEAPEND
;
190 msvcrt_set_errno(GetLastError());
192 return MSVCRT__HEAPBADBEGIN
;
193 return MSVCRT__HEAPBADNODE
;
195 } while (phe
.wFlags
& (PROCESS_HEAP_REGION
|PROCESS_HEAP_UNCOMMITTED_RANGE
));
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
)
210 struct MSVCRT__heapinfo heap
;
212 memset( &heap
, 0, sizeof(heap
) );
214 while ((retval
= _heapwalk(&heap
)) == MSVCRT__HEAPOK
)
216 if (heap
._useflag
== MSVCRT__FREEENTRY
)
217 memset(heap
._pentry
, value
, heap
._size
);
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
;
233 /*********************************************************************
236 MSVCRT_size_t
_msize(void* mem
)
238 long size
= HeapSize(GetProcessHeap(),0,mem
);
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 */
247 /*********************************************************************
250 void* MSVCRT_calloc(MSVCRT_size_t size
, MSVCRT_size_t count
)
252 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
255 /*********************************************************************
258 void MSVCRT_free(void* ptr
)
260 HeapFree(GetProcessHeap(),0,ptr
);
263 /*********************************************************************
266 void* MSVCRT_malloc(MSVCRT_size_t size
)
268 void *ret
= HeapAlloc(GetProcessHeap(),0,size
);
270 msvcrt_set_errno(GetLastError());
274 /*********************************************************************
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
);