mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
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>
42 lines
1.2 KiB
C++
42 lines
1.2 KiB
C++
#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_
|