/
io_buffer.c
152 lines (113 loc) · 3.03 KB
/
io_buffer.c
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
#include "ruby.h"
#include <sys/types.h>
struct buf_int {
uint8_t* top;
uint8_t* cur;
size_t size;
};
#define BUF_DEFAULT_SIZE 4096
#define BUF_TOLERANCE 32
static void buf_free(struct buf_int* internal) {
free(internal->top);
free(internal);
}
static VALUE buf_alloc(VALUE self) {
VALUE buf;
struct buf_int* internal;
buf = Data_Make_Struct(self, struct buf_int, 0, buf_free, internal);
internal->size = BUF_DEFAULT_SIZE;
internal->top = malloc(BUF_DEFAULT_SIZE);
internal->cur = internal->top;
return buf;
}
static VALUE buf_append(VALUE self, VALUE str) {
struct buf_int* b;
size_t used, str_len, new_size;
Data_Get_Struct(self, struct buf_int, b);
used = b->cur - b->top;
StringValue(str);
str_len = RSTRING_LEN(str);
new_size = used + str_len;
if(new_size > b->size) {
size_t n = b->size + (b->size / 2);
uint8_t* top;
uint8_t* old;
new_size = (n > new_size ? n : new_size + BUF_TOLERANCE);
top = malloc(new_size);
old = b->top;
memcpy(top, old, used);
b->top = top;
b->cur = top + used;
b->size = new_size;
free(old);
}
memcpy(b->cur, RSTRING_PTR(str), str_len);
b->cur += str_len;
return self;
}
static VALUE buf_append2(int argc, VALUE* argv, VALUE self) {
struct buf_int* b;
size_t used, new_size;
int i;
VALUE str;
Data_Get_Struct(self, struct buf_int, b);
used = b->cur - b->top;
new_size = used;
for(i = 0; i < argc; i++) {
StringValue(argv[i]);
str = argv[i];
new_size += RSTRING_LEN(str);
}
if(new_size > b->size) {
size_t n = b->size + (b->size / 2);
uint8_t* top;
uint8_t* old;
new_size = (n > new_size ? n : new_size + BUF_TOLERANCE);
top = malloc(new_size);
old = b->top;
memcpy(top, old, used);
b->top = top;
b->cur = top + used;
b->size = new_size;
free(old);
}
for(i = 0; i < argc; i++) {
long str_len;
str = argv[i];
str_len = RSTRING_LEN(str);
memcpy(b->cur, RSTRING_PTR(str), str_len);
b->cur += str_len;
}
}
static VALUE buf_to_str(VALUE self) {
struct buf_int* b;
Data_Get_Struct(self, struct buf_int, b);
return rb_str_new(b->top, b->cur - b->top);
}
static VALUE buf_used(VALUE self) {
struct buf_int* b;
Data_Get_Struct(self, struct buf_int, b);
return INT2FIX(b->cur - b->top);
}
static VALUE buf_capa(VALUE self) {
struct buf_int* b;
Data_Get_Struct(self, struct buf_int, b);
return INT2FIX(b->size);
}
static VALUE buf_reset(VALUE self) {
struct buf_int* b;
Data_Get_Struct(self, struct buf_int, b);
b->cur = b->top;
return self;
}
void Init_io_buffer(VALUE puma) {
VALUE buf = rb_define_class_under(puma, "IOBuffer", rb_cObject);
rb_define_alloc_func(buf, buf_alloc);
rb_define_method(buf, "<<", buf_append, 1);
rb_define_method(buf, "append", buf_append2, -1);
rb_define_method(buf, "to_str", buf_to_str, 0);
rb_define_method(buf, "to_s", buf_to_str, 0);
rb_define_method(buf, "used", buf_used, 0);
rb_define_method(buf, "capacity", buf_capa, 0);
rb_define_method(buf, "reset", buf_reset, 0);
}