-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuffer.c
More file actions
156 lines (128 loc) · 4.27 KB
/
buffer.c
File metadata and controls
156 lines (128 loc) · 4.27 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "buffer.h"
#include "frame.h"
typedef struct {
VALUE *profile_frames;
VALUE *frames;
int *lines;
int length, capacity;
} buffer_t;
static void buffer_mark(void *ptr)
{
buffer_t *buffer = ptr;
for (int i = 0; i < buffer->length; i++) {
rb_gc_mark(buffer->profile_frames[i]);
}
for (int i = 0; i < buffer->capacity; i++) {
rb_gc_mark(buffer->frames[i]);
}
}
static void buffer_free(void *ptr)
{
buffer_t *buffer = ptr;
ruby_xfree(buffer->profile_frames);
ruby_xfree(buffer->frames);
ruby_xfree(buffer->lines);
}
static size_t buffer_memsize(const void *ptr)
{
return sizeof(buffer_t);
}
const rb_data_type_t buffer_data_type = {
"stack_frames_buffer",
{ buffer_mark, buffer_free, buffer_memsize, },
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
};
static VALUE buffer_allocate(VALUE klass) {
VALUE obj;
buffer_t *buffer;
obj = TypedData_Make_Struct(klass, buffer_t, &buffer_data_type, buffer);
return obj;
}
static VALUE buffer_initialize(VALUE self, VALUE size) {
int capacity = NUM2INT(size);
buffer_t *buffer;
if (capacity <= 0) {
rb_raise(rb_eArgError, "non-positive buffer capacity");
}
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, buffer);
buffer->profile_frames = ALLOC_N(VALUE, capacity);
buffer->lines = ALLOC_N(int, capacity);
buffer->frames = ALLOC_N(VALUE, capacity);
buffer->capacity = 0;
for (int i = 0; i < capacity; i++) {
buffer->frames[i] = stack_frame_new(self, i);
buffer->capacity++;
}
return Qnil;
}
static VALUE buffer_capture(VALUE self) {
buffer_t *buffer;
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, buffer);
buffer->length = rb_profile_frames(0, buffer->capacity, buffer->profile_frames, buffer->lines);
return INT2NUM(buffer->length);
}
static VALUE buffer_length(VALUE self) {
buffer_t *buffer;
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, buffer);
return INT2NUM(buffer->length);
}
static VALUE buffer_capacity(VALUE self) {
buffer_t *buffer;
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, buffer);
return INT2NUM(buffer->capacity);
}
static VALUE buffer_aref(VALUE self, VALUE ruby_index) {
buffer_t *buffer;
int index = NUM2INT(ruby_index);
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, buffer);
if (index >= 0 && index < buffer->length) {
return buffer->frames[index];
}
return Qnil;
}
static VALUE buffer_each(VALUE self) {
buffer_t *buffer;
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, buffer);
for (int i = 0; i < buffer->length; i++) {
rb_yield(buffer->frames[i]);
}
return Qnil;
}
static VALUE buffer_find(VALUE self) {
buffer_t *buffer;
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, buffer);
for (int i = 0; i < buffer->length; i++) {
VALUE frame = buffer->frames[i];
if (RTEST(rb_yield(frame))) {
return frame;
}
}
return Qnil;
}
static void raise_frame_reference_error()
{
rb_raise(rb_eRuntimeError, "Stack frame is no longer valid, its buffer was re-used for another capture");
}
VALUE stack_buffer_profile_frame(VALUE buffer_obj, int index) {
buffer_t *buffer;
TypedData_Get_Struct(buffer_obj, buffer_t, &buffer_data_type, buffer);
if (index >= buffer->length) raise_frame_reference_error();
return buffer->profile_frames[index];
}
int stack_buffer_frame_lineno(VALUE buffer_obj, int index) {
buffer_t *buffer;
TypedData_Get_Struct(buffer_obj, buffer_t, &buffer_data_type, buffer);
if (index >= buffer->length) raise_frame_reference_error();
return buffer->lines[index];
}
void stack_buffer_define(VALUE mStackFrames) {
VALUE cBuffer = rb_define_class_under(mStackFrames, "Buffer", rb_cObject);
rb_define_alloc_func(cBuffer, buffer_allocate);
rb_define_method(cBuffer, "initialize", buffer_initialize, 1);
rb_define_method(cBuffer, "length", buffer_length, 0);
rb_define_method(cBuffer, "capacity", buffer_capacity, 0);
rb_define_method(cBuffer, "capture", buffer_capture, 0);
rb_define_method(cBuffer, "[]", buffer_aref, 1);
rb_define_method(cBuffer, "each", buffer_each, 0);
rb_define_method(cBuffer, "find", buffer_find, 0);
}