/tools/Ruby/lib/ruby/1.8/i386-mingw32/dl.h

http://github.com/agross/netopenspace · C Header · 313 lines · 266 code · 38 blank · 9 comment · 17 complexity · 6410dc24c177e15b0df2f3cb357e9dc6 MD5 · raw file

  1. /* -*- C -*-
  2. * $Id: dl.h 11708 2007-02-12 23:01:19Z shyouhei $
  3. */
  4. #ifndef RUBY_DL_H
  5. #define RUBY_DL_H
  6. #include <ruby.h>
  7. #include <dlconfig.h>
  8. #if defined(HAVE_DLFCN_H)
  9. # include <dlfcn.h>
  10. # /* some stranger systems may not define all of these */
  11. #ifndef RTLD_LAZY
  12. #define RTLD_LAZY 0
  13. #endif
  14. #ifndef RTLD_GLOBAL
  15. #define RTLD_GLOBAL 0
  16. #endif
  17. #ifndef RTLD_NOW
  18. #define RTLD_NOW 0
  19. #endif
  20. #else
  21. # if defined(HAVE_WINDOWS_H)
  22. # include <windows.h>
  23. # define dlclose(ptr) FreeLibrary((HINSTANCE)ptr)
  24. # define dlopen(name,flag) ((void*)LoadLibrary(name))
  25. # define dlerror() "unknown error"
  26. # define dlsym(handle,name) ((void*)GetProcAddress(handle,name))
  27. # define RTLD_LAZY -1
  28. # define RTLD_NOW -1
  29. # define RTLD_GLOBAL -1
  30. # endif
  31. #endif
  32. #if !defined(StringValue)
  33. # define StringValue(v) if(TYPE(v) != T_STRING) v = rb_str_to_str(v)
  34. #endif
  35. #if !defined(StringValuePtr)
  36. # define StringValuePtr(v) RSTRING((TYPE(v) == T_STRING) ? (v) : rb_str_to_str(v))->ptr
  37. #endif
  38. #ifdef DEBUG
  39. #define DEBUG_CODE(b) {printf("DEBUG:%d\n",__LINE__);b;}
  40. #define DEBUG_CODE2(b1,b2) {printf("DEBUG:%d\n",__LINE__);b1;}
  41. #else
  42. #define DEBUG_CODE(b)
  43. #define DEBUG_CODE2(b1,b2) b2
  44. #endif
  45. #define VOID_DLTYPE 0x00
  46. #define CHAR_DLTYPE 0x01
  47. #define SHORT_DLTYPE 0x02
  48. #define INT_DLTYPE 0x03
  49. #define LONG_DLTYPE 0x04
  50. #define FLOAT_DLTYPE 0x05
  51. #define DOUBLE_DLTYPE 0x06
  52. #define VOIDP_DLTYPE 0x07
  53. #define ARG_TYPE(x,i) (((x) & (0x07 << ((i)*3))) >> ((i)*3))
  54. #define PUSH_ARG(x,t) do{x <<= 3; x |= t;}while(0)
  55. #define PUSH_0(x) PUSH_ARG(x,VOID_DLTYPE)
  56. #if SIZEOF_INT == SIZEOF_LONG
  57. # define PUSH_I(x) PUSH_ARG(x,LONG_DLTYPE)
  58. # define ANY2I(x) x.l
  59. # define DLINT(x) (long)x
  60. #else
  61. # define PUSH_I(x) PUSH_ARG(x,INT_DLTYPE)
  62. # define ANY2I(x) x.i
  63. # define DLINT(x) (int)x
  64. #endif
  65. #define PUSH_L(x) PUSH_ARG(x,LONG_DLTYPE)
  66. #define ANY2L(x) x.l
  67. #define DLLONG(x) (long)x
  68. #if defined(WITH_TYPE_FLOAT)
  69. # if SIZEOF_FLOAT == SIZEOF_DOUBLE
  70. # define PUSH_F(x) PUSH_ARG(x,DOUBLE_DLTYPE)
  71. # define ANY2F(x) (x.d)
  72. # define DLFLOAT(x) ((double)x)
  73. # else
  74. # define PUSH_F(x) PUSH_ARG(x,FLOAT_DLTYPE)
  75. # define ANY2F(x) (x.f)
  76. # define DLFLOAT(x) ((float)x)
  77. # endif
  78. #else
  79. # define PUSH_F(x) PUSH_ARG(x,DOUBLE_DLTYPE)
  80. # define ANY2F(x) (x.d)
  81. # define DLFLOAT(x) ((double)x)
  82. #endif
  83. #define PUSH_D(x) PUSH_ARG(x,DOUBLE_DLTYPE)
  84. #define ANY2D(x) (x.d)
  85. #define DLDOUBLE(x) ((double)x)
  86. #if SIZEOF_INT == SIZEOF_VOIDP && SIZEOF_INT != SIZEOF_LONG
  87. # define PUSH_P(x) PUSH_ARG(x,INT_DLTYPE)
  88. # define ANY2P(x) (x.i)
  89. # define DLVOIDP(x) ((int)x)
  90. #elif SIZEOF_LONG == SIZEOF_VOIDP
  91. # define PUSH_P(x) PUSH_ARG(x,LONG_DLTYPE)
  92. # define ANY2P(x) (x.l)
  93. # define DLVOIDP(x) ((long)x)
  94. #else
  95. # define PUSH_P(x) PUSH_ARG(x,VOIDP_DLTYPE)
  96. # define ANY2P(x) (x.p)
  97. # define DLVOIDP(x) ((void*)p)
  98. #endif
  99. #if defined(WITH_TYPE_CHAR)
  100. # define PUSH_C(x) PUSH_ARG(x,CHAR_DLTYPE)
  101. # define ANY2C(x) (x.c)
  102. # define DLCHAR(x) ((char)x)
  103. #else
  104. # define PUSH_C(x) PUSH_I(x)
  105. # define ANY2C(x) ANY2I(x)
  106. # define DLCHAR(x) DLINT(x)
  107. #endif
  108. #if defined(WITH_TYPE_SHORT)
  109. # define PUSH_H(x) PUSH_ARG(x,SHORT_DLTYPE)
  110. # define ANY2H(x) (x.h)
  111. # define DLSHORT(x) ((short)x)
  112. #else
  113. # define PUSH_H(x) PUSH_I(x)
  114. # define ANY2H(x) ANY2I(x)
  115. # define DLSHORT(x) DLINT(x)
  116. #endif
  117. #define PUSH_S(x) PUSH_P(x)
  118. #define ANY2S(x) ANY2P(x)
  119. #define DLSTR(x) DLVOIDP(x)
  120. #define CBPUSH_0(x) PUSH_0(x)
  121. #define CBPUSH_C(x) PUSH_C(x)
  122. #define CBPUSH_H(x) PUSH_H(x)
  123. #define CBPUSH_I(x) PUSH_I(x)
  124. #define CBPUSH_L(x) PUSH_L(x)
  125. #define CBPUSH_F(x) PUSH_F(x)
  126. #define CBPUSH_D(x) PUSH_D(x)
  127. #if defined(WITH_CBTYPE_VOIDP)
  128. # define CBPUSH_P(x) PUSH_ARG(x,VOIDP_DLTYPE)
  129. #else
  130. # define CBPUSH_P(x) PUSH_P(x)
  131. #endif
  132. #if defined(USE_INLINE_ASM)
  133. # if defined(__i386__) && defined(__GNUC__)
  134. # define DLSTACK
  135. # define DLSTACK_METHOD "asm"
  136. # define DLSTACK_REVERSE
  137. # define DLSTACK_PROTO
  138. # define DLSTACK_ARGS
  139. # define DLSTACK_START(sym)
  140. # define DLSTACK_END(sym)
  141. # define DLSTACK_PUSH_C(x) asm volatile ("pushl %0" :: "g" (x));
  142. # define DLSTACK_PUSH_H(x) asm volatile ("pushl %0" :: "g" (x));
  143. # define DLSTACK_PUSH_I(x) asm volatile ("pushl %0" :: "g" (x));
  144. # define DLSTACK_PUSH_L(x) asm volatile ("pushl %0" :: "g" (x));
  145. # define DLSTACK_PUSH_P(x) asm volatile ("pushl %0" :: "g" (x));
  146. # define DLSTACK_PUSH_F(x) asm volatile ("flds %0"::"g"(x));\
  147. asm volatile ("subl $4,%esp");\
  148. asm volatile ("fstps (%esp)");
  149. # define DLSTACK_PUSH_D(x) asm volatile ("fldl %0"::"g"(x));\
  150. asm volatile ("subl $8,%esp");\
  151. asm volatile ("fstpl (%esp)")
  152. # else
  153. # error --with-asm is not supported on this machine
  154. # endif
  155. #elif defined(USE_DLSTACK)
  156. # define DLSTACK
  157. # define DLSTACK_GUARD
  158. # define DLSTACK_METHOD "dl"
  159. # define DLSTACK_PROTO long,long,long,long,long,\
  160. long,long,long,long,long,\
  161. long,long,long,long,long
  162. # define DLSTACK_ARGS stack[0],stack[1],stack[2],stack[3],stack[4],\
  163. stack[5],stack[6],stack[7],stack[8],stack[9],\
  164. stack[10],stack[11],stack[12],stack[13],stack[14]
  165. # define DLSTACK_SIZE (sizeof(long)*15)
  166. # define DLSTACK_START(sym)
  167. # define DLSTACK_END(sym)
  168. # define DLSTACK_PUSH_C(x) {long v=(long)x; memcpy(sp,&v,sizeof(long)); sp++;}
  169. # define DLSTACK_PUSH_H(x) {long v=(long)x; memcpy(sp,&v,sizeof(long)); sp++;}
  170. # define DLSTACK_PUSH_I(x) {long v=(long)x; memcpy(sp,&v,sizeof(long)); sp++;}
  171. # define DLSTACK_PUSH_L(x) memcpy(sp,&x,sizeof(long)); sp++;
  172. # define DLSTACK_PUSH_P(x) memcpy(sp,&x,sizeof(void*)); sp++;
  173. # define DLSTACK_PUSH_F(x) memcpy(sp,&x,sizeof(float)); sp+=sizeof(float)/sizeof(long);
  174. # define DLSTACK_PUSH_D(x) memcpy(sp,&x,sizeof(double)); sp+=sizeof(double)/sizeof(long);
  175. #else
  176. # define DLSTACK_METHOD "none"
  177. #endif
  178. extern VALUE rb_mDL;
  179. extern VALUE rb_mDLMemorySpace;
  180. extern VALUE rb_cDLHandle;
  181. extern VALUE rb_cDLSymbol;
  182. extern VALUE rb_cDLPtrData;
  183. extern VALUE rb_cDLStructData;
  184. extern VALUE rb_eDLError;
  185. extern VALUE rb_eDLTypeError;
  186. #if defined(LONG2NUM) && (SIZEOF_LONG == SIZEOF_VOIDP)
  187. # define DLLONG2NUM(x) LONG2NUM((long)x)
  188. # define DLNUM2LONG(x) (long)(NUM2LONG(x))
  189. #else
  190. # define DLLONG2NUM(x) INT2NUM((long)x)
  191. # define DLNUM2LONG(x) (long)(NUM2INT(x))
  192. #endif
  193. typedef struct { char c; void *x; } s_voidp;
  194. typedef struct { char c; short x; } s_short;
  195. typedef struct { char c; int x; } s_int;
  196. typedef struct { char c; long x; } s_long;
  197. typedef struct { char c; float x; } s_float;
  198. typedef struct { char c; double x; } s_double;
  199. #define ALIGN_VOIDP (sizeof(s_voidp) - sizeof(void *))
  200. #define ALIGN_SHORT (sizeof(s_short) - sizeof(short))
  201. #define ALIGN_INT (sizeof(s_int) - sizeof(int))
  202. #define ALIGN_LONG (sizeof(s_long) - sizeof(long))
  203. #define ALIGN_FLOAT (sizeof(s_float) - sizeof(float))
  204. #define ALIGN_DOUBLE (sizeof(s_double) - sizeof(double))
  205. /* for compatibility */
  206. #define VOIDP_ALIGN ALIGN_VOIDP
  207. #define SHORT_ALIGN ALIGN_SHORT
  208. #define INT_ALIGN ALIGN_INT
  209. #define LONG_ALIGN ALIGN_LONG
  210. #define FLOAT_ALIGN ALIGN_FLOAT
  211. #define DOUBLE_ALIGN ALIGN_DOUBLE
  212. #define DLALIGN(ptr,offset,align) {\
  213. while( (((unsigned long)((char *)ptr + offset)) % align) != 0 ) offset++;\
  214. }
  215. typedef void (*freefunc_t)(void *);
  216. #define DLFREEFUNC(func) ((freefunc_t)(func))
  217. typedef union {
  218. void* p;
  219. char c;
  220. short h;
  221. int i;
  222. long l;
  223. float f;
  224. double d;
  225. char *s;
  226. } ANY_TYPE;
  227. struct dl_handle {
  228. void *ptr;
  229. int open;
  230. int enable_close;
  231. };
  232. struct sym_data {
  233. void *func;
  234. char *name;
  235. char *type;
  236. int len;
  237. };
  238. enum DLPTR_CTYPE {
  239. DLPTR_CTYPE_UNKNOWN,
  240. DLPTR_CTYPE_STRUCT,
  241. DLPTR_CTYPE_UNION
  242. };
  243. struct ptr_data {
  244. void *ptr; /* a pointer to the data */
  245. freefunc_t free; /* free() */
  246. char *stype; /* array of type specifiers */
  247. int *ssize; /* size[i] = sizeof(type[i]) > 0 */
  248. int slen; /* the number of type specifiers */
  249. ID *ids;
  250. int ids_num;
  251. int ctype; /* DLPTR_CTYPE_UNKNOWN, DLPTR_CTYPE_STRUCT, DLPTR_CTYPE_UNION */
  252. long size;
  253. };
  254. #define RDLPTR(obj) ((struct ptr_data *)(DATA_PTR(obj)))
  255. #define RDLSYM(obj) ((struct sym_data *)(DATA_PTR(obj)))
  256. void dlfree(void*);
  257. void *dlmalloc(size_t);
  258. void *dlrealloc(void*,size_t);
  259. char *dlstrdup(const char *);
  260. size_t dlsizeof(const char *);
  261. void *rb_ary2cary(char t, VALUE ary, long *size);
  262. /*
  263. void rb_dlmem_delete(void *ptr);
  264. void rb_dlmem_aset(void *ptr, VALUE obj);
  265. VALUE rb_dlmem_aref(void *ptr);
  266. */
  267. void dlptr_free(struct ptr_data *data);
  268. void dlptr_init(VALUE val);
  269. VALUE rb_dlptr_new(void *ptr, long size, freefunc_t func);
  270. VALUE rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func);
  271. VALUE rb_dlptr_malloc(long size, freefunc_t func);
  272. void *rb_dlptr2cptr(VALUE val);
  273. VALUE rb_dlsym_new(void (*func)(), const char *name, const char *type);
  274. freefunc_t rb_dlsym2csym(VALUE val);
  275. #endif /* RUBY_DL_H */