-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
wasi: use memory-tracking allocator #30745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b09d63
src: port memory-tracking allocator from QUIC repo
addaleax 34a9e5f
http2: use shared memory tracking implementation
addaleax a550883
http2: track nghttp2-allocated memory in heap snapshot
addaleax 11135bb
deps: update uvwasi
addaleax 05d809e
wasi: use memory-tracking allocator
addaleax 8bf83d3
fixup! src: port memory-tracking allocator from QUIC repo
addaleax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#ifndef SRC_NODE_MEM_INL_H_ | ||
#define SRC_NODE_MEM_INL_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include "node_mem.h" | ||
#include "node_internals.h" | ||
|
||
namespace node { | ||
namespace mem { | ||
|
||
template <typename Class, typename AllocatorStruct> | ||
AllocatorStruct NgLibMemoryManager<Class, AllocatorStruct>::MakeAllocator() { | ||
return AllocatorStruct { | ||
static_cast<void*>(static_cast<Class*>(this)), | ||
MallocImpl, | ||
FreeImpl, | ||
CallocImpl, | ||
ReallocImpl | ||
}; | ||
} | ||
|
||
template <typename Class, typename T> | ||
void* NgLibMemoryManager<Class, T>::ReallocImpl(void* ptr, | ||
size_t size, | ||
void* user_data) { | ||
Class* manager = static_cast<Class*>(user_data); | ||
|
||
size_t previous_size = 0; | ||
char* original_ptr = nullptr; | ||
|
||
// We prepend each allocated buffer with a size_t containing the full | ||
// size of the allocation. | ||
if (size > 0) size += sizeof(size_t); | ||
|
||
if (ptr != nullptr) { | ||
// We are free()ing or re-allocating. | ||
original_ptr = static_cast<char*>(ptr) - sizeof(size_t); | ||
previous_size = *reinterpret_cast<size_t*>(original_ptr); | ||
// This means we called StopTracking() on this pointer before. | ||
if (previous_size == 0) { | ||
// Fall back to the standard Realloc() function. | ||
char* ret = UncheckedRealloc(original_ptr, size); | ||
if (ret != nullptr) | ||
ret += sizeof(size_t); | ||
return ret; | ||
} | ||
} | ||
|
||
manager->CheckAllocatedSize(previous_size); | ||
|
||
char* mem = UncheckedRealloc(original_ptr, size); | ||
|
||
if (mem != nullptr) { | ||
// Adjust the memory info counter. | ||
// TODO(addaleax): Avoid the double bookkeeping we do with | ||
// current_nghttp2_memory_ + AdjustAmountOfExternalAllocatedMemory | ||
// and provide versions of our memory allocation utilities that take an | ||
// Environment*/Isolate* parameter and call the V8 method transparently. | ||
const int64_t new_size = size - previous_size; | ||
manager->IncreaseAllocatedSize(new_size); | ||
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory( | ||
new_size); | ||
*reinterpret_cast<size_t*>(mem) = size; | ||
mem += sizeof(size_t); | ||
} else if (size == 0) { | ||
manager->DecreaseAllocatedSize(previous_size); | ||
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory( | ||
-static_cast<int64_t>(previous_size)); | ||
} | ||
return mem; | ||
} | ||
|
||
template <typename Class, typename T> | ||
void* NgLibMemoryManager<Class, T>::MallocImpl(size_t size, void* user_data) { | ||
return ReallocImpl(nullptr, size, user_data); | ||
} | ||
|
||
template <typename Class, typename T> | ||
void NgLibMemoryManager<Class, T>::FreeImpl(void* ptr, void* user_data) { | ||
if (ptr == nullptr) return; | ||
CHECK_NULL(ReallocImpl(ptr, 0, user_data)); | ||
} | ||
|
||
template <typename Class, typename T> | ||
void* NgLibMemoryManager<Class, T>::CallocImpl(size_t nmemb, | ||
size_t size, | ||
void* user_data) { | ||
size_t real_size = MultiplyWithOverflowCheck(nmemb, size); | ||
void* mem = MallocImpl(real_size, user_data); | ||
if (mem != nullptr) | ||
memset(mem, 0, real_size); | ||
return mem; | ||
} | ||
|
||
template <typename Class, typename T> | ||
void NgLibMemoryManager<Class, T>::StopTrackingMemory(void* ptr) { | ||
size_t* original_ptr = reinterpret_cast<size_t*>( | ||
static_cast<char*>(ptr) - sizeof(size_t)); | ||
Class* manager = static_cast<Class*>(this); | ||
manager->DecreaseAllocatedSize(*original_ptr); | ||
manager->env()->isolate()->AdjustAmountOfExternalAllocatedMemory( | ||
-static_cast<int64_t>(*original_ptr)); | ||
*original_ptr = 0; | ||
} | ||
|
||
} // namespace mem | ||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_NODE_MEM_INL_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef SRC_NODE_MEM_H_ | ||
#define SRC_NODE_MEM_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include <stddef.h> | ||
|
||
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); | ||
lundibundi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.