[PATCH] kernelbase: Allocate global memory handle table dynamically.

Rémi Bernon rbernon at codeweavers.com
Wed Apr 20 08:03:13 CDT 2022


Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=52763
Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---
 dlls/kernelbase/kernelbase.h |  1 +
 dlls/kernelbase/main.c       |  1 +
 dlls/kernelbase/memory.c     | 22 +++++++++++-----------
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/dlls/kernelbase/kernelbase.h b/dlls/kernelbase/kernelbase.h
index f4bd3e99283..6043fe49dd5 100644
--- a/dlls/kernelbase/kernelbase.h
+++ b/dlls/kernelbase/kernelbase.h
@@ -33,6 +33,7 @@ struct pseudo_console
 
 extern WCHAR *file_name_AtoW( LPCSTR name, BOOL alloc ) DECLSPEC_HIDDEN;
 extern DWORD file_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen ) DECLSPEC_HIDDEN;
+extern void init_global_data(void) DECLSPEC_HIDDEN;
 extern void init_startup_info( RTL_USER_PROCESS_PARAMETERS *params ) DECLSPEC_HIDDEN;
 extern void init_locale( HMODULE module ) DECLSPEC_HIDDEN;
 extern void init_console(void) DECLSPEC_HIDDEN;
diff --git a/dlls/kernelbase/main.c b/dlls/kernelbase/main.c
index 2f2312eccb8..0309ec91589 100644
--- a/dlls/kernelbase/main.c
+++ b/dlls/kernelbase/main.c
@@ -46,6 +46,7 @@ BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
     {
         DisableThreadLibraryCalls( hinst );
         IsWow64Process( GetCurrentProcess(), &is_wow64 );
+        init_global_data();
         init_locale( hinst );
         init_startup_info( NtCurrentTeb()->Peb->ProcessParameters );
         init_console();
diff --git a/dlls/kernelbase/memory.c b/dlls/kernelbase/memory.c
index 21e7c5ef9d9..ea223c3986a 100644
--- a/dlls/kernelbase/memory.c
+++ b/dlls/kernelbase/memory.c
@@ -608,14 +608,8 @@ struct mem_entry
 C_ASSERT(sizeof(struct mem_entry) == 2 * sizeof(void *));
 
 #define MAX_MEM_HANDLES  0x10000
-static struct mem_entry mem_entries[MAX_MEM_HANDLES];
-static struct mem_entry *next_free_mem = mem_entries;
-
-static struct kernelbase_global_data kernelbase_global_data =
-{
-    .mem_entries = mem_entries,
-    .mem_entries_end = mem_entries + MAX_MEM_HANDLES,
-};
+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
@@ -627,7 +621,7 @@ static struct kernelbase_global_data kernelbase_global_data =
 static inline struct mem_entry *unsafe_mem_from_HLOCAL( HLOCAL handle )
 {
     struct mem_entry *mem = CONTAINING_RECORD( handle, struct mem_entry, ptr );
-    struct kernelbase_global_data *data = &kernelbase_global_data;
+    struct kernelbase_global_data *data = &global_data;
     if (((UINT_PTR)handle & ((sizeof(void *) << 1) - 1)) != sizeof(void *)) return NULL;
     if (mem < data->mem_entries || mem >= data->mem_entries_end) return NULL;
     if (!(mem->flags & MEM_FLAG_USED)) return NULL;
@@ -646,6 +640,12 @@ static inline void *unsafe_ptr_from_HLOCAL( HLOCAL handle )
     return handle;
 }
 
+void init_global_data(void)
+{
+    global_data.mem_entries = VirtualAlloc( NULL, MAX_MEM_HANDLES * sizeof(struct mem_entry), MEM_COMMIT, PAGE_READWRITE );
+    if (!(next_free_mem = global_data.mem_entries)) ERR( "Failed to allocate kernelbase global handle table\n" );
+    global_data.mem_entries_end = global_data.mem_entries + MAX_MEM_HANDLES;
+}
 
 /***********************************************************************
  *           KernelBaseGetGlobalData   (kernelbase.@)
@@ -653,7 +653,7 @@ static inline void *unsafe_ptr_from_HLOCAL( HLOCAL handle )
 void *WINAPI KernelBaseGetGlobalData(void)
 {
     WARN_(globalmem)( "semi-stub!\n" );
-    return &kernelbase_global_data;
+    return &global_data;
 }
 
 
@@ -709,7 +709,7 @@ HLOCAL WINAPI DECLSPEC_HOTPATCH LocalAlloc( UINT flags, SIZE_T size )
     }
 
     RtlLockHeap( heap );
-    if ((mem = next_free_mem) < mem_entries || mem >= mem_entries + MAX_MEM_HANDLES)
+    if ((mem = next_free_mem) < global_data.mem_entries || mem >= global_data.mem_entries_end)
         mem = NULL;
     else
     {
-- 
2.35.1




More information about the wine-devel mailing list