diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2009-12-15 11:22:36 +0300 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2009-12-29 23:12:30 +0300 |
commit | 469e2648e59c19154fd67ae1df17487f673fe9fc (patch) | |
tree | bebc344e5ee4d347bb5d9099b83de14ef4b0d094 /src/node_buffer.h | |
parent | c819abccb611735afa8fd2cd26ca23d43a98527c (diff) |
More bindings, beginning tcp server code in js
Diffstat (limited to 'src/node_buffer.h')
-rw-r--r-- | src/node_buffer.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/node_buffer.h b/src/node_buffer.h index f7006428a6e..3afb3eeaeef 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -5,8 +5,53 @@ namespace node { +#define MIN(a,b) ((a) < (b) ? (a) : (b)) + +/* A buffer is a chunk of memory stored outside the V8 heap, mirrored by an + * object in javascript. The object is not totally opaque, one can access + * individual bytes with [] and slice it into substrings or sub-buffers + * without copying memory. + * + * // return an ascii encoded string - no memory iscopied + * buffer.asciiSlide(0, 3) + * + * // returns another buffer - no memory is copied + * buffer.slice(0, 3) + * + * Interally, each javascript buffer object is backed by a "struct buffer" + * object. These "struct buffer" objects are either a root buffer (in the + * case that buffer->root == NULL) or slice objects (in which case + * buffer->root != NULL). A root buffer is only GCed once all its slices + * are GCed. + */ + +struct buffer { + v8::Persistent<v8::Object> handle; // both + bool weak; // both + struct buffer *root; // both (NULL for root) + size_t offset; // both (0 for root) + size_t length; // both + unsigned int refs; // root only + char bytes[1]; // root only +}; + void InitBuffer(v8::Handle<v8::Object> target); +struct buffer* BufferUnwrap(v8::Handle<v8::Value> val); +bool IsBuffer(v8::Handle<v8::Value> val); + +static inline char * buffer_p(struct buffer *buffer, size_t off) { + struct buffer *root = buffer->root ? buffer->root : buffer; + if (buffer->offset + off >= root->length) return NULL; + return reinterpret_cast<char*>(&(root->bytes) + buffer->offset + off); +} + +static inline size_t buffer_remaining(struct buffer *buffer, size_t off) { + struct buffer *root = buffer->root ? buffer->root : buffer; + char *end = reinterpret_cast<char*>(&(root->bytes) + root->length); + return end - buffer_p(buffer, off); +} + } #endif // NODE_BUFFER |