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
29 #include <sys/types.h>
37 #include "kernel_private.h"
39 #include "wine/exception.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(globalmem
);
44 BOOLEAN WINAPI
RtlGetUserInfoHeap( HANDLE handle
, ULONG flags
, void *ptr
, void **user_value
, ULONG
*user_flags
);
45 BOOLEAN WINAPI
RtlSetUserValueHeap( HANDLE handle
, ULONG flags
, void *ptr
, void *user_value
);
47 /***********************************************************************
48 * HeapCreate (KERNEL32.@)
50 * Create a heap object.
53 * Handle of heap: Success
56 HANDLE WINAPI
HeapCreate(
57 DWORD flags
, /* [in] Heap allocation flag */
58 SIZE_T initialSize
, /* [in] Initial heap size */
59 SIZE_T maxSize
/* [in] Maximum heap size */
63 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
64 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
69 /***********************************************************************
70 * HeapDestroy (KERNEL32.@)
72 * Destroy a heap object.
78 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
80 if (!RtlDestroyHeap( heap
)) return TRUE
;
81 SetLastError( ERROR_INVALID_HANDLE
);
86 /***********************************************************************
87 * Global/local heap functions, keep in sync with kernelbase/memory.c
88 ***********************************************************************/
90 #define MEM_FLAG_USED 1
91 #define MEM_FLAG_MOVEABLE 2
92 #define MEM_FLAG_DISCARDABLE 4
93 #define MEM_FLAG_DISCARDED 8
94 #define MEM_FLAG_DDESHARE 0x8000
110 C_ASSERT(sizeof(struct mem_entry
) == 2 * sizeof(void *));
112 struct kernelbase_global_data
*kernelbase_global_data
;
114 static inline struct mem_entry
*unsafe_mem_from_HLOCAL( HLOCAL handle
)
116 struct mem_entry
*mem
= CONTAINING_RECORD( *(volatile HANDLE
*)&handle
, struct mem_entry
, ptr
);
117 struct kernelbase_global_data
*data
= kernelbase_global_data
;
118 if (((UINT_PTR
)handle
& ((sizeof(void *) << 1) - 1)) != sizeof(void *)) return NULL
;
119 if (mem
< data
->mem_entries
|| mem
>= data
->mem_entries_end
) return NULL
;
120 if (!(mem
->flags
& MEM_FLAG_USED
)) return NULL
;
124 static inline void *unsafe_ptr_from_HLOCAL( HLOCAL handle
)
126 if (((UINT_PTR
)handle
& ((sizeof(void *) << 1) - 1))) return NULL
;
131 /***********************************************************************
132 * GlobalLock (KERNEL32.@)
134 * Lock a global memory object and return a pointer to first byte of the memory
137 * handle [I] Handle of the global memory object
140 * Success: Pointer to first byte of the memory block
144 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
147 void *WINAPI
GlobalLock( HGLOBAL handle
)
149 return LocalLock( handle
);
153 /***********************************************************************
154 * GlobalUnlock (KERNEL32.@)
156 * Unlock a global memory object.
159 * handle [I] Handle of the global memory object
162 * Success: Object is still locked
163 * Failure: FALSE (The Object is unlocked)
166 * When the handle is invalid, last error is set to ERROR_INVALID_HANDLE
169 BOOL WINAPI
GlobalUnlock( HGLOBAL handle
)
171 if (unsafe_ptr_from_HLOCAL( handle
)) return TRUE
;
172 return LocalUnlock( handle
);
176 /***********************************************************************
177 * GlobalHandle (KERNEL32.@)
179 HGLOBAL WINAPI
GlobalHandle( const void *ptr
)
181 return LocalHandle( ptr
);
184 /***********************************************************************
185 * GlobalReAlloc (KERNEL32.@)
187 * Change the size or attributes of a global memory object.
193 HGLOBAL WINAPI
GlobalReAlloc( HGLOBAL handle
, SIZE_T size
, UINT flags
)
195 struct mem_entry
*mem
;
198 if ((mem
= unsafe_mem_from_HLOCAL( handle
)) && mem
->lock
) return 0;
199 if (!(handle
= LocalReAlloc( handle
, size
, flags
))) return 0;
201 /* GlobalReAlloc allows changing GMEM_FIXED to GMEM_MOVEABLE with GMEM_MODIFY */
202 if ((flags
& (GMEM_MOVEABLE
| GMEM_MODIFY
)) == (GMEM_MOVEABLE
| GMEM_MODIFY
) &&
203 (ptr
= unsafe_ptr_from_HLOCAL( handle
)))
205 if (!(handle
= LocalAlloc( flags
, 0 ))) return 0;
206 RtlSetUserValueHeap( GetProcessHeap(), 0, ptr
, handle
);
207 mem
= unsafe_mem_from_HLOCAL( handle
);
208 mem
->flags
&= ~MEM_FLAG_DISCARDED
;
216 /***********************************************************************
217 * GlobalSize (KERNEL32.@)
219 SIZE_T WINAPI
GlobalSize( HGLOBAL handle
)
221 return LocalSize( handle
);
225 /***********************************************************************
226 * GlobalWire (KERNEL32.@)
228 void *WINAPI
GlobalWire( HGLOBAL handle
)
230 return GlobalLock( handle
);
234 /***********************************************************************
235 * GlobalUnWire (KERNEL32.@)
237 BOOL WINAPI
GlobalUnWire( HGLOBAL handle
)
239 return GlobalUnlock( handle
);
243 /***********************************************************************
244 * GlobalFix (KERNEL32.@)
246 VOID WINAPI
GlobalFix( HGLOBAL handle
)
248 GlobalLock( handle
);
252 /***********************************************************************
253 * GlobalUnfix (KERNEL32.@)
255 VOID WINAPI
GlobalUnfix( HGLOBAL handle
)
257 GlobalUnlock( handle
);
261 /***********************************************************************
262 * GlobalFlags (KERNEL32.@)
264 * Get information about a global memory object.
267 * handle [I] Handle of the global memory object
270 * Failure: GMEM_INVALID_HANDLE, when the provided handle is invalid
271 * Success: Value specifying allocation flags and lock count
274 UINT WINAPI
GlobalFlags( HGLOBAL handle
)
276 HANDLE heap
= GetProcessHeap();
277 struct mem_entry
*mem
;
280 if (unsafe_ptr_from_HLOCAL( handle
)) return 0;
283 if ((mem
= unsafe_mem_from_HLOCAL( handle
)))
286 if (mem
->flags
& MEM_FLAG_DISCARDABLE
) flags
|= GMEM_DISCARDABLE
;
287 if (mem
->flags
& MEM_FLAG_DISCARDED
) flags
|= GMEM_DISCARDED
;
288 if (mem
->flags
& MEM_FLAG_DDESHARE
) flags
|= GMEM_DDESHARE
;
292 WARN_(globalmem
)( "invalid handle %p\n", handle
);
293 SetLastError( ERROR_INVALID_HANDLE
);
294 flags
= GMEM_INVALID_HANDLE
;
296 RtlUnlockHeap( heap
);
302 /***********************************************************************
303 * GlobalCompact (KERNEL32.@)
305 SIZE_T WINAPI
GlobalCompact( DWORD minfree
)
307 return 0; /* GlobalCompact does nothing in Win32 */
311 /***********************************************************************
312 * LocalCompact (KERNEL32.@)
314 SIZE_T WINAPI
LocalCompact( UINT minfree
)
316 return 0; /* LocalCompact does nothing in Win32 */
320 /***********************************************************************
321 * LocalFlags (KERNEL32.@)
323 * Get information about a local memory object.
326 * Value specifying allocation flags and lock count.
327 * LMEM_INVALID_HANDLE: Failure
330 * Windows memory management does not provide a separate local heap
333 UINT WINAPI
LocalFlags( HLOCAL handle
)
335 UINT flags
= GlobalFlags( handle
);
336 if (flags
& GMEM_DISCARDABLE
) flags
|= LMEM_DISCARDABLE
;
341 /***********************************************************************
342 * LocalHandle (KERNEL32.@)
344 HLOCAL WINAPI
LocalHandle( const void *ptr
)
346 HANDLE heap
= GetProcessHeap();
347 HLOCAL handle
= (HANDLE
)ptr
;
350 TRACE_(globalmem
)( "ptr %p\n", ptr
);
354 SetLastError( ERROR_INVALID_PARAMETER
);
359 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
) ||
360 !RtlGetUserInfoHeap( heap
, HEAP_NO_SERIALIZE
, (void *)ptr
, &handle
, &flags
))
362 SetLastError( ERROR_INVALID_HANDLE
);
365 RtlUnlockHeap( heap
);
370 /***********************************************************************
371 * LocalShrink (KERNEL32.@)
373 SIZE_T WINAPI
LocalShrink( HGLOBAL handle
, UINT newsize
)
375 return 0; /* LocalShrink does nothing in Win32 */
379 /***********************************************************************
380 * LocalSize (KERNEL32.@)
382 SIZE_T WINAPI
LocalSize( HLOCAL handle
)
384 HANDLE heap
= GetProcessHeap();
385 struct mem_entry
*mem
;
389 TRACE_(globalmem
)( "handle %p\n", handle
);
392 if ((ptr
= unsafe_ptr_from_HLOCAL( handle
)) &&
393 HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
))
394 ret
= HeapSize( heap
, HEAP_NO_SERIALIZE
, ptr
);
395 else if ((mem
= unsafe_mem_from_HLOCAL( handle
)))
397 if (!mem
->ptr
) ret
= 0;
398 else ret
= HeapSize( heap
, HEAP_NO_SERIALIZE
, mem
->ptr
);
402 WARN_(globalmem
)( "invalid handle %p\n", handle
);
403 SetLastError( ERROR_INVALID_HANDLE
);
405 RtlUnlockHeap( heap
);
407 if (ret
== ~(SIZE_T
)0) return 0;
412 /***********************************************************************
413 * GlobalMemoryStatus (KERNEL32.@)
414 * Provides information about the status of the memory, so apps can tell
415 * roughly how much they are able to allocate
420 VOID WINAPI
GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer
)
422 MEMORYSTATUSEX memstatus
;
423 OSVERSIONINFOW osver
;
425 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( GetModuleHandleW(0) );
428 /* Because GlobalMemoryStatus is identical to GlobalMemoryStatusEX save
429 for one extra field in the struct, and the lack of a bug, we simply
430 call GlobalMemoryStatusEx and copy the values across. */
431 memstatus
.dwLength
= sizeof(memstatus
);
432 GlobalMemoryStatusEx(&memstatus
);
434 lpBuffer
->dwLength
= sizeof(*lpBuffer
);
435 lpBuffer
->dwMemoryLoad
= memstatus
.dwMemoryLoad
;
437 /* Windows 2000 and later report -1 when values are greater than 4 Gb.
438 * NT reports values modulo 4 Gb.
441 osver
.dwOSVersionInfoSize
= sizeof(osver
);
442 GetVersionExW(&osver
);
444 lpBuffer
->dwTotalPhys
= memstatus
.ullTotalPhys
;
445 lpBuffer
->dwAvailPhys
= memstatus
.ullAvailPhys
;
446 lpBuffer
->dwTotalPageFile
= memstatus
.ullTotalPageFile
;
447 lpBuffer
->dwAvailPageFile
= memstatus
.ullAvailPageFile
;
448 lpBuffer
->dwTotalVirtual
= memstatus
.ullTotalVirtual
;
449 lpBuffer
->dwAvailVirtual
= memstatus
.ullAvailVirtual
;
452 if ( osver
.dwMajorVersion
>= 5 || osver
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
454 lpBuffer
->dwTotalPhys
= min( memstatus
.ullTotalPhys
, MAXDWORD
);
455 lpBuffer
->dwAvailPhys
= min( memstatus
.ullAvailPhys
, MAXDWORD
);
456 /* Limit value for apps that do not expect so much memory. Remove last 512 kb to make Sacrifice demo happy. */
457 lpBuffer
->dwTotalPageFile
= min( memstatus
.ullTotalPageFile
, 0xfff7ffff );
458 lpBuffer
->dwAvailPageFile
= min( memstatus
.ullAvailPageFile
, MAXDWORD
);
459 lpBuffer
->dwTotalVirtual
= min( memstatus
.ullTotalVirtual
, MAXDWORD
);
460 lpBuffer
->dwAvailVirtual
= min( memstatus
.ullAvailVirtual
, MAXDWORD
);
463 /* values are limited to 2Gb unless the app has the IMAGE_FILE_LARGE_ADDRESS_AWARE flag */
464 /* page file sizes are not limited (Adobe Illustrator 8 depends on this) */
465 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
))
467 if (lpBuffer
->dwTotalPhys
> MAXLONG
) lpBuffer
->dwTotalPhys
= MAXLONG
;
468 if (lpBuffer
->dwAvailPhys
> MAXLONG
) lpBuffer
->dwAvailPhys
= MAXLONG
;
469 if (lpBuffer
->dwTotalVirtual
> MAXLONG
) lpBuffer
->dwTotalVirtual
= MAXLONG
;
470 if (lpBuffer
->dwAvailVirtual
> MAXLONG
) lpBuffer
->dwAvailVirtual
= MAXLONG
;
473 /* work around for broken photoshop 4 installer */
474 if ( lpBuffer
->dwAvailPhys
+ lpBuffer
->dwAvailPageFile
>= 2U*1024*1024*1024)
475 lpBuffer
->dwAvailPageFile
= 2U*1024*1024*1024 - lpBuffer
->dwAvailPhys
- 1;
477 /* limit page file size for really old binaries */
478 if (nt
->OptionalHeader
.MajorSubsystemVersion
< 4 ||
479 nt
->OptionalHeader
.MajorOperatingSystemVersion
< 4)
481 if (lpBuffer
->dwTotalPageFile
> MAXLONG
) lpBuffer
->dwTotalPageFile
= MAXLONG
;
482 if (lpBuffer
->dwAvailPageFile
> MAXLONG
) lpBuffer
->dwAvailPageFile
= MAXLONG
;
486 TRACE("Length %lu, MemoryLoad %lu, TotalPhys %Ix, AvailPhys %Ix,"
487 " TotalPageFile %Ix, AvailPageFile %Ix, TotalVirtual %Ix, AvailVirtual %Ix\n",
488 lpBuffer
->dwLength
, lpBuffer
->dwMemoryLoad
, lpBuffer
->dwTotalPhys
,
489 lpBuffer
->dwAvailPhys
, lpBuffer
->dwTotalPageFile
, lpBuffer
->dwAvailPageFile
,
490 lpBuffer
->dwTotalVirtual
, lpBuffer
->dwAvailVirtual
);