2005-02-04 08:35:37 -05:00
|
|
|
/* -*- C -*-
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2007-06-09 23:06:15 -04:00
|
|
|
#include <ruby/ruby.h>
|
|
|
|
#include <ruby/io.h>
|
2005-02-04 08:35:37 -05:00
|
|
|
#include <ctype.h>
|
|
|
|
#include "dl.h"
|
|
|
|
|
|
|
|
VALUE rb_cDLCPtr;
|
|
|
|
|
2009-05-09 22:13:20 -04:00
|
|
|
static inline freefunc_t
|
|
|
|
get_freefunc(VALUE func)
|
|
|
|
{
|
|
|
|
if (NIL_P(func)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (rb_dlcfunc_kind_p(func)) {
|
|
|
|
return RCFUNC_DATA(func)->ptr;
|
|
|
|
}
|
|
|
|
return NUM2PTR(rb_Integer(func));
|
|
|
|
}
|
|
|
|
|
2005-02-06 01:54:12 -05:00
|
|
|
static ID id_to_ptr;
|
|
|
|
|
2005-02-04 08:35:37 -05:00
|
|
|
static void
|
2009-09-09 08:19:28 -04:00
|
|
|
dlptr_free(void *ptr)
|
2005-02-04 08:35:37 -05:00
|
|
|
{
|
2009-09-09 08:19:28 -04:00
|
|
|
struct ptr_data *data = ptr;
|
2009-03-16 21:15:35 -04:00
|
|
|
if (data->ptr) {
|
|
|
|
if (data->free) {
|
|
|
|
(*(data->free))(data->ptr);
|
|
|
|
}
|
|
|
|
}
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
static size_t
|
|
|
|
dlptr_memsize(const void *ptr)
|
2005-02-04 08:35:37 -05:00
|
|
|
{
|
2009-09-09 08:19:28 -04:00
|
|
|
const struct ptr_data *data = ptr;
|
|
|
|
return data ? sizeof(*data) + data->size : 0;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
static const rb_data_type_t dlptr_data_type = {
|
|
|
|
"dl/ptr",
|
|
|
|
0, dlptr_free, dlptr_memsize,
|
|
|
|
};
|
|
|
|
|
2005-02-04 08:35:37 -05:00
|
|
|
void
|
|
|
|
dlptr_init(VALUE val)
|
|
|
|
{
|
2009-03-16 21:15:35 -04:00
|
|
|
struct ptr_data *data;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(val, struct ptr_data, &dlptr_data_type, data);
|
2009-03-16 21:15:35 -04:00
|
|
|
OBJ_TAINT(val);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func)
|
|
|
|
{
|
2009-03-16 21:15:35 -04:00
|
|
|
struct ptr_data *data;
|
|
|
|
VALUE val;
|
|
|
|
|
|
|
|
rb_secure(4);
|
2009-09-09 08:19:28 -04:00
|
|
|
val = TypedData_Make_Struct(klass, struct ptr_data, &dlptr_data_type, data);
|
2009-03-16 21:15:35 -04:00
|
|
|
data->ptr = ptr;
|
|
|
|
data->free = func;
|
|
|
|
data->size = size;
|
|
|
|
dlptr_init(val);
|
|
|
|
|
|
|
|
return val;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dlptr_new(void *ptr, long size, freefunc_t func)
|
|
|
|
{
|
2009-03-16 21:15:35 -04:00
|
|
|
return rb_dlptr_new2(rb_cDLCPtr, ptr, size, func);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dlptr_malloc(long size, freefunc_t func)
|
|
|
|
{
|
2009-03-16 21:15:35 -04:00
|
|
|
void *ptr;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-03-16 21:15:35 -04:00
|
|
|
rb_secure(4);
|
|
|
|
ptr = ruby_xmalloc((size_t)size);
|
|
|
|
memset(ptr,0,(size_t)size);
|
|
|
|
return rb_dlptr_new(ptr, size, func);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
rb_dlptr2cptr(VALUE val)
|
|
|
|
{
|
2009-03-16 21:15:35 -04:00
|
|
|
struct ptr_data *data;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if (rb_obj_is_kind_of(val, rb_cDLCPtr)) {
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(val, struct ptr_data, &dlptr_data_type, data);
|
2009-03-16 21:15:35 -04:00
|
|
|
ptr = data->ptr;
|
|
|
|
}
|
|
|
|
else if (val == Qnil) {
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
rb_raise(rb_eTypeError, "DL::PtrData was expected");
|
|
|
|
}
|
2010-02-25 17:49:20 -05:00
|
|
|
|
2009-03-16 21:15:35 -04:00
|
|
|
return ptr;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_dlptr_s_allocate(VALUE klass)
|
|
|
|
{
|
2009-03-16 21:15:35 -04:00
|
|
|
VALUE obj;
|
|
|
|
struct ptr_data *data;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-03-16 21:15:35 -04:00
|
|
|
rb_secure(4);
|
2009-09-09 08:19:28 -04:00
|
|
|
obj = TypedData_Make_Struct(klass, struct ptr_data, &dlptr_data_type, data);
|
2009-03-16 21:15:35 -04:00
|
|
|
data->ptr = 0;
|
|
|
|
data->size = 0;
|
|
|
|
data->free = 0;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-03-16 21:15:35 -04:00
|
|
|
return obj;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 17:50:05 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* DL::CPtr.new(address) => dl_cptr
|
|
|
|
* DL::CPtr.new(address, size) => dl_cptr
|
|
|
|
* DL::CPtr.new(address, size, freefunc) => dl_cptr
|
|
|
|
*
|
|
|
|
* Create a new pointer to +address+ with an optional +size+ and +freefunc+.
|
|
|
|
* +freefunc+ will be called when the instance is garbage collected.
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
static VALUE
|
|
|
|
rb_dlptr_initialize(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
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*)(NUM2PTR(rb_Integer(ptr)));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
p = (void*)(NUM2PTR(rb_Integer(ptr)));
|
|
|
|
s = NUM2LONG(size);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
p = (void*)(NUM2PTR(rb_Integer(ptr)));
|
|
|
|
s = NUM2LONG(size);
|
2009-05-09 22:13:20 -04:00
|
|
|
f = get_freefunc(sym);
|
2009-05-09 21:47:10 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_bug("rb_dlptr_initialize");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p) {
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 21:47:10 -04:00
|
|
|
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;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-05-09 21:47:10 -04:00
|
|
|
return Qnil;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 17:50:05 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
*
|
|
|
|
* DL::CPtr.malloc(size, freefunc = nil) => dl cptr instance
|
|
|
|
*
|
|
|
|
* Allocate +size+ bytes of memory and associate it with an optional
|
|
|
|
* +freefunc+ that will be called when the pointer is garbage collected.
|
|
|
|
* +freefunc+ must be an address pointing to a function or an instance of
|
|
|
|
* DL::CFunc
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
static VALUE
|
|
|
|
rb_dlptr_s_malloc(int argc, VALUE argv[], VALUE klass)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
VALUE size, sym, obj;
|
2010-02-25 17:49:20 -05:00
|
|
|
int s;
|
2009-05-09 21:47:10 -04:00
|
|
|
freefunc_t f;
|
|
|
|
|
|
|
|
switch (rb_scan_args(argc, argv, "11", &size, &sym)) {
|
|
|
|
case 1:
|
|
|
|
s = NUM2LONG(size);
|
|
|
|
f = NULL;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
s = NUM2LONG(size);
|
2009-05-09 22:13:20 -04:00
|
|
|
f = get_freefunc(sym);
|
2009-05-09 21:47:10 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_bug("rb_dlptr_s_malloc");
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = rb_dlptr_malloc(s,f);
|
|
|
|
|
|
|
|
return obj;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 13:29:31 -05:00
|
|
|
/*
|
|
|
|
* call-seq: to_i
|
|
|
|
*
|
|
|
|
* Returns the integer memory location of this DL::CPtr.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_to_i(VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 21:47:10 -04:00
|
|
|
return PTR2NUM(data->ptr);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-10-31 21:46:56 -04:00
|
|
|
/*
|
|
|
|
* call-seq: to_value
|
|
|
|
*
|
|
|
|
* Cast this CPtr to a ruby object.
|
|
|
|
*/
|
2009-11-06 13:29:31 -05:00
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_to_value(VALUE self)
|
|
|
|
{
|
|
|
|
struct ptr_data *data;
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2005-02-04 08:35:37 -05:00
|
|
|
return (VALUE)(data->ptr);
|
|
|
|
}
|
|
|
|
|
2009-10-31 21:47:08 -04:00
|
|
|
/*
|
|
|
|
* call-seq: ptr
|
|
|
|
*
|
|
|
|
* Returns a DL::CPtr that is a dereferenced pointer for this DL::CPtr.
|
|
|
|
* Analogous to the star operator in C.
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
VALUE
|
|
|
|
rb_dlptr_ptr(VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 21:47:10 -04:00
|
|
|
return rb_dlptr_new(*((void**)(data->ptr)),0,0);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-10-31 21:47:08 -04:00
|
|
|
/*
|
|
|
|
* call-seq: ref
|
|
|
|
*
|
|
|
|
* Returns a DL::CPtr that is a reference pointer for this DL::CPtr.
|
|
|
|
* Analogous to the ampersand operator in C.
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
VALUE
|
|
|
|
rb_dlptr_ref(VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 21:47:10 -04:00
|
|
|
return rb_dlptr_new(&(data->ptr),0,0);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-10-31 21:46:44 -04:00
|
|
|
/*
|
|
|
|
* call-seq: null?
|
|
|
|
*
|
|
|
|
* Returns true if this is a null pointer.
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
VALUE
|
|
|
|
rb_dlptr_null_p(VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 21:47:10 -04:00
|
|
|
return data->ptr ? Qfalse : Qtrue;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-03 15:28:20 -05:00
|
|
|
/*
|
|
|
|
* call-seq: free=(function)
|
|
|
|
*
|
|
|
|
* Set the free function for this pointer to the DL::CFunc in +function+.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_free_set(VALUE self, VALUE val)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 22:13:20 -04:00
|
|
|
data->free = get_freefunc(val);
|
2009-05-09 21:47:10 -04:00
|
|
|
|
|
|
|
return Qnil;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-03 15:28:20 -05:00
|
|
|
/*
|
|
|
|
* call-seq: free
|
|
|
|
*
|
|
|
|
* Get the free function for this pointer. Returns DL::CFunc or nil.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_free_get(VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *pdata;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, pdata);
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-05-09 21:47:10 -04:00
|
|
|
return rb_dlcfunc_new(pdata->free, DLTYPE_VOID, "free<anonymous>", CFUNC_CDECL);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 13:29:31 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
*
|
|
|
|
* ptr.to_s => string
|
|
|
|
* ptr.to_s(len) => string
|
|
|
|
*
|
|
|
|
* Returns the pointer contents as a string. When called with no arguments,
|
|
|
|
* this method will return the contents until the first NULL byte. When
|
|
|
|
* called with +len+, a string of +len+ bytes will be returned.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_to_s(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
|
|
|
VALUE arg1, val;
|
|
|
|
int len;
|
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 21:47:10 -04:00
|
|
|
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;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 13:29:31 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
*
|
|
|
|
* ptr.to_str => string
|
|
|
|
* ptr.to_str(len) => string
|
|
|
|
*
|
|
|
|
* Returns the pointer contents as a string. When called with no arguments,
|
|
|
|
* this method will return the contents with the length of this pointer's
|
|
|
|
* +size+. When called with +len+, a string of +len+ bytes will be returned.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_to_str(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
|
|
|
VALUE arg1, val;
|
|
|
|
int len;
|
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-05-09 21:47:10 -04:00
|
|
|
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;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 13:02:30 -05:00
|
|
|
/*
|
|
|
|
* call-seq: inspect
|
|
|
|
*
|
|
|
|
* Returns a string formatted with an easily readable representation of the
|
|
|
|
* internal state of the DL::CPtr
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_inspect(VALUE self)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
struct ptr_data *data;
|
|
|
|
char str[1024];
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-09-09 08:19:28 -04:00
|
|
|
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
|
2009-11-07 16:37:27 -05:00
|
|
|
snprintf(str, 1023, "#<%s:%p ptr=%#lx size=%ld free=%#lx>",
|
2009-05-09 21:47:10 -04:00
|
|
|
rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free);
|
|
|
|
return rb_str_new2(str);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-03 12:12:46 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ptr == other => true or false
|
|
|
|
* ptr.eql?(other) => true or false
|
|
|
|
*
|
|
|
|
* Returns true if +other+ wraps the same pointer, otherwise returns
|
|
|
|
* false.
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
VALUE
|
|
|
|
rb_dlptr_eql(VALUE self, VALUE other)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
void *ptr1, *ptr2;
|
2009-11-03 12:12:46 -05:00
|
|
|
|
|
|
|
if(!rb_obj_is_kind_of(other, rb_cDLCPtr)) return Qfalse;
|
|
|
|
|
2009-05-09 21:47:10 -04:00
|
|
|
ptr1 = rb_dlptr2cptr(self);
|
|
|
|
ptr2 = rb_dlptr2cptr(other);
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-05-09 21:47:10 -04:00
|
|
|
return ptr1 == ptr2 ? Qtrue : Qfalse;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-03 12:12:46 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ptr <=> other => -1, 0, 1, or nil
|
|
|
|
*
|
|
|
|
* Returns -1 if less than, 0 if equal to, 1 if greater than +other+. Returns
|
|
|
|
* nil if +ptr+ cannot be compared to +other+.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_cmp(VALUE self, VALUE other)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
void *ptr1, *ptr2;
|
2009-05-23 05:48:54 -04:00
|
|
|
SIGNED_VALUE diff;
|
2009-11-03 12:12:46 -05:00
|
|
|
|
|
|
|
if(!rb_obj_is_kind_of(other, rb_cDLCPtr)) return Qnil;
|
|
|
|
|
2009-05-09 21:47:10 -04:00
|
|
|
ptr1 = rb_dlptr2cptr(self);
|
|
|
|
ptr2 = rb_dlptr2cptr(other);
|
2009-05-23 05:48:54 -04:00
|
|
|
diff = (SIGNED_VALUE)ptr1 - (SIGNED_VALUE)ptr2;
|
|
|
|
if (!diff) return INT2FIX(0);
|
|
|
|
return diff > 0 ? INT2NUM(1) : INT2NUM(-1);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 13:02:30 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ptr + n => new cptr
|
|
|
|
*
|
|
|
|
* Returns a new DL::CPtr that has been advanced +n+ bytes.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_plus(VALUE self, VALUE other)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
void *ptr;
|
|
|
|
long num, size;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-05-09 21:47:10 -04:00
|
|
|
ptr = rb_dlptr2cptr(self);
|
|
|
|
size = RPTR_DATA(self)->size;
|
|
|
|
num = NUM2LONG(other);
|
|
|
|
return rb_dlptr_new((char *)ptr + num, size - num, 0);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-06 13:02:30 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ptr - n => new cptr
|
|
|
|
*
|
|
|
|
* Returns a new DL::CPtr that has been moved back +n+ bytes.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_minus(VALUE self, VALUE other)
|
|
|
|
{
|
2009-05-09 21:47:10 -04:00
|
|
|
void *ptr;
|
|
|
|
long num, size;
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2009-05-09 21:47:10 -04:00
|
|
|
ptr = rb_dlptr2cptr(self);
|
|
|
|
size = RPTR_DATA(self)->size;
|
|
|
|
num = NUM2LONG(other);
|
|
|
|
return rb_dlptr_new((char *)ptr - num, size + num, 0);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-10-31 21:46:44 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ptr[index] -> an_integer
|
|
|
|
* ptr[start, length] -> a_string
|
|
|
|
*
|
|
|
|
* Returns integer stored at _index_. If _start_ and _length_ are given,
|
|
|
|
* a string containing the bytes from _start_ of length _length_ will be
|
|
|
|
* returned.
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
VALUE
|
|
|
|
rb_dlptr_aref(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
|
|
|
VALUE arg0, arg1;
|
2005-03-08 06:31:20 -05:00
|
|
|
VALUE retval = Qnil;
|
2005-02-04 08:35:37 -05:00
|
|
|
size_t offset, len;
|
|
|
|
|
|
|
|
switch( rb_scan_args(argc, argv, "11", &arg0, &arg1) ){
|
2009-05-09 21:47:10 -04:00
|
|
|
case 1:
|
2005-02-04 08:35:37 -05:00
|
|
|
offset = NUM2ULONG(arg0);
|
2005-03-08 06:31:20 -05:00
|
|
|
retval = INT2NUM(*((char*)RPTR_DATA(self)->ptr + offset));
|
2005-02-04 08:35:37 -05:00
|
|
|
break;
|
2009-05-09 21:47:10 -04:00
|
|
|
case 2:
|
2005-02-04 08:35:37 -05:00
|
|
|
offset = NUM2ULONG(arg0);
|
|
|
|
len = NUM2ULONG(arg1);
|
2005-03-08 06:31:20 -05:00
|
|
|
retval = rb_tainted_str_new((char *)RPTR_DATA(self)->ptr + offset, len);
|
2005-02-04 08:35:37 -05:00
|
|
|
break;
|
2009-05-09 21:47:10 -04:00
|
|
|
default:
|
2005-03-08 06:31:20 -05:00
|
|
|
rb_bug("rb_dlptr_aref()");
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
2005-03-08 06:31:20 -05:00
|
|
|
return retval;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-10-31 21:46:44 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ptr[index] = int -> int
|
|
|
|
* ptr[start, length] = string or cptr or addr -> string or dl_cptr or addr
|
|
|
|
*
|
|
|
|
* Set the value at +index+ to +int+. Or, set the memory at +start+ until
|
|
|
|
* +length+ with the contents of +string+, the memory from +dl_cptr+, or the
|
|
|
|
* memory pointed at by the memory address +addr+.
|
|
|
|
*/
|
2005-02-04 08:35:37 -05:00
|
|
|
VALUE
|
|
|
|
rb_dlptr_aset(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
|
|
|
VALUE arg0, arg1, arg2;
|
2005-03-08 06:31:20 -05:00
|
|
|
VALUE retval = Qnil;
|
2005-02-04 08:35:37 -05:00
|
|
|
size_t offset, len;
|
|
|
|
void *mem;
|
|
|
|
|
|
|
|
switch( rb_scan_args(argc, argv, "21", &arg0, &arg1, &arg2) ){
|
2009-05-09 21:47:10 -04:00
|
|
|
case 2:
|
2005-02-04 08:35:37 -05:00
|
|
|
offset = NUM2ULONG(arg0);
|
2005-03-08 06:31:20 -05:00
|
|
|
((char*)RPTR_DATA(self)->ptr)[offset] = NUM2UINT(arg1);
|
|
|
|
retval = arg1;
|
2005-02-04 08:35:37 -05:00
|
|
|
break;
|
2009-05-09 21:47:10 -04:00
|
|
|
case 3:
|
2005-02-04 08:35:37 -05:00
|
|
|
offset = NUM2ULONG(arg0);
|
|
|
|
len = NUM2ULONG(arg1);
|
|
|
|
if( TYPE(arg2) == T_STRING ){
|
|
|
|
mem = StringValuePtr(arg2);
|
|
|
|
}
|
|
|
|
else if( rb_obj_is_kind_of(arg2, rb_cDLCPtr) ){
|
|
|
|
mem = rb_dlptr2cptr(arg2);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
mem = NUM2PTR(arg2);
|
|
|
|
}
|
2005-03-08 06:31:20 -05:00
|
|
|
memcpy((char *)RPTR_DATA(self)->ptr + offset, mem, len);
|
|
|
|
retval = arg2;
|
2005-02-04 08:35:37 -05:00
|
|
|
break;
|
2009-05-09 21:47:10 -04:00
|
|
|
default:
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_bug("rb_dlptr_aset()");
|
|
|
|
}
|
2005-03-08 06:31:20 -05:00
|
|
|
return retval;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-10-31 21:46:44 -04:00
|
|
|
/*
|
|
|
|
* call-seq: size=(size)
|
|
|
|
*
|
|
|
|
* Set the size of this pointer to +size+
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_dlptr_size_set(VALUE self, VALUE size)
|
2005-02-04 08:35:37 -05:00
|
|
|
{
|
2009-10-31 21:46:44 -04:00
|
|
|
RPTR_DATA(self)->size = NUM2LONG(size);
|
|
|
|
return size;
|
|
|
|
}
|
2009-05-09 21:47:10 -04:00
|
|
|
|
2009-10-31 21:46:44 -04:00
|
|
|
/*
|
|
|
|
* call-seq: size
|
|
|
|
*
|
|
|
|
* Get the size of this pointer.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_dlptr_size_get(VALUE self)
|
|
|
|
{
|
|
|
|
return LONG2NUM(RPTR_DATA(self)->size);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
2009-11-03 15:28:20 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* DL::CPtr.to_ptr(val) => cptr
|
|
|
|
* DL::CPtr[val] => cptr
|
|
|
|
*
|
|
|
|
* Get the underlying pointer for ruby object +val+ and return it as a
|
|
|
|
* DL::CPtr object.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_dlptr_s_to_ptr(VALUE self, VALUE val)
|
|
|
|
{
|
2008-08-11 07:33:44 -04:00
|
|
|
VALUE ptr;
|
|
|
|
|
2009-11-03 15:28:20 -05:00
|
|
|
if (RTEST(rb_obj_is_kind_of(val, rb_cIO))){
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
rb_io_t *fptr;
|
2005-02-04 08:35:37 -05:00
|
|
|
FILE *fp;
|
|
|
|
GetOpenFile(val, fptr);
|
|
|
|
fp = rb_io_stdio_file(fptr);
|
2008-08-11 07:33:44 -04:00
|
|
|
ptr = rb_dlptr_new(fp, 0, NULL);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
2009-11-03 15:28:20 -05:00
|
|
|
else if (RTEST(rb_obj_is_kind_of(val, rb_cString))){
|
|
|
|
char *str = StringValuePtr(val);
|
|
|
|
ptr = rb_dlptr_new(str, RSTRING_LEN(val), NULL);
|
2005-03-08 06:31:20 -05:00
|
|
|
}
|
2008-08-11 07:33:44 -04:00
|
|
|
else if (rb_respond_to(val, id_to_ptr)){
|
2005-02-06 01:54:12 -05:00
|
|
|
VALUE vptr = rb_funcall(val, id_to_ptr, 0);
|
2008-08-11 07:33:44 -04:00
|
|
|
if (rb_obj_is_kind_of(vptr, rb_cDLCPtr)){
|
|
|
|
ptr = vptr;
|
2005-02-06 01:54:12 -05:00
|
|
|
}
|
|
|
|
else{
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 01:47:45 -05:00
|
|
|
rb_raise(rb_eDLError, "to_ptr should return a CPtr object");
|
2005-02-06 01:54:12 -05:00
|
|
|
}
|
|
|
|
}
|
2005-02-04 08:35:37 -05:00
|
|
|
else{
|
2008-08-11 07:33:44 -04:00
|
|
|
ptr = rb_dlptr_new(NUM2PTR(rb_Integer(val)), 0, NULL);
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
2008-08-11 07:33:44 -04:00
|
|
|
OBJ_INFECT(ptr, val);
|
2009-11-12 17:41:07 -05:00
|
|
|
rb_iv_set(ptr, "wrapping", val);
|
2008-08-11 07:33:44 -04:00
|
|
|
return ptr;
|
2005-02-04 08:35:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-09 02:25:14 -04:00
|
|
|
Init_dlptr(void)
|
2005-02-04 08:35:37 -05:00
|
|
|
{
|
2005-02-06 01:54:12 -05:00
|
|
|
id_to_ptr = rb_intern("to_ptr");
|
|
|
|
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_cDLCPtr = rb_define_class_under(rb_mDL, "CPtr", rb_cObject);
|
|
|
|
rb_define_alloc_func(rb_cDLCPtr, rb_dlptr_s_allocate);
|
|
|
|
rb_define_singleton_method(rb_cDLCPtr, "malloc", rb_dlptr_s_malloc, -1);
|
|
|
|
rb_define_singleton_method(rb_cDLCPtr, "to_ptr", rb_dlptr_s_to_ptr, 1);
|
|
|
|
rb_define_singleton_method(rb_cDLCPtr, "[]", rb_dlptr_s_to_ptr, 1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "initialize", rb_dlptr_initialize, -1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "free=", rb_dlptr_free_set, 1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "free", rb_dlptr_free_get, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "to_i", rb_dlptr_to_i, 0);
|
2009-12-04 18:21:01 -05:00
|
|
|
rb_define_method(rb_cDLCPtr, "to_int", rb_dlptr_to_i, 0);
|
2005-02-04 08:35:37 -05:00
|
|
|
rb_define_method(rb_cDLCPtr, "to_value", rb_dlptr_to_value, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "ptr", rb_dlptr_ptr, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "+@", rb_dlptr_ptr, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "ref", rb_dlptr_ref, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "-@", rb_dlptr_ref, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "null?", rb_dlptr_null_p, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "to_s", rb_dlptr_to_s, -1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "to_str", rb_dlptr_to_str, -1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "inspect", rb_dlptr_inspect, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "<=>", rb_dlptr_cmp, 1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "==", rb_dlptr_eql, 1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "eql?", rb_dlptr_eql, 1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "+", rb_dlptr_plus, 1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "-", rb_dlptr_minus, 1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "[]", rb_dlptr_aref, -1);
|
|
|
|
rb_define_method(rb_cDLCPtr, "[]=", rb_dlptr_aset, -1);
|
2009-10-31 21:46:44 -04:00
|
|
|
rb_define_method(rb_cDLCPtr, "size", rb_dlptr_size_get, 0);
|
|
|
|
rb_define_method(rb_cDLCPtr, "size=", rb_dlptr_size_set, 1);
|
2005-02-04 08:35:37 -05:00
|
|
|
|
|
|
|
rb_define_const(rb_mDL, "NULL", rb_dlptr_new(0, 0, 0));
|
|
|
|
}
|