Пример #1
0
static void
cfunc_pointer_destructor(mrb_state *mrb, void *p)
{
    struct cfunc_type_data *data = (struct cfunc_type_data *)p;
    if(data->autofree) {
        free(get_cfunc_pointer_data(data));
    }
    free(p);
}
Пример #2
0
mrb_value
cfunc_pointer_free(mrb_state *mrb, mrb_value self)
{
    struct cfunc_type_data *data = DATA_PTR(self);
    
    free(get_cfunc_pointer_data(data));
    data->autofree = false;
    set_cfunc_pointer_data(data, NULL);
    
    return self;
}
Пример #3
0
mrb_value
cfunc_pointer_offset(mrb_state *mrb, mrb_value self)
{
    struct cfunc_type_data *data = DATA_PTR(self);
    int offset;
    mrb_get_args(mrb, "i", &offset);

    mrb_value ptr = cfunc_pointer_new_with_pointer(mrb, (void*)((uint8_t*)get_cfunc_pointer_data(data) + offset), false);
    mrb_obj_iv_set(mrb, mrb_obj_ptr(ptr), mrb_intern(mrb, "parent_pointer"), self); // keep for GC
    return ptr;
}
Пример #4
0
mrb_value
cfunc_pointer_realloc(mrb_state *mrb, mrb_value self)
{
    struct cfunc_type_data *data = DATA_PTR(self);
    
    mrb_int alloc_size;
    mrb_get_args(mrb, "i", &alloc_size);
    set_cfunc_pointer_data(data, mrb_realloc(mrb, get_cfunc_pointer_data(data), alloc_size));
    
    return self;
}
Пример #5
0
mrb_value
cfunc_pointer_to_s(mrb_state *mrb, mrb_value self)
{
    struct cfunc_type_data *data = DATA_PTR(self);
    size_t len;
    mrb_value str;
    struct RString *s;
    const char* p = (const char*)get_cfunc_pointer_data(data);
        
    len = strlen(p);
    str = mrb_str_new(mrb, 0, len);
    s = mrb_str_ptr(str);
    strcpy(s->ptr, p);
    s->len = strlen(s->ptr);
    return str;
}
Пример #6
0
mrb_value
cfunc_pointer_inspect(mrb_state *mrb, mrb_value self)
{
    struct cfunc_type_data *data = DATA_PTR(self);
    char cstr[256];
    mrb_value str;
    
    const char* classname = mrb_obj_classname(mrb, self);
    if(!classname) {
        classname = "Unknown pointer";
    }
    snprintf(cstr, sizeof(cstr), "<%s=%p>", classname, get_cfunc_pointer_data(data));
    str = mrb_str_new2(mrb, cstr);
    
    return str;
}
Пример #7
0
mrb_value
cfunc_pointer_inspect(mrb_state *mrb, mrb_value self)
{
    struct cfunc_type_data *data = DATA_PTR(self);
    
    mrb_value type = mrb_funcall(mrb, mrb_obj_value(mrb_class(mrb, self)), "type", 0);
    const char* classname = mrb_class_name(mrb, (struct RClass*)mrb_object(type));
    if(!classname) {
        classname = "Unknown pointer";
    }

    char cstr[256];
    snprintf(cstr, sizeof(cstr), "<%s pointer=%p>", classname, get_cfunc_pointer_data(data));
    
    return mrb_str_new_cstr(mrb, cstr);
}
Пример #8
0
static mrb_value
cfunc_pointer_data_to_mrb(mrb_state *mrb, struct cfunc_type_data *data)
{
    return cfunc_pointer_new_with_pointer(mrb, get_cfunc_pointer_data(data), false);
}
Пример #9
0
mrb_value
cfunc_pointer_is_null(mrb_state *mrb, mrb_value self)
{
    struct cfunc_type_data *data = DATA_PTR(self);
    return (get_cfunc_pointer_data(data) == NULL) ? mrb_true_value() : mrb_false_value();
}