Class: DL::PtrData

Inherits:
Object
  • Object
show all
Defined in:
ext/rubysl/dl/ptr.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv[], self) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/rubysl/dl/ptr.c', line 178

static VALUE
rb_dlptr_initialize(int argc, VALUE argv[], VALUE self)
{
  VALUE ptr, sym, size;
  struct ptr_data *data;
  void *p = NULL;
  freefunc_t f = NULL;
  long s = 0;

  switch (rb_scan_args(argc, argv, "12", &ptr, &size, &sym)) {
  case 1:
    p = (void*)(DLNUM2LONG(rb_Integer(ptr)));
    break;
  case 2:
    p = (void*)(DLNUM2LONG(rb_Integer(ptr)));
    s = DLNUM2LONG(size);
    break;
  case 3:
    p = (void*)(DLNUM2LONG(rb_Integer(ptr)));
    s = DLNUM2LONG(size);
    f = rb_dlsym2csym(sym);
    break;
  default:
    rb_bug("rb_dlptr_initialize");
  }

  if (p) {
    Data_Get_Struct(self, struct ptr_data, data);
    if (data->ptr && data->free) {
      /* Free previous memory. Use of inappropriate initialize may cause SEGV. */
      (*(data->free))(data->ptr);
    }
    data->ptr  = p;
    data->size = s;
    data->free = f;
  }

  return Qnil;
}

Class Method Details

.malloc(argv[], klass) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'ext/rubysl/dl/ptr.c', line 218

static VALUE
rb_dlptr_s_malloc(int argc, VALUE argv[], VALUE klass)
{
  VALUE size, sym, obj;
  int   s = 0;
  freefunc_t f = NULL;

  switch (rb_scan_args(argc, argv, "11", &size, &sym)) {
  case 1:
    s = NUM2INT(size);
    break;
  case 2:
    s = NUM2INT(size);
    f = rb_dlsym2csym(sym);
    break;
  default:
    rb_bug("rb_dlptr_s_malloc");
  }

  obj = rb_dlptr_malloc(s,f);

  return obj;
}

Instance Method Details

#+(other) ⇒ Object



490
491
492
493
494
495
496
497
498
499
500
# File 'ext/rubysl/dl/ptr.c', line 490

VALUE
rb_dlptr_plus(VALUE self, VALUE other)
{
  void *ptr;
  long num, size;

  ptr = rb_dlptr2cptr(self);
  size = RDLPTR(self)->size;
  num = DLNUM2LONG(other);
  return rb_dlptr_new((char *)ptr + num, size - num, 0);
}

#+@Object



251
252
253
254
255
256
257
258
# File 'ext/rubysl/dl/ptr.c', line 251

VALUE
rb_dlptr_ptr(VALUE self)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);
  return rb_dlptr_new(*((void**)(data->ptr)),0,0);
}

#-(other) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
# File 'ext/rubysl/dl/ptr.c', line 502

VALUE
rb_dlptr_minus(VALUE self, VALUE other)
{
  void *ptr;
  long num, size;

  ptr = rb_dlptr2cptr(self);
  size = RDLPTR(self)->size;
  num = DLNUM2LONG(other);
  return rb_dlptr_new((char *)ptr - num, size + num, 0);
}

#-@Object



260
261
262
263
264
265
266
267
# File 'ext/rubysl/dl/ptr.c', line 260

VALUE
rb_dlptr_ref(VALUE self)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);
  return rb_dlptr_new(&(data->ptr),0,0);
}

#<=>(other) ⇒ Object



481
482
483
484
485
486
487
488
# File 'ext/rubysl/dl/ptr.c', line 481

VALUE
rb_dlptr_cmp(VALUE self, VALUE other)
{
  void *ptr1, *ptr2;
  ptr1 = rb_dlptr2cptr(self);
  ptr2 = rb_dlptr2cptr(other);
  return DLLONG2NUM((long)ptr1 - (long)ptr2);
}

#==(other) ⇒ Object



471
472
473
474
475
476
477
478
479
# File 'ext/rubysl/dl/ptr.c', line 471

