File: AutoPointer.c

package info (click to toggle)
libffi-ruby 0.6.3debian-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,072 kB
  • ctags: 1,458
  • sloc: ansic: 5,824; ruby: 5,402; xml: 144; sh: 73; makefile: 7
file content (60 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include <ruby.h>
#include "rbffi.h"
#include "AbstractMemory.h"
#include "Pointer.h"
#include "AutoPointer.h"

typedef struct AutoPointer {
    AbstractMemory memory;
    VALUE parent;
} AutoPointer;

static void autoptr_mark(AutoPointer* ptr);
static VALUE autoptr_allocate(VALUE klass);
static VALUE autoptr_set_parent(VALUE self, VALUE parent);

VALUE rbffi_AutoPointerClass = Qnil;

static VALUE
autoptr_allocate(VALUE klass)
{
    AutoPointer* p;
    VALUE obj = Data_Make_Struct(klass, AutoPointer, autoptr_mark, -1, p);
    p->parent = Qnil;
    p->memory.access = MEM_RD | MEM_WR;

    return obj;
}

static VALUE
autoptr_set_parent(VALUE self, VALUE parent)
{
    AutoPointer* p;
    AbstractMemory* ptr = rbffi_AbstractMemory_Cast(parent, rbffi_PointerClass);

    Data_Get_Struct(self, AutoPointer, p);
    p->memory = *ptr;
    p->parent = parent;

    return self;
}

static void
autoptr_mark(AutoPointer* ptr)
{
    rb_gc_mark(ptr->parent);
}

void
rbffi_AutoPointer_Init(VALUE moduleFFI)
{
    rbffi_AutoPointerClass = rb_define_class_under(moduleFFI, "AutoPointer", rbffi_PointerClass);
    rb_global_variable(&rbffi_AutoPointerClass);
    
    rb_define_alloc_func(rbffi_AutoPointerClass, autoptr_allocate);
    rb_define_protected_method(rbffi_AutoPointerClass, "parent=", autoptr_set_parent, 1);
}