source: trunk/src/crtdll/memory.c@ 4667

Last change on this file since 4667 was 4667, checked in by phaller, 25 years ago

Major move towards WINE CRTDLL, mixture between both code branches

File size: 5.6 KB
Line 
1/*
2 * CRTDLL memory functions
3 *
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
8 *
9 * Implementation Notes:
10 * MT Safe.
11 * heapwalk from win does not work. This is most likely due to internal
12 * differences between wine and win (see memory/heap.c comments). This
13 * version works fine, however.
14 */
15
16#include "crtdll.h"
17
18
19DEFAULT_DEBUG_CHANNEL(crtdll);
20
21static new_handler_type new_handler;
22
23
24/*********************************************************************
25 * new (CRTDLL.001)
26 *
27 * Allocate memory.
28 */
29LPVOID CDECL CRTDLL_new(DWORD size)
30{
31 VOID* result;
32 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
33 (*new_handler)();
34 return result;
35}
36
37
38/*********************************************************************
39 * delete (CRTDLL.002)
40 *
41 * Free memory created with new.
42 */
43VOID CDECL CRTDLL_delete(LPVOID ptr)
44{
45 HeapFree(GetProcessHeap(),0,ptr);
46}
47
48
49/*********************************************************************
50 * set_new_handler(CRTDLL.003)
51 */
52new_handler_type CDECL CRTDLL_set_new_handler(new_handler_type func)
53{
54 new_handler_type old_handler = new_handler;
55 new_handler = func;
56 return old_handler;
57}
58
59
60/*********************************************************************
61 * _expand (CRTDLL.088)
62 *
63 * Increase the size of a block of memory allocated with malloc()
64 * or realloc().
65 */
66LPVOID CDECL CRTDLL__expand(LPVOID ptr, INT size)
67{
68 return HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, ptr, size );
69}
70
71
72/*********************************************************************
73 * _heapchk (CRTDLL.130)
74 *
75 * Check the consistency of the process heap.
76 */
77INT CDECL CRTDLL__heapchk(VOID)
78{
79 // return (_heapchk());
80
81 if (!HeapValidate( GetProcessHeap(), 0, NULL))
82 {
83 __CRTDLL__set_errno(GetLastError());
84 return _HEAPBADNODE;
85 }
86 return _HEAPOK;
87}
88
89
90/*********************************************************************
91 * _heapmin (CRTDLL.131)
92 *
93 * Minimise the size of the heap.
94 */
95INT CDECL CRTDLL__heapmin(VOID)
96{
97 // return (_heapmin());
98
99 if (!HeapCompact( GetProcessHeap(), 0 ))
100 {
101 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
102 __CRTDLL__set_errno(GetLastError());
103 return -1;
104 }
105 return 0;
106}
107
108
109/*********************************************************************
110 * _heapset (CRTDLL.132)
111 *
112 * Fill free memory in the heap with a given value.
113 */
114INT CDECL CRTDLL__heapset(UINT value)
115{
116 // return (_heapset(fill));
117
118 INT retVal;
119 struct _heapinfo heap;
120
121 memset( &heap, 0, sizeof(heap) );
122
123 while ((retVal = CRTDLL__heapwalk(&heap)) == _HEAPOK)
124 {
125 if (heap._useflag == _FREEENTRY)
126 memset(heap._pentry, value, heap._size);
127 }
128 return retVal == _HEAPEND? _HEAPOK : retVal;
129}
130
131
132/*********************************************************************
133 * _heapwalk (CRTDLL.133)
134 *
135 * Walk the heap block by block.
136 */
137INT CDECL CRTDLL__heapwalk(struct _heapinfo *next)
138{
139 PROCESS_HEAP_ENTRY phe;
140
141 phe.lpData = next->_pentry;
142 phe.cbData = next->_size;
143 phe.wFlags = next->_useflag == _USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
144
145 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
146 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
147 {
148 __CRTDLL__set_errno(GetLastError());
149 return _HEAPBADNODE;
150 }
151
152 do
153 {
154 if (!HeapWalk( GetProcessHeap(), &phe ))
155 {
156 if (GetLastError() == ERROR_NO_MORE_ITEMS)
157 return _HEAPEND;
158 __CRTDLL__set_errno(GetLastError());
159 if (!phe.lpData)
160 return _HEAPBADBEGIN;
161 return _HEAPBADNODE;
162 }
163 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
164
165 next->_pentry = phe.lpData;
166 next->_size = phe.cbData;
167 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? _USEDENTRY : _FREEENTRY;
168 return _HEAPOK;
169}
170
171
172/*********************************************************************
173 * _msize (CRTDLL.234)
174 *
175 * Return the actual used size of an allocated block of memory.
176 *
177 */
178LONG CDECL CRTDLL__msize(LPVOID mem)
179{
180 // return (_msize(ptr));
181
182 LONG size = HeapSize(GetProcessHeap(),0,mem);
183 if (size == -1)
184 {
185 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
186 /* At least the win98/nt crtdlls also return -1 in this case */
187 }
188 return size;
189}
190
191
192/*********************************************************************
193 * calloc (CRTDLL.350)
194 *
195 * Allocate memory from the heap and initialise it to zero.
196 */
197LPVOID CDECL CRTDLL_calloc(DWORD size, DWORD count)
198{
199 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
200}
201
202
203/*********************************************************************
204 * free (CRTDLL.375)
205 *
206 * Free a block of memory allocated with malloc()
207 */
208VOID CDECL CRTDLL_free(LPVOID ptr)
209{
210 HeapFree(GetProcessHeap(),0,ptr);
211}
212
213
214/*********************************************************************
215 * malloc (CRTDLL.424)
216 *
217 * Alocate memory from the heap.
218 */
219LPVOID CDECL CRTDLL_malloc(DWORD size)
220{
221 LPVOID ret = HeapAlloc(GetProcessHeap(),0,size);
222 if (!ret)
223 __CRTDLL__set_errno(GetLastError());
224 return ret;
225}
226
227
228/*********************************************************************
229 * realloc (CRTDLL.444)
230 *
231 * Resize a block of memory allocated with malloc() or realloc().
232 */
233LPVOID CDECL CRTDLL_realloc( VOID *ptr, DWORD size )
234{
235 return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
236}
Note: See TracBrowser for help on using the repository browser.