Only copy the dwSize bytes really occupied by lpPropSheetPage.
[wine/hacks.git] / dlls / crtdll / memory.c
blob89a80908467d7189ff3511f84febb325f862ac26
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
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.
16 #include "crtdll.h"
19 DEFAULT_DEBUG_CHANNEL(crtdll);
21 static new_handler_type new_handler;
24 /*********************************************************************
25 * new (CRTDLL.001)
27 * Allocate memory.
29 LPVOID __cdecl CRTDLL_new(DWORD size)
31 VOID* result;
32 if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
33 (*new_handler)();
34 return result;
38 /*********************************************************************
39 * delete (CRTDLL.002)
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;
55 new_handler = func;
56 return old_handler;
60 /*********************************************************************
61 * _expand (CRTDLL.088)
63 * Increase the size of a block of memory allocated with malloc()
64 * or realloc().
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());
82 return _HEAPBADNODE;
84 return _HEAPOK;
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());
99 return -1;
101 return 0;
105 /*********************************************************************
106 * _heapset (CRTDLL.132)
108 * Fill free memory in the heap with a given value.
110 INT __cdecl CRTDLL__heapset(UINT value)
112 INT retVal;
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());
143 return _HEAPBADNODE;
148 if (!HeapWalk( GetProcessHeap(), &phe ))
150 if (GetLastError() == ERROR_NO_MORE_ITEMS)
151 return _HEAPEND;
152 __CRTDLL__set_errno(GetLastError());
153 if (!phe.lpData)
154 return _HEAPBADBEGIN;
155 return _HEAPBADNODE;
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;
162 return _HEAPOK;
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);
175 if (size == -1)
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 */
180 return size;
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 /*********************************************************************
196 * free (CRTDLL.375)
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);
214 if (!ret)
215 __CRTDLL__set_errno(GetLastError());
216 return ret;
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 );