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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
/*
* Copyright (C) 2007 Tony Arcieri
* You may redistribute this under the terms of the Ruby license.
* See LICENSE for details
*/
#include "ruby.h"
#include "rubyio.h"
#include <assert.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#ifndef GetReadFile
#define FPTR_TO_FD(fptr) (fptr->fd)
#else
#define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
#endif
/* Default number of bytes in each node's buffer */
#define DEFAULT_NODE_SIZE 16384
/* Maximum age of a buffer node in a memory pool, in seconds */
#define MAX_AGE 60
/* How often to scan the pool for old nodes */
#define PURGE_INTERVAL 10
struct buffer {
time_t last_purged_at;
unsigned size, node_size;
struct buffer_node *head, *tail;
struct buffer_node *pool_head, *pool_tail;
};
struct buffer_node {
time_t last_used_at;
unsigned start, end;
struct buffer_node *next;
unsigned char data[0];
};
static VALUE mEm = Qnil;
static VALUE cEm_Buffer = Qnil;
static VALUE Em_Buffer_allocate(VALUE klass);
static void Em_Buffer_mark(struct buffer *);
static void Em_Buffer_free(struct buffer *);
static VALUE Em_Buffer_initialize(int argc, VALUE *argv, VALUE self);
static VALUE Em_Buffer_clear(VALUE self);
static VALUE Em_Buffer_size(VALUE self);
static VALUE Em_Buffer_empty(VALUE self);
static VALUE Em_Buffer_append(VALUE self, VALUE data);
static VALUE Em_Buffer_prepend(VALUE self, VALUE data);
static VALUE Em_Buffer_read(int argc, VALUE *argv, VALUE self);
static VALUE Em_Buffer_to_str(VALUE self);
static VALUE Em_Buffer_read_from(VALUE self, VALUE io);
static VALUE Em_Buffer_write_to(VALUE self, VALUE io);
static struct buffer *buffer_new(void);
static void buffer_clear(struct buffer *buf);
static void buffer_free(struct buffer *buf);
static void buffer_gc(struct buffer *buf);
static void buffer_prepend(struct buffer *buf, char *str, unsigned len);
static void buffer_append(struct buffer *buf, char *str, unsigned len);
static void buffer_read(struct buffer *buf, char *str, unsigned len);
static void buffer_copy(struct buffer *buf, char *str, unsigned len);
static int buffer_read_from(struct buffer *buf, int fd);
static int buffer_write_to(struct buffer *buf, int fd);
/*
* High speed buffering geared towards non-blocking I/O.
*
* Data is stored in a byte queue implemented as a linked list of equal size
* chunks. Since every node in the list is the same size they are easily
* memory pooled. Routines are provided for high speed non-blocking reads
* and writes from Ruby IO objects.
*/
void Init_em_buffer()
{
mEm = rb_define_module("EventMachine");
cEm_Buffer = rb_define_class_under(mEm, "Buffer", rb_cObject);
rb_define_alloc_func(cEm_Buffer, Em_Buffer_allocate);
rb_define_method(cEm_Buffer, "initialize", Em_Buffer_initialize, -1);
rb_define_method(cEm_Buffer, "clear", Em_Buffer_clear, 0);
rb_define_method(cEm_Buffer, "size", Em_Buffer_size, 0);
rb_define_method(cEm_Buffer, "empty?", Em_Buffer_empty, 0);
rb_define_method(cEm_Buffer, "<<", Em_Buffer_append, 1);
rb_define_method(cEm_Buffer, "append", Em_Buffer_append, 1);
rb_define_method(cEm_Buffer, "prepend", Em_Buffer_prepend, 1);
rb_define_method(cEm_Buffer, "read", Em_Buffer_read, -1);
rb_define_method(cEm_Buffer, "to_str", Em_Buffer_to_str, 0);
rb_define_method(cEm_Buffer, "read_from", Em_Buffer_read_from, 1);
rb_define_method(cEm_Buffer, "write_to", Em_Buffer_write_to, 1);
}
static VALUE Em_Buffer_allocate(VALUE klass)
{
return Data_Wrap_Struct(klass, Em_Buffer_mark, Em_Buffer_free, buffer_new());
}
static void Em_Buffer_mark(struct buffer *buf)
{
/* Walks the pool of unused chunks and frees any that are beyond a certain age */
buffer_gc(buf);
}
static void Em_Buffer_free(struct buffer *buf)
{
buffer_free(buf);
}
/**
* call-seq:
* EventMachine::Buffer.new(size = DEFAULT_NODE_SIZE) -> EventMachine::Buffer
*
* Create a new EventMachine::Buffer with linked segments of the given size
*/
static VALUE Em_Buffer_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE node_size_obj;
int node_size;
struct buffer *buf;
if(rb_scan_args(argc, argv, "01", &node_size_obj) == 1) {
node_size = NUM2INT(node_size_obj);
if(node_size < 1) rb_raise(rb_eArgError, "invalid buffer size");
Data_Get_Struct(self, struct buffer, buf);
/* Make sure we're not changing the buffer size after data has been allocated */
assert(!buf->head);
assert(!buf->pool_head);
buf->node_size = node_size;
}
return Qnil;
}
/**
* call-seq:
* EventMachine::Buffer#clear -> nil
*
* Clear all data from the EventMachine::Buffer
*/
static VALUE Em_Buffer_clear(VALUE self)
{
struct buffer *buf;
Data_Get_Struct(self, struct buffer, buf);
buffer_clear(buf);
return Qnil;
}
/**
* call-seq:
* EventMachine::Buffer#size -> Integer
*
* Return the size of the buffer in bytes
*/
static VALUE Em_Buffer_size(VALUE self)
{
struct buffer *buf;
Data_Get_Struct(self, struct buffer, buf);
return INT2NUM(buf->size);
}
/**
* call-seq:
* EventMachine::Buffer#empty? -> Boolean
*
* Is the buffer empty?
*/
static VALUE Em_Buffer_empty(VALUE self)
{
struct buffer *buf;
Data_Get_Struct(self, struct buffer, buf);
return buf->size > 0 ? Qfalse : Qtrue;
}
/**
* call-seq:
* EventMachine::Buffer#append(data) -> String
*
* Append the given data to the end of the buffer
*/
static VALUE Em_Buffer_append(VALUE self, VALUE data)
{
struct buffer *buf;
Data_Get_Struct(self, struct buffer, buf);
/* Is this needed? Never seen anyone else do it... */
data = rb_convert_type(data, T_STRING, "String", "to_str");
buffer_append(buf, RSTRING_PTR(data), RSTRING_LEN(data));
return data;
}
/**
* call-seq:
* EventMachine::Buffer#prepend(data) -> String
*
* Prepend the given data to the beginning of the buffer
*/
static VALUE Em_Buffer_prepend(VALUE self, VALUE data)
{
struct buffer *buf;
Data_Get_Struct(self, struct buffer, buf);
data = rb_convert_type(data, T_STRING, "String", "to_str");
buffer_prepend(buf, RSTRING_PTR(data), RSTRING_LEN(data));
return data;
}
/**
* call-seq:
* EventMachine::Buffer#read(length = nil) -> String
*
* Read the specified abount of data from the buffer. If no value
* is given the entire contents of the buffer are returned. Any data
* read from the buffer is cleared.
*/
static VALUE Em_Buffer_read(int argc, VALUE *argv, VALUE self)
{
VALUE length_obj, str;
int length;
struct buffer *buf;
Data_Get_Struct(self, struct buffer, buf);
if(rb_scan_args(argc, argv, "01", &length_obj) == 1) {
length = NUM2INT(length_obj);
} else {
if(buf->size == 0)
return rb_str_new2("");
length = buf->size;
}
if(length > buf->size)
length = buf->size;
if(length < 1)
rb_raise(rb_eArgError, "length must be greater than zero");
str = rb_str_new(0, length);
buffer_read(buf, RSTRING_PTR(str), length);
return str;
}
/**
* call-seq:
* EventMachine::Buffer#to_str -> String
*
* Convert the Buffer to a String. The original buffer is unmodified.
*/
static VALUE Em_Buffer_to_str(VALUE self) {
VALUE str;
struct buffer *buf;
Data_Get_Struct(self, struct buffer, buf);
str = rb_str_new(0, buf->size);
buffer_copy(buf, RSTRING_PTR(str), buf->size);
return str;
}
/**
* call-seq:
* EventMachine::Buffer#read_from(io) -> Integer
*
* Perform a nonblocking read of the the given IO object and fill
* the buffer with any data received. The call will read as much
* data as it can until the read would block.
*/
static VALUE Em_Buffer_read_from(VALUE self, VALUE io) {
struct buffer *buf;
#if HAVE_RB_IO_T
rb_io_t *fptr;
#else
OpenFile *fptr;
#endif
Data_Get_Struct(self, struct buffer, buf);
GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
rb_io_set_nonblock(fptr);
#ifdef HAVE_RB_IO_FD
return INT2NUM(buffer_read_from(buf, rb_io_fd(io)));
#else
return INT2NUM(buffer_read_from(buf, FPTR_TO_FD(fptr)));
#endif
}
/**
* call-seq:
* EventMachine::Buffer#write_to(io) -> Integer
*
* Perform a nonblocking write of the buffer to the given IO object.
* As much data as possible is written until the call would block.
* Any data which is written is removed from the buffer.
*/
static VALUE Em_Buffer_write_to(VALUE self, VALUE io) {
struct buffer *buf;
#if HAVE_RB_IO_T
rb_io_t *fptr;
#else
OpenFile *fptr;
#endif
Data_Get_Struct(self, struct buffer, buf);
GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
rb_io_set_nonblock(fptr);
#ifdef HAVE_RB_IO_FD
return INT2NUM(buffer_read_from(buf, rb_io_fd(io)));
#else
return INT2NUM(buffer_read_from(buf, FPTR_TO_FD(fptr)));
#endif
}
/*
* Ruby bindings end here. Below is the actual implementation of
* the underlying data structures.
*/
/* Create a new buffer */
static struct buffer *buffer_new(void)
{
struct buffer *buf;
buf = (struct buffer *)xmalloc(sizeof(struct buffer));
buf->head = buf->tail = buf->pool_head = buf->pool_tail = 0;
buf->size = 0;
buf->node_size = DEFAULT_NODE_SIZE;
time(&buf->last_purged_at);
return buf;
}
/* Clear all data from a buffer */
static void buffer_clear(struct buffer *buf)
{
struct buffer_node *tmp;
/* Move everything into the buffer pool */
if(!buf->pool_tail)
buf->pool_head = buf->pool_tail = buf->head;
else
buf->pool_tail->next = buf->head;
buf->head = buf->tail = 0;
buf->size = 0;
}
/* Free a buffer */
static void buffer_free(struct buffer *buf)
{
struct buffer_node *tmp;
buffer_clear(buf);
while(buf->pool_head) {
tmp = buf->pool_head;
buf->pool_head = tmp->next;
free(tmp);
}
free(buf);
}
/* Run through the pool and find elements that haven't been used for awhile */
static void buffer_gc(struct buffer *buf)
{
struct buffer_node *cur, *tmp;
time_t now;
time(&now);
/* Only purge if we've passed the purge interval */
if(now - buf->last_purged_at < PURGE_INTERVAL)
return;
buf->last_purged_at = now;
while(buf->pool_head && now - buf->pool_head->last_used_at >= MAX_AGE) {
tmp = buf->pool_head;
buf->pool_head = buf->pool_head->next;
free(tmp);
}
if(!buf->pool_head)
buf->pool_tail = 0;
}
/* Create a new buffer_node (or pull one from the memory pool) */
static struct buffer_node *buffer_node_new(struct buffer *buf)
{
struct buffer_node *node;
/* Pull from the memory pool if available */
if(buf->pool_head) {
node = buf->pool_head;
buf->pool_head = node->next;
if(node->next)
node->next = 0;
else
buf->pool_tail = 0;
} else {
node = (struct buffer_node *)xmalloc(sizeof(struct buffer_node) + buf->node_size);
node->next = 0;
}
node->start = node->end = 0;
return node;
}
/* Free a buffer node (i.e. return it to the memory pool) */
static void buffer_node_free(struct buffer *buf, struct buffer_node *node)
{
/* Store when the node was freed */
time(&node->last_used_at);
node->next = buf->pool_head;
buf->pool_head = node;
if(!buf->pool_tail)
buf->pool_tail = node;
}
/* Prepend data to the front of the buffer */
static void buffer_prepend(struct buffer *buf, char *str, unsigned len)
{
struct buffer_node *node, *tmp;
buf->size += len;
/* If it fits in the beginning of the head */
if(buf->head && buf->head->start >= len) {
buf->head->start -= len;
memcpy(buf->head->data + buf->head->start, str, len);
} else {
node = buffer_node_new(buf);
node->next = buf->head;
buf->head = node;
if(!buf->tail) buf->tail = node;
while(len > buf->node_size) {
memcpy(node->data, str, buf->node_size);
node->end = buf->node_size;
tmp = buffer_node_new(buf);
tmp->next = node->next;
node->next = tmp;
if(buf->tail == node) buf->tail = tmp;
node = tmp;
str += buf->node_size;
len -= buf->node_size;
}
if(len > 0) {
memcpy(node->data, str, len);
node->end = len;
}
}
}
/* Append data to the front of the buffer */
static void buffer_append(struct buffer *buf, char *str, unsigned len)
{
unsigned nbytes;
buf->size += len;
/* If it fits in the remaining space in the tail */
if(buf->tail && len <= buf->node_size - buf->tail->end) {
memcpy(buf->tail->data + buf->tail->end, str, len);
buf->tail->end += len;
return;
}
/* Empty list needs initialized */
if(!buf->head) {
buf->head = buffer_node_new(buf);
buf->tail = buf->head;
}
/* Build links out of the data */
while(len > 0) {
nbytes = buf->node_size - buf->tail->end;
if(len < nbytes) nbytes = len;
memcpy(buf->tail->data + buf->tail->end, str, nbytes);
str += nbytes;
len -= nbytes;
buf->tail->end += nbytes;
if(len > 0) {
buf->tail->next = buffer_node_new(buf);
buf->tail = buf->tail->next;
}
}
}
/* Read data from the buffer (and clear what we've read) */
static void buffer_read(struct buffer *buf, char *str, unsigned len)
{
unsigned nbytes;
struct buffer_node *tmp;
while(buf->size > 0 && len > 0) {
nbytes = buf->head->end - buf->head->start;
if(len < nbytes) nbytes = len;
memcpy(str, buf->head->data + buf->head->start, nbytes);
str += nbytes;
len -= nbytes;
buf->head->start += nbytes;
buf->size -= nbytes;
if(buf->head->start == buf->head->end) {
tmp = buf->head;
buf->head = tmp->next;
buffer_node_free(buf, tmp);
if(!buf->head) buf->tail = 0;
}
}
}
/* Copy data from the buffer without clearing it */
static void buffer_copy(struct buffer *buf, char *str, unsigned len)
{
unsigned nbytes;
struct buffer_node *node;
node = buf->head;
while(node && len > 0) {
nbytes = node->end - node->start;
if(len < nbytes) nbytes = len;
memcpy(str, node->data + node->start, nbytes);
str += nbytes;
len -= nbytes;
if(node->start + nbytes == node->end)
node = node->next;
}
}
/* Write data from the buffer to a file descriptor */
static int buffer_write_to(struct buffer *buf, int fd)
{
int bytes_written, total_bytes_written = 0;
struct buffer_node *tmp;
while(buf->head) {
bytes_written = write(fd, buf->head->data + buf->head->start, buf->head->end - buf->head->start);
/* If the write failed... */
if(bytes_written < 0) {
if(errno != EAGAIN)
rb_sys_fail("write");
return total_bytes_written;
}
total_bytes_written += bytes_written;
buf->size -= bytes_written;
/* If the write blocked... */
if(bytes_written < buf->head->end - buf->head->start) {
buf->head->start += bytes_written;
return total_bytes_written;
}
/* Otherwise we wrote the whole buffer */
tmp = buf->head;
buf->head = tmp->next;
buffer_node_free(buf, tmp);
if(!buf->head) buf->tail = 0;
}
return total_bytes_written;
}
/* Read data from a file descriptor to a buffer */
/* Append data to the front of the buffer */
static int buffer_read_from(struct buffer *buf, int fd)
{
int bytes_read, total_bytes_read = 0;
unsigned nbytes;
/* Empty list needs initialized */
if(!buf->head) {
buf->head = buffer_node_new(buf);
buf->tail = buf->head;
}
do {
nbytes = buf->node_size - buf->tail->end;
bytes_read = read(fd, buf->tail->data + buf->tail->end, nbytes);
if(bytes_read < 1) {
if(errno != EAGAIN)
rb_sys_fail("read");
return total_bytes_read;
}
total_bytes_read += bytes_read;
buf->tail->end += nbytes;
buf->size += nbytes;
if(buf->tail->end == buf->node_size) {
buf->tail->next = buffer_node_new(buf);
buf->tail = buf->tail->next;
}
} while(bytes_read == nbytes);
return total_bytes_read;
}
|