Skip to content

Commit

Permalink
squash: properly manage memory for builtin source
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Dawson <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Aug 25, 2022
1 parent 3a745a1 commit 5bb29e0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/node_builtins.cc
Expand Up @@ -5,8 +5,6 @@
#include "node_internals.h"
#include "util-inl.h"

#include <unicode/unistr.h>

namespace node {
namespace builtins {

Expand Down Expand Up @@ -239,7 +237,6 @@ MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
#endif // NODE_BUILTIN_MODULES_PATH
}

static std::unique_ptr<icu::UnicodeString> save_source_utf16 = nullptr;
void BuiltinLoader::AddExternalizedBuiltin(const char* id,
const char* filename) {
std::string source;
Expand All @@ -257,12 +254,10 @@ void BuiltinLoader::AddExternalizedBuiltin(const char* id,
icu::StringPiece(source.data(), source.length()));
auto source_utf16 = std::make_unique<icu::UnicodeString>(utf16);
Add(id,
UnionBytes(reinterpret_cast<const uint16_t*>(utf16.getBuffer()),
UnionBytes(reinterpret_cast<const uint16_t*>((*source_utf16).getBuffer()),
utf16.length()));
// TODO(mhdawson): store pointers in env to handle them being freed.
// and to allow more than one
// env->add_externalized_builtin_string(std::move(source_utf16));
save_source_utf16 = std::move(source_utf16);
// keep source bytes for builtin alive while BuiltinLoader exists
GetInstance()->externalized_source_bytes_.push_back(std::move(source_utf16));
}

// Returns Local<Function> of the compiled module if return_code_cache
Expand Down
3 changes: 3 additions & 0 deletions src/node_builtins.h
Expand Up @@ -3,6 +3,8 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <unicode/unistr.h>
#include <list>
#include <map>
#include <memory>
#include <set>
Expand Down Expand Up @@ -127,6 +129,7 @@ class NODE_EXTERN_PRIVATE BuiltinLoader {
BuiltinSourceMap source_;
BuiltinCodeCacheMap code_cache_;
UnionBytes config_;
std::list<std::unique_ptr<icu::UnicodeString>> externalized_source_bytes_;

// Used to synchronize access to the code cache map
Mutex code_cache_mutex_;
Expand Down

0 comments on commit 5bb29e0

Please sign in to comment.