forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.cpp
65 lines (47 loc) · 1.61 KB
/
data.cpp
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
61
62
63
64
65
#include "objectmemory.hpp"
#include "builtin/class.hpp"
#include "builtin/data.hpp"
#include "capi/capi.hpp"
#include "capi/ruby.h"
using namespace rubinius;
using namespace rubinius::capi;
namespace rubinius {
namespace capi {
void flush_cached_rdata(NativeMethodEnvironment* env, Handle* handle) {
if(handle->is_rdata()) {
Data* data = c_as<Data>(handle->object());
RData* rdata = handle->as_rdata(env);
data->mark(env->state(), rdata->dmark);
data->free(env->state(), rdata->dfree);
data->data(env->state(), rdata->data);
}
}
RData* Handle::as_rdata(NativeMethodEnvironment* env) {
if(type_ != cRData) {
Data* data = c_as<Data>(object());
type_ = cRData;
as_.rdata = reinterpret_cast<RData*>(data->exposed());
}
env->state()->om->remember_object(object());
return as_.rdata;
}
}
}
extern "C" {
struct RData* capi_rdata_struct(VALUE data_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Handle* handle = Handle::from(data_handle);
return handle->as_rdata(env);
}
VALUE rb_data_object_alloc(VALUE klass, void* ptr,
RUBY_DATA_FUNC mark, RUBY_DATA_FUNC free) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Class* data_klass = c_as<Class>(env->get_object(klass));
Data* data = Data::create(env->state(), ptr, mark, free);
data->klass(env->state(), data_klass);
// Data objects are directly manipulated, so we have to always
// track them.
env->state()->om->remember_object(data);
return env->get_handle(data);
}
}