This improves performance of unix calls a little by removing one level of indirection and a branch.
From: Rémi Bernon rbernon@codeweavers.com
This improves performance of unix calls a little by removing one level of indirection and a branch. --- dlls/winecrt0/Makefile.in | 1 - dlls/winecrt0/unix_lib.c | 56 --------------------------------------- include/wine/unixlib.h | 32 ++++++++++++++++++++-- 3 files changed, 30 insertions(+), 59 deletions(-) delete mode 100644 dlls/winecrt0/unix_lib.c
diff --git a/dlls/winecrt0/Makefile.in b/dlls/winecrt0/Makefile.in index 6915e69eb4b..db68f399ec3 100644 --- a/dlls/winecrt0/Makefile.in +++ b/dlls/winecrt0/Makefile.in @@ -18,4 +18,3 @@ C_SRCS = \ register.c \ setjmp.c \ stub.c \ - unix_lib.c diff --git a/dlls/winecrt0/unix_lib.c b/dlls/winecrt0/unix_lib.c deleted file mode 100644 index c86897b9905..00000000000 --- a/dlls/winecrt0/unix_lib.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Support for the Unix part of builtin dlls - * - * Copyright 2019 Alexandre Julliard - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifdef __WINE_PE_BUILD - -#include <stdarg.h> - -#include "ntstatus.h" -#define WIN32_NO_STATUS -#include "windef.h" -#include "winbase.h" -#include "winternl.h" -#include "wine/unixlib.h" - -static NTSTATUS (WINAPI *p__wine_unix_call)( unixlib_handle_t, unsigned int, void * ); - -static void load_func( void **func, const char *name, void *def ) -{ - if (!*func) - { - HMODULE module = GetModuleHandleW( L"ntdll.dll" ); - void *proc = GetProcAddress( module, name ); - InterlockedExchangePointer( func, proc ? proc : def ); - } -} -#define LOAD_FUNC(name) load_func( (void **)&p ## name, #name, fallback ## name ) - -static NTSTATUS __cdecl fallback__wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ) -{ - return STATUS_DLL_NOT_FOUND; -} - -NTSTATUS WINAPI __wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ) -{ - LOAD_FUNC( __wine_unix_call ); - return p__wine_unix_call( handle, code, args ); -} - -#endif /* __WINE_PE_BUILD */ diff --git a/include/wine/unixlib.h b/include/wine/unixlib.h index ef60b32184c..fe7a08effbd 100644 --- a/include/wine/unixlib.h +++ b/include/wine/unixlib.h @@ -24,10 +24,10 @@ typedef NTSTATUS (*unixlib_entry_t)( void *args ); typedef UINT64 unixlib_handle_t;
-extern NTSTATUS WINAPI __wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ); - #ifdef WINE_UNIX_LIB
+extern NTSTATUS WINAPI __wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ); + /* some useful helpers from ntdll */ extern const char *ntdll_get_build_dir(void); extern const char *ntdll_get_data_dir(void); @@ -264,6 +264,34 @@ static inline ULONG ntdll_wcstoul( const WCHAR *s, WCHAR **end, int base ) #define wcstol(str,e,b) ntdll_wcstol(str,e,b) #define wcstoul(str,e,b) ntdll_wcstoul(str,e,b)
+#else + +#include "winternl.h" + +static inline NTSTATUS WINAPI delayload__wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ); +static NTSTATUS (WINAPI *__wine_unix_call)( unixlib_handle_t, unsigned int, void * ) = delayload__wine_unix_call; +static inline NTSTATUS WINAPI fallback__wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ) +{ + return STATUS_DLL_NOT_FOUND; +} + +static inline NTSTATUS WINAPI delayload__wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ) +{ + UNICODE_STRING ntdll_str; + ANSI_STRING name_str; + HMODULE module; + void *proc; + + RtlInitUnicodeString( &ntdll_str, L"ntdll.dll" ); + RtlInitAnsiString( &name_str, "__wine_unix_call" ); + + if (LdrGetDllHandleEx( LDR_GET_DLL_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL, &ntdll_str, &module )) proc = NULL; + else if (LdrGetProcedureAddress( module, &name_str, 0, &proc )) proc = NULL; + + InterlockedExchangePointer( (void *)&__wine_unix_call, proc ? proc : fallback__wine_unix_call ); + return __wine_unix_call( handle, code, args ); +} + #endif /* WINE_UNIX_LIB */
#endif /* __WINE_WINE_UNIXLIB_H */
Jacek Caban (@jacek) commented about include/wine/unixlib.h:
#define wcstol(str,e,b) ntdll_wcstol(str,e,b) #define wcstoul(str,e,b) ntdll_wcstoul(str,e,b)
+#else
+#include "winternl.h"
+static inline NTSTATUS WINAPI delayload__wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args ); +static NTSTATUS (WINAPI *__wine_unix_call)( unixlib_handle_t, unsigned int, void * ) = delayload__wine_unix_call;
I don't expect inlining `delayload__wine_unix_call` to matter at all, it will be called only once per module (if anything, I think that inlining like that would make it slightly worse by calling it once per .c file instead). Maybe we could declare __wine_unix_call as an extern pointer here, have the pointer in winecrt0 and keep delayload there?
On Fri Nov 4 14:21:43 2022 +0000, Jacek Caban wrote:
I don't expect inlining `delayload__wine_unix_call` to matter at all, it will be called only once per module (if anything, I think that inlining like that would make it slightly worse by calling it once per .c file instead). Maybe we could declare __wine_unix_call as an extern pointer here, have the pointer in winecrt0 and keep delayload there?
Ah yeah of course, I didn't mean to inline its call, but rather to avoid an unused static function warning. Though it's always used as the __wine_unix_call pointer initializer.
I guess this could probably be done without header inlines at all and simply with an extern pointer as you said.
This merge request was closed by Rémi Bernon.