VALUE
rb_dlptr_eql(VALUE self, VALUE other)
{
  void *ptr1, *ptr2;
  ptr1 = rb_dlptr2cptr(self);
  ptr2 = rb_dlptr2cptr(other);

  return ptr1 == ptr2 ? Qtrue : Qfalse;
}

#[](argv[], self) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# File 'ext/rubysl/dl/ptr.c', line 724

VALUE
rb_dlptr_aref(int argc, VALUE argv[], VALUE self)
{
  VALUE key = Qnil, num = Qnil;
  ID id;
  struct ptr_data *data;
  int i;
  int offset;

  if (rb_scan_args(argc, argv, "11", &key, &num) == 1) {
    num = INT2NUM(0);
  }

  if (TYPE(key) == T_FIXNUM || TYPE(key) == T_BIGNUM) {
    VALUE pass[1];
    pass[0] = num;
    return rb_dlptr_to_str(1, pass, rb_dlptr_plus(self, key));
  }
  rb_to_id(key);
  if (! (TYPE(key) == T_STRING || TYPE(key) == T_SYMBOL)) {
    rb_raise(rb_eTypeError, "the key must be a string or symbol");
  }

  id = rb_to_id(key);
  Data_Get_Struct(self, struct ptr_data, data);
  offset = 0;
  switch (data->ctype) {
  case DLPTR_CTYPE_STRUCT:
    for (i=0; i < data->ids_num; i++) {
      switch (data->stype[i]) {
      case 'I':
        DLALIGN(data->ptr,offset,INT_ALIGN);
        break;
      case 'L':
        DLALIGN(data->ptr,offset,LONG_ALIGN);
        break;
      case 'P':
      case 'S':
        DLALIGN(data->ptr,offset,VOIDP_ALIGN);
        break;
      case 'F':
        DLALIGN(data->ptr,offset,FLOAT_ALIGN);
        break;
      case 'D':
        DLALIGN(data->ptr,offset,DOUBLE_ALIGN);
        break;
      case 'C':
        break;
      case 'H':
        DLALIGN(data->ptr,offset,SHORT_ALIGN);
        break;
      default:
        rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]);
      }
      if (data->ids[i] == id) {
  return cary2ary((char *)data->ptr + offset, data->stype[i], data->ssize[i]);
      }
      switch (data->stype[i]) {
      case 'I':
  offset += sizeof(int) * data->ssize[i];
  break;
      case 'L':
  offset += sizeof(long) * data->ssize[i];
  break;
      case 'P':
      case 'S':
  offset += sizeof(void*) * data->ssize[i];
  break;
      case 'F':
  offset += sizeof(float) * data->ssize[i];
  break;
      case 'D':
  offset += sizeof(double) * data->ssize[i];
  break;
      case 'C':
  offset += sizeof(char) * data->ssize[i];
  break;
      case 'H':
  offset += sizeof(short) * data->ssize[i];
  break;
      default:
  rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]);
      }
    }
    break;
  case DLPTR_CTYPE_UNION:
    for (i=0; i < data->ids_num; i++) {
      if (data->ids[i] == id) {
  return cary2ary((char *)data->ptr + offset, data->stype[i], data->ssize[i]);
      }
    }
    break;
  } /* end of switch */

  rb_raise(rb_eNameError, "undefined key `%s' for %s",
     rb_id2name(id), rb_class2name(CLASS_OF(self)));

  return Qnil;
}

#[]=(argv[], self) ⇒ Object



838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
# File 'ext/rubysl/dl/ptr.c', line 838

