Skip to content

Commit

Permalink
src: use constant strings for memory info names
Browse files Browse the repository at this point in the history
PR-URL: #46087
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
legendecas authored and juanarbol committed Jan 25, 2023
1 parent cea44d8 commit 39662eb
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 14 deletions.
12 changes: 9 additions & 3 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,19 @@ MaybeLocal<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
return ret;
}

std::string AsyncWrap::MemoryInfoName() const {
const char* AsyncWrap::MemoryInfoName() const {
return provider_names[provider_type()];
}

std::string AsyncWrap::diagnostic_name() const {
return MemoryInfoName() + " (" + std::to_string(env()->thread_id()) + ":" +
std::to_string(static_cast<int64_t>(async_id_)) + ")";
char buf[64];
snprintf(buf,
sizeof(buf),
"%s(%" PRIu64 ":%.0f)",
MemoryInfoName(),
env()->thread_id(),
async_id_);
return buf;
}

Local<Object> AsyncWrap::GetOwner() {
Expand Down
2 changes: 1 addition & 1 deletion src/async_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class AsyncWrap : public BaseObject {
v8::Local<v8::Value>* argv);

virtual std::string diagnostic_name() const;
std::string MemoryInfoName() const override;
const char* MemoryInfoName() const override;

static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info);

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {

AdditionalParams* params() { return &params_; }

std::string MemoryInfoName() const override {
const char* MemoryInfoName() const override {
return CryptoJobTraits::JobName;
}

Expand Down
4 changes: 2 additions & 2 deletions src/memory_tracker-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MemoryRetainerNode : public v8::EmbedderGraph::Node {
is_root_node_ = is_root_node;
}

const char* Name() override { return name_.c_str(); }
const char* Name() override { return name_; }
const char* NamePrefix() override { return "Node /"; }
size_t SizeInBytes() override { return size_; }
// TODO(addaleax): Merging this with the "official" WrapperNode() method
Expand Down Expand Up @@ -75,7 +75,7 @@ class MemoryRetainerNode : public v8::EmbedderGraph::Node {

// Otherwise (retainer == nullptr), we set these fields in an ad-hoc way
bool is_root_node_ = false;
std::string name_;
const char* name_;
size_t size_ = 0;
v8::EmbedderGraph::Node::Detachedness detachedness_ =
v8::EmbedderGraph::Node::Detachedness::kUnknown;
Expand Down
6 changes: 3 additions & 3 deletions src/memory_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace node {

// Set the node name of a MemoryRetainer to klass
#define SET_MEMORY_INFO_NAME(Klass) \
inline std::string MemoryInfoName() const override { return #Klass; }
inline const char* MemoryInfoName() const override { return #Klass; }

// Set the self size of a MemoryRetainer to the stack-allocated size of a
// certain class
Expand Down Expand Up @@ -68,7 +68,7 @@ class CleanupHookCallback;
* }
*
* // Or use SET_MEMORY_INFO_NAME(ExampleRetainer)
* std::string MemoryInfoName() const override {
* const char* MemoryInfoName() const override {
* return "ExampleRetainer";
* }
*
Expand Down Expand Up @@ -119,7 +119,7 @@ class MemoryRetainer {
// where all the edges start from the node of the current retainer,
// and point to the nodes as specified by tracker->Track* calls.
virtual void MemoryInfo(MemoryTracker* tracker) const = 0;
virtual std::string MemoryInfoName() const = 0;
virtual const char* MemoryInfoName() const = 0;
virtual size_t SelfSize() const = 0;

virtual v8::Local<v8::Object> WrappedObject() const {
Expand Down
4 changes: 2 additions & 2 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ static void GetActiveResourcesInfo(const FunctionCallbackInfo<Value>& args) {
AsyncWrap* w = req_wrap->GetAsyncWrap();
if (w->persistent().IsEmpty()) continue;
resources_info.emplace_back(
OneByteString(env->isolate(), w->MemoryInfoName().c_str()));
OneByteString(env->isolate(), w->MemoryInfoName()));
}

// Active handles
for (HandleWrap* w : *env->handle_wrap_queue()) {
if (w->persistent().IsEmpty() || !HandleWrap::HasRef(w)) continue;
resources_info.emplace_back(
OneByteString(env->isolate(), w->MemoryInfoName().c_str()));
OneByteString(env->isolate(), w->MemoryInfoName()));
}

// Active timeouts
Expand Down
2 changes: 1 addition & 1 deletion src/node_realm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ void Realm::VerifyNoStrongBaseObjects() {
if (obj->IsNotIndicativeOfMemoryLeakAtExit()) return;
fprintf(stderr,
"Found bad BaseObject during clean exit: %s\n",
obj->MemoryInfoName().c_str());
obj->MemoryInfoName());
fflush(stderr);
ABORT();
});
Expand Down
2 changes: 1 addition & 1 deletion src/tcp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {

SET_NO_MEMORY_INFO()
SET_SELF_SIZE(TCPWrap)
std::string MemoryInfoName() const override {
const char* MemoryInfoName() const override {
switch (provider_type()) {
case ProviderType::PROVIDER_TCPWRAP:
return "TCPSocketWrap";
Expand Down

0 comments on commit 39662eb

Please sign in to comment.