[PATCH 3/4] kernel32: Use HLOCAL helpers from kernelbase kernelbase.h.

Rémi Bernon rbernon at codeweavers.com
Wed Mar 30 03:35:35 CDT 2022


Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---
 dlls/kernel32/Makefile.in    |  1 +
 dlls/kernel32/heap.c         | 81 ++++++++++--------------------------
 dlls/kernelbase/kernelbase.h |  1 +
 3 files changed, 24 insertions(+), 59 deletions(-)

diff --git a/dlls/kernel32/Makefile.in b/dlls/kernel32/Makefile.in
index 7e68a950b71..1ee6df2f89f 100644
--- a/dlls/kernel32/Makefile.in
+++ b/dlls/kernel32/Makefile.in
@@ -2,6 +2,7 @@ EXTRADEFS = -D_KERNEL32_ -D_NORMALIZE_
 MODULE    = kernel32.dll
 IMPORTLIB = kernel32
 IMPORTS   = kernelbase ntdll winecrt0
+PARENTSRC = ../kernelbase
 
 EXTRADLLFLAGS = -nodefaultlibs -Wb,-F,KERNEL32.dll -Wl,--image-base,0x7b600000
 
diff --git a/dlls/kernel32/heap.c b/dlls/kernel32/heap.c
index fefb88d6857..54d50411d69 100644
--- a/dlls/kernel32/heap.c
+++ b/dlls/kernel32/heap.c
@@ -33,6 +33,9 @@
 #include "winerror.h"
 #include "winnt.h"
 #include "winternl.h"
+
+#include "kernelbase.h"
+
 #include "wine/exception.h"
 #include "wine/debug.h"
 
@@ -140,43 +143,6 @@ BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
 }
 
 
-/*
- * Win32 Global heap functions (GlobalXXX).
- * These functions included in Win32 for compatibility with 16 bit Windows
- * Especially the moveable blocks and handles are oldish.
- * But the ability to directly allocate memory with GPTR and LPTR is widely
- * used.
- *
- * The handle stuff looks horrible, but it's implemented almost like Win95
- * does it.
- *
- */
-
-#define MAGIC_GLOBAL_USED 0x5342
-#define HANDLE_TO_INTERN(h)  ((PGLOBAL32_INTERN)(((char *)(h))-2))
-#define INTERN_TO_HANDLE(i)  (&((i)->Pointer))
-#define POINTER_TO_HANDLE(p) (*(((const HGLOBAL *)(p))-2))
-#define ISHANDLE(h)          (((ULONG_PTR)(h)&2)!=0)
-#define ISPOINTER(h)         (((ULONG_PTR)(h)&2)==0)
-/* align the storage needed for the HGLOBAL on an 8byte boundary thus
- * GlobalAlloc/GlobalReAlloc'ing with GMEM_MOVEABLE of memory with
- * size = 8*k, where k=1,2,3,... alloc's exactly the given size.
- * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
- * the output jpeg's > 1 MB if not */
-#define HGLOBAL_STORAGE      (sizeof(HGLOBAL)*2)
-
-#include "pshpack1.h"
-
-typedef struct __GLOBAL32_INTERN
-{
-   WORD         Magic;
-   LPVOID       Pointer;
-   BYTE         Flags;
-   BYTE         LockCount;
-} GLOBAL32_INTERN, *PGLOBAL32_INTERN;
-
-#include "poppack.h"
-
 /***********************************************************************
  *           GlobalLock   (KERNEL32.@)
  *
@@ -217,7 +183,7 @@ void *WINAPI GlobalLock( HGLOBAL handle )
  */
 BOOL WINAPI GlobalUnlock( HGLOBAL handle )
 {
-    if (ISPOINTER( handle )) return TRUE;
+    if (is_pointer( handle )) return TRUE;
     return LocalUnlock( handle );
 }
 
@@ -233,7 +199,7 @@ BOOL WINAPI GlobalUnlock( HGLOBAL handle )
  */
 HGLOBAL WINAPI GlobalHandle( const void *ptr )
 {
-    PGLOBAL32_INTERN mem;
+    struct mem_entry *mem;
     HGLOBAL handle;
     LPCVOID test;
 
@@ -253,7 +219,7 @@ HGLOBAL WINAPI GlobalHandle( const void *ptr )
         /* note that if ptr is a pointer to a block allocated by           */
         /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate  */
         /* will fail.                                                      */
-        if (ISPOINTER( ptr ))
+        if (is_pointer( (HLOCAL)ptr ))
         {
             if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, ptr ))
             {
@@ -265,11 +231,10 @@ HGLOBAL WINAPI GlobalHandle( const void *ptr )
         else handle = (HGLOBAL)ptr;
 
         /* Now test handle either passed in or retrieved from pointer */
-        mem = HANDLE_TO_INTERN( handle );
-        if (mem->Magic == MAGIC_GLOBAL_USED)
+        if ((mem = unsafe_mem_from_HLOCAL( handle )))
         {
-            test = mem->Pointer;
-            if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
+            test = mem->ptr;
+            if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, (const char *)test - HLOCAL_STORAGE ) && /* obj(-handle) valid arena? */
                 HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, mem )) /* intern valid arena? */
                 break; /* valid moveable block */
         }
