[PATCH 3/6] kernelbase: Stop using an offset for HLOCAL pointers.

Rémi Bernon wine at gitlab.winehq.org
Fri Jun 10 03:19:56 CDT 2022


From: Rémi Bernon <rbernon at codeweavers.com>

Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---
 dlls/kernel32/heap.c       | 30 +++---------------------------
 dlls/kernel32/tests/heap.c |  6 ------
 dlls/kernelbase/memory.c   | 27 +++++++++------------------
 3 files changed, 12 insertions(+), 51 deletions(-)

diff --git a/dlls/kernel32/heap.c b/dlls/kernel32/heap.c
index c1074e70e2e..e414ad03493 100644
--- a/dlls/kernel32/heap.c
+++ b/dlls/kernel32/heap.c
@@ -172,13 +172,6 @@ C_ASSERT(sizeof(struct mem_entry) == 2 * sizeof(void *));
 
 struct kernelbase_global_data *kernelbase_global_data;
 
-/* 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.
- * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
- * the output jpeg's > 1 MB if not */
-#define HLOCAL_STORAGE      (sizeof(HLOCAL) * 2)
-
 static inline struct mem_entry *unsafe_mem_from_HLOCAL( HLOCAL handle )
 {
     struct mem_entry *mem = CONTAINING_RECORD( handle, struct mem_entry, ptr );
@@ -254,7 +247,6 @@ HGLOBAL WINAPI GlobalHandle( const void *ptr )
 {
     struct mem_entry *mem;
     HGLOBAL handle;
-    LPCVOID test;
     ULONG flags;
 
     TRACE_(globalmem)( "ptr %p\n", ptr );
@@ -275,12 +267,7 @@ HGLOBAL WINAPI GlobalHandle( const void *ptr )
         /* will fail.                                                      */
         if ((ptr = unsafe_ptr_from_HLOCAL( (HLOCAL)ptr )))
         {
-            if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, ptr ))
-            {
-                handle = (HGLOBAL)ptr; /* valid fixed block */
-                break;
-            }
-            if (!RtlGetUserInfoHeap( GetProcessHeap(), HEAP_NO_SERIALIZE, (char *)ptr - HLOCAL_STORAGE, &handle, &flags ))
+            if (!RtlGetUserInfoHeap( GetProcessHeap(), HEAP_NO_SERIALIZE, (void *)ptr, &handle, &flags ))
             {
                 SetLastError( ERROR_INVALID_HANDLE );
                 handle = 0;
@@ -292,8 +279,7 @@ HGLOBAL WINAPI GlobalHandle( const void *ptr )
         /* Now test handle either passed in or retrieved from pointer */
         if ((mem = unsafe_mem_from_HLOCAL( handle )))
         {
-            test = mem->ptr;
-            if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, (const char *)test - HLOCAL_STORAGE )) /* obj(-handle) valid arena? */
+            if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, mem->ptr )) /* obj(-handle) valid arena? */
                 break; /* valid moveable block */
         }
         handle = 0;
@@ -356,14 +342,7 @@ SIZE_T WINAPI GlobalSize( HGLOBAL handle )
     }
 
     if ((ptr = unsafe_ptr_from_HLOCAL( handle )))
