Blender  V3.3
dynlib.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "MEM_guardedalloc.h"
13 
14 #include "BLI_dynlib.h"
15 
17  void *handle;
18 };
19 
20 #ifdef WIN32
21 # define WIN32_LEAN_AND_MEAN
22 # include "utf_winfunc.h"
23 # include "utfconv.h"
24 # include <windows.h>
25 
26 DynamicLibrary *BLI_dynlib_open(const char *name)
27 {
29  void *handle;
30 
31  UTF16_ENCODE(name);
32  handle = LoadLibraryW(name_16);
33  UTF16_UN_ENCODE(name);
34 
35  if (!handle) {
36  return NULL;
37  }
38 
39  lib = MEM_callocN(sizeof(*lib), "Dynamic Library");
40  lib->handle = handle;
41 
42  return lib;
43 }
44 
45 void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname)
46 {
47  return GetProcAddress(lib->handle, symname);
48 }
49 
51 {
52  int err;
53 
54  /* if lib is NULL reset the last error code */
55  err = GetLastError();
56  if (!lib) {
57  SetLastError(ERROR_SUCCESS);
58  }
59 
60  if (err) {
61  static char buf[1024];
62 
63  if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
64  NULL,
65  err,
66  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
67  buf,
68  sizeof(buf),
69  NULL)) {
70  return buf;
71  }
72  }
73 
74  return NULL;
75 }
76 
78 {
79  FreeLibrary(lib->handle);
80  MEM_freeN(lib);
81 }
82 
83 #else /* Unix */
84 
85 # include <dlfcn.h>
86 
87 DynamicLibrary *BLI_dynlib_open(const char *name)
88 {
90  void *handle = dlopen(name, RTLD_LAZY);
91 
92  if (!handle) {
93  return NULL;
94  }
95 
96  lib = MEM_callocN(sizeof(*lib), "Dynamic Library");
97  lib->handle = handle;
98 
99  return lib;
100 }
101 
102 void *BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname)
103 {
104  return dlsym(lib->handle, symname);
105 }
106 
108 {
109  (void)lib; /* unused */
110  return dlerror();
111 }
112 
114 {
115  dlclose(lib->handle);
116  MEM_freeN(lib);
117 }
118 
119 #endif
Read Guarded memory(de)allocation.
SyclQueue void void size_t num_bytes void
char * BLI_dynlib_get_error_as_string(DynamicLibrary *lib)
Definition: dynlib.c:107
void BLI_dynlib_close(DynamicLibrary *lib)
Definition: dynlib.c:113
DynamicLibrary * BLI_dynlib_open(const char *name)
Definition: dynlib.c:87
void * BLI_dynlib_find_symbol(DynamicLibrary *lib, const char *symname)
Definition: dynlib.c:102
DRWShaderLibrary * lib
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void * handle
Definition: dynlib.c:17
#define UTF16_ENCODE(in8str)
Definition: utfconv.h:83
#define UTF16_UN_ENCODE(in8str)
Definition: utfconv.h:87
static FT_Error err