windowscodecs: Silence fixme for IID_CMetaBitmapRenderTarget.
[wine.git] / dlls / kernel32 / heap.c
blob1ec2f5cce0da746fb5b0ed15b246813071a72218
1 /*
2 * Win32 heap functions
4 * Copyright 1995, 1996 Alexandre Julliard
5 * Copyright 1996 Huw Davies
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <assert.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winnt.h"
35 #include "winternl.h"
37 #include "kernel_private.h"
39 #include "wine/exception.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(globalmem);
45 /***********************************************************************
46 * HeapCreate (KERNEL32.@)
48 * Create a heap object.
50 * RETURNS
51 * Handle of heap: Success
52 * NULL: Failure
54 HANDLE WINAPI HeapCreate(
55 DWORD flags, /* [in] Heap allocation flag */
56 SIZE_T initialSize, /* [in] Initial heap size */
57 SIZE_T maxSize /* [in] Maximum heap size */
58 ) {
59 HANDLE ret;
61 ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
62 if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
63 return ret;
67 /***********************************************************************
68 * HeapDestroy (KERNEL32.@)
70 * Destroy a heap object.
72 * RETURNS
73 * TRUE: Success
74 * FALSE: Failure
76 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
78 if (!RtlDestroyHeap( heap )) return TRUE;
79 SetLastError( ERROR_INVALID_HANDLE );
80 return FALSE;
84 /***********************************************************************
85 * Global/local heap functions, keep in sync with kernelbase/memory.c
86 ***********************************************************************/
88 #define MEM_FLAG_USED 1
89 #define MEM_FLAG_MOVEABLE 2
90 #define MEM_FLAG_DISCARDABLE 4
91 #define MEM_FLAG_DISCARDED 8
92 #define MEM_FLAG_DDESHARE 0x8000
94 struct mem_entry
96 union
98 struct
100 WORD flags;
101 BYTE lock;
103 void *next_free;
105 void *ptr;
108 C_ASSERT(sizeof(struct mem_entry) == 2 * sizeof(void *));
110 struct kernelbase_global_data *kernelbase_global_data;
112 static inline struct mem_entry *unsafe_mem_from_HLOCAL( HLOCAL handle )
114 struct mem_entry *mem = CONTAINING_RECORD( *(volatile HANDLE *)&handle, struct mem_entry, ptr );
115 struct kernelbase_global_data *data = kernelbase_global_data;
116 if (((UINT_PTR)handle & ((sizeof(void *) << 1) - 1)) != sizeof(void *)) return NULL;
117 if (mem < data->mem_entries || mem >= data->mem_entries_end) return NULL;
118 if (!(mem->flags & MEM_FLAG_USED)) return NULL;
119 return mem;
122 static inline void *unsafe_ptr_from_HLOCAL( HLOCAL handle )
124 if (((UINT_PTR)handle & ((sizeof(void *) << 1) - 1))) return NULL;
125 return handle;
129 /***********************************************************************
130 * GlobalLock (KERNEL32.@)
132 * Lock a global memory object and return a pointer to first byte of the memory
134 * PARAMS
135 * handle [I] Handle of the global memory object
137 * RETURNS
138 * Success: Pointer to first byte of the memory block
139 * Failure: NULL
141 * NOTES
142 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
145 void *WINAPI GlobalLock( HGLOBAL handle )
147 return LocalLock( handle );
151 /***********************************************************************
152 * GlobalUnlock (KERNEL32.@)
154 * Unlock a global memory object.
156 * PARAMS
157 * handle [I] Handle of the global memory object
159 * RETURNS
160 * Success: Object is still locked
161 * Failure: FALSE (The Object is unlocked)
163 * NOTES
164 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
167 BOOL WINAPI GlobalUnlock( HGLOBAL handle )
169 if (unsafe_ptr_from_HLOCAL( handle )) return TRUE;
170 return LocalUnlock( handle );
174 /***********************************************************************
175 * GlobalHandle (KERNEL32.@)
177 HGLOBAL WINAPI GlobalHandle( const void *ptr )
179 return LocalHandle( ptr );
182 /***********************************************************************
183 * GlobalReAlloc (KERNEL32.@)
185 * Change the size or attributes of a global memory object.
187 * RETURNS
188 * Handle: Success
189 * NULL: Failure
191 HGLOBAL WINAPI GlobalReAlloc( HGLOBAL handle, SIZE_T size, UINT flags )
193 struct mem_entry *mem;
194 void *ptr;
196 if (!(flags & GMEM_MODIFY) && (mem = unsafe_mem_from_HLOCAL( handle )) &&
197 mem->lock && (!size || (flags & GMEM_DISCARDABLE)))
198 return 0;
200 if (!(handle = LocalReAlloc( handle, size, flags ))) return 0;
202 /* GlobalReAlloc allows changing GMEM_FIXED to GMEM_MOVEABLE with GMEM_MODIFY */
203 if ((flags & (GMEM_MOVEABLE | GMEM_MODIFY)) == (GMEM_MOVEABLE | GMEM_MODIFY) &&
204 (ptr = unsafe_ptr_from_HLOCAL( handle )))
206 if (!(handle = LocalAlloc( flags, 0 ))) return 0;
207 RtlSetUserValueHeap( GetProcessHeap(), 0, ptr, handle );
208 mem = unsafe_mem_from_HLOCAL( handle );
209 mem->flags &= ~MEM_FLAG_DISCARDED;
210 mem->ptr = ptr;
213 return handle;
217 /***********************************************************************
218 * GlobalSize (KERNEL32.@)
220 SIZE_T WINAPI GlobalSize( HGLOBAL handle )
222 return LocalSize( handle );
226 /***********************************************************************
227 * GlobalWire (KERNEL32.@)
229 void *WINAPI GlobalWire( HGLOBAL handle )
231 return GlobalLock( handle );
235 /***********************************************************************
236 * GlobalUnWire (KERNEL32.@)
238 BOOL WINAPI GlobalUnWire( HGLOBAL handle )
240 return GlobalUnlock( handle );
244 /***********************************************************************
245 * GlobalFix (KERNEL32.@)
247 VOID WINAPI GlobalFix( HGLOBAL handle )
249 GlobalLock( handle );
253 /***********************************************************************
254 * GlobalUnfix (KERNEL32.@)
256 VOID WINAPI GlobalUnfix( HGLOBAL handle )
258 GlobalUnlock( handle );
262 /***********************************************************************
263 * GlobalFlags (KERNEL32.@)
265 * Get information about a global memory object.
267 * PARAMS
268 * handle [I] Handle of the global memory object
270 * RETURNS
271 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
272 * Success: Value specifying allocation flags and lock count
275 UINT WINAPI GlobalFlags( HGLOBAL handle )
277 HANDLE heap = GetProcessHeap();
278 struct mem_entry *mem;
279 UINT flags;
281 if (unsafe_ptr_from_HLOCAL( handle )) return 0;
283 RtlLockHeap( heap );
284 if ((mem = unsafe_mem_from_HLOCAL( handle )))
286 flags = mem->lock;
287 if (mem->flags & MEM_FLAG_DISCARDABLE) flags |= GMEM_DISCARDABLE;
288 if (mem->flags & MEM_FLAG_DISCARDED) flags |= GMEM_DISCARDED;
289 if (mem->flags & MEM_FLAG_DDESHARE) flags |= GMEM_DDESHARE;
291 else
293 WARN_(globalmem)( "invalid handle %p\n", handle );
294 SetLastError( ERROR_INVALID_HANDLE );
295 flags = GMEM_INVALID_HANDLE;
297 RtlUnlockHeap( heap );
299 return flags;
303 /***********************************************************************
304 * GlobalCompact (KERNEL32.@)
306 SIZE_T WINAPI GlobalCompact( DWORD minfree )
308 return 0; /* GlobalCompact does nothing in Win32 */
312 /***********************************************************************
313 * LocalCompact (KERNEL32.@)
315 SIZE_T WINAPI LocalCompact( UINT minfree )
317 return 0; /* LocalCompact does nothing in Win32 */
321 /***********************************************************************
322 * LocalFlags (KERNEL32.@)
324 * Get information about a local memory object.
326 * RETURNS
327 * Value specifying allocation flags and lock count.
328 * LMEM_INVALID_HANDLE: Failure
330 * NOTES
331 * Windows memory management does not provide a separate local heap
332 * and global heap.
334 UINT WINAPI LocalFlags( HLOCAL handle )
336 UINT flags = GlobalFlags( handle );
337 if (flags & GMEM_DISCARDABLE) flags |= LMEM_DISCARDABLE;
338 return flags;
342 /***********************************************************************
343 * LocalHandle (KERNEL32.@)
345 HLOCAL WINAPI LocalHandle( const void *ptr )
347 HANDLE heap = GetProcessHeap();
348 HLOCAL handle = (HANDLE)ptr;
349 ULONG flags;
351 TRACE_(globalmem)( "ptr %p\n", ptr );
353 if (!ptr)
355 SetLastError( ERROR_INVALID_PARAMETER );
356 return 0;
359 RtlLockHeap( heap );
360 if (!HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ) ||
361 !RtlGetUserInfoHeap( heap, HEAP_NO_SERIALIZE, (void *)ptr, &handle, &flags ))
363 SetLastError( ERROR_INVALID_HANDLE );
364 handle = 0;
366 RtlUnlockHeap( heap );
368 return handle;
371 /***********************************************************************
372 * LocalShrink (KERNEL32.@)
374 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
376 return 0; /* LocalShrink does nothing in Win32 */
380 /***********************************************************************
381 * LocalSize (KERNEL32.@)
383 SIZE_T WINAPI LocalSize( HLOCAL handle )
385 HANDLE heap = GetProcessHeap();
386 struct mem_entry *mem;
387 SIZE_T ret = 0;
388 void *ptr;
390 TRACE_(globalmem)( "handle %p\n", handle );
392 RtlLockHeap( heap );
393 if ((ptr = unsafe_ptr_from_HLOCAL( handle )) &&
394 HeapValidate( heap, HEAP_NO_SERIALIZE, ptr ))
395 ret = HeapSize( heap, HEAP_NO_SERIALIZE, ptr );
396 else if ((mem = unsafe_mem_from_HLOCAL( handle )))
398 if (!mem->ptr) ret = 0;
399 else ret = HeapSize( heap, HEAP_NO_SERIALIZE, mem->ptr );
401 else
403 WARN_(globalmem)( "invalid handle %p\n", handle );
404 SetLastError( ERROR_INVALID_HANDLE );
406 RtlUnlockHeap( heap );
408 if (ret == ~(SIZE_T)0) return 0;
409 return ret;
413 /***********************************************************************
414 * GlobalMemoryStatus (KERNEL32.@)
415 * Provides information about the status of the memory, so apps can tell
416 * roughly how much they are able to allocate
418 * RETURNS
419 * None
421 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
423 MEMORYSTATUSEX memstatus;
424 OSVERSIONINFOW osver;
425 #ifndef _WIN64
426 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( GetModuleHandleW(0) );
427 #endif
429 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
430 for one extra field in the struct, and the lack of a bug, we simply
431 call GlobalMemoryStatusEx and copy the values across. */
432 memstatus.dwLength = sizeof(memstatus);
433 GlobalMemoryStatusEx(&memstatus);
435 lpBuffer->dwLength = sizeof(*lpBuffer);
436 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
438 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
439 * NT reports values modulo 4 Gb.
442 osver.dwOSVersionInfoSize = sizeof(osver);
443 GetVersionExW(&osver);
445 lpBuffer->dwTotalPhys = memstatus.ullTotalPhys;
446 lpBuffer->dwAvailPhys = memstatus.ullAvailPhys;
447 lpBuffer->dwTotalPageFile = memstatus.ullTotalPageFile;
448 lpBuffer->dwAvailPageFile = memstatus.ullAvailPageFile;
449 lpBuffer->dwTotalVirtual = memstatus.ullTotalVirtual;
450 lpBuffer->dwAvailVirtual = memstatus.ullAvailVirtual;
452 #ifndef _WIN64
453 if ( osver.dwMajorVersion >= 5 || osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
455 lpBuffer->dwTotalPhys = min( memstatus.ullTotalPhys, MAXDWORD );
456 lpBuffer->dwAvailPhys = min( memstatus.ullAvailPhys, MAXDWORD );
457 /* Limit value for apps that do not expect so much memory. Remove last 512 kb to make Sacrifice demo happy. */
458 lpBuffer->dwTotalPageFile = min( memstatus.ullTotalPageFile, 0xfff7ffff );
459 lpBuffer->dwAvailPageFile = min( memstatus.ullAvailPageFile, MAXDWORD );
460 lpBuffer->dwTotalVirtual = min( memstatus.ullTotalVirtual, MAXDWORD );
461 lpBuffer->dwAvailVirtual = min( memstatus.ullAvailVirtual, MAXDWORD );
464 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
465 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
466 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE))
468 if (lpBuffer->dwTotalPhys > MAXLONG) lpBuffer->dwTotalPhys = MAXLONG;
469 if (lpBuffer->dwAvailPhys > MAXLONG) lpBuffer->dwAvailPhys = MAXLONG;
470 if (lpBuffer->dwTotalVirtual > MAXLONG) lpBuffer->dwTotalVirtual = MAXLONG;
471 if (lpBuffer->dwAvailVirtual > MAXLONG) lpBuffer->dwAvailVirtual = MAXLONG;
474 /* work around for broken photoshop 4 installer */
475 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
476 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;
478 /* limit page file size for really old binaries */
479 if (nt->OptionalHeader.MajorSubsystemVersion < 4 ||
480 nt->OptionalHeader.MajorOperatingSystemVersion < 4)
482 if (lpBuffer->dwTotalPageFile > MAXLONG) lpBuffer->dwTotalPageFile = MAXLONG;
483 if (lpBuffer->dwAvailPageFile > MAXLONG) lpBuffer->dwAvailPageFile = MAXLONG;
485 #endif
487 TRACE("Length %lu, MemoryLoad %lu, TotalPhys %Ix, AvailPhys %Ix,"
488 " TotalPageFile %Ix, AvailPageFile %Ix, TotalVirtual %Ix, AvailVirtual %Ix\n",
489 lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys,
490 lpBuffer->dwAvailPhys, lpBuffer->dwTotalPageFile, lpBuffer->dwAvailPageFile,
491 lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual );