From d1ffdde7ca5efb0e55b31d61583bf07213cb96d5 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 13 May 2022 19:54:13 +0530 Subject: [PATCH] src: delete AllocatedBuffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since all its uses are now gone, it's time to say goodbye to AllocatedBuffer. Refs: https://github.com/nodejs/node/pull/39941 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/43008 Reviewed-By: Tobias Nießen Reviewed-By: Rich Trott Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Zeyu "Alex" Yang Reviewed-By: Anna Henningsen --- node.gyp | 2 - src/allocated_buffer-inl.h | 110 ------------------------------------ src/allocated_buffer.h | 73 ------------------------ src/crypto/README.md | 11 ---- src/crypto/crypto_aes.cc | 7 +-- src/crypto/crypto_aes.h | 1 - src/crypto/crypto_bio.cc | 1 - src/crypto/crypto_cipher.cc | 3 +- src/crypto/crypto_common.cc | 1 - src/crypto/crypto_dh.cc | 5 +- src/crypto/crypto_ec.cc | 5 +- src/crypto/crypto_ec.h | 7 +-- src/crypto/crypto_hash.cc | 1 - src/crypto/crypto_hash.h | 3 +- src/crypto/crypto_hkdf.cc | 3 +- src/crypto/crypto_hkdf.h | 5 +- src/crypto/crypto_hmac.cc | 5 +- src/crypto/crypto_hmac.h | 3 +- src/crypto/crypto_keygen.cc | 1 - src/crypto/crypto_keygen.h | 5 +- src/crypto/crypto_pbkdf2.cc | 3 +- src/crypto/crypto_random.cc | 3 +- src/crypto/crypto_random.h | 3 +- src/crypto/crypto_rsa.cc | 5 +- src/crypto/crypto_rsa.h | 1 - src/crypto/crypto_scrypt.cc | 3 +- src/crypto/crypto_sig.cc | 5 +- src/crypto/crypto_sig.h | 3 +- src/crypto/crypto_util.cc | 3 +- src/crypto/crypto_util.h | 5 +- src/env-inl.h | 26 +++++---- src/env.cc | 1 - src/env.h | 20 +++++-- src/node.cc | 1 - src/node_buffer.cc | 1 - src/node_http2.cc | 3 +- src/stream_base.cc | 1 - 37 files changed, 63 insertions(+), 276 deletions(-) delete mode 100644 src/allocated_buffer-inl.h delete mode 100644 src/allocated_buffer.h diff --git a/node.gyp b/node.gyp index 875eda26dfc2ff..83582f99b6db30 100644 --- a/node.gyp +++ b/node.gyp @@ -506,8 +506,6 @@ 'src/aliased_buffer.h', 'src/aliased_struct.h', 'src/aliased_struct-inl.h', - 'src/allocated_buffer.h', - 'src/allocated_buffer-inl.h', 'src/async_wrap.h', 'src/async_wrap-inl.h', 'src/base_object.h', diff --git a/src/allocated_buffer-inl.h b/src/allocated_buffer-inl.h deleted file mode 100644 index 2dee6f09a3e9d4..00000000000000 --- a/src/allocated_buffer-inl.h +++ /dev/null @@ -1,110 +0,0 @@ -#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 New(Environment* env, - v8::Local 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 bs = - v8::ArrayBuffer::NewBackingStore(env->isolate(), size); - return AllocatedBuffer(env, std::move(bs)); -} - -AllocatedBuffer::AllocatedBuffer( - Environment* env, std::unique_ptr 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(backing_store_->Data()); -} - -const char* AllocatedBuffer::data() const { - if (!backing_store_) return nullptr; - return static_cast(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 AllocatedBuffer::ToBuffer() { - v8::Local ab = ToArrayBuffer(); - return Buffer::New(env_, ab, 0, ab->ByteLength()) - .FromMaybe(v8::Local()); -} - -v8::Local AllocatedBuffer::ToArrayBuffer() { - return v8::ArrayBuffer::New(env_->isolate(), std::move(backing_store_)); -} - -} // namespace node - -#endif // SRC_ALLOCATED_BUFFER_INL_H_ diff --git a/src/allocated_buffer.h b/src/allocated_buffer.h deleted file mode 100644 index 9cf41bffdc19dc..00000000000000 --- a/src/allocated_buffer.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef SRC_ALLOCATED_BUFFER_H_ -#define SRC_ALLOCATED_BUFFER_H_ - -#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS - -#include "base_object.h" -#include "uv.h" -#include "v8.h" -#include "env.h" - -namespace node { - -class Environment; - -// Disables zero-filling for ArrayBuffer allocations in this scope. This is -// similar to how we implement Buffer.allocUnsafe() in JS land. -class NoArrayBufferZeroFillScope{ - public: - inline explicit NoArrayBufferZeroFillScope(IsolateData* isolate_data); - inline ~NoArrayBufferZeroFillScope(); - - private: - NodeArrayBufferAllocator* node_allocator_; - - friend class Environment; -}; - -// A unique-pointer-ish object that is compatible with the JS engine's -// ArrayBuffer::Allocator. -// TODO(addaleax): We may want to start phasing this out as it's only a -// thin wrapper around v8::BackingStore at this point -struct AllocatedBuffer { - public: - // Utilities that allocate memory using the Isolate's ArrayBuffer::Allocator. - // In particular, using AllocateManaged() will provide a RAII-style object - // with easy conversion to `Buffer` and `ArrayBuffer` objects. - inline static AllocatedBuffer AllocateManaged(Environment* env, size_t size); - - AllocatedBuffer() = default; - inline AllocatedBuffer( - Environment* env, std::unique_ptr bs); - // For this constructor variant, `buffer` *must* come from an earlier call - // to .release - inline AllocatedBuffer(Environment* env, uv_buf_t buffer); - - inline void Resize(size_t len); - - inline uv_buf_t release(); - inline char* data(); - inline const char* data() const; - inline size_t size() const; - inline void clear(); - - inline v8::MaybeLocal ToBuffer(); - inline v8::Local ToArrayBuffer(); - - AllocatedBuffer(AllocatedBuffer&& other) = default; - AllocatedBuffer& operator=(AllocatedBuffer&& other) = default; - AllocatedBuffer(const AllocatedBuffer& other) = delete; - AllocatedBuffer& operator=(const AllocatedBuffer& other) = delete; - - private: - Environment* env_ = nullptr; - std::unique_ptr backing_store_; - - friend class Environment; -}; - -} // namespace node - -#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS - -#endif // SRC_ALLOCATED_BUFFER_H_ diff --git a/src/crypto/README.md b/src/crypto/README.md index 8ebc3003da3150..28351eb024196c 100644 --- a/src/crypto/README.md +++ b/src/crypto/README.md @@ -112,17 +112,6 @@ their underlying data pointers. It is used extensively through `src/crypto` to make it easier to deal with inputs that allow any `ArrayBuffer`-backed object. -### `AllocatedBuffer` - -The `AllocatedBuffer` utility is defined in `allocated_buffer.h` and is not -specific to `src/crypto`. It is used extensively within `src/crypto` to hold -allocated data that is intended to be output in response to various -crypto functions (generated hash values, or ciphertext, for instance). - -_Currently, we are working to transition away from using `AllocatedBuffer` -to directly using the `v8::BackingStore` API. This will take some time. -New uses of `AllocatedBuffer` should be avoided if possible._ - ### Key objects Most crypto operations involve the use of keys -- cryptographic inputs diff --git a/src/crypto/crypto_aes.cc b/src/crypto/crypto_aes.cc index e6a6c77cba2771..76d3e3853451d4 100644 --- a/src/crypto/crypto_aes.cc +++ b/src/crypto/crypto_aes.cc @@ -1,10 +1,9 @@ #include "crypto/crypto_aes.h" +#include "async_wrap-inl.h" +#include "base_object-inl.h" #include "crypto/crypto_cipher.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" -#include "async_wrap-inl.h" -#include "base_object-inl.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "threadpoolwork-inl.h" @@ -30,7 +29,7 @@ namespace crypto { namespace { // Implements general AES encryption and decryption for CBC // The key_data must be a secret key. -// On success, this function sets out to a new AllocatedBuffer +// On success, this function sets out to a new ByteSource // instance containing the results and returns WebCryptoCipherStatus::OK. WebCryptoCipherStatus AES_Cipher( Environment* env, diff --git a/src/crypto/crypto_aes.h b/src/crypto/crypto_aes.h index 3ffe04766cc339..9dfa5edc6544e7 100644 --- a/src/crypto/crypto_aes.h +++ b/src/crypto/crypto_aes.h @@ -6,7 +6,6 @@ #include "crypto/crypto_cipher.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_util.h" -#include "allocated_buffer.h" #include "env.h" #include "v8.h" diff --git a/src/crypto/crypto_bio.cc b/src/crypto/crypto_bio.cc index 13793aa7f2ed65..25ea640ad83077 100644 --- a/src/crypto/crypto_bio.cc +++ b/src/crypto/crypto_bio.cc @@ -22,7 +22,6 @@ #include "crypto/crypto_bio.h" #include "base_object-inl.h" #include "memory_tracker-inl.h" -#include "allocated_buffer-inl.h" #include "util-inl.h" #include diff --git a/src/crypto/crypto_cipher.cc b/src/crypto/crypto_cipher.cc index f513ffbb6f65c2..324e3120c65e7f 100644 --- a/src/crypto/crypto_cipher.cc +++ b/src/crypto/crypto_cipher.cc @@ -1,7 +1,6 @@ #include "crypto/crypto_cipher.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" #include "base_object-inl.h" +#include "crypto/crypto_util.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "node_buffer.h" diff --git a/src/crypto/crypto_common.cc b/src/crypto/crypto_common.cc index a5aa39c23c1708..5796e96708ddc6 100644 --- a/src/crypto/crypto_common.cc +++ b/src/crypto/crypto_common.cc @@ -1,4 +1,3 @@ -#include "allocated_buffer-inl.h" #include "base_object-inl.h" #include "env-inl.h" #include "node_buffer.h" diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc index 590d4c8d67ca86..c02e22bb542ff5 100644 --- a/src/crypto/crypto_dh.cc +++ b/src/crypto/crypto_dh.cc @@ -1,9 +1,8 @@ #include "crypto/crypto_dh.h" -#include "crypto/crypto_keys.h" -#include "crypto/crypto_groups.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" #include "base_object-inl.h" +#include "crypto/crypto_groups.h" +#include "crypto/crypto_keys.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "threadpoolwork-inl.h" diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index e05b5388f0450a..706affa0c21c03 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -1,9 +1,8 @@ #include "crypto/crypto_ec.h" -#include "crypto/crypto_common.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" #include "base_object-inl.h" +#include "crypto/crypto_common.h" +#include "crypto/crypto_util.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "node_buffer.h" diff --git a/src/crypto/crypto_ec.h b/src/crypto/crypto_ec.h index bc4160fc8bee01..9782ce0bf35a66 100644 --- a/src/crypto/crypto_ec.h +++ b/src/crypto/crypto_ec.h @@ -3,12 +3,11 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "crypto/crypto_keys.h" -#include "crypto/crypto_keygen.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer.h" #include "async_wrap.h" #include "base_object.h" +#include "crypto/crypto_keygen.h" +#include "crypto/crypto_keys.h" +#include "crypto/crypto_util.h" #include "env.h" #include "memory_tracker.h" #include "node_internals.h" diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index ceea9e595708ba..8f7128569c7aa5 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -1,5 +1,4 @@ #include "crypto/crypto_hash.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" #include "base_object-inl.h" #include "env-inl.h" diff --git a/src/crypto/crypto_hash.h b/src/crypto/crypto_hash.h index cfc09334ce0195..96a9804420db63 100644 --- a/src/crypto/crypto_hash.h +++ b/src/crypto/crypto_hash.h @@ -3,10 +3,9 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "base_object.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_util.h" -#include "allocated_buffer.h" -#include "base_object.h" #include "env.h" #include "memory_tracker.h" #include "v8.h" diff --git a/src/crypto/crypto_hkdf.cc b/src/crypto/crypto_hkdf.cc index 0aa96ada47abe4..b1efcbe55fa898 100644 --- a/src/crypto/crypto_hkdf.cc +++ b/src/crypto/crypto_hkdf.cc @@ -1,8 +1,7 @@ #include "crypto/crypto_hkdf.h" -#include "crypto/crypto_keys.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" #include "base_object-inl.h" +#include "crypto/crypto_keys.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "threadpoolwork-inl.h" diff --git a/src/crypto/crypto_hkdf.h b/src/crypto/crypto_hkdf.h index 666aad65474a2e..ef2d03c2091595 100644 --- a/src/crypto/crypto_hkdf.h +++ b/src/crypto/crypto_hkdf.h @@ -3,11 +3,10 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "crypto/crypto_keys.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer.h" #include "async_wrap.h" #include "base_object.h" +#include "crypto/crypto_keys.h" +#include "crypto/crypto_util.h" #include "v8.h" namespace node { diff --git a/src/crypto/crypto_hmac.cc b/src/crypto/crypto_hmac.cc index d6a652ff8f5ee0..296ae541a3e68f 100644 --- a/src/crypto/crypto_hmac.cc +++ b/src/crypto/crypto_hmac.cc @@ -1,10 +1,9 @@ #include "crypto/crypto_hmac.h" +#include "async_wrap-inl.h" +#include "base_object-inl.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_sig.h" #include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" -#include "async_wrap-inl.h" -#include "base_object-inl.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "node_buffer.h" diff --git a/src/crypto/crypto_hmac.h b/src/crypto/crypto_hmac.h index c37fc4a82c6e95..c80cc36f11dddc 100644 --- a/src/crypto/crypto_hmac.h +++ b/src/crypto/crypto_hmac.h @@ -3,11 +3,10 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "base_object.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_sig.h" #include "crypto/crypto_util.h" -#include "allocated_buffer.h" -#include "base_object.h" #include "env.h" #include "memory_tracker.h" #include "v8.h" diff --git a/src/crypto/crypto_keygen.cc b/src/crypto/crypto_keygen.cc index 24943883b7ba0a..e4e9c227458397 100644 --- a/src/crypto/crypto_keygen.cc +++ b/src/crypto/crypto_keygen.cc @@ -1,5 +1,4 @@ #include "crypto/crypto_keygen.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" #include "base_object-inl.h" #include "debug_utils-inl.h" diff --git a/src/crypto/crypto_keygen.h b/src/crypto/crypto_keygen.h index f8d863a2d93990..01407de83c7b14 100644 --- a/src/crypto/crypto_keygen.h +++ b/src/crypto/crypto_keygen.h @@ -3,11 +3,10 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "crypto/crypto_keys.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer.h" #include "async_wrap.h" #include "base_object.h" +#include "crypto/crypto_keys.h" +#include "crypto/crypto_util.h" #include "env.h" #include "memory_tracker.h" #include "v8.h" diff --git a/src/crypto/crypto_pbkdf2.cc b/src/crypto/crypto_pbkdf2.cc index 495722927ab5be..345c7fe5ce6c15 100644 --- a/src/crypto/crypto_pbkdf2.cc +++ b/src/crypto/crypto_pbkdf2.cc @@ -1,7 +1,6 @@ #include "crypto/crypto_pbkdf2.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" +#include "crypto/crypto_util.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "node_buffer.h" diff --git a/src/crypto/crypto_random.cc b/src/crypto/crypto_random.cc index 7a05dcc16b7389..648fda211c4305 100644 --- a/src/crypto/crypto_random.cc +++ b/src/crypto/crypto_random.cc @@ -1,7 +1,6 @@ #include "crypto/crypto_random.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" +#include "crypto/crypto_util.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "threadpoolwork-inl.h" diff --git a/src/crypto/crypto_random.h b/src/crypto/crypto_random.h index 970306c30cd8e3..a2807ed6ec8743 100644 --- a/src/crypto/crypto_random.h +++ b/src/crypto/crypto_random.h @@ -3,9 +3,8 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "crypto/crypto_util.h" #include "base_object.h" -#include "allocated_buffer.h" +#include "crypto/crypto_util.h" #include "env.h" #include "memory_tracker.h" #include "node_internals.h" diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc index 62d54db8a3dde3..bd732a70a8ffe6 100644 --- a/src/crypto/crypto_rsa.cc +++ b/src/crypto/crypto_rsa.cc @@ -1,10 +1,9 @@ #include "crypto/crypto_rsa.h" +#include "async_wrap-inl.h" +#include "base_object-inl.h" #include "crypto/crypto_bio.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" -#include "async_wrap-inl.h" -#include "base_object-inl.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "threadpoolwork-inl.h" diff --git a/src/crypto/crypto_rsa.h b/src/crypto/crypto_rsa.h index 4f6fbad4f6e6b0..bd00320ca8a5be 100644 --- a/src/crypto/crypto_rsa.h +++ b/src/crypto/crypto_rsa.h @@ -7,7 +7,6 @@ #include "crypto/crypto_keygen.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_util.h" -#include "allocated_buffer.h" #include "env.h" #include "memory_tracker.h" #include "v8.h" diff --git a/src/crypto/crypto_scrypt.cc b/src/crypto/crypto_scrypt.cc index 077c26554b2f1f..4ddf705703c723 100644 --- a/src/crypto/crypto_scrypt.cc +++ b/src/crypto/crypto_scrypt.cc @@ -1,7 +1,6 @@ #include "crypto/crypto_scrypt.h" -#include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" +#include "crypto/crypto_util.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "node_buffer.h" diff --git a/src/crypto/crypto_sig.cc b/src/crypto/crypto_sig.cc index 90031b0ac4257f..c4483a71744e35 100644 --- a/src/crypto/crypto_sig.cc +++ b/src/crypto/crypto_sig.cc @@ -1,10 +1,9 @@ #include "crypto/crypto_sig.h" +#include "async_wrap-inl.h" +#include "base_object-inl.h" #include "crypto/crypto_ec.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_util.h" -#include "allocated_buffer-inl.h" -#include "async_wrap-inl.h" -#include "base_object-inl.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "threadpoolwork-inl.h" diff --git a/src/crypto/crypto_sig.h b/src/crypto/crypto_sig.h index eba18be7c7d019..b6502ba4296f0b 100644 --- a/src/crypto/crypto_sig.h +++ b/src/crypto/crypto_sig.h @@ -3,10 +3,9 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "base_object.h" #include "crypto/crypto_keys.h" #include "crypto/crypto_util.h" -#include "allocated_buffer.h" -#include "base_object.h" #include "env.h" #include "memory_tracker.h" diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index 7b3015d9d78b7d..3dfc5483eb072b 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -1,8 +1,7 @@ #include "crypto/crypto_util.h" +#include "async_wrap-inl.h" #include "crypto/crypto_bio.h" #include "crypto/crypto_keys.h" -#include "allocated_buffer-inl.h" -#include "async_wrap-inl.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "node_buffer.h" diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index c431159e6f77f8..7a795fe5e81f94 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -3,15 +3,14 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "env.h" #include "async_wrap.h" -#include "allocated_buffer.h" +#include "env.h" #include "node_errors.h" #include "node_external_reference.h" #include "node_internals.h" +#include "string_bytes.h" #include "util.h" #include "v8.h" -#include "string_bytes.h" #include #include diff --git a/src/env-inl.h b/src/env-inl.h index e0093ebcfecd18..49dea0952fa2b9 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -25,11 +25,11 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include "aliased_buffer.h" -#include "allocated_buffer-inl.h" #include "callback_queue-inl.h" #include "env.h" #include "node.h" #include "node_context_data.h" +#include "node_internals.h" #include "node_perf_common.h" #include "util-inl.h" #include "uv.h" @@ -43,6 +43,16 @@ namespace node { +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; +} + inline v8::Isolate* IsolateData::isolate() const { return isolate_; } @@ -970,7 +980,7 @@ inline uv_buf_t Environment::allocate_managed_buffer( std::unique_ptr bs = v8::ArrayBuffer::NewBackingStore(isolate(), suggested_size); uv_buf_t buf = uv_buf_init(static_cast(bs->Data()), bs->ByteLength()); - released_allocated_buffers()->emplace(buf.base, std::move(bs)); + released_allocated_buffers_.emplace(buf.base, std::move(bs)); return buf; } @@ -978,20 +988,14 @@ inline std::unique_ptr Environment::release_managed_buffer( const uv_buf_t& buf) { std::unique_ptr bs; if (buf.base != nullptr) { - auto map = released_allocated_buffers(); - auto it = map->find(buf.base); - CHECK_NE(it, map->end()); + auto it = released_allocated_buffers_.find(buf.base); + CHECK_NE(it, released_allocated_buffers_.end()); bs = std::move(it->second); - map->erase(it); + released_allocated_buffers_.erase(it); } return bs; } -std::unordered_map>* - Environment::released_allocated_buffers() { - return &released_allocated_buffers_; -} - inline void Environment::ThrowError(const char* errmsg) { ThrowError(v8::Exception::Error, errmsg); } diff --git a/src/env.cc b/src/env.cc index 8631ac39a92876..e2e4854f5e02b6 100644 --- a/src/env.cc +++ b/src/env.cc @@ -1,5 +1,4 @@ #include "env.h" -#include "allocated_buffer-inl.h" #include "async_wrap.h" #include "base_object-inl.h" #include "debug_utils-inl.h" diff --git a/src/env.h b/src/env.h index d7112a2663bc21..9917e15ff4fd7e 100644 --- a/src/env.h +++ b/src/env.h @@ -138,6 +138,19 @@ enum class FsStatsOffset { constexpr size_t kFsStatsBufferLength = static_cast(FsStatsOffset::kFsStatsFieldsNumber) * 2; +// Disables zero-filling for ArrayBuffer allocations in this scope. This is +// similar to how we implement Buffer.allocUnsafe() in JS land. +class NoArrayBufferZeroFillScope { + public: + inline explicit NoArrayBufferZeroFillScope(IsolateData* isolate_data); + inline ~NoArrayBufferZeroFillScope(); + + private: + NodeArrayBufferAllocator* node_allocator_; + + friend class Environment; +}; + // PER_ISOLATE_* macros: We have a lot of per-isolate properties // and adding and maintaining their getters and setters by hand would be // difficult so let's make the preprocessor generate them for us. @@ -555,7 +568,6 @@ constexpr size_t kFsStatsBufferLength = V(url_constructor_function, v8::Function) class Environment; -struct AllocatedBuffer; typedef size_t SnapshotIndex; class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { @@ -1456,8 +1468,6 @@ class Environment : public MemoryRetainer { inline uv_buf_t allocate_managed_buffer(const size_t suggested_size); inline std::unique_ptr release_managed_buffer( const uv_buf_t& buf); - inline std::unordered_map>* - released_allocated_buffers(); void AddUnmanagedFd(int fd); void RemoveUnmanagedFd(int fd); @@ -1631,8 +1641,8 @@ class Environment : public MemoryRetainer { // the source passed to LoadEnvironment() directly instead. std::unique_ptr main_utf16_; - // Used by AllocatedBuffer::release() to keep track of the BackingStore for - // a given pointer. + // Used by allocate_managed_buffer() and release_managed_buffer() to keep + // track of the BackingStore for a given pointer. std::unordered_map> released_allocated_buffers_; }; diff --git a/src/node.cc b/src/node.cc index 22b07af7d84fe8..2abc8ba5334c47 100644 --- a/src/node.cc +++ b/src/node.cc @@ -43,7 +43,6 @@ #include "node_version.h" #if HAVE_OPENSSL -#include "allocated_buffer-inl.h" // Inlined functions needed by node_crypto.h #include "node_crypto.h" #include #endif diff --git a/src/node_buffer.cc b/src/node_buffer.cc index dcf5d84ca34a2e..5b2186feb8c707 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -20,7 +20,6 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "node_buffer.h" -#include "allocated_buffer-inl.h" #include "node.h" #include "node_blob.h" #include "node_errors.h" diff --git a/src/node_http2.cc b/src/node_http2.cc index df19efa5379945..b1665782a90c12 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1,12 +1,11 @@ +#include "node_http2.h" #include "aliased_buffer.h" -#include "allocated_buffer-inl.h" #include "aliased_struct-inl.h" #include "debug_utils-inl.h" #include "histogram-inl.h" #include "memory_tracker-inl.h" #include "node.h" #include "node_buffer.h" -#include "node_http2.h" #include "node_http_common-inl.h" #include "node_mem-inl.h" #include "node_perf.h" diff --git a/src/stream_base.cc b/src/stream_base.cc index 2b3fbe38ff2872..a9ea347ca6bd47 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -1,7 +1,6 @@ #include "stream_base.h" // NOLINT(build/include_inline) #include "stream_base-inl.h" #include "stream_wrap.h" -#include "allocated_buffer-inl.h" #include "env-inl.h" #include "js_stream.h"