From 0ae46a7a862e26ccd611aed4d189a5e480639a4c Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 21 Apr 2019 13:47:30 +0800 Subject: src: use predefined AliasedBuffer types in the code base Instead of allowing the callers to instantiate the template with any numeric types (such as aliasing a Uint8Array to double[]), predefine types that make sense and use those instead. PR-URL: https://github.com/nodejs/node/pull/27334 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/aliased_buffer.h | 58 ++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 27 deletions(-) (limited to 'src/aliased_buffer.h') diff --git a/src/aliased_buffer.h b/src/aliased_buffer.h index 498bab4d232..868d495be9e 100644 --- a/src/aliased_buffer.h +++ b/src/aliased_buffer.h @@ -3,12 +3,16 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "v8.h" +#include #include "util-inl.h" +#include "v8.h" namespace node { /** + * Do not use this class directly when creating instances of it - use the + * Aliased*Array defined at the end of this file instead. + * * This class encapsulates the technique of having a native buffer mapped to * a JS object. Writes to the native buffer can happen efficiently without * going through JS, and the data is then available to user's via the exposed @@ -22,18 +26,16 @@ namespace node { * The encapsulation herein provides a placeholder where such writes can be * observed. Any notification APIs will be left as a future exercise. */ -template ::value>> -class AliasedBuffer { +class AliasedBufferBase { public: - AliasedBuffer(v8::Isolate* isolate, const size_t count) - : isolate_(isolate), - count_(count), - byte_offset_(0) { + AliasedBufferBase(v8::Isolate* isolate, const size_t count) + : isolate_(isolate), count_(count), byte_offset_(0) { CHECK_GT(count, 0); const v8::HandleScope handle_scope(isolate_); - const size_t size_in_bytes = MultiplyWithOverflowCheck(sizeof(NativeT), count); @@ -48,22 +50,20 @@ class AliasedBuffer { } /** - * Create an AliasedBuffer over a sub-region of another aliased buffer. + * Create an AliasedBufferBase over a sub-region of another aliased buffer. * The two will share a v8::ArrayBuffer instance & * a native buffer, but will each read/write to different sections of the * native buffer. * * Note that byte_offset must by aligned by sizeof(NativeT). */ - // TODO(refack): refactor into a non-owning `AliasedBufferView` - AliasedBuffer(v8::Isolate* isolate, - const size_t byte_offset, - const size_t count, - const AliasedBuffer& backing_buffer) - : isolate_(isolate), - count_(count), - byte_offset_(byte_offset) { + // TODO(refack): refactor into a non-owning `AliasedBufferBaseView` + AliasedBufferBase( + v8::Isolate* isolate, + const size_t byte_offset, + const size_t count, + const AliasedBufferBase& backing_buffer) + : isolate_(isolate), count_(count), byte_offset_(byte_offset) { const v8::HandleScope handle_scope(isolate_); v8::Local ab = backing_buffer.GetArrayBuffer(); @@ -81,7 +81,7 @@ class AliasedBuffer { js_array_ = v8::Global(isolate, js_array); } - AliasedBuffer(const AliasedBuffer& that) + AliasedBufferBase(const AliasedBufferBase& that) : isolate_(that.isolate_), count_(that.count_), byte_offset_(that.byte_offset_), @@ -89,8 +89,8 @@ class AliasedBuffer { js_array_ = v8::Global(that.isolate_, that.GetJSArray()); } - AliasedBuffer& operator=(AliasedBuffer&& that) noexcept { - this->~AliasedBuffer(); + AliasedBufferBase& operator=(AliasedBufferBase&& that) noexcept { + this->~AliasedBufferBase(); isolate_ = that.isolate_; count_ = that.count_; byte_offset_ = that.byte_offset_; @@ -109,10 +109,8 @@ class AliasedBuffer { */ class Reference { public: - Reference(AliasedBuffer* aliased_buffer, size_t index) - : aliased_buffer_(aliased_buffer), - index_(index) { - } + Reference(AliasedBufferBase* aliased_buffer, size_t index) + : aliased_buffer_(aliased_buffer), index_(index) {} Reference(const Reference& that) : aliased_buffer_(that.aliased_buffer_), @@ -149,7 +147,7 @@ class AliasedBuffer { } private: - AliasedBuffer* aliased_buffer_; + AliasedBufferBase* aliased_buffer_; size_t index_; }; @@ -216,7 +214,7 @@ class AliasedBuffer { // Should only be used to extend the array. // Should only be used on an owning array, not one created as a sub array of - // an owning `AliasedBuffer`. + // an owning `AliasedBufferBase`. void reserve(size_t new_capacity) { DCHECK_GE(new_capacity, count_); DCHECK_EQ(byte_offset_, 0); @@ -251,6 +249,12 @@ class AliasedBuffer { NativeT* buffer_; v8::Global js_array_; }; + +typedef AliasedBufferBase AliasedInt32Array; +typedef AliasedBufferBase AliasedUint8Array; +typedef AliasedBufferBase AliasedUint32Array; +typedef AliasedBufferBase AliasedFloat64Array; +typedef AliasedBufferBase AliasedBigUint64Array; } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -- cgit v1.2.3