Ruby  1.9.3p547(2014-05-14revision45962)
dl.h
Go to the documentation of this file.
1 #ifndef RUBY_DL_H
2 #define RUBY_DL_H
3 
4 #include <ruby.h>
5 
6 #if !defined(FUNC_CDECL)
7 # define FUNC_CDECL(x) x
8 #endif
9 
10 #if defined(HAVE_DLFCN_H)
11 # include <dlfcn.h>
12 # /* some stranger systems may not define all of these */
13 #ifndef RTLD_LAZY
14 #define RTLD_LAZY 0
15 #endif
16 #ifndef RTLD_GLOBAL
17 #define RTLD_GLOBAL 0
18 #endif
19 #ifndef RTLD_NOW
20 #define RTLD_NOW 0
21 #endif
22 #else
23 # if defined(_WIN32)
24 # include <windows.h>
25 # define dlopen(name,flag) ((void*)LoadLibrary(name))
26 # define dlerror() strerror(rb_w32_map_errno(GetLastError()))
27 # define dlsym(handle,name) ((void*)GetProcAddress((handle),(name)))
28 # define RTLD_LAZY -1
29 # define RTLD_NOW -1
30 # define RTLD_GLOBAL -1
31 # endif
32 #endif
33 
34 #define MAX_CALLBACK 5
35 #define DLSTACK_TYPE SIGNED_VALUE
36 #define DLSTACK_SIZE (20)
37 #define DLSTACK_PROTO \
38  DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,\
39  DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,\
40  DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,\
41  DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE
42 #define DLSTACK_ARGS(stack) \
43  (stack)[0],(stack)[1],(stack)[2],(stack)[3],(stack)[4],\
44  (stack)[5],(stack)[6],(stack)[7],(stack)[8],(stack)[9],\
45  (stack)[10],(stack)[11],(stack)[12],(stack)[13],(stack)[14],\
46  (stack)[15],(stack)[16],(stack)[17],(stack)[18],(stack)[19]
47 
48 #define DLSTACK_PROTO0_ void
49 #define DLSTACK_PROTO1_ DLSTACK_TYPE
50 #define DLSTACK_PROTO2_ DLSTACK_PROTO1_, DLSTACK_TYPE
51 #define DLSTACK_PROTO3_ DLSTACK_PROTO2_, DLSTACK_TYPE
52 #define DLSTACK_PROTO4_ DLSTACK_PROTO3_, DLSTACK_TYPE
53 #define DLSTACK_PROTO4_ DLSTACK_PROTO3_, DLSTACK_TYPE
54 #define DLSTACK_PROTO5_ DLSTACK_PROTO4_, DLSTACK_TYPE
55 #define DLSTACK_PROTO6_ DLSTACK_PROTO5_, DLSTACK_TYPE
56 #define DLSTACK_PROTO7_ DLSTACK_PROTO6_, DLSTACK_TYPE
57 #define DLSTACK_PROTO8_ DLSTACK_PROTO7_, DLSTACK_TYPE
58 #define DLSTACK_PROTO9_ DLSTACK_PROTO8_, DLSTACK_TYPE
59 #define DLSTACK_PROTO10_ DLSTACK_PROTO9_, DLSTACK_TYPE
60 #define DLSTACK_PROTO11_ DLSTACK_PROTO10_, DLSTACK_TYPE
61 #define DLSTACK_PROTO12_ DLSTACK_PROTO11_, DLSTACK_TYPE
62 #define DLSTACK_PROTO13_ DLSTACK_PROTO12_, DLSTACK_TYPE
63 #define DLSTACK_PROTO14_ DLSTACK_PROTO13_, DLSTACK_TYPE
64 #define DLSTACK_PROTO14_ DLSTACK_PROTO13_, DLSTACK_TYPE
65 #define DLSTACK_PROTO15_ DLSTACK_PROTO14_, DLSTACK_TYPE
66 #define DLSTACK_PROTO16_ DLSTACK_PROTO15_, DLSTACK_TYPE
67 #define DLSTACK_PROTO17_ DLSTACK_PROTO16_, DLSTACK_TYPE
68 #define DLSTACK_PROTO18_ DLSTACK_PROTO17_, DLSTACK_TYPE
69 #define DLSTACK_PROTO19_ DLSTACK_PROTO18_, DLSTACK_TYPE
70 #define DLSTACK_PROTO20_ DLSTACK_PROTO19_, DLSTACK_TYPE
71 
72 /*
73  * Add ",..." as the last argument.
74  * This is required for variable argument functions such
75  * as fprintf() on x86_64-linux.
76  *
77  * http://refspecs.linuxfoundation.org/elf/x86_64-abi-0.95.pdf
78  * page 19:
79  *
80  * For calls that may call functions that use varargs or stdargs
81  * (prototype-less calls or calls to functions containing ellipsis
82  * (...) in the declaration) %al is used as hidden argument to
83  * specify the number of SSE registers used.
84  */
85 #define DLSTACK_PROTO0 void
86 #define DLSTACK_PROTO1 DLSTACK_PROTO1_, ...
87 #define DLSTACK_PROTO2 DLSTACK_PROTO2_, ...
88 #define DLSTACK_PROTO3 DLSTACK_PROTO3_, ...
89 #define DLSTACK_PROTO4 DLSTACK_PROTO4_, ...
90 #define DLSTACK_PROTO4 DLSTACK_PROTO4_, ...
91 #define DLSTACK_PROTO5 DLSTACK_PROTO5_, ...
92 #define DLSTACK_PROTO6 DLSTACK_PROTO6_, ...
93 #define DLSTACK_PROTO7 DLSTACK_PROTO7_, ...
94 #define DLSTACK_PROTO8 DLSTACK_PROTO8_, ...
95 #define DLSTACK_PROTO9 DLSTACK_PROTO9_, ...
96 #define DLSTACK_PROTO10 DLSTACK_PROTO10_, ...
97 #define DLSTACK_PROTO11 DLSTACK_PROTO11_, ...
98 #define DLSTACK_PROTO12 DLSTACK_PROTO12_, ...
99 #define DLSTACK_PROTO13 DLSTACK_PROTO13_, ...
100 #define DLSTACK_PROTO14 DLSTACK_PROTO14_, ...
101 #define DLSTACK_PROTO14 DLSTACK_PROTO14_, ...
102 #define DLSTACK_PROTO15 DLSTACK_PROTO15_, ...
103 #define DLSTACK_PROTO16 DLSTACK_PROTO16_, ...
104 #define DLSTACK_PROTO17 DLSTACK_PROTO17_, ...
105 #define DLSTACK_PROTO18 DLSTACK_PROTO18_, ...
106 #define DLSTACK_PROTO19 DLSTACK_PROTO19_, ...
107 #define DLSTACK_PROTO20 DLSTACK_PROTO20_, ...
108 
109 #define DLSTACK_ARGS0(stack)
110 #define DLSTACK_ARGS1(stack) (stack)[0]
111 #define DLSTACK_ARGS2(stack) DLSTACK_ARGS1(stack), (stack)[1]
112 #define DLSTACK_ARGS3(stack) DLSTACK_ARGS2(stack), (stack)[2]
113 #define DLSTACK_ARGS4(stack) DLSTACK_ARGS3(stack), (stack)[3]
114 #define DLSTACK_ARGS5(stack) DLSTACK_ARGS4(stack), (stack)[4]
115 #define DLSTACK_ARGS6(stack) DLSTACK_ARGS5(stack), (stack)[5]
116 #define DLSTACK_ARGS7(stack) DLSTACK_ARGS6(stack), (stack)[6]
117 #define DLSTACK_ARGS8(stack) DLSTACK_ARGS7(stack), (stack)[7]
118 #define DLSTACK_ARGS9(stack) DLSTACK_ARGS8(stack), (stack)[8]
119 #define DLSTACK_ARGS10(stack) DLSTACK_ARGS9(stack), (stack)[9]
120 #define DLSTACK_ARGS11(stack) DLSTACK_ARGS10(stack), (stack)[10]
121 #define DLSTACK_ARGS12(stack) DLSTACK_ARGS11(stack), (stack)[11]
122 #define DLSTACK_ARGS13(stack) DLSTACK_ARGS12(stack), (stack)[12]
123 #define DLSTACK_ARGS14(stack) DLSTACK_ARGS13(stack), (stack)[13]
124 #define DLSTACK_ARGS15(stack) DLSTACK_ARGS14(stack), (stack)[14]
125 #define DLSTACK_ARGS16(stack) DLSTACK_ARGS15(stack), (stack)[15]
126 #define DLSTACK_ARGS17(stack) DLSTACK_ARGS16(stack), (stack)[16]
127 #define DLSTACK_ARGS18(stack) DLSTACK_ARGS17(stack), (stack)[17]
128 #define DLSTACK_ARGS19(stack) DLSTACK_ARGS18(stack), (stack)[18]
129 #define DLSTACK_ARGS20(stack) DLSTACK_ARGS19(stack), (stack)[19]
130 
131 extern VALUE rb_mDL;
132 extern VALUE rb_cDLHandle;
133 extern VALUE rb_cDLSymbol;
134 extern VALUE rb_eDLError;
135 extern VALUE rb_eDLTypeError;
136 
137 typedef struct { char c; void *x; } s_voidp;
138 typedef struct { char c; short x; } s_short;
139 typedef struct { char c; int x; } s_int;
140 typedef struct { char c; long x; } s_long;
141 typedef struct { char c; float x; } s_float;
142 typedef struct { char c; double x; } s_double;
143 #if HAVE_LONG_LONG
144 typedef struct { char c; LONG_LONG x; } s_long_long;
145 #endif
146 
147 #define ALIGN_VOIDP (sizeof(s_voidp) - sizeof(void *))
148 #define ALIGN_SHORT (sizeof(s_short) - sizeof(short))
149 #define ALIGN_CHAR (1)
150 #define ALIGN_INT (sizeof(s_int) - sizeof(int))
151 #define ALIGN_LONG (sizeof(s_long) - sizeof(long))
152 #if HAVE_LONG_LONG
153 #define ALIGN_LONG_LONG (sizeof(s_long_long) - sizeof(LONG_LONG))
154 #endif
155 #define ALIGN_FLOAT (sizeof(s_float) - sizeof(float))
156 #define ALIGN_DOUBLE (sizeof(s_double) - sizeof(double))
157 
158 #define DLALIGN(ptr,offset,align) {\
159  while( (((unsigned long)((char *)(ptr) + (offset))) % (align)) != 0 ) (offset)++;\
160 }
161 
162 
163 #define DLTYPE_VOID 0
164 #define DLTYPE_VOIDP 1
165 #define DLTYPE_CHAR 2
166 #define DLTYPE_SHORT 3
167 #define DLTYPE_INT 4
168 #define DLTYPE_LONG 5
169 #if HAVE_LONG_LONG
170 #define DLTYPE_LONG_LONG 6
171 #endif
172 #define DLTYPE_FLOAT 7
173 #define DLTYPE_DOUBLE 8
174 #define MAX_DLTYPE 9
175 
176 #if SIZEOF_VOIDP == SIZEOF_LONG
177 # define PTR2NUM(x) (ULONG2NUM((unsigned long)(x)))
178 # define NUM2PTR(x) ((void*)(NUM2ULONG(x)))
179 #else
180 /* # error --->> Ruby/DL2 requires sizeof(void*) == sizeof(long) to be compiled. <<--- */
181 # define PTR2NUM(x) (ULL2NUM((unsigned long long)(x)))
182 # define NUM2PTR(x) ((void*)(NUM2ULL(x)))
183 #endif
184 
185 #define BOOL2INT(x) (((x) == Qtrue)?1:0)
186 #define INT2BOOL(x) ((x)?Qtrue:Qfalse)
187 
188 typedef void (*freefunc_t)(void*);
189 
190 struct dl_handle {
191  void *ptr;
192  int open;
194 };
195 
196 
197 struct cfunc_data {
198  void *ptr;
199  char *name;
200  int type;
203 };
204 extern ID rbdl_id_cdecl;
205 extern ID rbdl_id_stdcall;
206 #define CFUNC_CDECL (rbdl_id_cdecl)
207 #define CFUNC_STDCALL (rbdl_id_stdcall)
208 
209 struct ptr_data {
210  void *ptr;
211  long size;
214 };
215 
216 #define RDL_HANDLE(obj) ((struct dl_handle *)(DATA_PTR(obj)))
217 #define RCFUNC_DATA(obj) ((struct cfunc_data *)(DATA_PTR(obj)))
218 #define RPTR_DATA(obj) ((struct ptr_data *)(DATA_PTR(obj)))
219 
220 VALUE rb_dlcfunc_new(void (*func)(), int dltype, const char * name, ID calltype);
222 VALUE rb_dlptr_new(void *ptr, long size, freefunc_t func);
223 VALUE rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func);
225 
226 #endif
short x
Definition: dl.h:138
Definition: dl.h:142
VALUE rb_dlptr_malloc(long size, freefunc_t func)
Definition: cptr.c:107
freefunc_t free
Definition: dl.h:212
Definition: dl.h:140
int open
Definition: dl.h:192
Definition: dl.h:190
char * name
Definition: dl.h:199
int x
Definition: dl.h:139
Definition: dl.h:138
int rb_dlcfunc_kind_p(VALUE func)
Definition: cfunc.c:139
VALUE rb_mDL
Definition: dl.c:13
void * x
Definition: dl.h:137
Definition: dl.h:141
int type
Definition: dl.h:200
VALUE rb_dlptr_new(void *ptr, long size, freefunc_t func)
Definition: cptr.c:101
long x
Definition: dl.h:140
Definition: dl.h:137
int enable_close
Definition: dl.h:193
VALUE rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func)
Definition: cptr.c:85
VALUE wrap[2]
Definition: dl.h:213
void * ptr
Definition: dl.h:191
void * ptr
Definition: dl.h:210
Definition: dl.h:197
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:104
Definition: dl.h:209
unsigned long ID
Definition: ruby.h:89
unsigned long VALUE
Definition: ruby.h:88
VALUE rb_eDLError
Definition: dl.c:14
int size
Definition: encoding.c:51
void * ptr
Definition: dl.h:198
ID rbdl_id_cdecl
Definition: dl.c:17
Definition: dl.h:139
VALUE wrap
Definition: dl.h:202
VALUE rb_eDLTypeError
Definition: dl.c:15
VALUE rb_dlcfunc_new(void(*func)(), int dltype, const char *name, ID calltype)
Definition: cfunc.c:83
const char * name
Definition: nkf.c:208
double x
Definition: dl.h:142
VALUE rb_cDLHandle
Definition: handle.c:8
void(* freefunc_t)(void *)
Definition: dl.h:188
VALUE rb_cDLSymbol
ID rbdl_id_stdcall
Definition: dl.c:18
long size
Definition: dl.h:211
float x
Definition: dl.h:141
ID calltype
Definition: dl.h:201