summaryrefslogtreecommitdiff
path: root/src/node_buffer.h
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2009-12-15 09:22:36 +0100
committerRyan Dahl <ry@tinyclouds.org>2009-12-29 21:12:30 +0100
commit469e2648e59c19154fd67ae1df17487f673fe9fc (patch)
treebebc344e5ee4d347bb5d9099b83de14ef4b0d094 /src/node_buffer.h
parentc819abccb611735afa8fd2cd26ca23d43a98527c (diff)
downloadandroid-node-v8-469e2648e59c19154fd67ae1df17487f673fe9fc.tar.gz
android-node-v8-469e2648e59c19154fd67ae1df17487f673fe9fc.tar.bz2
android-node-v8-469e2648e59c19154fd67ae1df17487f673fe9fc.zip
More bindings, beginning tcp server code in js
Diffstat (limited to 'src/node_buffer.h')
-rw-r--r--src/node_buffer.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/node_buffer.h b/src/node_buffer.h
index f7006428a6..3afb3eeaee 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