• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_ALLOCATED_BUFFER_H_
2 #define SRC_ALLOCATED_BUFFER_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "base_object.h"
7 #include "uv.h"
8 #include "v8.h"
9 #include "env.h"
10 
11 namespace node {
12 
13 class Environment;
14 
15 // Disables zero-filling for ArrayBuffer allocations in this scope. This is
16 // similar to how we implement Buffer.allocUnsafe() in JS land.
17 class NoArrayBufferZeroFillScope{
18  public:
19   inline explicit NoArrayBufferZeroFillScope(IsolateData* isolate_data);
20   inline ~NoArrayBufferZeroFillScope();
21 
22  private:
23   NodeArrayBufferAllocator* node_allocator_;
24 
25   friend class Environment;
26 };
27 
28 // A unique-pointer-ish object that is compatible with the JS engine's
29 // ArrayBuffer::Allocator.
30 // TODO(addaleax): We may want to start phasing this out as it's only a
31 // thin wrapper around v8::BackingStore at this point
32 struct AllocatedBuffer {
33  public:
34   // Utilities that allocate memory using the Isolate's ArrayBuffer::Allocator.
35   // In particular, using AllocateManaged() will provide a RAII-style object
36   // with easy conversion to `Buffer` and `ArrayBuffer` objects.
37   inline static AllocatedBuffer AllocateManaged(Environment* env, size_t size);
38 
39   AllocatedBuffer() = default;
40   inline AllocatedBuffer(
41       Environment* env, std::unique_ptr<v8::BackingStore> bs);
42   // For this constructor variant, `buffer` *must* come from an earlier call
43   // to .release
44   inline AllocatedBuffer(Environment* env, uv_buf_t buffer);
45 
46   inline void Resize(size_t len);
47 
48   inline uv_buf_t release();
49   inline char* data();
50   inline const char* data() const;
51   inline size_t size() const;
52   inline void clear();
53 
54   inline v8::MaybeLocal<v8::Object> ToBuffer();
55   inline v8::Local<v8::ArrayBuffer> ToArrayBuffer();
56 
57   AllocatedBuffer(AllocatedBuffer&& other) = default;
58   AllocatedBuffer& operator=(AllocatedBuffer&& other) = default;
59   AllocatedBuffer(const AllocatedBuffer& other) = delete;
60   AllocatedBuffer& operator=(const AllocatedBuffer& other) = delete;
61 
62  private:
63   Environment* env_ = nullptr;
64   std::unique_ptr<v8::BackingStore> backing_store_;
65 
66   friend class Environment;
67 };
68 
69 }  // namespace node
70 
71 #endif  // NODE_WANT_INTERNALS
72 
73 #endif  // SRC_ALLOCATED_BUFFER_H_
74