Ruby
2.0.0p247(2013-06-27revision41674)
|
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 # /* some stranger systems may not define all of these */ 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(_WIN32) 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 * Add ",..." as the last argument. 00074 * This is required for variable argument functions such 00075 * as fprintf() on x86_64-linux. 00076 * 00077 * http://refspecs.linuxfoundation.org/elf/x86_64-abi-0.95.pdf 00078 * page 19: 00079 * 00080 * For calls that may call functions that use varargs or stdargs 00081 * (prototype-less calls or calls to functions containing ellipsis 00082 * (...) in the declaration) %al is used as hidden argument to 00083 * specify the number of SSE registers used. 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 #define ALIGN_OF(type) offsetof(struct {char align_c; type align_x;}, align_x) 00138 00139 #define ALIGN_VOIDP ALIGN_OF(void*) 00140 #define ALIGN_SHORT ALIGN_OF(short) 00141 #define ALIGN_CHAR ALIGN_OF(char) 00142 #define ALIGN_INT ALIGN_OF(int) 00143 #define ALIGN_LONG ALIGN_OF(long) 00144 #if HAVE_LONG_LONG 00145 #define ALIGN_LONG_LONG ALIGN_OF(LONG_LONG) 00146 #endif 00147 #define ALIGN_FLOAT ALIGN_OF(float) 00148 #define ALIGN_DOUBLE ALIGN_OF(double) 00149 00150 #define DLALIGN(ptr,offset,align) \ 00151 ((offset) += ((align) - ((uintptr_t)((char *)(ptr) + (offset))) % (align)) % (align)) 00152 00153 00154 #define DLTYPE_VOID 0 00155 #define DLTYPE_VOIDP 1 00156 #define DLTYPE_CHAR 2 00157 #define DLTYPE_SHORT 3 00158 #define DLTYPE_INT 4 00159 #define DLTYPE_LONG 5 00160 #if HAVE_LONG_LONG 00161 #define DLTYPE_LONG_LONG 6 00162 #endif 00163 #define DLTYPE_FLOAT 7 00164 #define DLTYPE_DOUBLE 8 00165 #define MAX_DLTYPE 9 00166 00167 #if SIZEOF_VOIDP == SIZEOF_LONG 00168 # define PTR2NUM(x) (ULONG2NUM((unsigned long)(x))) 00169 # define NUM2PTR(x) ((void*)(NUM2ULONG(x))) 00170 #else 00171 /* # error --->> Ruby/DL2 requires sizeof(void*) == sizeof(long) to be compiled. <<--- */ 00172 # define PTR2NUM(x) (ULL2NUM((unsigned long long)(x))) 00173 # define NUM2PTR(x) ((void*)(NUM2ULL(x))) 00174 #endif 00175 00176 #define BOOL2INT(x) (((x) == Qtrue)?1:0) 00177 #define INT2BOOL(x) ((x)?Qtrue:Qfalse) 00178 00179 typedef void (*freefunc_t)(void*); 00180 00181 struct dl_handle { 00182 void *ptr; 00183 int open; 00184 int enable_close; 00185 }; 00186 00187 00188 struct cfunc_data { 00189 void *ptr; 00190 char *name; 00191 int type; 00192 ID calltype; 00193 VALUE wrap; 00194 }; 00195 extern ID rbdl_id_cdecl; 00196 extern ID rbdl_id_stdcall; 00197 #define CFUNC_CDECL (rbdl_id_cdecl) 00198 #define CFUNC_STDCALL (rbdl_id_stdcall) 00199 00200 struct ptr_data { 00201 void *ptr; 00202 long size; 00203 freefunc_t free; 00204 VALUE wrap[2]; 00205 }; 00206 00207 #define RDL_HANDLE(obj) ((struct dl_handle *)(DATA_PTR(obj))) 00208 #define RCFUNC_DATA(obj) ((struct cfunc_data *)(DATA_PTR(obj))) 00209 #define RPTR_DATA(obj) ((struct ptr_data *)(DATA_PTR(obj))) 00210 00211 VALUE rb_dlcfunc_new(void (*func)(), int dltype, const char * name, ID calltype); 00212 int rb_dlcfunc_kind_p(VALUE func); 00213 VALUE rb_dlptr_new(void *ptr, long size, freefunc_t func); 00214 VALUE rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func); 00215 VALUE rb_dlptr_malloc(long size, freefunc_t func); 00216 00217 #endif 00218