VALUE
rb_dlptr_aset(int argc, VALUE argv[], VALUE self)
{
  VALUE key = Qnil, num = Qnil, val = Qnil;
  ID id;
  struct ptr_data *data;
  int i;
  int offset;
  long memsize;
  void *memimg;

  rb_secure(4);
  switch (rb_scan_args(argc, argv, "21", &key, &num, &val)) {
  case 2:
    val = num;
    num = Qnil;
    break;
  }

  if (TYPE(key) == T_FIXNUM || TYPE(key) == T_BIGNUM) {
    void *dst, *src;
    long len;

    StringValue(val);
    Data_Get_Struct(self, struct ptr_data, data);
    dst = (void*)((long)(data->ptr) + DLNUM2LONG(key));
    src = RSTRING(val)->ptr;
    len = RSTRING(val)->len;
    if (num == Qnil) {
      memcpy(dst, src, len);
    }
    else{
      long n = NUM2INT(num);
      memcpy(dst, src, n < len ? n : len);
      if (n > len) MEMZERO((char*)dst + len, char, n - len);
    }
    return val;
  }

  id = rb_to_id(key);
  Data_Get_Struct(self, struct ptr_data, data);
  switch (data->ctype) {
  case DLPTR_CTYPE_STRUCT:
    offset = 0;
    for (i=0; i < data->ids_num; i++) {
      switch (data->stype[i]) {
      case 'I':
        DLALIGN(data->ptr,offset,INT_ALIGN);
        break;
      case 'L':
        DLALIGN(data->ptr,offset,LONG_ALIGN);
        break;
      case 'P':
      case 'S':
        DLALIGN(data->ptr,offset,VOIDP_ALIGN);
        break;
      case 'D':
        DLALIGN(data->ptr,offset,DOUBLE_ALIGN);
        break;
      case 'F':
        DLALIGN(data->ptr,offset,FLOAT_ALIGN);
        break;
      case 'C':
        break;
      case 'H':
        DLALIGN(data->ptr,offset,SHORT_ALIGN);
        break;
      default:
        rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]);
      }
      if (data->ids[i] == id) {
  memimg = ary2cary(data->stype[i], val, &memsize);
  memcpy((char *)data->ptr + offset, memimg, memsize);
        dlfree(memimg);
  return val;
      }
      switch (data->stype[i]) {
      case 'I':
      case 'i':
  offset += sizeof(int) * data->ssize[i];
  break;
      case 'L':
      case 'l':
  offset += sizeof(long) * data->ssize[i];
  break;
      case 'P':
      case 'p':
      case 'S':
      case 's':
  offset += sizeof(void*) * data->ssize[i];
  break;
      case 'D':
      case 'd':
  offset += sizeof(double) * data->ssize[i];
  break;
      case 'F':
      case 'f':
  offset += sizeof(float) * data->ssize[i];
  break;
      case 'C':
      case 'c':
  offset += sizeof(char) * data->ssize[i];
  break;
      case 'H':
      case 'h':
  offset += sizeof(short) * data->ssize[i];
  break;
      default:
  rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]);
      }
    }
    return val;
    /* break; */
  case DLPTR_CTYPE_UNION:
    for (i=0; i < data->ids_num; i++) {
      if (data->ids[i] == id) {
  switch (data->stype[i]) {
  case 'I': case 'i':
    memsize = sizeof(int) * data->ssize[i];
    break;
  case 'L': case 'l':
    memsize = sizeof(long) * data->ssize[i];
    break;
  case 'P': case 'p':
  case 'S': case 's':
    memsize = sizeof(void*) * data->ssize[i];
    break;
  case 'F': case 'f':
    memsize = sizeof(float) * data->ssize[i];
    break;
  case 'D': case 'd':
    memsize = sizeof(double) * data->ssize[i];
    break;
  case 'C': case 'c':
    memsize = sizeof(char) * data->ssize[i];
    break;
  case 'H': case 'h':
    memsize = sizeof(short) * data->ssize[i];
    break;
  default:
    rb_raise(rb_eDLTypeError, "unsupported type '%c'", data->stype[i]);
  }
  memimg = ary2cary(data->stype[i], val, NULL);
  memcpy(data->ptr, memimg, memsize);
        dlfree(memimg);
      }
    }
    return val;
    /* break; */
  }

  rb_raise(rb_eNameError, "undefined key `%s' for %s",
     rb_id2name(id), rb_class2name(CLASS_OF(self)));

  return Qnil;
}

#data_typeObject



623
624
625
626
627
628
629
630
631
632
633
634
# File 'ext/rubysl/dl/ptr.c', line 623

