diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-18 17:30:10 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-02-25 02:01:03 +0100 |
commit | 376735345088d4b37e0e289c2eb7720419ed37ee (patch) | |
tree | 4771426f776ca9e7e30fab08e1a188f8959b29af /src/node_internals.h | |
parent | a8ba838e1a94c17ecf7a290c1951040d2957cb62 (diff) | |
download | node-new-376735345088d4b37e0e289c2eb7720419ed37ee.tar.gz |
src: add debugging array allocator
Add a subclass of `ArrayBufferAllocator` that performs additional
debug checking, which in particular verifies that:
- All `ArrayBuffer` backing stores have been allocated with this
allocator, or have been explicitly marked as coming from a
compatible source.
- All memory allocated by the allocator has been freed once it is
destroyed.
PR-URL: https://github.com/nodejs/node/pull/26207
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/node_internals.h')
-rw-r--r-- | src/node_internals.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/node_internals.h b/src/node_internals.h index 6f09708371..9ac2b0a331 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -109,11 +109,34 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { void* AllocateUninitialized(size_t size) override { return node::UncheckedMalloc(size); } void Free(void* data, size_t) override { free(data); } + virtual void* Reallocate(void* data, size_t old_size, size_t size) { + return static_cast<void*>( + UncheckedRealloc<char>(static_cast<char*>(data), size)); + } + virtual void RegisterPointer(void* data, size_t size) {} + virtual void UnregisterPointer(void* data, size_t size) {} private: uint32_t zero_fill_field_ = 1; // Boolean but exposed as uint32 to JS land. }; +class DebuggingArrayBufferAllocator final : public ArrayBufferAllocator { + public: + ~DebuggingArrayBufferAllocator() override; + void* Allocate(size_t size) override; + void* AllocateUninitialized(size_t size) override; + void Free(void* data, size_t size) override; + void* Reallocate(void* data, size_t old_size, size_t size) override; + void RegisterPointer(void* data, size_t size) override; + void UnregisterPointer(void* data, size_t size) override; + + private: + void RegisterPointerInternal(void* data, size_t size); + void UnregisterPointerInternal(void* data, size_t size); + Mutex mutex_; + std::unordered_map<void*, size_t> allocations_; +}; + namespace Buffer { v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len); v8::MaybeLocal<v8::Object> New(Environment* env, size_t size); |