Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 630bb7a

Browse files
committed
Rename blob to buffer.
1 parent 0afed52 commit 630bb7a

File tree

6 files changed

+134
-95
lines changed

6 files changed

+134
-95
lines changed

src/node.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <errno.h>
1111
#include <dlfcn.h> /* dlopen(), dlsym() */
1212

13-
#include <node_blob.h>
13+
#include <node_buffer.h>
1414
#include <node_events.h>
1515
#include <node_dns.h>
1616
#include <node_net.h>
@@ -848,7 +848,7 @@ static Local<Object> Load(int argc, char *argv[]) {
848848

849849

850850
// Initialize the C++ modules..................filename of module
851-
InitBlob(process); // stdio.cc
851+
InitBuffer(process); // buffer.cc
852852
Stdio::Initialize(process); // stdio.cc
853853
Timer::Initialize(process); // timer.cc
854854
SignalHandler::Initialize(process); // signal_handler.cc

src/node_blob.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/node_blob.cc renamed to src/node_buffer.cc

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,109 +24,113 @@ using namespace v8;
2424
static Persistent<String> length_symbol;
2525
static Persistent<FunctionTemplate> constructor_template;
2626

27-
/* A blob is a chunk of memory stored outside the V8 heap mirrored by an
27+
/* A buffer is a chunk of memory stored outside the V8 heap, mirrored by an
2828
* object in javascript. The object is not totally opaque, one can access
29-
* individual bytes with [] and one can slice the blob into substrings or
30-
* subblobs without copying memory.
29+
* individual bytes with [] and slice it into substrings or sub-buffers
30+
* without copying memory.
3131
*
32-
* blob.asciiSlide(0, 3) // return an ascii encoded string - no memory iscopied
33-
* blob.slice(0, 3) // returns another blob - no memory is copied
32+
* // return an ascii encoded string - no memory iscopied
33+
* buffer.asciiSlide(0, 3)
3434
*
35-
* Interally, each javascript blob object is backed by a "struct blob" object.
36-
* These "struct blob" objects are either the root object (in the case that
37-
* blob->root == NULL) or slice objects (in which case blob->root != NULL).
38-
* The root blob is only GCed once all it's slices are GCed.
35+
* // returns another buffer - no memory is copied
36+
* buffer.slice(0, 3)
37+
*
38+
* Interally, each javascript buffer object is backed by a "struct buffer"
39+
* object. These "struct buffer" objects are either a root buffer (in the
40+
* case that buffer->root == NULL) or slice objects (in which case
41+
* buffer->root != NULL). A root buffer is only GCed once all its slices
42+
* are GCed.
3943
*/
4044

41-
struct blob {
45+
struct buffer {
4246
Persistent<Object> handle; // both
4347
bool weak; // both
44-
struct blob *root; // both (NULL for root)
48+
struct buffer *root; // both (NULL for root)
4549
size_t offset; // both (0 for root)
4650
size_t length; // both
4751
unsigned int refs; // root only
48-
char bytes[1]; // root only
52+
char bytes[1]; // root only
4953
};
5054

5155

52-
static inline struct blob* blob_root(blob *blob) {
53-
return blob->root ? blob->root : blob;
56+
static inline struct buffer* buffer_root(buffer *buffer) {
57+
return buffer->root ? buffer->root : buffer;
5458
}
5559

5660
/* Determines the absolute position for a relative offset */
57-
static inline size_t blob_abs_off(blob *blob, size_t off) {
58-
struct blob *root = blob_root(blob);
61+
static inline size_t buffer_abs_off(buffer *buffer, size_t off) {
62+
struct buffer *root = buffer_root(buffer);
5963
off += root->offset;
6064
return MIN(root->length, off);
6165
}
6266

6367

64-
static inline void blob_ref(struct blob *blob) {
65-
assert(blob->root == NULL);
66-
blob->refs++;
68+
static inline void buffer_ref(struct buffer *buffer) {
69+
assert(buffer->root == NULL);
70+
buffer->refs++;
6771
}
6872

6973

70-
static inline void blob_unref(struct blob *blob) {
71-
assert(blob->root == NULL);
72-
assert(blob->refs > 0);
73-
blob->refs--;
74-
if (blob->refs == 0 && blob->weak) free(blob);
74+
static inline void buffer_unref(struct buffer *buffer) {
75+
assert(buffer->root == NULL);
76+
assert(buffer->refs > 0);
77+
buffer->refs--;
78+
if (buffer->refs == 0 && buffer->weak) free(buffer);
7579
}
7680

7781

78-
static inline struct blob* Unwrap(Handle<Value> val) {
82+
static inline struct buffer* Unwrap(Handle<Value> val) {
7983
assert(val->IsObject());
8084
HandleScope scope;
8185
Local<Object> obj = val->ToObject();
8286
assert(obj->InternalFieldCount() == 1);
8387
Local<External> ext = Local<External>::Cast(obj->GetInternalField(0));
84-
return static_cast<struct blob*>(ext->Value());
88+
return static_cast<struct buffer*>(ext->Value());
8589
}
8690

8791

8892
static void RootWeakCallback(Persistent<Value> value, void *data)
8993
{
90-
struct blob *blob = static_cast<struct blob*>(data);
91-
assert(blob->root == NULL); // this is the root
92-
assert(value == blob->handle);
93-
blob->handle.Dispose();
94-
if (blob->refs) {
95-
blob->weak = true;
94+
struct buffer *buffer = static_cast<struct buffer*>(data);
95+
assert(buffer->root == NULL); // this is the root
96+
assert(value == buffer->handle);
97+
buffer->handle.Dispose();
98+
if (buffer->refs) {
99+
buffer->weak = true;
96100
} else {
97-
free(blob);
101+
free(buffer);
98102
}
99103
}
100104

101105

102106
static void SliceWeakCallback(Persistent<Value> value, void *data)
103107
{
104-
struct blob *blob = static_cast<struct blob*>(data);
105-
assert(blob->root != NULL); // this is a slice
106-
assert(value == blob->handle);
107-
blob->handle.Dispose();
108-
blob_unref(blob->root);
108+
struct buffer *buffer = static_cast<struct buffer*>(data);
109+
assert(buffer->root != NULL); // this is a slice
110+
assert(value == buffer->handle);
111+
buffer->handle.Dispose();
112+
buffer_unref(buffer->root);
109113
}
110114

111115

112116
static Handle<Value> Constructor(const Arguments &args) {
113117
HandleScope scope;
114118

115119
size_t length;
116-
struct blob *blob;
120+
struct buffer *buffer;
117121

118122
if (constructor_template->HasInstance(args[0])) {
119123
// slice slice
120124
SLICE_ARGS(args[1], args[2])
121125

122-
struct blob *parent = Unwrap(args[0]);
126+
struct buffer *parent = Unwrap(args[0]);
123127

124-
size_t start_abs = blob_abs_off(parent, start);
125-
size_t end_abs = blob_abs_off(parent, end);
128+
size_t start_abs = buffer_abs_off(parent, start);
129+
size_t end_abs = buffer_abs_off(parent, end);
126130
assert(start_abs <= end_abs);
127131
length = end_abs - start_abs;
128132

129-
void *d = malloc(sizeof(struct blob));
133+
void *d = malloc(sizeof(struct buffer));
130134

131135
if (!d) {
132136
V8::LowMemoryNotification();
@@ -135,17 +139,17 @@ static Handle<Value> Constructor(const Arguments &args) {
135139

136140
}
137141

138-
blob = static_cast<struct blob*>(d);
142+
buffer = static_cast<struct buffer*>(d);
139143

140-
blob->length = length;
141-
blob->offset = start_abs;
142-
blob->weak = false;
143-
blob->refs = 0;
144-
blob->root = blob_root(parent);
145-
blob->handle = Persistent<Object>::New(args.This());
146-
blob->handle.MakeWeak(blob, SliceWeakCallback);
144+
buffer->length = length;
145+
buffer->offset = start_abs;
146+
buffer->weak = false;
147+
buffer->refs = 0;
148+
buffer->root = buffer_root(parent);
149+
buffer->handle = Persistent<Object>::New(args.This());
150+
buffer->handle.MakeWeak(buffer, SliceWeakCallback);
147151

148-
blob_ref(blob->root);
152+
buffer_ref(buffer->root);
149153
} else {
150154
// Root slice
151155

@@ -157,31 +161,31 @@ static Handle<Value> Constructor(const Arguments &args) {
157161
}
158162

159163
// TODO alignment. modify the length?
160-
void *d = malloc(sizeof(struct blob) + length - 1);
164+
void *d = malloc(sizeof(struct buffer) + length - 1);
161165

162166
if (!d) {
163167
V8::LowMemoryNotification();
164168
return ThrowException(Exception::Error(
165169
String::New("Could not allocate enough memory")));
166170
}
167171

168-
blob = static_cast<struct blob*>(d);
172+
buffer = static_cast<struct buffer*>(d);
169173

170-
blob->offset = 0;
171-
blob->length = length;
172-
blob->weak = false;
173-
blob->refs = 0;
174-
blob->root = NULL;
175-
blob->handle = Persistent<Object>::New(args.This());
176-
blob->handle.MakeWeak(blob, RootWeakCallback);
174+
buffer->offset = 0;
175+
buffer->length = length;
176+
buffer->weak = false;
177+
buffer->refs = 0;
178+
buffer->root = NULL;
179+
buffer->handle = Persistent<Object>::New(args.This());
180+
buffer->handle.MakeWeak(buffer, RootWeakCallback);
177181
}
178182

179-
args.This()->SetInternalField(0, v8::External::New(blob));
183+
args.This()->SetInternalField(0, v8::External::New(buffer));
180184

181-
struct blob *root = blob_root(blob);
185+
struct buffer *root = buffer_root(buffer);
182186

183187
args.This()->
184-
SetIndexedPropertiesToExternalArrayData(&root->bytes + blob->offset,
188+
SetIndexedPropertiesToExternalArrayData(&root->bytes + buffer->offset,
185189
kExternalUnsignedByteArray,
186190
length);
187191

@@ -194,16 +198,16 @@ static Handle<Value> Constructor(const Arguments &args) {
194198
class AsciiSliceExt: public String::ExternalAsciiStringResource {
195199
public:
196200

197-
AsciiSliceExt(struct blob *root, size_t start, size_t end)
201+
AsciiSliceExt(struct buffer *root, size_t start, size_t end)
198202
{
199203
data_ = root->bytes + start;
200204
len_ = end - start;
201205
root_ = root;
202-
blob_ref(root_);
206+
buffer_ref(root_);
203207
}
204208

205209
~AsciiSliceExt() {
206-
blob_unref(root_);
210+
buffer_unref(root_);
207211
}
208212

209213
const char* data() const {
@@ -217,7 +221,7 @@ class AsciiSliceExt: public String::ExternalAsciiStringResource {
217221
private:
218222
const char *data_;
219223
size_t len_;
220-
struct blob *root_;
224+
struct buffer *root_;
221225
};
222226

223227
static Handle<Value> AsciiSlice(const Arguments &args) {
@@ -226,17 +230,17 @@ static Handle<Value> AsciiSlice(const Arguments &args) {
226230
SLICE_ARGS(args[0], args[1])
227231

228232
assert(args.This()->InternalFieldCount() == 1);
229-
struct blob *parent = Unwrap(args.This());
233+
struct buffer *parent = Unwrap(args.This());
230234

231-
size_t start_abs = blob_abs_off(parent, start);
232-
size_t end_abs = blob_abs_off(parent, end);
235+
size_t start_abs = buffer_abs_off(parent, start);
236+
size_t end_abs = buffer_abs_off(parent, end);
233237

234238
assert(start_abs <= end_abs);
235239

236-
AsciiSliceExt *s = new AsciiSliceExt(blob_root(parent), start_abs, end_abs);
240+
AsciiSliceExt *s = new AsciiSliceExt(buffer_root(parent), start_abs, end_abs);
237241
Local<String> string = String::NewExternal(s);
238242

239-
struct blob *root = blob_root(parent);
243+
struct buffer *root = buffer_root(parent);
240244
assert(root->refs > 0);
241245

242246
return scope.Close(string);
@@ -247,12 +251,12 @@ static Handle<Value> Utf8Slice(const Arguments &args) {
247251

248252
SLICE_ARGS(args[0], args[1])
249253

250-
struct blob *parent = Unwrap(args.This());
251-
size_t start_abs = blob_abs_off(parent, start);
252-
size_t end_abs = blob_abs_off(parent, end);
254+
struct buffer *parent = Unwrap(args.This());
255+
size_t start_abs = buffer_abs_off(parent, start);
256+
size_t end_abs = buffer_abs_off(parent, end);
253257
assert(start_abs <= end_abs);
254258

255-
struct blob *root = blob_root(parent);
259+
struct buffer *root = buffer_root(parent);
256260

257261
Local<String> string =
258262
String::New(reinterpret_cast<const char*>(&root->bytes + start_abs),
@@ -271,15 +275,15 @@ static Handle<Value> Slice(const Arguments &args) {
271275
return scope.Close(slice);
272276
}
273277

274-
void InitBlob(Handle<Object> target) {
278+
void InitBuffer(Handle<Object> target) {
275279
HandleScope scope;
276280

277281
length_symbol = Persistent<String>::New(String::NewSymbol("length"));
278282

279283
Local<FunctionTemplate> t = FunctionTemplate::New(Constructor);
280284
constructor_template = Persistent<FunctionTemplate>::New(t);
281285
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
282-
constructor_template->SetClassName(String::NewSymbol("Blob"));
286+
constructor_template->SetClassName(String::NewSymbol("Buffer"));
283287

284288
// copy free
285289
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiSlice", AsciiSlice);
@@ -288,7 +292,7 @@ void InitBlob(Handle<Object> target) {
288292
// copy
289293
NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Slice", Utf8Slice);
290294

291-
target->Set(String::NewSymbol("Blob"), constructor_template->GetFunction());
295+
target->Set(String::NewSymbol("Buffer"), constructor_template->GetFunction());
292296
}
293297

294298
} // namespace node

src/node_buffer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef NODE_BUFFER
2+
#define NODE_BUFFER
3+
4+
#include <v8.h>
5+
6+
namespace node {
7+
8+
void InitBuffer(v8::Handle<v8::Object> target);
9+
10+
}
11+
12+
#endif // NODE_BUFFER

0 commit comments

Comments
 (0)