@@ -320,7 +285,7 @@ HGLOBAL WINAPI GlobalReAlloc( HGLOBAL handle, SIZE_T size, UINT flags )
  */
 SIZE_T WINAPI GlobalSize( HGLOBAL handle )
 {
-    PGLOBAL32_INTERN mem;
+    struct mem_entry *mem;
     SIZE_T retval;
 
     TRACE_(globalmem)( "handle %p\n", handle );
@@ -331,27 +296,26 @@ SIZE_T WINAPI GlobalSize( HGLOBAL handle )
         return 0;
     }
 
-    if (ISPOINTER( handle ))
+    if (is_pointer( handle ))
     {
         retval = HeapSize( GetProcessHeap(), 0, handle );
         if (retval == ~(SIZE_T)0) /* It might be a GMEM_MOVEABLE data pointer */
         {
-            retval = HeapSize( GetProcessHeap(), 0, (char *)handle - HGLOBAL_STORAGE );
-            if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
+            retval = HeapSize( GetProcessHeap(), 0, (char *)handle - HLOCAL_STORAGE );
+            if (retval != ~(SIZE_T)0) retval -= HLOCAL_STORAGE;
         }
     }
     else
     {
         RtlLockHeap( GetProcessHeap() );
-        mem = HANDLE_TO_INTERN( handle );
-        if (mem->Magic == MAGIC_GLOBAL_USED)
+        if ((mem = unsafe_mem_from_HLOCAL( handle )))
         {
-            if (!mem->Pointer) /* handle case of GlobalAlloc( ??,0) */
+            if (!mem->ptr) /* handle case of GlobalAlloc( ??,0) */
                 retval = 0;
             else
             {
-                retval = HeapSize( GetProcessHeap(), 0, (char *)mem->Pointer - HGLOBAL_STORAGE );
-                if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
+                retval = HeapSize( GetProcessHeap(), 0, (char *)mem->ptr - HLOCAL_STORAGE );
+                if (retval != ~(SIZE_T)0) retval -= HLOCAL_STORAGE;
             }
         }
         else
@@ -418,23 +382,22 @@ VOID WINAPI GlobalUnfix( HGLOBAL handle )
  */
 UINT WINAPI GlobalFlags( HGLOBAL handle )
 {
-    PGLOBAL32_INTERN mem;
+    struct mem_entry *mem;
     DWORD retval;
 
     TRACE_(globalmem)( "handle %p\n", handle );
 
-    if (ISPOINTER( handle ))
+    if (is_pointer( handle ))
     {
         retval = 0;
     }
     else
     {
         RtlLockHeap( GetProcessHeap() );
-        mem = HANDLE_TO_INTERN( handle );
-        if (mem->Magic == MAGIC_GLOBAL_USED)
+        if ((mem = unsafe_mem_from_HLOCAL( handle )))
         {
-            retval = mem->LockCount + (mem->Flags << 8);
-            if (mem->Pointer == 0) retval |= GMEM_DISCARDED;
+            retval = mem->lock + (mem->flags << 8);
+            if (mem->ptr == 0) retval |= GMEM_DISCARDED;
         }
         else
         {
diff --git a/dlls/kernelbase/kernelbase.h b/dlls/kernelbase/kernelbase.h
index 7d1bbad3012..4be7177d74d 100644
--- a/dlls/kernelbase/kernelbase.h
+++ b/dlls/kernelbase/kernelbase.h
@@ -67,6 +67,7 @@ struct mem_entry
 #include "poppack.h"
 
 #define MAGIC_LOCAL_USED    0x5342
+#define POINTER_TO_HANDLE( p ) (*(((const HGLOBAL *)( p )) - 2))
 /* align the storage needed for the HLOCAL on an 8-byte boundary thus
  * LocalAlloc/LocalReAlloc'ing with LMEM_MOVEABLE of memory with
  * size = 8*k, where k=1,2,3,... allocs exactly the given size.
-- 
2.35.1




More information about the wine-devel mailing list