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