00001 #ifndef RUBY_DL_H
00002 #define RUBY_DL_H
00003
00004 #include <ruby.h>
00005
00006 #if !defined(FUNC_CDECL)
00007 # define FUNC_CDECL(x) x
00008 #endif
00009
00010 #if defined(HAVE_DLFCN_H)
00011 # include <dlfcn.h>
00012 #
00013 #ifndef RTLD_LAZY
00014 #define RTLD_LAZY 0
00015 #endif
00016 #ifndef RTLD_GLOBAL
00017 #define RTLD_GLOBAL 0
00018 #endif
00019 #ifndef RTLD_NOW
00020 #define RTLD_NOW 0
00021 #endif
00022 #else
00023 # if defined(HAVE_WINDOWS_H)
00024 # include <windows.h>
00025 # define dlopen(name,flag) ((void*)LoadLibrary(name))
00026 # define dlerror() strerror(rb_w32_map_errno(GetLastError()))
00027 # define dlsym(handle,name) ((void*)GetProcAddress(handle,name))
00028 # define RTLD_LAZY -1
00029 # define RTLD_NOW -1
00030 # define RTLD_GLOBAL -1
00031 # endif
00032 #endif
00033
00034 #define MAX_CALLBACK 5
00035 #define DLSTACK_TYPE SIGNED_VALUE
00036 #define DLSTACK_SIZE (20)
00037 #define DLSTACK_PROTO \
00038 DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,\
00039 DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,\
00040 DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,\
00041 DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE,DLSTACK_TYPE
00042 #define DLSTACK_ARGS(stack) \
00043 stack[0],stack[1],stack[2],stack[3],stack[4],\
00044 stack[5],stack[6],stack[7],stack[8],stack[9],\
00045 stack[10],stack[11],stack[12],stack[13],stack[14],\
00046 stack[15],stack[16],stack[17],stack[18],stack[19]
00047
00048 #define DLSTACK_PROTO0_ void
00049 #define DLSTACK_PROTO1_ DLSTACK_TYPE
00050 #define DLSTACK_PROTO2_ DLSTACK_PROTO1_, DLSTACK_TYPE
00051 #define DLSTACK_PROTO3_ DLSTACK_PROTO2_, DLSTACK_TYPE
00052 #define DLSTACK_PROTO4_ DLSTACK_PROTO3_, DLSTACK_TYPE
00053 #define DLSTACK_PROTO4_ DLSTACK_PROTO3_, DLSTACK_TYPE
00054 #define DLSTACK_PROTO5_ DLSTACK_PROTO4_, DLSTACK_TYPE
00055 #define DLSTACK_PROTO6_ DLSTACK_PROTO5_, DLSTACK_TYPE
00056 #define DLSTACK_PROTO7_ DLSTACK_PROTO6_, DLSTACK_TYPE
00057 #define DLSTACK_PROTO8_ DLSTACK_PROTO7_, DLSTACK_TYPE
00058 #define DLSTACK_PROTO9_ DLSTACK_PROTO8_, DLSTACK_TYPE
00059 #define DLSTACK_PROTO10_ DLSTACK_PROTO9_, DLSTACK_TYPE
00060 #define DLSTACK_PROTO11_ DLSTACK_PROTO10_, DLSTACK_TYPE
00061 #define DLSTACK_PROTO12_ DLSTACK_PROTO11_, DLSTACK_TYPE
00062 #define DLSTACK_PROTO13_ DLSTACK_PROTO12_, DLSTACK_TYPE
00063 #define DLSTACK_PROTO14_ DLSTACK_PROTO13_, DLSTACK_TYPE
00064 #define DLSTACK_PROTO14_ DLSTACK_PROTO13_, DLSTACK_TYPE
00065 #define DLSTACK_PROTO15_ DLSTACK_PROTO14_, DLSTACK_TYPE
00066 #define DLSTACK_PROTO16_ DLSTACK_PROTO15_, DLSTACK_TYPE
00067 #define DLSTACK_PROTO17_ DLSTACK_PROTO16_, DLSTACK_TYPE
00068 #define DLSTACK_PROTO18_ DLSTACK_PROTO17_, DLSTACK_TYPE
00069 #define DLSTACK_PROTO19_ DLSTACK_PROTO18_, DLSTACK_TYPE
00070 #define DLSTACK_PROTO20_ DLSTACK_PROTO19_, DLSTACK_TYPE
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 #define DLSTACK_PROTO0 void
00086 #define DLSTACK_PROTO1 DLSTACK_PROTO1_, ...
00087 #define DLSTACK_PROTO2 DLSTACK_PROTO2_, ...
00088 #define DLSTACK_PROTO3 DLSTACK_PROTO3_, ...
00089 #define DLSTACK_PROTO4 DLSTACK_PROTO4_, ...
00090 #define DLSTACK_PROTO4 DLSTACK_PROTO4_, ...
00091 #define DLSTACK_PROTO5 DLSTACK_PROTO5_, ...
00092 #define DLSTACK_PROTO6 DLSTACK_PROTO6_, ...
00093 #define DLSTACK_PROTO7 DLSTACK_PROTO7_, ...
00094 #define DLSTACK_PROTO8 DLSTACK_PROTO8_, ...
00095 #define DLSTACK_PROTO9 DLSTACK_PROTO9_, ...
00096 #define DLSTACK_PROTO10 DLSTACK_PROTO10_, ...
00097 #define DLSTACK_PROTO11 DLSTACK_PROTO11_, ...
00098 #define DLSTACK_PROTO12 DLSTACK_PROTO12_, ...
00099 #define DLSTACK_PROTO13 DLSTACK_PROTO13_, ...
00100 #define DLSTACK_PROTO14 DLSTACK_PROTO14_, ...
00101 #define DLSTACK_PROTO14 DLSTACK_PROTO14_, ...
00102 #define DLSTACK_PROTO15 DLSTACK_PROTO15_, ...
00103 #define DLSTACK_PROTO16 DLSTACK_PROTO16_, ...
00104 #define DLSTACK_PROTO17 DLSTACK_PROTO17_, ...
00105 #define DLSTACK_PROTO18 DLSTACK_PROTO18_, ...
00106 #define DLSTACK_PROTO19 DLSTACK_PROTO19_, ...
00107 #define DLSTACK_PROTO20 DLSTACK_PROTO20_, ...
00108
00109 #define DLSTACK_ARGS0(stack)
00110 #define DLSTACK_ARGS1(stack) stack[0]
00111 #define DLSTACK_ARGS2(stack) DLSTACK_ARGS1(stack), stack[1]
00112 #define DLSTACK_ARGS3(stack) DLSTACK_ARGS2(stack), stack[2]
00113 #define DLSTACK_ARGS4(stack) DLSTACK_ARGS3(stack), stack[3]
00114 #define DLSTACK_ARGS5(stack) DLSTACK_ARGS4(stack), stack[4]
00115 #define DLSTACK_ARGS6(stack) DLSTACK_ARGS5(stack), stack[5]
00116 #define DLSTACK_ARGS7(stack) DLSTACK_ARGS6(stack), stack[6]
00117 #define DLSTACK_ARGS8(stack) DLSTACK_ARGS7(stack), stack[7]
00118 #define DLSTACK_ARGS9(stack) DLSTACK_ARGS8(stack), stack[8]
00119 #define DLSTACK_ARGS10(stack) DLSTACK_ARGS9(stack), stack[9]
00120 #define DLSTACK_ARGS11(stack) DLSTACK_ARGS10(stack), stack[10]
00121 #define DLSTACK_ARGS12(stack) DLSTACK_ARGS11(stack), stack[11]
00122 #define DLSTACK_ARGS13(stack) DLSTACK_ARGS12(stack), stack[12]
00123 #define DLSTACK_ARGS14(stack) DLSTACK_ARGS13(stack), stack[13]
00124 #define DLSTACK_ARGS15(stack) DLSTACK_ARGS14(stack), stack[14]
00125 #define DLSTACK_ARGS16(stack) DLSTACK_ARGS15(stack), stack[15]
00126 #define DLSTACK_ARGS17(stack) DLSTACK_ARGS16(stack), stack[16]
00127 #define DLSTACK_ARGS18(stack) DLSTACK_ARGS17(stack), stack[17]
00128 #define DLSTACK_ARGS19(stack) DLSTACK_ARGS18(stack), stack[18]
00129 #define DLSTACK_ARGS20(stack) DLSTACK_ARGS19(stack), stack[19]
00130
00131 extern VALUE rb_mDL;
00132 extern VALUE rb_cDLHandle;
00133 extern VALUE rb_cDLSymbol;
00134 extern VALUE rb_eDLError;
00135 extern VALUE rb_eDLTypeError;
00136
00137 typedef struct { char c; void *x; } s_voidp;
00138 typedef struct { char c; short x; } s_short;
00139 typedef struct { char c; int x; } s_int;
00140 typedef struct { char c; long x; } s_long;
00141 typedef struct { char c; float x; } s_float;
00142 typedef struct { char c; double x; } s_double;
00143 #if HAVE_LONG_LONG
00144 typedef struct { char c; LONG_LONG x; } s_long_long;
00145 #endif
00146
00147 #define ALIGN_VOIDP (sizeof(s_voidp) - sizeof(void *))
00148 #define ALIGN_SHORT (sizeof(s_short) - sizeof(short))
00149 #define ALIGN_CHAR (1)
00150 #define ALIGN_INT (sizeof(s_int) - sizeof(int))
00151 #define ALIGN_LONG (sizeof(s_long) - sizeof(long))
00152 #if HAVE_LONG_LONG
00153 #define ALIGN_LONG_LONG (sizeof(s_long_long) - sizeof(LONG_LONG))
00154 #endif
00155 #define ALIGN_FLOAT (sizeof(s_float) - sizeof(float))
00156 #define ALIGN_DOUBLE (sizeof(s_double) - sizeof(double))
00157
00158 #define DLALIGN(ptr,offset,align) {\
00159 while( (((unsigned long)((char *)ptr + offset)) % align) != 0 ) offset++;\
00160 }
00161
00162
00163 #define DLTYPE_VOID 0
00164 #define DLTYPE_VOIDP 1
00165 #define DLTYPE_CHAR 2
00166 #define DLTYPE_SHORT 3
00167 #define DLTYPE_INT 4
00168 #define DLTYPE_LONG 5
00169 #if HAVE_LONG_LONG
00170 #define DLTYPE_LONG_LONG 6
00171 #endif
00172 #define DLTYPE_FLOAT 7
00173 #define DLTYPE_DOUBLE 8
00174 #define MAX_DLTYPE 9
00175
00176 #if SIZEOF_VOIDP == SIZEOF_LONG
00177 # define PTR2NUM(x) (ULONG2NUM((unsigned long)(x)))
00178 # define NUM2PTR(x) ((void*)(NUM2ULONG(x)))
00179 #else
00180
00181 # define PTR2NUM(x) (ULL2NUM((unsigned long long)(x)))
00182 # define NUM2PTR(x) ((void*)(NUM2ULL(x)))
00183 #endif
00184
00185 #define BOOL2INT(x) ((x == Qtrue)?1:0)
00186 #define INT2BOOL(x) (x?Qtrue:Qfalse)
00187
00188 typedef void (*freefunc_t)(void*);
00189
00190 struct dl_handle {
00191 void *ptr;
00192 int open;
00193 int enable_close;
00194 };
00195
00196
00197 struct cfunc_data {
00198 void *ptr;
00199 char *name;
00200 int type;
00201 ID calltype;
00202 };
00203 extern ID rbdl_id_cdecl;
00204 extern ID rbdl_id_stdcall;
00205 #define CFUNC_CDECL (rbdl_id_cdecl)
00206 #define CFUNC_STDCALL (rbdl_id_stdcall)
00207
00208 struct ptr_data {
00209 void *ptr;
00210 long size;
00211 freefunc_t free;
00212 };
00213
00214 #define RDL_HANDLE(obj) ((struct dl_handle *)(DATA_PTR(obj)))
00215 #define RCFUNC_DATA(obj) ((struct cfunc_data *)(DATA_PTR(obj)))
00216 #define RPTR_DATA(obj) ((struct ptr_data *)(DATA_PTR(obj)))
00217
00218 VALUE rb_dlcfunc_new(void (*func)(), int dltype, const char * name, ID calltype);
00219 int rb_dlcfunc_kind_p(VALUE func);
00220 VALUE rb_dlptr_new(void *ptr, long size, freefunc_t func);
00221 VALUE rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func);
00222 VALUE rb_dlptr_malloc(long size, freefunc_t func);
00223
00224 #endif
00225