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
);
45 /***********************************************************************
46 * HeapCreate (KERNEL32.@)
48 * Create a heap object.
51 * Handle of heap: Success
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 */
61 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
62 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
67 /***********************************************************************
68 * HeapDestroy (KERNEL32.@)
70 * Destroy a heap object.
76 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
78 if (!RtlDestroyHeap( heap
)) return TRUE
;
79 SetLastError( ERROR_INVALID_HANDLE
);
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
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
;
122 static inline void *unsafe_ptr_from_HLOCAL( HLOCAL handle
)
124 if (((UINT_PTR
)handle
& ((sizeof(void *) << 1) - 1))) return NULL
;
129 /***********************************************************************
130 * GlobalLock (KERNEL32.@)
132 * Lock a global memory object and return a pointer to first byte of the memory
135 * handle [I] Handle of the global memory object
138 * Success: Pointer to first byte of the memory block
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.
157 * handle [I] Handle of the global memory object
160 * Success: Object is still locked
161 * Failure: FALSE (The Object is unlocked)
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.
191 HGLOBAL WINAPI
GlobalReAlloc( HGLOBAL handle
, SIZE_T size
, UINT flags
)
193 struct mem_entry
*mem
;
196 if (!(flags
& GMEM_MODIFY
) && (mem
= unsafe_mem_from_HLOCAL( handle
)) &&
197 mem
->lock
&& (!size
|| (flags
& GMEM_DISCARDABLE
)))
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
;
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.
268 * handle [I] Handle of the global memory object
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
;
281 if (unsafe_ptr_from_HLOCAL( handle
)) return 0;
284 if ((mem
= unsafe_mem_from_HLOCAL( handle
)))
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
;
293 WARN_(globalmem
)( "invalid handle %p\n", handle
);
294 SetLastError( ERROR_INVALID_HANDLE
);
295 flags
= GMEM_INVALID_HANDLE
;
297 RtlUnlockHeap( heap
);
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.
327 * Value specifying allocation flags and lock count.
328 * LMEM_INVALID_HANDLE: Failure
331 * Windows memory management does not provide a separate local heap
334 UINT WINAPI
LocalFlags( HLOCAL handle
)
336 UINT flags
= GlobalFlags( handle
);
337 if (flags
& GMEM_DISCARDABLE
) flags
|= LMEM_DISCARDABLE
;
342 /***********************************************************************
343 * LocalHandle (KERNEL32.@)
345 HLOCAL WINAPI
LocalHandle( const void *ptr
)
347 HANDLE heap
= GetProcessHeap();
348 HLOCAL handle
= (HANDLE
)ptr
;
351 TRACE_(globalmem
)( "ptr %p\n", ptr
);
355 SetLastError( ERROR_INVALID_PARAMETER
);
360 if (!HeapValidate( heap
, HEAP_NO_SERIALIZE
, ptr
) ||
361 !RtlGetUserInfoHeap( heap
, HEAP_NO_SERIALIZE
, (void *)ptr
, &handle
, &flags
))
363 SetLastError( ERROR_INVALID_HANDLE
);
366 RtlUnlockHeap( heap
);
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
;
390 TRACE_(globalmem
)( "handle %p\n", handle
);
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
);
403 WARN_(globalmem
)( "invalid handle %p\n", handle
);
404 SetLastError( ERROR_INVALID_HANDLE
);
406 RtlUnlockHeap( heap
);
408 if (ret
== ~(SIZE_T
)0) return 0;
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
421 VOID WINAPI
GlobalMemoryStatus( LPMEMORYSTATUS lpBuffer
)
423 MEMORYSTATUSEX memstatus
;
424 OSVERSIONINFOW osver
;
426 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( GetModuleHandleW(0) );
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
;
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
;
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
);