summaryrefslogtreecommitdiff
path: root/src/allocated_buffer-inl.h
blob: 2dee6f09a3e9d4d50a7b745bb38a217b68bb668e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef SRC_ALLOCATED_BUFFER_INL_H_
#define SRC_ALLOCATED_BUFFER_INL_H_

#include "allocated_buffer.h"
#include "base_object-inl.h"
#include "node_buffer.h"
#include "env-inl.h"
#include "uv.h"
#include "v8.h"
#include "util-inl.h"
#include "node_internals.h"

namespace node {

// It's a bit awkward to define this Buffer::New() overload here, but it
// avoids a circular dependency with node_internals.h.
namespace Buffer {
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
                                   v8::Local<v8::ArrayBuffer> ab,
                                   size_t byte_offset,
                                   size_t length);
}

NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
    IsolateData* isolate_data)
  : node_allocator_(isolate_data->node_allocator()) {
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
}

NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
}

AllocatedBuffer AllocatedBuffer::AllocateManaged(
    Environment* env,
    size_t size) {
  NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
  std::unique_ptr<v8::BackingStore> bs =
      v8::ArrayBuffer::NewBackingStore(env->isolate(), size);
  return AllocatedBuffer(env, std::move(bs));
}

AllocatedBuffer::AllocatedBuffer(
    Environment* env, std::unique_ptr<v8::BackingStore> bs)
    : env_(env), backing_store_(std::move(bs)) {}

AllocatedBuffer::AllocatedBuffer(
    Environment* env, uv_buf_t buffer)
    : env_(env) {
  if (buffer.base == nullptr) return;
  auto map = env->released_allocated_buffers();
  auto it = map->find(buffer.base);
  CHECK_NE(it, map->end());
  backing_store_ = std::move(it->second);
  map->erase(it);
}

void AllocatedBuffer::Resize(size_t len) {
  if (len == 0) {
    backing_store_ = v8::ArrayBuffer::NewBackingStore(env_->isolate(), 0);
    return;
  }
  NoArrayBufferZeroFillScope no_zero_fill_scope(env_->isolate_data());
  backing_store_ = v8::BackingStore::Reallocate(
      env_->isolate(), std::move(backing_store_), len);
}

uv_buf_t AllocatedBuffer::release() {
  if (data() == nullptr) return uv_buf_init(nullptr, 0);

  CHECK_NOT_NULL(env_);
  uv_buf_t ret = uv_buf_init(data(), size());
  env_->released_allocated_buffers()->emplace(
      ret.base, std::move(backing_store_));
  return ret;
}

char* AllocatedBuffer::data() {
  if (!backing_store_) return nullptr;
  return static_cast<char*>(backing_store_->Data());
}

const char* AllocatedBuffer::data() const {
  if (!backing_store_) return nullptr;
  return static_cast<char*>(backing_store_->Data());
}


size_t AllocatedBuffer::size() const {
  if (!backing_store_) return 0;
  return backing_store_->ByteLength();
}

void AllocatedBuffer::clear() {
  backing_store_.reset();
}

v8::MaybeLocal<v8::Object> AllocatedBuffer::ToBuffer() {
  v8::Local<v8::ArrayBuffer> ab = ToArrayBuffer();
  return Buffer::New(env_, ab, 0, ab->ByteLength())
      .FromMaybe(v8::Local<v8::Uint8Array>());
}

v8::Local<v8::ArrayBuffer> AllocatedBuffer::ToArrayBuffer() {
  return v8::ArrayBuffer::New(env_->isolate(), std::move(backing_store_));
}

}  // namespace node

#endif  // SRC_ALLOCATED_BUFFER_INL_H_