Module: DL

Defined in:
lib/dl/types.rb,
lib/dl/import.rb,
lib/dl/struct.rb,
dl.c

Defined Under Namespace

Modules: Importable, MemorySpace Classes: DLError, DLTypeError, Handle, PtrData, Symbol, Types

Constant Summary collapse

FuncTable =
DLFuncTable
RTLD_GLOBAL =
INT2NUM(RTLD_GLOBAL)
RTLD_LAZY =
INT2NUM(RTLD_LAZY)
RTLD_NOW =
INT2NUM(RTLD_NOW)
ALIGN_INT =
INT2NUM(ALIGN_INT)
ALIGN_LONG =
INT2NUM(ALIGN_LONG)
ALIGN_FLOAT =
INT2NUM(ALIGN_FLOAT)
ALIGN_SHORT =
INT2NUM(ALIGN_SHORT)
ALIGN_DOUBLE =
INT2NUM(ALIGN_DOUBLE)
ALIGN_VOIDP =
INT2NUM(ALIGN_VOIDP)
MAX_ARG =
INT2NUM(MAX_ARG)
DLSTACK =
rb_tainted_str_new2(DLSTACK_METHOD)
FREE =
rb_dlsym_new(dlfree, "free", "0P")

Class Method Summary collapse

Class Method Details

.callback(argv[], self) ⇒ Object



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'dl.c', line 590

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);
}

.define_callback(argv[], self) ⇒ Object



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'dl.c', line 590

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);
}

.dlopen(argv[], self) ⇒ Object



563
564
565
566
567
568
# File 'dl.c', line 563

VALUE
rb_dl_dlopen(int argc, VALUE argv[], VALUE self)
{
  rb_secure(4);
  return rb_class_new_instance(argc, argv, rb_cDLHandle);
}

.last_errorObject



323
324
325
326
327
# File 'sym.c', line 323

static VALUE
rb_dl_get_last_error(VALUE self)
{
  return rb_thread_local_aref(rb_thread_current(), rb_dl_id_DLErrno);
}

.last_error=(val) ⇒ Object



329
330
331
332
333
334
335
# File 'sym.c', line 329

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;
}

.malloc(size) ⇒ Object



570
571
572
573
574
575
# File 'dl.c', line 570

VALUE
rb_dl_malloc(VALUE self, VALUE size)
{
  rb_secure(4);
  return rb_dlptr_malloc(DLNUM2LONG(size), dlfree);
}

.remove_callback(sym) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'dl.c', line 660

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;
}

.sizeof(str) ⇒ Object



584
585
586
587
588
# File 'dl.c', line 584

static VALUE
rb_dl_sizeof(VALUE self, VALUE str)
{
  return INT2NUM(dlsizeof(StringValuePtr(str)));
}

.strdup(str) ⇒ Object



577
578
579
580
581
582
# File 'dl.c', line 577

VALUE
rb_dl_strdup(VALUE self, VALUE str)
{
  SafeStringValue(str);
  return rb_dlptr_new(strdup(RSTRING(str)->ptr), RSTRING(str)->len, dlfree);
}

.win32_last_errorObject



341
342
343
344
345
# File 'sym.c', line 341

static VALUE
rb_dl_win32_get_last_error(VALUE self)
{
  return rb_thread_local_aref(rb_thread_current(), rb_dl_id_DLW32Error);
}

.win32_last_error=(val) ⇒ Object



347
348
349
350
351
352
353
# File 'sym.c', line 347

static VALUE
rb_dl_win32_set_last_error(VALUE self, VALUE val)
{
    SetLastError(NUM2INT(val));
    rb_thread_local_aset(rb_thread_current(), rb_dl_id_DLW32Error, val);
    return Qnil;
}