00001 #ifndef FIDDLE_H
00002 #define FIDDLE_H
00003
00004 #include <ruby.h>
00005 #include <errno.h>
00006
00007 #if defined(_WIN32)
00008 #include <windows.h>
00009 #endif
00010
00011 #ifdef HAVE_SYS_MMAN_H
00012 #include <sys/mman.h>
00013 #endif
00014
00015 #if defined(HAVE_DLFCN_H)
00016 # include <dlfcn.h>
00017 #
00018 #ifndef RTLD_LAZY
00019 #define RTLD_LAZY 0
00020 #endif
00021 #ifndef RTLD_GLOBAL
00022 #define RTLD_GLOBAL 0
00023 #endif
00024 #ifndef RTLD_NOW
00025 #define RTLD_NOW 0
00026 #endif
00027 #else
00028 # if defined(_WIN32)
00029 # include <windows.h>
00030 # define dlopen(name,flag) ((void*)LoadLibrary(name))
00031 # define dlerror() strerror(rb_w32_map_errno(GetLastError()))
00032 # define dlsym(handle,name) ((void*)GetProcAddress((handle),(name)))
00033 # define RTLD_LAZY -1
00034 # define RTLD_NOW -1
00035 # define RTLD_GLOBAL -1
00036 # endif
00037 #endif
00038
00039 #ifdef USE_HEADER_HACKS
00040 #include <ffi/ffi.h>
00041 #else
00042 #include <ffi.h>
00043 #endif
00044
00045 #undef ffi_type_uchar
00046 #undef ffi_type_schar
00047 #undef ffi_type_ushort
00048 #undef ffi_type_sshort
00049 #undef ffi_type_uint
00050 #undef ffi_type_sint
00051 #undef ffi_type_ulong
00052 #undef ffi_type_slong
00053
00054 #if CHAR_BIT == 8
00055 # define ffi_type_uchar ffi_type_uint8
00056 # define ffi_type_schar ffi_type_sint8
00057 #else
00058 # error "CHAR_BIT not supported"
00059 #endif
00060
00061 # if SIZEOF_SHORT == 2
00062 # define ffi_type_ushort ffi_type_uint16
00063 # define ffi_type_sshort ffi_type_sint16
00064 # elif SIZEOF_SHORT == 4
00065 # define ffi_type_ushort ffi_type_uint32
00066 # define ffi_type_sshort ffi_type_sint32
00067 # else
00068 # error "short size not supported"
00069 # endif
00070
00071 # if SIZEOF_INT == 2
00072 # define ffi_type_uint ffi_type_uint16
00073 # define ffi_type_sint ffi_type_sint16
00074 # elif SIZEOF_INT == 4
00075 # define ffi_type_uint ffi_type_uint32
00076 # define ffi_type_sint ffi_type_sint32
00077 # elif SIZEOF_INT == 8
00078 # define ffi_type_uint ffi_type_uint64
00079 # define ffi_type_sint ffi_type_sint64
00080 # else
00081 # error "int size not supported"
00082 # endif
00083
00084 # if SIZEOF_LONG == 4
00085 # define ffi_type_ulong ffi_type_uint32
00086 # define ffi_type_slong ffi_type_sint32
00087 # elif SIZEOF_LONG == 8
00088 # define ffi_type_ulong ffi_type_uint64
00089 # define ffi_type_slong ffi_type_sint64
00090 # else
00091 # error "long size not supported"
00092 # endif
00093
00094 #if HAVE_LONG_LONG
00095 # if SIZEOF_LONG_LONG == 8
00096 # define ffi_type_slong_long ffi_type_sint64
00097 # define ffi_type_ulong_long ffi_type_uint64
00098 # else
00099 # error "long long size not supported"
00100 # endif
00101 #endif
00102
00103 #include <closure.h>
00104 #include <conversions.h>
00105 #include <function.h>
00106
00107
00108
00109
00110
00111
00112 #define TYPE_VOID 0
00113 #define TYPE_VOIDP 1
00114 #define TYPE_CHAR 2
00115 #define TYPE_SHORT 3
00116 #define TYPE_INT 4
00117 #define TYPE_LONG 5
00118 #if HAVE_LONG_LONG
00119 #define TYPE_LONG_LONG 6
00120 #endif
00121 #define TYPE_FLOAT 7
00122 #define TYPE_DOUBLE 8
00123
00124 #define ALIGN_OF(type) offsetof(struct {char align_c; type align_x;}, align_x)
00125
00126 #define ALIGN_VOIDP ALIGN_OF(void*)
00127 #define ALIGN_SHORT ALIGN_OF(short)
00128 #define ALIGN_CHAR ALIGN_OF(char)
00129 #define ALIGN_INT ALIGN_OF(int)
00130 #define ALIGN_LONG ALIGN_OF(long)
00131 #if HAVE_LONG_LONG
00132 #define ALIGN_LONG_LONG ALIGN_OF(LONG_LONG)
00133 #endif
00134 #define ALIGN_FLOAT ALIGN_OF(float)
00135 #define ALIGN_DOUBLE ALIGN_OF(double)
00136
00137 extern VALUE mFiddle;
00138 extern VALUE rb_eFiddleError;
00139
00140 VALUE rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type);
00141
00142 #endif
00143
00144