diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-12-01 03:30:40 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2019-12-09 12:23:15 +0300 |
commit | 041daaa273e7ef68b590afbcb233e56d9661b950 (patch) | |
tree | f760a3ed4f7a30e285970d1fb134d3da23bfc420 /src/node_mem.h | |
parent | ccf0917aefa31725c405daefddef881c8f9c7ea5 (diff) |
src: port memory-tracking allocator from QUIC repo
This implements a memory-tracking allocator that can be used to
provide memory allocation facilities to several thread-safe C
libraries, including nghttp2, nghttp3, ngtcp3 and uvwasi.
Refs: https://github.com/nodejs/quic/blob/34ee0bc96f804c73cb22b2945a1a78f780938492/src/node_mem.h
Co-authored-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/30745
Refs: https://github.com/nodejs/quic/pull/126
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'src/node_mem.h')
-rw-r--r-- | src/node_mem.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/node_mem.h b/src/node_mem.h new file mode 100644 index 00000000000..0d3388ad476 --- /dev/null +++ b/src/node_mem.h @@ -0,0 +1,41 @@ +#ifndef SRC_NODE_MEM_H_ +#define SRC_NODE_MEM_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include <cstddef> + +namespace node { +namespace mem { + +// Both ngtcp2 and nghttp2 allow custom allocators that +// follow exactly the same structure and behavior, but +// use different struct names. To allow for code re-use, +// the NgLibMemoryManager template class can be used for both. + +template <typename Class, typename AllocatorStructName> +class NgLibMemoryManager { + public: + // Class needs to provide these methods: + // void CheckAllocatedSize(size_t previous_size) const; + // void IncreaseAllocatedSize(size_t size); + // void DecreaseAllocatedSize(size_t size); + // Environment* env() const; + + AllocatorStructName MakeAllocator(); + + void StopTrackingMemory(void* ptr); + + private: + static void* ReallocImpl(void* ptr, size_t size, void* user_data); + static void* MallocImpl(size_t size, void* user_data); + static void FreeImpl(void* ptr, void* user_data); + static void* CallocImpl(size_t nmemb, size_t size, void* user_data); +}; + +} // namespace mem +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_NODE_MEM_H_ |