-    {
         retval = HeapSize( GetProcessHeap(), 0, ptr );
-        if (retval == ~(SIZE_T)0) /* It might be a GMEM_MOVEABLE data pointer */
-        {
-            retval = HeapSize( GetProcessHeap(), 0, (char *)ptr - HLOCAL_STORAGE );
-            if (retval != ~(SIZE_T)0) retval -= HLOCAL_STORAGE;
-        }
-    }
     else
     {
         RtlLockHeap( GetProcessHeap() );
@@ -372,10 +351,7 @@ SIZE_T WINAPI GlobalSize( HGLOBAL handle )
             if (!mem->ptr) /* handle case of GlobalAlloc( ??,0) */
                 retval = 0;
             else
-            {
-                retval = HeapSize( GetProcessHeap(), 0, (char *)mem->ptr - HLOCAL_STORAGE );
-                if (retval != ~(SIZE_T)0) retval -= HLOCAL_STORAGE;
-            }
+                retval = HeapSize( GetProcessHeap(), 0, mem->ptr );
         }
         else
         {
diff --git a/dlls/kernel32/tests/heap.c b/dlls/kernel32/tests/heap.c
index ec2fe7479ca..4496df35579 100644
--- a/dlls/kernel32/tests/heap.c
+++ b/dlls/kernel32/tests/heap.c
@@ -1400,29 +1400,23 @@ static void test_GlobalAlloc(void)
         ret = HeapValidate( GetProcessHeap(), 0, entry );
         ok( !ret, "HeapValidate succeeded\n" );
         ret = HeapValidate( GetProcessHeap(), 0, entry->ptr );
-        todo_wine
         ok( ret, "HeapValidate failed, error %lu\n", GetLastError() );
         size = HeapSize( GetProcessHeap(), 0, entry->ptr );
-        todo_wine
         ok( size == alloc_size, "HeapSize returned %Iu\n", size );
 
         tmp_mem = invalid_mem;
         tmp_flags = 0xdeadbeef;
         ret = pRtlGetUserInfoHeap( GetProcessHeap(), 0, entry->ptr, (void **)&tmp_mem, &tmp_flags );
         ok( ret, "RtlGetUserInfoHeap failed, error %lu\n", GetLastError() );
-        todo_wine
         ok( tmp_mem == mem, "got user ptr %p\n", tmp_mem );
         todo_wine
         ok( tmp_flags == 0x200, "got user flags %#lx\n", tmp_flags );
 
         ret = pRtlSetUserValueHeap( GetProcessHeap(), 0, entry->ptr, invalid_mem );
-        todo_wine
         ok( ret, "RtlSetUserValueHeap failed, error %lu\n", GetLastError() );
         tmp_mem = GlobalHandle( entry->ptr );
-        todo_wine
         ok( tmp_mem == invalid_mem, "GlobalHandle returned unexpected handle\n" );
         ret = pRtlSetUserValueHeap( GetProcessHeap(), 0, entry->ptr, mem );
-        todo_wine
         ok( ret, "RtlSetUserValueHeap failed, error %lu\n", GetLastError() );
 
         ptr = GlobalLock( mem );
diff --git a/dlls/kernelbase/memory.c b/dlls/kernelbase/memory.c
index 409b35089e3..16a068f5b5f 100644
--- a/dlls/kernelbase/memory.c
+++ b/dlls/kernelbase/memory.c
@@ -752,13 +752,6 @@ C_ASSERT(sizeof(struct mem_entry) == 2 * sizeof(void *));
 static struct mem_entry *next_free_mem;
 static struct kernelbase_global_data global_data = {0};
 
-/* 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.
- * The Minolta DiMAGE Image Viewer heavily relies on this, corrupting
- * the output jpeg's > 1 MB if not */
-#define HLOCAL_STORAGE      (sizeof(HLOCAL) * 2)
-
 static inline struct mem_entry *unsafe_mem_from_HLOCAL( HLOCAL handle )
 {
     struct mem_entry *mem = CONTAINING_RECORD( *(volatile HANDLE *)&handle, struct mem_entry, ptr );
@@ -872,10 +865,9 @@ HLOCAL WINAPI DECLSPEC_HOTPATCH LocalAlloc( UINT flags, SIZE_T size )
     if (!size) mem->flags |= MEM_FLAG_DISCARDED;
     else
     {
-        if (!(ptr = HeapAlloc( heap, heap_flags, size + HLOCAL_STORAGE ))) goto failed;
+        if (!(ptr = HeapAlloc( heap, heap_flags, size ))) goto failed;
         RtlSetUserValueHeap( heap, heap_flags, ptr, handle );
-        *(HLOCAL *)ptr = handle;
-        mem->ptr = (char *)ptr + HLOCAL_STORAGE;
+        mem->ptr = ptr;
     }
 
     TRACE_(globalmem)( "return handle %p, ptr %p\n", handle, mem->ptr );
@@ -907,7 +899,7 @@ HLOCAL WINAPI DECLSPEC_HOTPATCH LocalFree( HLOCAL handle )
     }
     else if ((mem = unsafe_mem_from_HLOCAL( handle )))
     {
-        if (!mem->ptr || HeapFree( heap, HEAP_NO_SERIALIZE, (char *)mem->ptr - HLOCAL_STORAGE )) ret = 0;
+        if (HeapFree( heap, HEAP_NO_SERIALIZE, mem->ptr )) ret = 0;
         mem->ptr = NULL;
         mem->next_free = next_free_mem;
         next_free_mem = mem;
@@ -1026,25 +1018,24 @@ HLOCAL WINAPI DECLSPEC_HOTPATCH LocalReAlloc( HLOCAL handle, SIZE_T size, UINT f
             /* reallocate a moveable block */
             if (size != 0)
             {
-                if (size <= INT_MAX - HLOCAL_STORAGE)
+                if (size <= INT_MAX)
                 {
                     if (mem->ptr)
                     {
-                        if ((ptr = HeapReAlloc( GetProcessHeap(), heap_flags, (char *)mem->ptr - HLOCAL_STORAGE,
-                                                size + HLOCAL_STORAGE )))
+                        if ((ptr = HeapReAlloc( GetProcessHeap(), heap_flags, mem->ptr, size )))
                         {
                             RtlSetUserValueHeap( GetProcessHeap(), heap_flags, ptr, handle );
-                            mem->ptr = (char *)ptr + HLOCAL_STORAGE;
+                            mem->ptr = ptr;
                             ret = handle;
                         }
                     }
                     else
                     {
-                        if ((ptr = HeapAlloc( GetProcessHeap(), heap_flags, size + HLOCAL_STORAGE )))
+                        if ((ptr = HeapAlloc( GetProcessHeap(), heap_flags, size )))
                         {
                             RtlSetUserValueHeap( GetProcessHeap(), heap_flags, ptr, handle );
                             *(HLOCAL *)ptr = handle;
-                            mem->ptr = (char *)ptr + HLOCAL_STORAGE;
+                            mem->ptr = ptr;
                             ret = handle;
                         }
                     }
@@ -1057,7 +1048,7 @@ HLOCAL WINAPI DECLSPEC_HOTPATCH LocalReAlloc( HLOCAL handle, SIZE_T size, UINT f
                 {
                     if (mem->ptr)
                     {
-                        HeapFree( GetProcessHeap(), 0, (char *)mem->ptr - HLOCAL_STORAGE );
+                        HeapFree( GetProcessHeap(), 0, mem->ptr );
                         mem->ptr = NULL;
                     }
                     ret = handle;
-- 
GitLab


https://gitlab.winehq.org/wine/wine/-/merge_requests/222



More information about the wine-devel mailing list