VALUE
rb_dlptr_get_data_type(VALUE self)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);
  if (data->stype)
    return rb_assoc_new(INT2FIX(data->ctype),
      rb_tainted_str_new(data->stype, data->slen));
  else
    return rb_assoc_new(INT2FIX(data->ctype), Qnil);
}

#define_data_type(argv[], self) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'ext/rubysl/dl/ptr.c', line 514

VALUE
rb_dlptr_define_data_type(int argc, VALUE argv[], VALUE self)
{
  VALUE data_type, type, rest, vid;
  struct ptr_data *data;
  int i, t, num;
  char *ctype;

  rb_scan_args(argc, argv, "11*", &data_type, &type, &rest);
  Data_Get_Struct(self, struct ptr_data, data);

  if (argc == 1 || (argc == 2 && type == Qnil)) {
    if (NUM2INT(data_type) == DLPTR_CTYPE_UNKNOWN) {
      data->ctype = DLPTR_CTYPE_UNKNOWN;
      data->slen = 0;
      data->ids_num  = 0;
      if (data->stype) {
  dlfree(data->stype);
  data->stype = NULL;
      }
      if (data->ids) {
  dlfree(data->ids);
  data->ids = NULL;
      }
      return Qnil;
    }
    else{
      rb_raise(rb_eArgError, "wrong arguments");
    }
  }

  t = NUM2INT(data_type);
  StringValue(type);
  Check_Type(rest, T_ARRAY);
  num = RARRAY_LEN(rest);
  for (i=0; i<num; i++) {
    rb_to_id(rb_ary_entry(rest,i));
  }

  data->ctype = t;
  data->slen = num;
  data->ids_num  = num;
  if (data->stype) dlfree(data->stype);
  data->stype = (char*)dlmalloc(sizeof(char) * num);
  if (data->ssize) dlfree(data->ssize);
  data->ssize = (int*)dlmalloc(sizeof(int) * num);
  if (data->ids) dlfree(data->ids);
  data->ids  = (ID*)dlmalloc(sizeof(ID) * data->ids_num);

  ctype = StringValuePtr(type);
  for (i=0; i<num; i++) {
    vid = rb_ary_entry(rest,i);
    data->ids[i] = rb_to_id(vid);
    data->stype[i] = *ctype;
    ctype ++;
    if (isdigit(*ctype)) {
      char *p, *d;
      for (p=ctype; isdigit(*p); p++) ;
      d = ALLOCA_N(char, p - ctype + 1);
      strncpy(d, ctype, p - ctype);
      d[p - ctype] = '\0';
      data->ssize[i] = atoi(d);
      ctype = p;
    }
    else{
      data->ssize[i] = 1;
    }
  }

  if (*ctype) {
    rb_raise(rb_eArgError, "too few/many arguments");
  }

  if (!data->size)
    data->size = dlsizeof(RSTRING(type)->ptr);

  return Qnil;
}

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


471
472
473
474
475
476
477
478
479
# File 'ext/rubysl/dl/ptr.c', line 471

VALUE
rb_dlptr_eql(VALUE self, VALUE other)
{
  void *ptr1, *ptr2;
  ptr1 = rb_dlptr2cptr(self);
  ptr2 = rb_dlptr2cptr(other);

  return ptr1 == ptr2 ? Qtrue : Qfalse;
}

#freeObject



290
291
292
293
294
295
296
297
298
# File 'ext/rubysl/dl/ptr.c', line 290

VALUE
rb_dlptr_free_get(VALUE self)
{
  struct ptr_data *pdata;

  Data_Get_Struct(self, struct ptr_data, pdata);

  return rb_dlsym_new(pdata->free,"(free)","0P");
}

#free=(val) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
# File 'ext/rubysl/dl/ptr.c', line 278

VALUE
rb_dlptr_free_set(VALUE self, VALUE val)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);

  data->free = DLFREEFUNC(rb_dlsym2csym(val));

  return Qnil;
}

#inspectObject



457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'ext/rubysl/dl/ptr.c', line 457

