static VALUE
rb_dl_callback(int argc, VALUE argv[], VALUE self)
{
VALUE type, proc;
int rettype, entry, i;
char fname[127];
rb_secure(4);
proc = Qnil;
switch (rb_scan_args(argc, argv, "11", &type, &proc)) {
case 1:
if (rb_block_given_p()) {
proc = rb_block_proc();
}
else{
proc = Qnil;
}
default:
break;
}
StringValue(type);
switch (RSTRING(type)->ptr[0]) {
case '0':
rettype = 0x00;
break;
case 'C':
rettype = 0x01;
break;
case 'H':
rettype = 0x02;
break;
case 'I':
rettype = 0x03;
break;
case 'L':
rettype = 0x04;
break;
case 'F':
rettype = 0x05;
break;
case 'D':
rettype = 0x06;
break;
case 'P':
rettype = 0x07;
break;
default:
rb_raise(rb_eDLTypeError, "unsupported type `%c'", RSTRING(type)->ptr[0]);
}
entry = -1;
for (i=0; i < MAX_CALLBACK; i++) {
if (rb_hash_aref(DLFuncTable, rb_assoc_new(INT2NUM(rettype), INT2NUM(i))) == Qnil) {
entry = i;
break;
}
}
if (entry < 0) {
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);
return rb_dlsym_new((void (*)())rb_dl_callback_table[rettype][entry],
fname, RSTRING(type)->ptr);
}
static VALUE
rb_dl_callback(int argc, VALUE argv[], VALUE self)
{
VALUE type, proc;
int rettype, entry, i;
char fname[127];
rb_secure(4);
proc = Qnil;
switch (rb_scan_args(argc, argv, "11", &type, &proc)) {
case 1:
if (rb_block_given_p()) {
proc = rb_block_proc();
}
else{
proc = Qnil;
}
default:
break;
}
StringValue(type);
switch (RSTRING(type)->ptr[0]) {
case '0':
rettype = 0x00;
break;
case 'C':
rettype = 0x01;
break;
case 'H':
rettype = 0x02;
break;
case 'I':
rettype = 0x03;
break;
case 'L':
rettype = 0x04;
break;
case 'F':
rettype = 0x05;
break;
case 'D':
rettype = 0x06;
break;
case 'P':
rettype = 0x07;
break;
default:
rb_raise(rb_eDLTypeError, "unsupported type `%c'", RSTRING(type)->ptr[0]);
}
entry = -1;
for (i=0; i < MAX_CALLBACK; i++) {
if (rb_hash_aref(DLFuncTable, rb_assoc_new(INT2NUM(rettype), INT2NUM(i))) == Qnil) {
entry = i;
break;
}
}
if (entry < 0) {
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);
return rb_dlsym_new((void (*)())rb_dl_callback_table[rettype][entry],
fname, RSTRING(type)->ptr);
}
VALUE
rb_dl_dlopen(int argc, VALUE argv[], VALUE self)
{
rb_secure(2);
return rb_class_new_instance(argc, argv, rb_cDLHandle);
}
static VALUE
rb_dl_get_last_error(VALUE self)
{
return rb_thread_local_aref(rb_thread_current(), rb_dl_id_DLErrno);
}
static VALUE
rb_dl_set_last_error(VALUE self, VALUE val)
{
errno = NUM2INT(val);
rb_thread_local_aset(rb_thread_current(), rb_dl_id_DLErrno, val);
return Qnil;
}
VALUE
rb_dl_malloc(VALUE self, VALUE size)
{
rb_secure(4);
return rb_dlptr_malloc(DLNUM2LONG(size), dlfree);
}
static VALUE
rb_dl_remove_callback(VALUE mod, VALUE sym)
{
freefunc_t f;
int i, j;
rb_secure(4);
f = rb_dlsym2csym(sym);
for (i=0; i < CALLBACK_TYPES; i++) {
for (j=0; j < MAX_CALLBACK; j++) {
if (rb_dl_callback_table[i][j] == f) {
rb_hash_aset(DLFuncTable, rb_assoc_new(INT2NUM(i),INT2NUM(j)),Qnil);
break;
}
}
}
return Qnil;
}
static VALUE
rb_dl_sizeof(VALUE self, VALUE str)
{
return INT2NUM(dlsizeof(StringValuePtr(str)));
}
VALUE
rb_dl_strdup(VALUE self, VALUE str)
{
SafeStringValue(str);
return rb_dlptr_new(strdup(RSTRING(str)->ptr), RSTRING(str)->len, dlfree);
}
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please see Improve the docs, or visit Documenting-ruby.org.