src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / node_buffer.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #ifndef SRC_NODE_BUFFER_H_
23 #define SRC_NODE_BUFFER_H_
24
25 #include "node.h"
26 #include "smalloc.h"
27 #include "v8.h"
28
29 #if defined(NODE_WANT_INTERNALS)
30 #include "env.h"
31 #endif  // defined(NODE_WANT_INTERNALS)
32
33 namespace node {
34 namespace Buffer {
35
36 static const unsigned int kMaxLength = smalloc::kMaxLength;
37
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);
44
45 // public constructor
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);
50 })
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);
59 })
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,
63                                       const char* data,
64                                       size_t len);
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);
68 })
69 // public constructor - data is used, callback is passed data on object gc
70 NODE_EXTERN v8::Local<v8::Object> New(v8::Isolate* isolate,
71                                       char* data,
72                                       size_t length,
73                                       smalloc::FreeCallback callback,
74                                       void* hint);
75 NODE_DEPRECATED("Use New(isolate, ...)",
76                 inline v8::Local<v8::Object> New(char* data,
77                                                  size_t length,
78                                                  smalloc::FreeCallback callback,
79                                                  void* hint) {
80   return New(v8::Isolate::GetCurrent(), data, length, callback, hint);
81 })
82
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,
86                                       char* data,
87                                       uint32_t len);
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);
91 })
92
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
97   if (off > max)
98     return false;
99
100   // Asking for more than is left over in the buffer
101   if (max - off < len)
102     return false;
103
104   // Otherwise we're in bounds
105   return true;
106 }
107
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,
115                           char* data,
116                           size_t length,
117                           smalloc::FreeCallback callback,
118                           void* hint);
119 v8::Local<v8::Object> Use(Environment* env, char* data, uint32_t length);
120 #endif  // defined(NODE_WANT_INTERNALS)
121
122 }  // namespace Buffer
123 }  // namespace node
124
125 #endif  // SRC_NODE_BUFFER_H_