VALUE
rb_dlptr_inspect(VALUE self)
{
  struct ptr_data *data;
  char str[1024];

  Data_Get_Struct(self, struct ptr_data, data);
  snprintf(str, 1023, "#<%s:0x%lx ptr=0x%lx size=%ld free=0x%lx>",
     rb_class2name(CLASS_OF(self)), (long unsigned)data,
     (long unsigned)data->ptr, data->size,
     (long)data->free);
  return rb_str_new2(str);
}

#null?Boolean

Returns:

  • (Boolean)


269
270
271
272
273
274
275
276
# File 'ext/rubysl/dl/ptr.c', line 269

VALUE
rb_dlptr_null_p(VALUE self)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);
  return data->ptr ? Qfalse : Qtrue;
}

#ptrObject



251
252
253
254
255
256
257
258
# File 'ext/rubysl/dl/ptr.c', line 251

VALUE
rb_dlptr_ptr(VALUE self)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);
  return rb_dlptr_new(*((void**)(data->ptr)),0,0);
}

#refObject



260
261
262
263
264
265
266
267
# File 'ext/rubysl/dl/ptr.c', line 260

VALUE
rb_dlptr_ref(VALUE self)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);
  return rb_dlptr_new(&(data->ptr),0,0);
}

#size(argv[], self) ⇒ Object



995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'ext/rubysl/dl/ptr.c', line 995

VALUE
rb_dlptr_size(int argc, VALUE argv[], VALUE self)
{
  VALUE size;

  if (rb_scan_args(argc, argv, "01", &size) == 0){
    return DLLONG2NUM(RDLPTR(self)->size);
  }
  else{
    RDLPTR(self)->size = DLNUM2LONG(size);
    return size;
  }
}

#size=(argv[], self) ⇒ Object



995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'ext/rubysl/dl/ptr.c', line 995

VALUE
rb_dlptr_size(int argc, VALUE argv[], VALUE self)
{
  VALUE size;

  if (rb_scan_args(argc, argv, "01", &size) == 0){
    return DLLONG2NUM(RDLPTR(self)->size);
  }
  else{
    RDLPTR(self)->size = DLNUM2LONG(size);
    return size;
  }
}

#struct!(argv[], self) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'ext/rubysl/dl/ptr.c', line 593

VALUE
rb_dlptr_define_struct(int argc, VALUE argv[], VALUE self)
{
  VALUE *pass_argv;
  int pass_argc, i;

  pass_argc = argc + 1;
  pass_argv = ALLOCA_N(VALUE, pass_argc);
  pass_argv[0] = INT2FIX(DLPTR_CTYPE_STRUCT);
  for (i=1; i<pass_argc; i++) {
    pass_argv[i] = argv[i-1];
  }
  return rb_dlptr_define_data_type(pass_argc, pass_argv, self);
}

#to_a(argv[], self) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'ext/rubysl/dl/ptr.c', line 300

