1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef SRC_NODE_BUFFER_H_
23 #define SRC_NODE_BUFFER_H_
29 #if defined(NODE_WANT_INTERNALS)
31 #endif // defined(NODE_WANT_INTERNALS)
36 static const unsigned int kMaxLength = smalloc::kMaxLength;
38 NODE_EXTERN bool HasInstance(v8::Handle<v8::Value> val);
39 NODE_EXTERN bool HasInstance(v8::Handle<v8::Object> val);
40 NODE_EXTERN char* Data(v8::Handle<v8::Value> val);
41 NODE_EXTERN char* Data(v8::Handle<v8::Object> val);
42 NODE_EXTERN size_t Length(v8::Handle<v8::Value> val);
43 NODE_EXTERN size_t Length(v8::Handle<v8::Object> val);
46 NODE_EXTERN v8::Local<v8::Object> New(v8::Isolate* isolate, size_t length);
47 NODE_DEPRECATED("Use New(isolate, ...)",
48 inline v8::Local<v8::Object> New(size_t length) {
49 return New(v8::Isolate::GetCurrent(), length);
51 // public constructor from string
52 NODE_EXTERN v8::Local<v8::Object> New(v8::Isolate* isolate,
53 v8::Handle<v8::String> string,
54 enum encoding enc = UTF8);
55 NODE_DEPRECATED("Use New(isolate, ...)",
56 inline v8::Local<v8::Object> New(v8::Handle<v8::String> string,
57 enum encoding enc = UTF8) {
58 return New(v8::Isolate::GetCurrent(), string, enc);
60 // public constructor - data is copied
61 // TODO(trevnorris): should be something like Copy()
62 NODE_EXTERN v8::Local<v8::Object> New(v8::Isolate* isolate,
65 NODE_DEPRECATED("Use New(isolate, ...)",
66 inline v8::Local<v8::Object> New(const char* data, size_t len) {
67 return New(v8::Isolate::GetCurrent(), data, len);
69 // public constructor - data is used, callback is passed data on object gc
70 NODE_EXTERN v8::Local<v8::Object> New(v8::Isolate* isolate,
73 smalloc::FreeCallback callback,
75 NODE_DEPRECATED("Use New(isolate, ...)",
76 inline v8::Local<v8::Object> New(char* data,
78 smalloc::FreeCallback callback,
80 return New(v8::Isolate::GetCurrent(), data, length, callback, hint);
83 // public constructor - data is used.
84 // TODO(trevnorris): should be New() for consistency
85 NODE_EXTERN v8::Local<v8::Object> Use(v8::Isolate* isolate,
88 NODE_DEPRECATED("Use Use(isolate, ...)",
89 inline v8::Local<v8::Object> Use(char* data, uint32_t len) {
90 return Use(v8::Isolate::GetCurrent(), data, len);
93 // This is verbose to be explicit with inline commenting
94 static inline bool IsWithinBounds(size_t off, size_t len, size_t max) {
95 // Asking to seek too far into the buffer
96 // check to avoid wrapping in subsequent subtraction
100 // Asking for more than is left over in the buffer
104 // Otherwise we're in bounds
108 // Internal. Not for public consumption. We can't define these in
109 // src/node_internals.h due to a circular dependency issue with
110 // the smalloc.h and node_internals.h headers.
111 #if defined(NODE_WANT_INTERNALS)
112 v8::Local<v8::Object> New(Environment* env, size_t size);
113 v8::Local<v8::Object> New(Environment* env, const char* data, size_t len);
114 v8::Local<v8::Object> New(Environment* env,
117 smalloc::FreeCallback callback,
119 v8::Local<v8::Object> Use(Environment* env, char* data, uint32_t length);
120 #endif // defined(NODE_WANT_INTERNALS)
122 } // namespace Buffer
125 #endif // SRC_NODE_BUFFER_H_