2 * CRTDLL memory functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * Implementation Notes:
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.
19 DEFAULT_DEBUG_CHANNEL(crtdll
);
21 static new_handler_type new_handler
;
24 /*********************************************************************
29 LPVOID __cdecl
CRTDLL_new(DWORD size
)
32 if(!(result
= HeapAlloc(GetProcessHeap(),0,size
)) && new_handler
)
38 /*********************************************************************
41 * Free memory created with new.
43 VOID __cdecl
CRTDLL_delete(LPVOID ptr
)
45 HeapFree(GetProcessHeap(),0,ptr
);
49 /*********************************************************************
50 * set_new_handler(CRTDLL.003)
52 new_handler_type __cdecl
CRTDLL_set_new_handler(new_handler_type func
)
54 new_handler_type old_handler
= new_handler
;
60 /*********************************************************************
61 * _expand (CRTDLL.088)
63 * Increase the size of a block of memory allocated with malloc()
66 LPVOID __cdecl
CRTDLL__expand(LPVOID ptr
, INT size
)
68 return HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, ptr
, size
);
72 /*********************************************************************
73 * _heapchk (CRTDLL.130)
75 * Check the consistency of the process heap.
77 INT __cdecl
CRTDLL__heapchk(VOID
)
79 if (!HeapValidate( GetProcessHeap(), 0, NULL
))
81 __CRTDLL__set_errno(GetLastError());
88 /*********************************************************************
89 * _heapmin (CRTDLL.131)
91 * Minimise the size of the heap.
93 INT __cdecl
CRTDLL__heapmin(VOID
)
95 if (!HeapCompact( GetProcessHeap(), 0 ))
97 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
98 __CRTDLL__set_errno(GetLastError());
105 /*********************************************************************
106 * _heapset (CRTDLL.132)
108 * Fill free memory in the heap with a given value.
110 INT __cdecl
CRTDLL__heapset(UINT value
)
113 struct _heapinfo heap
;
115 memset( &heap
, 0, sizeof(heap
) );
117 while ((retVal
= CRTDLL__heapwalk(&heap
)) == _HEAPOK
)
119 if (heap
._useflag
== _FREEENTRY
)
120 memset(heap
._pentry
, value
, heap
._size
);
122 return retVal
== _HEAPEND
? _HEAPOK
: retVal
;
126 /*********************************************************************
127 * _heapwalk (CRTDLL.133)
129 * Walk the heap block by block.
131 INT __cdecl
CRTDLL__heapwalk(struct _heapinfo
*next
)
133 PROCESS_HEAP_ENTRY phe
;
135 phe
.lpData
= next
->_pentry
;
136 phe
.cbData
= next
->_size
;
137 phe
.wFlags
= next
->_useflag
== _USEDENTRY
? PROCESS_HEAP_ENTRY_BUSY
: 0;
139 if (phe
.lpData
&& phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
&&
140 !HeapValidate( GetProcessHeap(), 0, phe
.lpData
))
142 __CRTDLL__set_errno(GetLastError());
148 if (!HeapWalk( GetProcessHeap(), &phe
))
150 if (GetLastError() == ERROR_NO_MORE_ITEMS
)
152 __CRTDLL__set_errno(GetLastError());
154 return _HEAPBADBEGIN
;
157 } while (phe
.wFlags
& (PROCESS_HEAP_REGION
|PROCESS_HEAP_UNCOMMITTED_RANGE
));
159 next
->_pentry
= phe
.lpData
;
160 next
->_size
= phe
.cbData
;
161 next
->_useflag
= phe
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
? _USEDENTRY
: _FREEENTRY
;
166 /*********************************************************************
167 * _msize (CRTDLL.234)
169 * Return the actual used size of an allocated block of memory.
172 LONG __cdecl
CRTDLL__msize(LPVOID mem
)
174 LONG size
= HeapSize(GetProcessHeap(),0,mem
);
177 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
178 /* At least the win98/nt crtdlls also return -1 in this case */
184 /*********************************************************************
185 * calloc (CRTDLL.350)
187 * Allocate memory from the heap and initialise it to zero.
189 LPVOID __cdecl
CRTDLL_calloc(DWORD size
, DWORD count
)
191 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* count
);
195 /*********************************************************************
198 * Free a block of memory allocated with malloc()
200 VOID __cdecl
CRTDLL_free(LPVOID ptr
)
202 HeapFree(GetProcessHeap(),0,ptr
);
206 /*********************************************************************
207 * malloc (CRTDLL.424)
209 * Alocate memory from the heap.
211 LPVOID __cdecl
CRTDLL_malloc(DWORD size
)
213 LPVOID ret
= HeapAlloc(GetProcessHeap(),0,size
);
215 __CRTDLL__set_errno(GetLastError());
220 /*********************************************************************
221 * realloc (CRTDLL.444)
223 * Resize a block of memory allocated with malloc() or realloc().
225 LPVOID __cdecl
CRTDLL_realloc( VOID
*ptr
, DWORD size
)
227 return HeapReAlloc( GetProcessHeap(), 0, ptr
, size
);