wined3d: Pass a wined3d_device_context to wined3d_cs_emit_set_material().
[wine.git] / dlls / kernel32 / heap.c
blob02f4587d43b3310550856bdabfb3f8834653b89a
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"
36 #include "wine/exception.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(globalmem);
41 /* address where we try to map the system heap */
42 #define SYSTEM_HEAP_BASE ((void*)0x80000000)
43 #define SYSTEM_HEAP_SIZE 0x1000000 /* Default heap size = 16Mb */
45 static HANDLE systemHeap; /* globally shared heap */
48 /***********************************************************************
49 * HEAP_CreateSystemHeap
51 * Create the system heap.
53 static inline HANDLE HEAP_CreateSystemHeap(void)
55 int created;
56 void *base;
57 HANDLE map, event;
59 /* create the system heap event first */
60 event = CreateEventA( NULL, TRUE, FALSE, "__wine_system_heap_event" );
62 if (!(map = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE,
63 0, SYSTEM_HEAP_SIZE, "__wine_system_heap" ))) return 0;
64 created = (GetLastError() != ERROR_ALREADY_EXISTS);
66 if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
68 /* pre-defined address not available */
69 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
70 return 0;
73 if (created) /* newly created heap */
75 systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
76 SYSTEM_HEAP_SIZE, NULL, NULL );
77 SetEvent( event );
79 else
81 /* wait for the heap to be initialized */
82 WaitForSingleObject( event, INFINITE );
83 systemHeap = base;
85 CloseHandle( map );
86 return systemHeap;
90 /***********************************************************************
91 * HeapCreate (KERNEL32.@)
93 * Create a heap object.
95 * RETURNS
96 * Handle of heap: Success
97 * NULL: Failure
99 HANDLE WINAPI HeapCreate(
100 DWORD flags, /* [in] Heap allocation flag */
101 SIZE_T initialSize, /* [in] Initial heap size */
102 SIZE_T maxSize /* [in] Maximum heap size */
104 HANDLE ret;
106 if ( flags & HEAP_SHARED )
108 if (!systemHeap) HEAP_CreateSystemHeap();
109 else WARN( "Shared Heap requested, returning system heap.\n" );
110 ret = systemHeap;
112 else
114 ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
115 if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
117 return ret;
121 /***********************************************************************
122 * HeapDestroy (KERNEL32.@)
124 * Destroy a heap object.
126 * RETURNS
127 * TRUE: Success
128 * FALSE: Failure
130 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
132 if (heap == systemHeap)
134 WARN( "attempt to destroy system heap, returning TRUE!\n" );
135 return TRUE;
137 if (!RtlDestroyHeap( heap )) return TRUE;
138 SetLastError( ERROR_INVALID_HANDLE );
139 return FALSE;
144 * Win32 Global heap functions (GlobalXXX).
145 * These functions included in Win32 for compatibility with 16 bit Windows
146 * Especially the moveable blocks and handles are oldish.
147 * But the ability to directly allocate memory with GPTR and LPTR is widely
148 * used.
150 * The handle stuff looks horrible, but it's implemented almost like Win95
151 * does it.
155 #define MAGIC_GLOBAL_USED 0x5342
156 #define HANDLE_TO_INTERN(h) ((PGLOBAL32_INTERN)(((char *)(h))-2))
157 #define INTERN_TO_HANDLE(i) (&((i)->Pointer))
158 #define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
159 #define ISHANDLE(h) (((ULONG_PTR)(h)&2)!=0)
160 #define ISPOINTER(h) (((ULONG_PTR)(h)&2)==0)
161 /* align the storage needed for the HGLOBAL on an 8byte boundary thus
162 * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
163 * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
164 * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
165 * the output jpeg's > 1 MB if not */
166 #define HGLOBAL_STORAGE (sizeof(HGLOBAL)*2)
168 #include "pshpack1.h"
170 typedef struct __GLOBAL32_INTERN
172 WORD Magic;
173 LPVOID Pointer;
174 BYTE Flags;
175 BYTE LockCount;
176 } GLOBAL32_INTERN, *PGLOBAL32_INTERN;
178 #include "poppack.h"
180 /***********************************************************************
181 * GlobalLock (KERNEL32.@)
183 * Lock a global memory object and return a pointer to first byte of the memory
185 * PARAMS
186 * hmem [I] Handle of the global memory object
188 * RETURNS
189 * Success: Pointer to first byte of the memory block
190 * Failure: NULL
192 * NOTES
193 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
196 LPVOID WINAPI GlobalLock(HGLOBAL hmem)
198 return LocalLock( hmem );
202 /***********************************************************************
203 * GlobalUnlock (KERNEL32.@)
205 * Unlock a global memory object.
207 * PARAMS
208 * hmem [I] Handle of the global memory object
210 * RETURNS
211 * Success: Object is still locked
212 * Failure: FALSE (The Object is unlocked)
214 * NOTES
215 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
218 BOOL WINAPI GlobalUnlock(HGLOBAL hmem)
220 if (ISPOINTER( hmem )) return TRUE;
221 return LocalUnlock( hmem );
225 /***********************************************************************
226 * GlobalHandle (KERNEL32.@)
228 * Get the handle associated with the pointer to a global memory block.
230 * RETURNS
231 * Handle: Success
232 * NULL: Failure
234 HGLOBAL WINAPI GlobalHandle(
235 LPCVOID pmem /* [in] Pointer to global memory block */
237 HGLOBAL handle;
238 PGLOBAL32_INTERN maybe_intern;
239 LPCVOID test;
241 if (!pmem)
243 SetLastError( ERROR_INVALID_PARAMETER );
244 return 0;
247 RtlLockHeap(GetProcessHeap());
248 __TRY
250 handle = 0;
252 /* note that if pmem is a pointer to a block allocated by */
253 /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate */
254 /* will fail. */
255 if (ISPOINTER(pmem)) {
256 if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, pmem )) {
257 handle = (HGLOBAL)pmem; /* valid fixed block */
258 break;
260 handle = POINTER_TO_HANDLE(pmem);
261 } else
262 handle = (HGLOBAL)pmem;
264 /* Now test handle either passed in or retrieved from pointer */
265 maybe_intern = HANDLE_TO_INTERN( handle );
266 if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
267 test = maybe_intern->Pointer;
268 if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
269 HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, maybe_intern )) /* intern valid arena? */
270 break; /* valid moveable block */
272 handle = 0;
273 SetLastError( ERROR_INVALID_HANDLE );
275 __EXCEPT_PAGE_FAULT
277 SetLastError( ERROR_INVALID_HANDLE );
278 handle = 0;
280 __ENDTRY
281 RtlUnlockHeap(GetProcessHeap());
283 return handle;
287 /***********************************************************************
288 * GlobalReAlloc (KERNEL32.@)
290 * Change the size or attributes of a global memory object.
292 * RETURNS
293 * Handle: Success
294 * NULL: Failure
296 HGLOBAL WINAPI GlobalReAlloc( HGLOBAL hmem, SIZE_T size, UINT flags )
298 return LocalReAlloc( hmem, size, flags );
302 /***********************************************************************
303 * GlobalSize (KERNEL32.@)
305 * Get the size of a global memory object.
307 * PARAMS
308 * hmem [I] Handle of the global memory object
310 * RETURNS
311 * Failure: 0
312 * Success: Size in Bytes of the global memory object
314 * NOTES
315 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
318 SIZE_T WINAPI GlobalSize(HGLOBAL hmem)
320 SIZE_T retval;
321 PGLOBAL32_INTERN pintern;
323 if (!((ULONG_PTR)hmem >> 16))
325 SetLastError(ERROR_INVALID_HANDLE);
326 return 0;
329 if(ISPOINTER(hmem))
331 retval=HeapSize(GetProcessHeap(), 0, hmem);
333 if (retval == ~(SIZE_T)0) /* It might be a GMEM_MOVEABLE data pointer */
335 retval = HeapSize(GetProcessHeap(), 0, (char*)hmem - HGLOBAL_STORAGE);
336 if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
339 else
341 RtlLockHeap(GetProcessHeap());
342 pintern=HANDLE_TO_INTERN(hmem);
344 if(pintern->Magic==MAGIC_GLOBAL_USED)
346 if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
347 retval = 0;
348 else
350 retval = HeapSize(GetProcessHeap(), 0, (char *)pintern->Pointer - HGLOBAL_STORAGE );
351 if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
354 else
356 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
357 SetLastError(ERROR_INVALID_HANDLE);
358 retval=0;
360 RtlUnlockHeap(GetProcessHeap());
362 if (retval == ~(SIZE_T)0) retval = 0;
363 return retval;
367 /***********************************************************************
368 * GlobalWire (KERNEL32.@)
370 LPVOID WINAPI GlobalWire(HGLOBAL hmem)
372 return GlobalLock( hmem );
376 /***********************************************************************
377 * GlobalUnWire (KERNEL32.@)
379 BOOL WINAPI GlobalUnWire(HGLOBAL hmem)
381 return GlobalUnlock( hmem);
385 /***********************************************************************
386 * GlobalFix (KERNEL32.@)
388 VOID WINAPI GlobalFix(HGLOBAL hmem)
390 GlobalLock( hmem );
394 /***********************************************************************
395 * GlobalUnfix (KERNEL32.@)
397 VOID WINAPI GlobalUnfix(HGLOBAL hmem)
399 GlobalUnlock( hmem);
403 /***********************************************************************
404 * GlobalFlags (KERNEL32.@)
406 * Get information about a global memory object.
408 * PARAMS
409 * hmem [I] Handle of the global memory object
411 * RETURNS
412 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
413 * Success: Value specifying allocation flags and lock count
416 UINT WINAPI GlobalFlags(HGLOBAL hmem)
418 DWORD retval;
419 PGLOBAL32_INTERN pintern;
421 if(ISPOINTER(hmem))
423 retval=0;
425 else
427 RtlLockHeap(GetProcessHeap());
428 pintern=HANDLE_TO_INTERN(hmem);
429 if(pintern->Magic==MAGIC_GLOBAL_USED)
431 retval=pintern->LockCount + (pintern->Flags<<8);
432 if(pintern->Pointer==0)
433 retval|= GMEM_DISCARDED;
435 else
437 WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
438 SetLastError(ERROR_INVALID_HANDLE);
439 retval = GMEM_INVALID_HANDLE;
441 RtlUnlockHeap(GetProcessHeap());
443 return retval;
447 /***********************************************************************
448 * GlobalCompact (KERNEL32.@)
450 SIZE_T WINAPI GlobalCompact( DWORD minfree )
452 return 0; /* GlobalCompact does nothing in Win32 */
456 /***********************************************************************
457 * LocalCompact (KERNEL32.@)
459 SIZE_T WINAPI LocalCompact( UINT minfree )
461 return 0; /* LocalCompact does nothing in Win32 */
465 /***********************************************************************
466 * LocalFlags (KERNEL32.@)
468 * Get information about a local memory object.
470 * RETURNS
471 * Value specifying allocation flags and lock count.
472 * LMEM_INVALID_HANDLE: Failure
474 * NOTES
475 * Windows memory management does not provide a separate local heap
476 * and global heap.
478 UINT WINAPI LocalFlags(
479 HLOCAL handle /* [in] Handle of memory object */
481 return GlobalFlags( handle );
485 /***********************************************************************
486 * LocalHandle (KERNEL32.@)
488 * Get the handle associated with the pointer to a local memory block.
490 * RETURNS
491 * Handle: Success
492 * NULL: Failure
494 * NOTES
495 * Windows memory management does not provide a separate local heap
496 * and global heap.
498 HLOCAL WINAPI LocalHandle(
499 LPCVOID ptr /* [in] Address of local memory block */
501 return GlobalHandle( ptr );
505 /***********************************************************************
506 * LocalShrink (KERNEL32.@)
508 SIZE_T WINAPI LocalShrink( HGLOBAL handle, UINT newsize )
510 return 0; /* LocalShrink does nothing in Win32 */
514 /***********************************************************************
515 * LocalSize (KERNEL32.@)
517 * Get the size of a local memory object.
519 * RETURNS
520 * Size: Success
521 * 0: Failure
523 * NOTES
524 * Windows memory management does not provide a separate local heap
525 * and global heap.
527 SIZE_T WINAPI LocalSize(
528 HLOCAL handle /* [in] Handle of memory object */
530 return GlobalSize( handle );
534 /***********************************************************************
535 * GlobalMemoryStatus (KERNEL32.@)
536 * Provides information about the status of the memory, so apps can tell
537 * roughly how much they are able to allocate
539 * RETURNS
540 * None
542 VOID WINAPI GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer )
544 MEMORYSTATUSEX memstatus;
545 OSVERSIONINFOW osver;
546 #ifndef _WIN64
547 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( GetModuleHandleW(0) );
548 #endif
550 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
551 for one extra field in the struct, and the lack of a bug, we simply
552 call GlobalMemoryStatusEx and copy the values across. */
553 memstatus.dwLength = sizeof(memstatus);
554 GlobalMemoryStatusEx(&memstatus);
556 lpBuffer->dwLength = sizeof(*lpBuffer);
557 lpBuffer->dwMemoryLoad = memstatus.dwMemoryLoad;
559 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
560 * NT reports values modulo 4 Gb.
563 osver.dwOSVersionInfoSize = sizeof(osver);
564 GetVersionExW(&osver);
566 lpBuffer->dwTotalPhys = memstatus.ullTotalPhys;
567 lpBuffer->dwAvailPhys = memstatus.ullAvailPhys;
568 lpBuffer->dwTotalPageFile = memstatus.ullTotalPageFile;
569 lpBuffer->dwAvailPageFile = memstatus.ullAvailPageFile;
570 lpBuffer->dwTotalVirtual = memstatus.ullTotalVirtual;
571 lpBuffer->dwAvailVirtual = memstatus.ullAvailVirtual;
573 #ifndef _WIN64
574 if ( osver.dwMajorVersion >= 5 || osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
576 lpBuffer->dwTotalPhys = min( memstatus.ullTotalPhys, MAXDWORD );
577 lpBuffer->dwAvailPhys = min( memstatus.ullAvailPhys, MAXDWORD );
578 /* Limit value for apps that do not expect so much memory. Remove last 512 kb to make Sacrifice demo happy. */
579 lpBuffer->dwTotalPageFile = min( memstatus.ullTotalPageFile, 0xfff7ffff );
580 lpBuffer->dwAvailPageFile = min( memstatus.ullAvailPageFile, MAXDWORD );
581 lpBuffer->dwTotalVirtual = min( memstatus.ullTotalVirtual, MAXDWORD );
582 lpBuffer->dwAvailVirtual = min( memstatus.ullAvailVirtual, MAXDWORD );
585 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
586 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
587 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE))
589 if (lpBuffer->dwTotalPhys > MAXLONG) lpBuffer->dwTotalPhys = MAXLONG;
590 if (lpBuffer->dwAvailPhys > MAXLONG) lpBuffer->dwAvailPhys = MAXLONG;
591 if (lpBuffer->dwTotalVirtual > MAXLONG) lpBuffer->dwTotalVirtual = MAXLONG;
592 if (lpBuffer->dwAvailVirtual > MAXLONG) lpBuffer->dwAvailVirtual = MAXLONG;
595 /* work around for broken photoshop 4 installer */
596 if ( lpBuffer->dwAvailPhys + lpBuffer->dwAvailPageFile >= 2U*1024*1024*1024)
597 lpBuffer->dwAvailPageFile = 2U*1024*1024*1024 - lpBuffer->dwAvailPhys - 1;
599 /* limit page file size for really old binaries */
600 if (nt->OptionalHeader.MajorSubsystemVersion < 4 ||
601 nt->OptionalHeader.MajorOperatingSystemVersion < 4)
603 if (lpBuffer->dwTotalPageFile > MAXLONG) lpBuffer->dwTotalPageFile = MAXLONG;
604 if (lpBuffer->dwAvailPageFile > MAXLONG) lpBuffer->dwAvailPageFile = MAXLONG;
606 #endif
608 TRACE("Length %u, MemoryLoad %u, TotalPhys %lx, AvailPhys %lx,"
609 " TotalPageFile %lx, AvailPageFile %lx, TotalVirtual %lx, AvailVirtual %lx\n",
610 lpBuffer->dwLength, lpBuffer->dwMemoryLoad, lpBuffer->dwTotalPhys,
611 lpBuffer->dwAvailPhys, lpBuffer->dwTotalPageFile, lpBuffer->dwAvailPageFile,
612 lpBuffer->dwTotalVirtual, lpBuffer->dwAvailVirtual );