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