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/* FIXME
108 * These constants need to match up with DL. We need to refactor this to use
109 * the DL header files or vice versa.
110 */
111
112#define TYPE_VOID  0
113#define TYPE_VOIDP 1
114#define TYPE_CHAR  2
115#define TYPE_SHORT 3
116#define TYPE_INT   4
117#define TYPE_LONG  5
118#if HAVE_LONG_LONG
119#define TYPE_LONG_LONG 6
120#endif
121#define TYPE_FLOAT 7
122#define TYPE_DOUBLE 8
123
124#define ALIGN_OF(type) offsetof(struct {char align_c; type align_x;}, align_x)
125
126#define ALIGN_VOIDP  ALIGN_OF(void*)
127#define ALIGN_SHORT  ALIGN_OF(short)
128#define ALIGN_CHAR   ALIGN_OF(char)
129#define ALIGN_INT    ALIGN_OF(int)
130#define ALIGN_LONG   ALIGN_OF(long)
131#if HAVE_LONG_LONG
132#define ALIGN_LONG_LONG ALIGN_OF(LONG_LONG)
133#endif
134#define ALIGN_FLOAT  ALIGN_OF(float)
135#define ALIGN_DOUBLE ALIGN_OF(double)
136
137extern VALUE mFiddle;
138extern VALUE rb_eFiddleError;
139
140VALUE rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type);
141
142#endif
143/* vim: set noet sws=4 sw=4: */
144