Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
fiddle.h
Go to the documentation of this file.
1 #ifndef FIDDLE_H
2 #define FIDDLE_H
3 
4 #include <ruby.h>
5 #include <errno.h>
6 
7 #if defined(_WIN32)
8 #include <windows.h>
9 #endif
10 
11 #ifdef HAVE_SYS_MMAN_H
12 #include <sys/mman.h>
13 #endif
14 
15 #if defined(HAVE_LINK_H)
16 # include <link.h>
17 #endif
18 
19 #if defined(HAVE_DLFCN_H)
20 # include <dlfcn.h>
21 # /* some stranger systems may not define all of these */
22 #ifndef RTLD_LAZY
23 #define RTLD_LAZY 0
24 #endif
25 #ifndef RTLD_GLOBAL
26 #define RTLD_GLOBAL 0
27 #endif
28 #ifndef RTLD_NOW
29 #define RTLD_NOW 0
30 #endif
31 #else
32 # if defined(_WIN32)
33 # include <windows.h>
34 # define dlopen(name,flag) ((void*)LoadLibrary(name))
35 # define dlerror() strerror(rb_w32_map_errno(GetLastError()))
36 # define dlsym(handle,name) ((void*)GetProcAddress((handle),(name)))
37 # define RTLD_LAZY -1
38 # define RTLD_NOW -1
39 # define RTLD_GLOBAL -1
40 # endif
41 #endif
42 
43 #ifdef USE_HEADER_HACKS
44 #include <ffi/ffi.h>
45 #else
46 #include <ffi.h>
47 #endif
48 
49 #undef ffi_type_uchar
50 #undef ffi_type_schar
51 #undef ffi_type_ushort
52 #undef ffi_type_sshort
53 #undef ffi_type_uint
54 #undef ffi_type_sint
55 #undef ffi_type_ulong
56 #undef ffi_type_slong
57 
58 #if CHAR_BIT == 8
59 # define ffi_type_uchar ffi_type_uint8
60 # define ffi_type_schar ffi_type_sint8
61 #else
62 # error "CHAR_BIT not supported"
63 #endif
64 
65 #if SIZEOF_SHORT == 2
66 # define ffi_type_ushort ffi_type_uint16
67 # define ffi_type_sshort ffi_type_sint16
68 #elif SIZEOF_SHORT == 4
69 # define ffi_type_ushort ffi_type_uint32
70 # define ffi_type_sshort ffi_type_sint32
71 #else
72 # error "short size not supported"
73 #endif
74 
75 #if SIZEOF_INT == 2
76 # define ffi_type_uint ffi_type_uint16
77 # define ffi_type_sint ffi_type_sint16
78 #elif SIZEOF_INT == 4
79 # define ffi_type_uint ffi_type_uint32
80 # define ffi_type_sint ffi_type_sint32
81 #elif SIZEOF_INT == 8
82 # define ffi_type_uint ffi_type_uint64
83 # define ffi_type_sint ffi_type_sint64
84 #else
85 # error "int size not supported"
86 #endif
87 
88 #if SIZEOF_LONG == 4
89 # define ffi_type_ulong ffi_type_uint32
90 # define ffi_type_slong ffi_type_sint32
91 #elif SIZEOF_LONG == 8
92 # define ffi_type_ulong ffi_type_uint64
93 # define ffi_type_slong ffi_type_sint64
94 #else
95 # error "long size not supported"
96 #endif
97 
98 #if HAVE_LONG_LONG
99 # if SIZEOF_LONG_LONG == 8
100 # define ffi_type_slong_long ffi_type_sint64
101 # define ffi_type_ulong_long ffi_type_uint64
102 # else
103 # error "long long size not supported"
104 # endif
105 #endif
106 
107 #include <closure.h>
108 #include <conversions.h>
109 #include <function.h>
110 
111 #define TYPE_VOID 0
112 #define TYPE_VOIDP 1
113 #define TYPE_CHAR 2
114 #define TYPE_SHORT 3
115 #define TYPE_INT 4
116 #define TYPE_LONG 5
117 #if HAVE_LONG_LONG
118 #define TYPE_LONG_LONG 6
119 #endif
120 #define TYPE_FLOAT 7
121 #define TYPE_DOUBLE 8
122 #define TYPE_VARIADIC 9
123 #define TYPE_CONST_STRING 10
124 
125 #define TYPE_INT8_T TYPE_CHAR
126 #if SIZEOF_SHORT == 2
127 # define TYPE_INT16_T TYPE_SHORT
128 #elif SIZEOF_INT == 2
129 # define TYPE_INT16_T TYPE_INT
130 #endif
131 #if SIZEOF_SHORT == 4
132 # define TYPE_INT32_T TYPE_SHORT
133 #elif SIZEOF_INT == 4
134 # define TYPE_INT32_T TYPE_INT
135 #elif SIZEOF_LONG == 4
136 # define TYPE_INT32_T TYPE_LONG
137 #endif
138 #if SIZEOF_INT == 8
139 # define TYPE_INT64_T TYPE_INT
140 #elif SIZEOF_LONG == 8
141 # define TYPE_INT64_T TYPE_LONG
142 #elif defined(TYPE_LONG_LONG)
143 # define TYPE_INT64_T TYPE_LONG_LONG
144 #endif
145 
146 #ifndef TYPE_SSIZE_T
147 # if SIZEOF_SIZE_T == SIZEOF_INT
148 # define TYPE_SSIZE_T TYPE_INT
149 # elif SIZEOF_SIZE_T == SIZEOF_LONG
150 # define TYPE_SSIZE_T TYPE_LONG
151 # elif defined HAVE_LONG_LONG && SIZEOF_SIZE_T == SIZEOF_LONG_LONG
152 # define TYPE_SSIZE_T TYPE_LONG_LONG
153 # endif
154 #endif
155 #define TYPE_SIZE_T (-1*SIGNEDNESS_OF_SIZE_T*TYPE_SSIZE_T)
156 
157 #ifndef TYPE_PTRDIFF_T
158 # if SIZEOF_PTRDIFF_T == SIZEOF_INT
159 # define TYPE_PTRDIFF_T TYPE_INT
160 # elif SIZEOF_PTRDIFF_T == SIZEOF_LONG
161 # define TYPE_PTRDIFF_T TYPE_LONG
162 # elif defined HAVE_LONG_LONG && SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
163 # define TYPE_PTRDIFF_T TYPE_LONG_LONG
164 # endif
165 #endif
166 
167 #ifndef TYPE_INTPTR_T
168 # if SIZEOF_INTPTR_T == SIZEOF_INT
169 # define TYPE_INTPTR_T TYPE_INT
170 # elif SIZEOF_INTPTR_T == SIZEOF_LONG
171 # define TYPE_INTPTR_T TYPE_LONG
172 # elif defined HAVE_LONG_LONG && SIZEOF_INTPTR_T == SIZEOF_LONG_LONG
173 # define TYPE_INTPTR_T TYPE_LONG_LONG
174 # endif
175 #endif
176 #define TYPE_UINTPTR_T (-TYPE_INTPTR_T)
177 
178 #define ALIGN_OF(type) offsetof(struct {char align_c; type align_x;}, align_x)
179 
180 #define ALIGN_VOIDP ALIGN_OF(void*)
181 #define ALIGN_CHAR ALIGN_OF(char)
182 #define ALIGN_SHORT ALIGN_OF(short)
183 #define ALIGN_INT ALIGN_OF(int)
184 #define ALIGN_LONG ALIGN_OF(long)
185 #if HAVE_LONG_LONG
186 #define ALIGN_LONG_LONG ALIGN_OF(LONG_LONG)
187 #endif
188 #define ALIGN_FLOAT ALIGN_OF(float)
189 #define ALIGN_DOUBLE ALIGN_OF(double)
190 
191 #define ALIGN_INT8_T ALIGN_OF(int8_t)
192 #define ALIGN_INT16_T ALIGN_OF(int16_t)
193 #define ALIGN_INT32_T ALIGN_OF(int32_t)
194 #define ALIGN_INT64_T ALIGN_OF(int64_t)
195 
196 extern VALUE mFiddle;
197 extern VALUE rb_eFiddleDLError;
198 
199 VALUE rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type);
200 
201 typedef void (*rb_fiddle_freefunc_t)(void*);
202 VALUE rb_fiddle_ptr_new_wrap(void *ptr, long size, rb_fiddle_freefunc_t func, VALUE wrap0, VALUE wrap1);
203 
204 #endif
205 /* vim: set noet sws=4 sw=4: */
rb_fiddle_freefunc_t
void(* rb_fiddle_freefunc_t)(void *)
Definition: fiddle.h:201
closure.h
ptr
struct RIMemo * ptr
Definition: debug.c:87
rb_fiddle_ptr_new_wrap
VALUE rb_fiddle_ptr_new_wrap(void *ptr, long size, rb_fiddle_freefunc_t func, VALUE wrap0, VALUE wrap1)
Definition: pointer.c:145
conversions.h
ruby.h
mFiddle
VALUE mFiddle
Definition: fiddle.c:3
VALUE
unsigned long VALUE
Definition: value.h:38
function.h
rb_fiddle_new_function
VALUE rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type)
Definition: function.c:69
size
rb_atomic_t size
Definition: signal.c:509
rb_eFiddleDLError
VALUE rb_eFiddleDLError
Definition: fiddle.c:4