2004-05-23 04:11:36 -04:00
|
|
|
/* -*- mode: C; c-file-style: "gnu" -*-
|
2002-04-02 05:56:13 -05:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ruby.h>
|
|
|
|
#include <rubyio.h>
|
2002-06-11 10:59:43 -04:00
|
|
|
#include <ctype.h>
|
2002-04-02 05:56:13 -05:00
|
|
|
#include "dl.h"
|
|
|
|
|
|
|
|
VALUE rb_mDL;
|
|
|
|
VALUE rb_eDLError;
|
|
|
|
VALUE rb_eDLTypeError;
|
|
|
|
|
|
|
|
static VALUE DLFuncTable;
|
2002-06-09 13:47:34 -04:00
|
|
|
static void *rb_dl_callback_table[CALLBACK_TYPES][MAX_CALLBACK];
|
2002-04-02 05:56:13 -05:00
|
|
|
static ID id_call;
|
|
|
|
|
2002-06-09 13:47:34 -04:00
|
|
|
static int
|
|
|
|
rb_dl_scan_callback_args(long stack[], const char *proto,
|
2002-06-11 07:16:56 -04:00
|
|
|
int *argc, VALUE argv[])
|
2002-06-09 13:47:34 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
long *sp;
|
|
|
|
VALUE val;
|
|
|
|
|
|
|
|
sp = stack;
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=1; proto[i]; i++) {
|
|
|
|
switch (proto[i]) {
|
2002-06-09 13:47:34 -04:00
|
|
|
case 'C':
|
|
|
|
{
|
|
|
|
char v;
|
2002-06-10 03:52:10 -04:00
|
|
|
v = (char)(*sp);
|
2002-06-09 13:47:34 -04:00
|
|
|
sp++;
|
|
|
|
val = INT2NUM(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
{
|
|
|
|
short v;
|
2002-06-10 03:52:10 -04:00
|
|
|
v = (short)(*sp);
|
2002-06-09 13:47:34 -04:00
|
|
|
sp++;
|
|
|
|
val = INT2NUM(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
{
|
|
|
|
int v;
|
2002-06-10 03:52:10 -04:00
|
|
|
v = (int)(*sp);
|
2002-06-09 13:47:34 -04:00
|
|
|
sp++;
|
|
|
|
val = INT2NUM(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
{
|
|
|
|
long v;
|
2002-06-10 03:52:10 -04:00
|
|
|
v = (long)(*sp);
|
2002-06-09 13:47:34 -04:00
|
|
|
sp++;
|
|
|
|
val = INT2NUM(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
{
|
|
|
|
float v;
|
|
|
|
memcpy(&v, sp, sizeof(float));
|
|
|
|
sp += sizeof(float)/sizeof(long);
|
|
|
|
val = rb_float_new(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
{
|
|
|
|
double v;
|
|
|
|
memcpy(&v, sp, sizeof(double));
|
|
|
|
sp += sizeof(double)/sizeof(long);
|
|
|
|
val = rb_float_new(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
{
|
|
|
|
void *v;
|
|
|
|
memcpy(&v, sp, sizeof(void*));
|
|
|
|
sp++;
|
|
|
|
val = rb_dlptr_new(v, 0, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
{
|
|
|
|
char *v;
|
|
|
|
memcpy(&v, sp, sizeof(void*));
|
|
|
|
sp++;
|
|
|
|
val = rb_tainted_str_new2(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2004-11-25 03:15:29 -05:00
|
|
|
rb_raise(rb_eDLTypeError, "scan_callback_args: unsupported type `%c'", proto[i]);
|
2002-06-09 13:47:34 -04:00
|
|
|
break;
|
|
|
|
}
|
2002-06-11 07:16:56 -04:00
|
|
|
argv[i-1] = val;
|
2002-06-09 13:47:34 -04:00
|
|
|
}
|
|
|
|
*argc = (i - 1);
|
|
|
|
|
|
|
|
return (*argc);
|
|
|
|
}
|
|
|
|
|
2002-04-02 05:56:13 -05:00
|
|
|
#include "callback.func"
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_dl_func_table(){
|
|
|
|
#include "cbtable.func"
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
void *
|
|
|
|
dlmalloc(size_t size)
|
|
|
|
{
|
|
|
|
DEBUG_CODE2({
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
printf("dlmalloc(%d)",size);
|
|
|
|
ptr = xmalloc(size);
|
|
|
|
printf(":0x%x\n",ptr);
|
|
|
|
return ptr;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
return xmalloc(size);
|
|
|
|
});
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
void *
|
|
|
|
dlrealloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
DEBUG_CODE({
|
|
|
|
printf("dlrealloc(0x%x,%d)\n",ptr,size);
|
|
|
|
});
|
|
|
|
return xrealloc(ptr, size);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
dlfree(void *ptr)
|
|
|
|
{
|
|
|
|
DEBUG_CODE({
|
|
|
|
printf("dlfree(0x%x)\n",ptr);
|
|
|
|
});
|
|
|
|
xfree(ptr);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
char*
|
|
|
|
dlstrdup(const char *str)
|
|
|
|
{
|
|
|
|
char *newstr;
|
|
|
|
|
|
|
|
newstr = (char*)dlmalloc(strlen(str));
|
|
|
|
strcpy(newstr,str);
|
|
|
|
|
|
|
|
return newstr;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
size_t
|
|
|
|
dlsizeof(const char *cstr)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
int i, len, n, dlen;
|
|
|
|
char *d;
|
|
|
|
|
|
|
|
len = strlen(cstr);
|
|
|
|
size = 0;
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i<len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
n = 1;
|
2004-05-23 04:11:36 -04:00
|
|
|
if (ISDIGIT(cstr[i+1])) {
|
2002-04-02 05:56:13 -05:00
|
|
|
dlen = 1;
|
2004-05-23 04:11:36 -04:00
|
|
|
while (ISDIGIT(cstr[i+dlen])) { dlen ++; };
|
2002-04-02 05:56:13 -05:00
|
|
|
dlen --;
|
|
|
|
d = ALLOCA_N(char, dlen + 1);
|
|
|
|
strncpy(d, cstr + i + 1, dlen);
|
|
|
|
d[dlen] = '\0';
|
|
|
|
n = atoi(d);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
dlen = 0;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (cstr[i]) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case 'I':
|
|
|
|
DLALIGN(0,size,INT_ALIGN);
|
|
|
|
case 'i':
|
|
|
|
size += sizeof(int) * n;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
DLALIGN(0,size,LONG_ALIGN);
|
|
|
|
case 'l':
|
|
|
|
size += sizeof(long) * n;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
DLALIGN(0,size,FLOAT_ALIGN);
|
|
|
|
case 'f':
|
|
|
|
size += sizeof(float) * n;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
DLALIGN(0,size,DOUBLE_ALIGN);
|
|
|
|
case 'd':
|
|
|
|
size += sizeof(double) * n;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
case 'c':
|
|
|
|
size += sizeof(char) * n;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
DLALIGN(0,size,SHORT_ALIGN);
|
|
|
|
case 'h':
|
|
|
|
size += sizeof(short) * n;
|
|
|
|
break;
|
|
|
|
case 'P':
|
2002-11-26 05:53:11 -05:00
|
|
|
case 'S':
|
2002-04-02 05:56:13 -05:00
|
|
|
DLALIGN(0,size,VOIDP_ALIGN);
|
|
|
|
case 'p':
|
2002-11-26 05:53:11 -05:00
|
|
|
case 's':
|
2002-04-02 05:56:13 -05:00
|
|
|
size += sizeof(void*) * n;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type '%c'", cstr[i]);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
i += dlen;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return size;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static float *
|
|
|
|
c_farray(VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
float *ary;
|
|
|
|
VALUE e;
|
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
|
|
|
*size = sizeof(float) * len;
|
|
|
|
ary = dlmalloc(*size);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
e = rb_ary_entry(v, i);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(e)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_FLOAT:
|
|
|
|
ary[i] = (float)(RFLOAT(e)->value);
|
|
|
|
break;
|
|
|
|
case T_NIL:
|
|
|
|
ary[i] = 0.0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return ary;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static double *
|
|
|
|
c_darray(VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
double *ary;
|
|
|
|
VALUE e;
|
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
|
|
|
*size = sizeof(double) * len;
|
|
|
|
ary = dlmalloc(*size);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
e = rb_ary_entry(v, i);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(e)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_FLOAT:
|
|
|
|
ary[i] = (double)(RFLOAT(e)->value);
|
|
|
|
break;
|
|
|
|
case T_NIL:
|
|
|
|
ary[i] = 0.0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return ary;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static long *
|
|
|
|
c_larray(VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
long *ary;
|
|
|
|
VALUE e;
|
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
|
|
|
*size = sizeof(long) * len;
|
|
|
|
ary = dlmalloc(*size);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
e = rb_ary_entry(v, i);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(e)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
case T_BIGNUM:
|
|
|
|
ary[i] = (long)(NUM2INT(e));
|
|
|
|
break;
|
|
|
|
case T_NIL:
|
|
|
|
ary[i] = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return ary;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static int *
|
|
|
|
c_iarray(VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
int *ary;
|
|
|
|
VALUE e;
|
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
|
|
|
*size = sizeof(int) * len;
|
|
|
|
ary = dlmalloc(*size);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
e = rb_ary_entry(v, i);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(e)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
case T_BIGNUM:
|
|
|
|
ary[i] = (int)(NUM2INT(e));
|
|
|
|
break;
|
|
|
|
case T_NIL:
|
|
|
|
ary[i] = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return ary;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static short *
|
|
|
|
c_harray(VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
short *ary;
|
|
|
|
VALUE e;
|
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
|
|
|
*size = sizeof(short) * len;
|
|
|
|
ary = dlmalloc(*size);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
e = rb_ary_entry(v, i);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(e)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
case T_BIGNUM:
|
|
|
|
ary[i] = (short)(NUM2INT(e));
|
|
|
|
break;
|
|
|
|
case T_NIL:
|
|
|
|
ary[i] = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return ary;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static char *
|
|
|
|
c_carray(VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
char *ary;
|
|
|
|
VALUE e;
|
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
|
|
|
*size = sizeof(char) * len;
|
|
|
|
ary = dlmalloc(*size);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
e = rb_ary_entry(v, i);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(e)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
case T_BIGNUM:
|
|
|
|
ary[i] = (char)(NUM2INT(e));
|
|
|
|
break;
|
|
|
|
case T_NIL:
|
|
|
|
ary[i] = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return ary;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static void *
|
|
|
|
c_parray(VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
void **ary;
|
2003-03-24 04:38:37 -05:00
|
|
|
VALUE e, tmp;
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
|
|
|
*size = sizeof(void*) * len;
|
|
|
|
ary = dlmalloc(*size);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < len; i++) {
|
2002-04-02 05:56:13 -05:00
|
|
|
e = rb_ary_entry(v, i);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(e)) {
|
|
|
|
default:
|
|
|
|
tmp = rb_check_string_type(e);
|
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
|
|
|
}
|
|
|
|
e = tmp;
|
|
|
|
/* fall through */
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_STRING:
|
2003-03-24 04:38:37 -05:00
|
|
|
rb_check_safe_str(e);
|
2002-04-02 05:56:13 -05:00
|
|
|
{
|
|
|
|
char *str, *src;
|
2002-04-04 03:27:48 -05:00
|
|
|
src = RSTRING(e)->ptr;
|
2002-04-02 05:56:13 -05:00
|
|
|
str = dlstrdup(src);
|
|
|
|
ary[i] = (void*)str;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
|
|
|
case T_NIL:
|
|
|
|
ary[i] = NULL;
|
|
|
|
break;
|
|
|
|
case T_DATA:
|
2003-03-24 04:38:37 -05:00
|
|
|
if (rb_obj_is_kind_of(e, rb_cDLPtrData)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
struct ptr_data *pdata;
|
|
|
|
Data_Get_Struct(e, struct ptr_data, pdata);
|
|
|
|
ary[i] = (void*)(pdata->ptr);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i);
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return ary;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
void *
|
|
|
|
rb_ary2cary(char t, VALUE v, long *size)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
VALUE val0;
|
|
|
|
|
2003-03-24 04:38:37 -05:00
|
|
|
val0 = rb_check_array_type(v);
|
|
|
|
if(NIL_P(TYPE(val0))) {
|
2002-04-02 05:56:13 -05:00
|
|
|
rb_raise(rb_eDLTypeError, "an array is expected.");
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2003-03-24 04:38:37 -05:00
|
|
|
v = val0;
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
len = RARRAY(v)->len;
|
2003-03-24 04:38:37 -05:00
|
|
|
if (len == 0) {
|
2002-04-02 05:56:13 -05:00
|
|
|
return NULL;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2003-03-24 04:38:37 -05:00
|
|
|
if (!size) {
|
2002-04-02 05:56:13 -05:00
|
|
|
size = ALLOCA_N(long,1);
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
val0 = rb_ary_entry(v,0);
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (TYPE(val0)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
case T_BIGNUM:
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (t) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case 'C': case 'c':
|
|
|
|
return (void*)c_carray(v,size);
|
|
|
|
case 'H': case 'h':
|
|
|
|
return (void*)c_harray(v,size);
|
|
|
|
case 'I': case 'i':
|
|
|
|
return (void*)c_iarray(v,size);
|
|
|
|
case 'L': case 'l': case 0:
|
|
|
|
return (void*)c_larray(v,size);
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "type mismatch");
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
case T_STRING:
|
|
|
|
return (void*)c_parray(v,size);
|
|
|
|
case T_FLOAT:
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (t) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case 'F': case 'f':
|
|
|
|
return (void*)c_farray(v,size);
|
|
|
|
case 'D': case 'd': case 0:
|
|
|
|
return (void*)c_darray(v,size);
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
rb_raise(rb_eDLTypeError, "type mismatch");
|
|
|
|
case T_DATA:
|
2003-03-24 04:38:37 -05:00
|
|
|
if (rb_obj_is_kind_of(val0, rb_cDLPtrData)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
return (void*)c_parray(v,size);
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
rb_raise(rb_eDLTypeError, "type mismatch");
|
2002-11-05 08:11:29 -05:00
|
|
|
case T_NIL:
|
|
|
|
return (void*)c_parray(v, size);
|
2002-04-02 05:56:13 -05:00
|
|
|
default:
|
|
|
|
rb_raise(rb_eDLTypeError, "unsupported type");
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str_to_ptr(VALUE self)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = RSTRING(self)->len;
|
|
|
|
ptr = (char*)dlmalloc(len + 1);
|
2002-04-03 06:59:01 -05:00
|
|
|
memcpy(ptr, RSTRING(self)->ptr, len);
|
2002-04-02 05:56:13 -05:00
|
|
|
ptr[len] = '\0';
|
|
|
|
return rb_dlptr_new((void*)ptr,len,dlfree);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ary_to_ptr(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
VALUE t;
|
|
|
|
long size;
|
|
|
|
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (rb_scan_args(argc, argv, "01", &t)) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case 1:
|
2002-04-03 06:59:01 -05:00
|
|
|
ptr = rb_ary2cary(StringValuePtr(t)[0], self, &size);
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
ptr = rb_ary2cary(0, self, &size);
|
|
|
|
break;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
return ptr ? rb_dlptr_new(ptr, size, dlfree) : Qnil;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_io_to_ptr(VALUE self)
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
GetOpenFile(self, fptr);
|
2004-12-08 08:26:27 -05:00
|
|
|
fp = rb_io_stdio_file(fptr);
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
return fp ? rb_dlptr_new(fp, sizeof(FILE), 0) : Qnil;
|
2002-11-07 11:42:07 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dl_dlopen(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
2003-03-24 02:56:37 -05:00
|
|
|
rb_secure(4);
|
2002-08-04 09:49:13 -04:00
|
|
|
return rb_class_new_instance(argc, argv, rb_cDLHandle);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dl_malloc(VALUE self, VALUE size)
|
|
|
|
{
|
2003-03-24 02:56:37 -05:00
|
|
|
rb_secure(4);
|
2002-05-20 11:35:48 -04:00
|
|
|
return rb_dlptr_malloc(DLNUM2LONG(size), dlfree);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dl_strdup(VALUE self, VALUE str)
|
|
|
|
{
|
2004-05-23 04:11:36 -04:00
|
|
|
void *ptr;
|
|
|
|
long len;
|
|
|
|
|
2003-03-24 04:38:37 -05:00
|
|
|
SafeStringValue(str);
|
2004-05-23 04:11:36 -04:00
|
|
|
len = RSTRING(str)->len;
|
|
|
|
ptr = memcpy(dlmalloc(len + 1), RSTRING(str)->ptr, len + 1);
|
|
|
|
((char *)ptr)[len] = '\0';
|
|
|
|
return rb_dlptr_new(ptr, len, dlfree);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_dl_sizeof(VALUE self, VALUE str)
|
|
|
|
{
|
2002-04-03 06:59:01 -05:00
|
|
|
return INT2NUM(dlsizeof(StringValuePtr(str)));
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
static VALUE
|
2002-06-09 13:47:34 -04:00
|
|
|
rb_dl_callback(int argc, VALUE argv[], VALUE self)
|
2002-04-02 05:56:13 -05:00
|
|
|
{
|
2002-06-09 13:47:34 -04:00
|
|
|
VALUE type, proc;
|
|
|
|
int rettype, entry, i;
|
|
|
|
char fname[127];
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2003-03-24 02:56:37 -05:00
|
|
|
rb_secure(4);
|
2002-06-09 13:47:34 -04:00
|
|
|
proc = Qnil;
|
2003-03-24 04:38:37 -05:00
|
|
|
switch (rb_scan_args(argc, argv, "11", &type, &proc)) {
|
2002-06-09 13:47:34 -04:00
|
|
|
case 1:
|
2003-03-24 04:38:37 -05:00
|
|
|
if (rb_block_given_p()) {
|
2003-06-16 03:25:38 -04:00
|
|
|
proc = rb_block_proc();
|
2002-06-09 13:47:34 -04:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
proc = Qnil;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2003-03-24 04:38:37 -05:00
|
|
|
StringValue(type);
|
|
|
|
switch (RSTRING(type)->ptr[0]) {
|
2002-04-02 05:56:13 -05:00
|
|
|
case '0':
|
2002-06-09 13:47:34 -04:00
|
|
|
rettype = 0x00;
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
2002-06-09 13:47:34 -04:00
|
|
|
case 'C':
|
|
|
|
rettype = 0x01;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
rettype = 0x02;
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
|
|
|
case 'I':
|
2002-06-09 13:47:34 -04:00
|
|
|
rettype = 0x03;
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
|
|
|
case 'L':
|
2002-06-09 13:47:34 -04:00
|
|
|
rettype = 0x04;
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
|
|
|
case 'F':
|
2002-06-09 13:47:34 -04:00
|
|
|
rettype = 0x05;
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
|
|
|
case 'D':
|
2002-06-09 13:47:34 -04:00
|
|
|
rettype = 0x06;
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
2002-06-09 13:47:34 -04:00
|
|
|
case 'P':
|
|
|
|
rettype = 0x07;
|
2002-04-02 05:56:13 -05:00
|
|
|
break;
|
|
|
|
default:
|
2002-06-11 10:44:53 -04:00
|
|
|
rb_raise(rb_eDLTypeError, "unsupported type `%c'", RSTRING(type)->ptr[0]);
|
2002-06-09 13:47:34 -04:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2002-06-09 13:47:34 -04:00
|
|
|
entry = -1;
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < MAX_CALLBACK; i++) {
|
|
|
|
if (rb_hash_aref(DLFuncTable, rb_assoc_new(INT2NUM(rettype), INT2NUM(i))) == Qnil) {
|
2002-06-09 13:47:34 -04:00
|
|
|
entry = i;
|
|
|
|
break;
|
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
}
|
2003-03-24 04:38:37 -05:00
|
|
|
if (entry < 0) {
|
2002-06-09 13:47:34 -04:00
|
|
|
rb_raise(rb_eDLError, "too many callbacks are defined.");
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_hash_aset(DLFuncTable,
|
|
|
|
rb_assoc_new(INT2NUM(rettype),INT2NUM(entry)),
|
|
|
|
rb_assoc_new(type,proc));
|
|
|
|
sprintf(fname, "rb_dl_callback_func_%d_%d", rettype, entry);
|
2002-06-11 10:44:53 -04:00
|
|
|
return rb_dlsym_new((void (*)())rb_dl_callback_table[rettype][entry],
|
|
|
|
fname, RSTRING(type)->ptr);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2002-06-09 13:47:34 -04:00
|
|
|
static VALUE
|
|
|
|
rb_dl_remove_callback(VALUE mod, VALUE sym)
|
2002-04-02 05:56:13 -05:00
|
|
|
{
|
2003-03-24 02:56:37 -05:00
|
|
|
freefunc_t f;
|
2002-06-09 13:47:34 -04:00
|
|
|
int i, j;
|
|
|
|
|
2003-03-24 02:56:37 -05:00
|
|
|
rb_secure(4);
|
|
|
|
f = rb_dlsym2csym(sym);
|
2003-03-24 04:38:37 -05:00
|
|
|
for (i=0; i < CALLBACK_TYPES; i++) {
|
|
|
|
for (j=0; j < MAX_CALLBACK; j++) {
|
|
|
|
if (rb_dl_callback_table[i][j] == f) {
|
2002-06-09 13:47:34 -04:00
|
|
|
rb_hash_aset(DLFuncTable, rb_assoc_new(INT2NUM(i),INT2NUM(j)),Qnil);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Qnil;
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
Init_dl()
|
|
|
|
{
|
|
|
|
void Init_dlptr();
|
|
|
|
void Init_dlsym();
|
|
|
|
void Init_dlhandle();
|
|
|
|
|
|
|
|
id_call = rb_intern("call");
|
|
|
|
|
|
|
|
rb_mDL = rb_define_module("DL");
|
|
|
|
|
|
|
|
rb_eDLError = rb_define_class_under(rb_mDL, "DLError", rb_eStandardError);
|
|
|
|
rb_eDLTypeError = rb_define_class_under(rb_mDL, "DLTypeError", rb_eDLError);
|
|
|
|
|
|
|
|
DLFuncTable = rb_hash_new();
|
|
|
|
init_dl_func_table();
|
|
|
|
rb_define_const(rb_mDL, "FuncTable", DLFuncTable);
|
|
|
|
|
|
|
|
rb_define_const(rb_mDL, "RTLD_GLOBAL", INT2NUM(RTLD_GLOBAL));
|
|
|
|
rb_define_const(rb_mDL, "RTLD_LAZY", INT2NUM(RTLD_LAZY));
|
|
|
|
rb_define_const(rb_mDL, "RTLD_NOW", INT2NUM(RTLD_NOW));
|
|
|
|
|
|
|
|
rb_define_const(rb_mDL, "ALIGN_INT", INT2NUM(ALIGN_INT));
|
|
|
|
rb_define_const(rb_mDL, "ALIGN_LONG", INT2NUM(ALIGN_LONG));
|
|
|
|
rb_define_const(rb_mDL, "ALIGN_FLOAT", INT2NUM(ALIGN_FLOAT));
|
|
|
|
rb_define_const(rb_mDL, "ALIGN_SHORT", INT2NUM(ALIGN_SHORT));
|
|
|
|
rb_define_const(rb_mDL, "ALIGN_DOUBLE",INT2NUM(ALIGN_DOUBLE));
|
|
|
|
rb_define_const(rb_mDL, "ALIGN_VOIDP", INT2NUM(ALIGN_VOIDP));
|
|
|
|
|
|
|
|
rb_define_const(rb_mDL, "MAX_ARG", INT2NUM(MAX_ARG));
|
2002-05-15 02:23:05 -04:00
|
|
|
rb_define_const(rb_mDL, "DLSTACK", rb_tainted_str_new2(DLSTACK_METHOD));
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
rb_define_module_function(rb_mDL, "dlopen", rb_dl_dlopen, -1);
|
2002-06-09 13:47:34 -04:00
|
|
|
rb_define_module_function(rb_mDL, "callback", rb_dl_callback, -1);
|
|
|
|
rb_define_module_function(rb_mDL, "define_callback", rb_dl_callback, -1);
|
|
|
|
rb_define_module_function(rb_mDL, "remove_callback", rb_dl_remove_callback, 1);
|
2002-04-02 05:56:13 -05:00
|
|
|
rb_define_module_function(rb_mDL, "malloc", rb_dl_malloc, 1);
|
|
|
|
rb_define_module_function(rb_mDL, "strdup", rb_dl_strdup, 1);
|
|
|
|
rb_define_module_function(rb_mDL, "sizeof", rb_dl_sizeof, 1);
|
|
|
|
|
|
|
|
Init_dlptr();
|
|
|
|
Init_dlsym();
|
|
|
|
Init_dlhandle();
|
|
|
|
|
|
|
|
rb_define_const(rb_mDL, "FREE", rb_dlsym_new(dlfree, "free", "0P"));
|
|
|
|
|
|
|
|
rb_define_method(rb_cString, "to_ptr", rb_str_to_ptr, 0);
|
|
|
|
rb_define_method(rb_cArray, "to_ptr", rb_ary_to_ptr, -1);
|
|
|
|
rb_define_method(rb_cIO, "to_ptr", rb_io_to_ptr, 0);
|
2002-04-03 06:59:01 -05:00
|
|
|
}
|