VALUE
rb_dlptr_to_array(int argc, VALUE argv[], VALUE self)
{
  struct ptr_data *data;
  int n = 0;
  int i;
  int t = 0;
  VALUE ary;
  VALUE type, size;

  Data_Get_Struct(self, struct ptr_data, data);

  switch (rb_scan_args(argc, argv, "11", &type, &size)) {
  case 2:
    t = StringValuePtr(type)[0];
    n = NUM2INT(size);
    break;
  case 1:
    t = StringValuePtr(type)[0];
    switch (t) {
    case 'C':
      n = data->size;
      break;
    case 'H':
      n = data->size / sizeof(short);
      break;
    case 'I':
      n = data->size / sizeof(int);
      break;
    case 'L':
      n = data->size / sizeof(long);
      break;
    case 'F':
      n = data->size / sizeof(float);
      break;
    case 'D':
      n = data->size / sizeof(double);
      break;
    case  'P': case 'p':
      n = data->size / sizeof(void*);
      break;
    case 'S': case 's':
      n = data->size / sizeof(char*);
      break;
    default:
  n = 0;
    }
    break;
  default:
    rb_bug("rb_dlptr_to_array");
  }

  ary = rb_ary_new();

  for (i=0; i < n; i++) {
    switch (t) {
    case 'C':
      rb_ary_push(ary, INT2NUM(((char*)(data->ptr))[i]));
      break;
    case 'H':
      rb_ary_push(ary, INT2NUM(((short*)(data->ptr))[i]));
      break;
    case 'I':
      rb_ary_push(ary, INT2NUM(((int*)(data->ptr))[i]));
      break;
    case 'L':
      rb_ary_push(ary, DLLONG2NUM(((long*)(data->ptr))[i]));
      break;
    case 'D':
      rb_ary_push(ary, rb_float_new(((double*)(data->ptr))[i]));
      break;
    case 'F':
      rb_ary_push(ary, rb_float_new(((float*)(data->ptr))[i]));
      break;
    case 'S':
      {
  char *str = ((char**)(data->ptr))[i];
  if (str) {
    rb_ary_push(ary, rb_tainted_str_new2(str));
  }
  else{
    rb_ary_push(ary, Qnil);
  }
      }
      break;
    case 's':
      {
  char *str = ((char**)(data->ptr))[i];
  if (str) {
    rb_ary_push(ary, rb_tainted_str_new2(str));
    xfree(str);
  }
  else{
    rb_ary_push(ary, Qnil);
  }
      }
      break;
    case 'P':
      rb_ary_push(ary, rb_dlptr_new(((void**)(data->ptr))[i],0,0));
      break;
    case 'p':
      rb_ary_push(ary,
      rb_dlptr_new(((void**)(data->ptr))[i],0,dlfree));
      break;
    }
  }

  return ary;
}

#to_iObject



242
243
244
245
246
247
248
249
# File 'ext/rubysl/dl/ptr.c', line 242

VALUE
rb_dlptr_to_i(VALUE self)
{
  struct ptr_data *data;

  Data_Get_Struct(self, struct ptr_data, data);
  return DLLONG2NUM(data->ptr);
}

#to_s(argv[], self) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'ext/rubysl/dl/ptr.c', line 411

VALUE
rb_dlptr_to_s(int argc, VALUE argv[], VALUE self)
{
  struct ptr_data *data;
  VALUE arg1, val = Qnil;
  int len;

  Data_Get_Struct(self, struct ptr_data, data);
  switch (rb_scan_args(argc, argv, "01", &arg1)) {
  case 0:
    val = rb_tainted_str_new2((char*)(data->ptr));
    break;
  case 1:
    len = NUM2INT(arg1);
    val = rb_tainted_str_new((char*)(data->ptr), len);
    break;
  default:
    rb_bug("rb_dlptr_to_s");
  }

  return val;
}

#to_str(argv[], self) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'ext/rubysl/dl/ptr.c', line 434

VALUE
rb_dlptr_to_str(int argc, VALUE argv[], VALUE self)
{
  struct ptr_data *data;
  VALUE arg1, val = Qnil;
  int len;

  Data_Get_Struct(self, struct ptr_data, data);
  switch (rb_scan_args(argc, argv, "01", &arg1)) {
  case 0:
    val = rb_tainted_str_new((char*)(data->ptr),data->size);
    break;
  case 1:
    len = NUM2INT(arg1);
    val = rb_tainted_str_new((char*)(data->ptr), len);
    break;
  default:
    rb_bug("rb_dlptr_to_str");
  }

  return val;
}

#union!(argv[], self) ⇒ Object



608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'ext/rubysl/dl/ptr.c', line 608

VALUE
rb_dlptr_define_union(int argc, VALUE argv[], VALUE self)
{
  VALUE *pass_argv;
  int pass_argc, i;

  pass_argc = argc + 1;
  pass_argv = ALLOCA_N(VALUE, pass_argc);
  pass_argv[0] = INT2FIX(DLPTR_CTYPE_UNION);
  for (i=1; i<pass_argc; i++) {
    pass_argv[i] = argv[i-1];
  }
  return rb_dlptr_define_data_type(pass_argc, pass_argv, self);
}