diff options
author | Vincent Ambo <Vincent Ambo> | 2020-01-11T23·36+0000 |
---|---|---|
committer | Vincent Ambo <Vincent Ambo> | 2020-01-11T23·40+0000 |
commit | 7ef0d62730840ded097b524104cc0a0904591a63 (patch) | |
tree | a670f96103667aeca4789a95d94ca0dff550c4ce /third_party/git/compat/win32/lazyload.h | |
parent | 6a2a3007077818e24a3d56fc492ada9206a10cf0 (diff) | |
parent | 1b593e1ea4d2af0f6444d9a7788d5d99abd6fde5 (diff) |
merge(third_party/git): Merge squashed git subtree at v2.23.0 r/373
Merge commit '1b593e1ea4d2af0f6444d9a7788d5d99abd6fde5' as 'third_party/git'
Diffstat (limited to 'third_party/git/compat/win32/lazyload.h')
-rw-r--r-- | third_party/git/compat/win32/lazyload.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/third_party/git/compat/win32/lazyload.h b/third_party/git/compat/win32/lazyload.h new file mode 100644 index 000000000000..9e631c8593ff --- /dev/null +++ b/third_party/git/compat/win32/lazyload.h @@ -0,0 +1,57 @@ +#ifndef LAZYLOAD_H +#define LAZYLOAD_H + +/* + * A pair of macros to simplify loading of DLL functions. Example: + * + * DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW, + * LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES); + * + * if (!INIT_PROC_ADDR(CreateHardLinkW)) + * return error("Could not find CreateHardLinkW() function"; + * + * if (!CreateHardLinkW(source, target, NULL)) + * return error("could not create hardlink from %S to %S", + * source, target); + */ + +struct proc_addr { + const char *const dll; + const char *const function; + FARPROC pfunction; + unsigned initialized : 1; +}; + +/* Declares a function to be loaded dynamically from a DLL. */ +#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \ + static struct proc_addr proc_addr_##function = \ + { #dll, #function, NULL, 0 }; \ + static rettype (WINAPI *function)(__VA_ARGS__) + +/* + * Loads a function from a DLL (once-only). + * Returns non-NULL function pointer on success. + * Returns NULL + errno == ENOSYS on failure. + * This function is not thread-safe. + */ +#define INIT_PROC_ADDR(function) \ + (function = get_proc_addr(&proc_addr_##function)) + +static inline void *get_proc_addr(struct proc_addr *proc) +{ + /* only do this once */ + if (!proc->initialized) { + HANDLE hnd; + proc->initialized = 1; + hnd = LoadLibraryExA(proc->dll, NULL, + LOAD_LIBRARY_SEARCH_SYSTEM32); + if (hnd) + proc->pfunction = GetProcAddress(hnd, proc->function); + } + /* set ENOSYS if DLL or function was not found */ + if (!proc->pfunction) + errno = ENOSYS; + return proc->pfunction; +} + +#endif |