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