-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
bytebuffer.c
428 lines (334 loc) · 12.1 KB
/
bytebuffer.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "nio4r.h"
static VALUE mNIO = Qnil;
static VALUE cNIO_ByteBuffer = Qnil;
static VALUE cNIO_ByteBuffer_OverflowError = Qnil;
static VALUE cNIO_ByteBuffer_UnderflowError = Qnil;
static VALUE cNIO_ByteBuffer_MarkUnsetError = Qnil;
/* Allocator/deallocator */
static VALUE NIO_ByteBuffer_allocate(VALUE klass);
static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *byteBuffer);
static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *byteBuffer);
/* Methods */
static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity);
static VALUE NIO_ByteBuffer_clear(VALUE self);
static VALUE NIO_ByteBuffer_get_position(VALUE self);
static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position);
static VALUE NIO_ByteBuffer_get_limit(VALUE self);
static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit);
static VALUE NIO_ByteBuffer_capacity(VALUE self);
static VALUE NIO_ByteBuffer_remaining(VALUE self);
static VALUE NIO_ByteBuffer_full(VALUE self);
static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self);
static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index);
static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string);
static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE file);
static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE file);
static VALUE NIO_ByteBuffer_flip(VALUE self);
static VALUE NIO_ByteBuffer_rewind(VALUE self);
static VALUE NIO_ByteBuffer_mark(VALUE self);
static VALUE NIO_ByteBuffer_reset(VALUE self);
static VALUE NIO_ByteBuffer_compact(VALUE self);
static VALUE NIO_ByteBuffer_each(VALUE self);
static VALUE NIO_ByteBuffer_inspect(VALUE self);
#define MARK_UNSET -1
void Init_NIO_ByteBuffer()
{
mNIO = rb_define_module("NIO");
cNIO_ByteBuffer = rb_define_class_under(mNIO, "ByteBuffer", rb_cObject);
rb_define_alloc_func(cNIO_ByteBuffer, NIO_ByteBuffer_allocate);
cNIO_ByteBuffer_OverflowError = rb_define_class_under(cNIO_ByteBuffer, "OverflowError", rb_eIOError);
cNIO_ByteBuffer_UnderflowError = rb_define_class_under(cNIO_ByteBuffer, "UnderflowError", rb_eIOError);
cNIO_ByteBuffer_MarkUnsetError = rb_define_class_under(cNIO_ByteBuffer, "MarkUnsetError", rb_eIOError);
rb_include_module(cNIO_ByteBuffer, rb_mEnumerable);
rb_define_method(cNIO_ByteBuffer, "initialize", NIO_ByteBuffer_initialize, 1);
rb_define_method(cNIO_ByteBuffer, "clear", NIO_ByteBuffer_clear, 0);
rb_define_method(cNIO_ByteBuffer, "position", NIO_ByteBuffer_get_position, 0);
rb_define_method(cNIO_ByteBuffer, "position=", NIO_ByteBuffer_set_position, 1);
rb_define_method(cNIO_ByteBuffer, "limit", NIO_ByteBuffer_get_limit, 0);
rb_define_method(cNIO_ByteBuffer, "limit=", NIO_ByteBuffer_set_limit, 1);
rb_define_method(cNIO_ByteBuffer, "capacity", NIO_ByteBuffer_capacity, 0);
rb_define_method(cNIO_ByteBuffer, "size", NIO_ByteBuffer_capacity, 0);
rb_define_method(cNIO_ByteBuffer, "remaining", NIO_ByteBuffer_remaining, 0);
rb_define_method(cNIO_ByteBuffer, "full?", NIO_ByteBuffer_full, 0);
rb_define_method(cNIO_ByteBuffer, "get", NIO_ByteBuffer_get, -1);
rb_define_method(cNIO_ByteBuffer, "[]", NIO_ByteBuffer_fetch, 1);
rb_define_method(cNIO_ByteBuffer, "<<", NIO_ByteBuffer_put, 1);
rb_define_method(cNIO_ByteBuffer, "read_from", NIO_ByteBuffer_read_from, 1);
rb_define_method(cNIO_ByteBuffer, "write_to", NIO_ByteBuffer_write_to, 1);
rb_define_method(cNIO_ByteBuffer, "flip", NIO_ByteBuffer_flip, 0);
rb_define_method(cNIO_ByteBuffer, "rewind", NIO_ByteBuffer_rewind, 0);
rb_define_method(cNIO_ByteBuffer, "mark", NIO_ByteBuffer_mark, 0);
rb_define_method(cNIO_ByteBuffer, "reset", NIO_ByteBuffer_reset, 0);
rb_define_method(cNIO_ByteBuffer, "compact", NIO_ByteBuffer_compact, 0);
rb_define_method(cNIO_ByteBuffer, "each", NIO_ByteBuffer_each, 0);
rb_define_method(cNIO_ByteBuffer, "inspect", NIO_ByteBuffer_inspect, 0);
}
static VALUE NIO_ByteBuffer_allocate(VALUE klass)
{
struct NIO_ByteBuffer *bytebuffer = (struct NIO_ByteBuffer *)xmalloc(sizeof(struct NIO_ByteBuffer));
bytebuffer->buffer = NULL;
return Data_Wrap_Struct(klass, NIO_ByteBuffer_gc_mark, NIO_ByteBuffer_free, bytebuffer);
}
static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *buffer)
{
}
static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *buffer)
{
if (buffer->buffer)
xfree(buffer->buffer);
xfree(buffer);
}
static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
buffer->capacity = NUM2INT(capacity);
buffer->buffer = xmalloc(buffer->capacity);
NIO_ByteBuffer_clear(self);
return self;
}
static VALUE NIO_ByteBuffer_clear(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
memset(buffer->buffer, 0, buffer->capacity);
buffer->position = 0;
buffer->limit = buffer->capacity;
buffer->mark = MARK_UNSET;
return self;
}
static VALUE NIO_ByteBuffer_get_position(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
return INT2NUM(buffer->position);
}
static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position)
{
int pos;
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
pos = NUM2INT(new_position);
if (pos < 0) {
rb_raise(rb_eArgError, "negative position given");
}
if (pos > buffer->limit) {
rb_raise(rb_eArgError, "specified position exceeds limit");
}
buffer->position = pos;
if (buffer->mark > buffer->position) {
buffer->mark = MARK_UNSET;
}
return new_position;
}
static VALUE NIO_ByteBuffer_get_limit(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
return INT2NUM(buffer->limit);
}
static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit)
{
int lim;
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
lim = NUM2INT(new_limit);
if (lim < 0) {
rb_raise(rb_eArgError, "negative limit given");
}
if (lim > buffer->capacity) {
rb_raise(rb_eArgError, "specified limit exceeds capacity");
}
buffer->limit = lim;
if (buffer->position > lim) {
buffer->position = lim;
}
if (buffer->mark > lim) {
buffer->mark = MARK_UNSET;
}
return new_limit;
}
static VALUE NIO_ByteBuffer_capacity(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
return INT2NUM(buffer->capacity);
}
static VALUE NIO_ByteBuffer_remaining(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
return INT2NUM(buffer->limit - buffer->position);
}
static VALUE NIO_ByteBuffer_full(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
return buffer->position == buffer->limit ? Qtrue : Qfalse;
}
static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self)
{
int len;
VALUE length, result;
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
rb_scan_args(argc, argv, "01", &length);
if (length == Qnil) {
len = buffer->limit - buffer->position;
} else {
len = NUM2INT(length);
}
if (len < 0) {
rb_raise(rb_eArgError, "negative length given");
}
if (len > buffer->limit - buffer->position) {
rb_raise(cNIO_ByteBuffer_UnderflowError, "not enough data in buffer");
}
result = rb_str_new(buffer->buffer + buffer->position, len);
buffer->position += len;
return result;
}
static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index)
{
int i;
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
i = NUM2INT(index);
if (i < 0) {
rb_raise(rb_eArgError, "negative index given");
}
if (i >= buffer->limit) {
rb_raise(rb_eArgError, "specified index exceeds limit");
}
return INT2NUM(buffer->buffer[i]);
}
static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string)
{
long length;
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
StringValue(string);
length = RSTRING_LEN(string);
if (length > buffer->limit - buffer->position) {
rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
}
memcpy(buffer->buffer + buffer->position, StringValuePtr(string), length);
buffer->position += length;
return self;
}
static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)
{
struct NIO_ByteBuffer *buffer;
rb_io_t *fptr;
ssize_t nbytes, bytes_read;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
rb_io_set_nonblock(fptr);
nbytes = buffer->limit - buffer->position;
if (nbytes == 0) {
rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
}
bytes_read = read(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
if (bytes_read < 0) {
if (errno == EAGAIN) {
return INT2NUM(0);
} else {
rb_sys_fail("write");
}
}
buffer->position += bytes_read;
return INT2NUM(bytes_read);
}
static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io)
{
struct NIO_ByteBuffer *buffer;
rb_io_t *fptr;
ssize_t nbytes, bytes_written;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
rb_io_set_nonblock(fptr);
nbytes = buffer->limit - buffer->position;
if (nbytes == 0) {
rb_raise(cNIO_ByteBuffer_UnderflowError, "no data remaining in buffer");
}
bytes_written = write(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
if (bytes_written < 0) {
if (errno == EAGAIN) {
return INT2NUM(0);
} else {
rb_sys_fail("write");
}
}
buffer->position += bytes_written;
return INT2NUM(bytes_written);
}
static VALUE NIO_ByteBuffer_flip(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
buffer->limit = buffer->position;
buffer->position = 0;
buffer->mark = MARK_UNSET;
return self;
}
static VALUE NIO_ByteBuffer_rewind(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
buffer->position = 0;
buffer->mark = MARK_UNSET;
return self;
}
static VALUE NIO_ByteBuffer_mark(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
buffer->mark = buffer->position;
return self;
}
static VALUE NIO_ByteBuffer_reset(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
if (buffer->mark < 0) {
rb_raise(cNIO_ByteBuffer_MarkUnsetError, "mark has not been set");
} else {
buffer->position = buffer->mark;
}
return self;
}
static VALUE NIO_ByteBuffer_compact(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
memmove(buffer->buffer, buffer->buffer + buffer->position, buffer->limit - buffer->position);
buffer->position = buffer->limit - buffer->position;
buffer->limit = buffer->capacity;
return self;
}
static VALUE NIO_ByteBuffer_each(VALUE self)
{
int i;
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
if (rb_block_given_p()) {
for (i = 0; i < buffer->limit; i++) {
rb_yield(INT2NUM(buffer->buffer[i]));
}
} else {
rb_raise(rb_eArgError, "no block given");
}
return self;
}
static VALUE NIO_ByteBuffer_inspect(VALUE self)
{
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
return rb_sprintf(
"#<%s:%p @position=%d @limit=%d @capacity=%d>",
rb_class2name(CLASS_OF(self)),
(void *)self,
buffer->position,
buffer->limit,
buffer->capacity);
}