Skip to content

feat: set typed gc object more #145

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

Open
wants to merge 4 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions deps/message-port/nd/gc-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "nd-debug.h"
#include "nd-logger.h"

namespace nd {

#define CLR_YELLOW "\033[0;33m"
#define CLR_MAGENTA "\033[0;35m"
#define TRACE_GC_START(fmt, ...) TRACEF0(GC, "GC" fmt, ##__VA_ARGS__)
Expand Down Expand Up @@ -253,18 +255,25 @@ void MemoryUtil::PrintEveryReachableGCObjects() {
#endif
}

void MemoryUtil::GcFull() {
void MemoryUtil::GcFull(bool includeStack) {
TRACE_GC_START();
LOG_HANDLER("[FULL GC]");
GC_register_mark_stack_func([]() {
// do nothing for skip stack
// assume there is no gc-object on stack
});

if (includeStack) {
GC_register_mark_stack_func([]() {
// do nothing for skip stack
// assume there is no gc-object on stack
});
}

GC_gcollect();
GC_gcollect();
GC_gcollect_and_unmap();
GC_register_mark_stack_func(nullptr);

if (includeStack) {
GC_register_mark_stack_func(nullptr);
}

GC_gcollect();
TRACE_GC_END();
}
Expand Down Expand Up @@ -336,3 +345,5 @@ void MemoryUtil::GcRegisterFinalizer(
void* gcPtr, GCAllocatedMemoryFinalizerWithData callback, void* data) {
REGISTER_FINALIZER(gcPtr, callback, data);
}

} // namespace nd
6 changes: 5 additions & 1 deletion deps/message-port/nd/gc-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ValueRef;
class ObjectRef;
} // namespace Escargot

namespace nd {

// typedef of GC-aware vector
template <typename T,
bool isEraseStrategyStrict = false,
Expand Down Expand Up @@ -113,7 +115,7 @@ class MemoryUtil {

static void GcSetWarningListener(OnGCWarnEventListener callback);
static void GcPrintGCMemoryUsage(void* data);
static void GcFull();
static void GcFull(bool includeStack = true);
static void GcInvokeFinalizers();
static void Gc();

Expand Down Expand Up @@ -141,3 +143,5 @@ class MemoryUtil {
std::function<bool(uint, double)> filter = nullptr);
static GCTracer tracer;
};

} // namespace nd
6 changes: 3 additions & 3 deletions include/lwnode/gc-descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

#define _TYPED_GC_NEW_ARG1(Class) \
void* operator new(size_t size) { \
static bool typeInited = false; \
static GC_descr descr; \
thread_local static bool typeInited = false; \
thread_local static GC_descr descr; \
if (!typeInited) { \
GC_word desc[GC_BITMAP_SIZE(Class)] = {0}; \
Class::fillGCDescriptor(desc); \
Expand Down Expand Up @@ -57,7 +57,7 @@ inline void markHashTable(GC_word* desc, size_t base) {
#endif
}

#define SET_GC_COLLECTION(Class, name) \
#define SET_GC_MAP_OR_SET(Class, name) \
markHashTable(desc, GC_WORD_OFFSET(Class, name));

#define END_IMPLEMENT_TYPED_GC_NEW() }
Expand Down
1 change: 1 addition & 0 deletions message-port.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'<(source_dir)/nd/es-helper.cc',
'<(source_dir)/nd/nd-debug.cc',
'<(source_dir)/nd/nd-logger.cc',
'<(source_dir)/nd/gc-util.cc',
],
'all_dependent_settings': {
'include_dirs': [
Expand Down
3 changes: 3 additions & 0 deletions src/api/arraybuffer-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ArrayBufferAllocatorDecorator : public v8::ArrayBuffer::Allocator,
}
void printState();

BEGIN_IMPLEMENT_TYPED_GC_NEW(ArrayBufferAllocatorDecorator);
END_IMPLEMENT_TYPED_GC_NEW();

private:
size_t currentMemorySize_ = 0;
size_t peakMemorySize_ = 0;
Expand Down
13 changes: 11 additions & 2 deletions src/api/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,23 @@ class ContextWrap : public ValueWrap {

void initDebugger();

private:
EmbedderDataMap* embedder_data_{nullptr};
BEGIN_IMPLEMENT_TYPED_GC_NEW(ContextWrap, ValueWrap);
SET_GC_POINTER(ContextWrap, embedder_data_);
SET_GC_POINTER(ContextWrap, isolate_);
SET_GC_POINTER(ContextWrap, context_);
SET_GC_POINTER(ContextWrap, bindingObject_);
SET_GC_POINTER(ContextWrap, security_token_);
SET_GC_POINTER(ContextWrap, callSite_);
END_IMPLEMENT_TYPED_GC_NEW();

private:
ContextWrap(IsolateWrap* isolate,
v8::ExtensionConfiguration* extensionConfiguration);
void setEmbedderData(int index, void* value);
void* getEmbedderData(int index);

EmbedderDataMap* embedder_data_{nullptr};

IsolateWrap* isolate_ = nullptr;
Escargot::ContextRef* context_ = nullptr;
Escargot::ObjectRef* bindingObject_ = nullptr;
Expand Down
8 changes: 8 additions & 0 deletions src/api/global-handles.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class GlobalHandles : public gc {

virtual size_t handles_count() const = 0;

BEGIN_IMPLEMENT_TYPED_GC_NEW(GlobalHandles);
END_IMPLEMENT_TYPED_GC_NEW();

private:
Isolate* const isolate_ = nullptr;
};
Expand Down Expand Up @@ -107,6 +110,11 @@ class GlobalHandles final : public v8::internal::GlobalHandles {
GcObjectInfo* findGcObjectInfo(ValueWrap* value);
void removeGcObjectInfo(ValueWrap* lwValue);

BEGIN_IMPLEMENT_TYPED_GC_NEW(GlobalHandles, v8::internal::GlobalHandles);
SET_GC_MAP_OR_SET(GlobalHandles, persistentValues_);
SET_GC_POINTER(GlobalHandles, isolate_);
END_IMPLEMENT_TYPED_GC_NEW();

private:
GCUnorderedMap<ValueWrap*, size_t> persistentValues_;
IsolateWrap* isolate_{nullptr};
Expand Down
8 changes: 8 additions & 0 deletions src/api/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class HandleWrap : public gc {
std::string getHandleInfoString() const;
static HandleWrap* as(void* address);

BEGIN_IMPLEMENT_TYPED_GC_NEW(HandleWrap);
SET_GC_POINTER(HandleWrap, val_);
END_IMPLEMENT_TYPED_GC_NEW();

protected:
HandleWrap() = default;
void copy(HandleWrap* that, Location location);
Expand Down Expand Up @@ -130,6 +134,10 @@ class PersistentWrap : public ValueWrap {
std::string getPersistentInfoString();
void invokeFinalizer();

BEGIN_IMPLEMENT_TYPED_GC_NEW(PersistentWrap, ValueWrap);
SET_GC_POINTER(PersistentWrap, holder_);
END_IMPLEMENT_TYPED_GC_NEW();

private:
PersistentWrap(ValueWrap* ptr);

Expand Down
4 changes: 4 additions & 0 deletions src/api/handlescope.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class HandleScopeWrap : public gc {
Type type() const { return type_; }
v8Scope_t* v8Scope() const { return v8scope_; }

BEGIN_IMPLEMENT_TYPED_GC_NEW(HandleScopeWrap);
SET_GC_POINTER(HandleScopeWrap, handles_);
END_IMPLEMENT_TYPED_GC_NEW();

private:
HandleScopeWrap(HandleScopeWrap::Type type);
void add(HandleWrap* value);
Expand Down
9 changes: 5 additions & 4 deletions src/api/isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ IsolateWrap::IsolateWrap() {

threadManager_ = new ThreadManager();

// NOTE: check lock_gc_release(); is needed (and where)
// lock_gc_release();
// The following ensures this instance is retained using PersistentHolder.
lock_gc_release();

Memory::gcRegisterFinalizer(this, [](void* self) {
reinterpret_cast<IsolateWrap*>(self)->~IsolateWrap();
});
Expand Down Expand Up @@ -320,8 +321,8 @@ IsolateWrap* IsolateWrap::New() {
void IsolateWrap::Dispose() {
LWNODE_CALL_TRACE_ID(ISOWRAP);
LWNODE_CALL_TRACE_GC_START();
// NOTE: check unlock_gc_release(); is needed (and where)
// unlock_gc_release();

unlock_gc_release();

global_handles()->dispose();
RegisteredExtension::unregisterAll();
Expand Down
22 changes: 20 additions & 2 deletions src/api/isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class Isolate : public gc {

EscargotShim::GlobalHandles* global_handles() { return global_handles_; }

BEGIN_IMPLEMENT_TYPED_GC_NEW(Isolate);
SET_GC_POINTER(Isolate, scheduled_exception_);
SET_GC_POINTER(Isolate, global_handles_);
SET_GC_POINTER(Isolate, pending_exception_);
SET_GC_POINTER(Isolate, pending_message_obj_);
END_IMPLEMENT_TYPED_GC_NEW();

protected:
void set_pending_exception(Escargot::ValueRef* exception_obj);
void set_pending_message_obj(Escargot::ValueRef* message_obj);
Expand Down Expand Up @@ -197,8 +204,6 @@ class IsolateWrap final : public v8::internal::Isolate {
return arrayBufferDecorator_->array_buffer_allocator();
}

ArrayBufferAllocatorDecorator* arrayBufferDecorator_ = nullptr;

static IsolateWrap* GetCurrent();

// HandleScope & Handle
Expand Down Expand Up @@ -279,6 +284,19 @@ class IsolateWrap final : public v8::internal::Isolate {

State getState() { return state_; }

BEGIN_IMPLEMENT_TYPED_GC_NEW(IsolateWrap, v8::internal::Isolate);
SET_GC_POINTER(IsolateWrap, arrayBufferDecorator_);
SET_GC_POINTER(IsolateWrap, eternals_);
SET_GC_MAP_OR_SET(IsolateWrap, backingStoreCounter_);
SET_GC_POINTER(IsolateWrap, handleScopes_);
SET_GC_POINTER(IsolateWrap, contextScopes_);
SET_GC_POINTER(IsolateWrap, apiSymbols_);
SET_GC_POINTER(IsolateWrap, apiPrivateSymbols_);
SET_GC_POINTER(IsolateWrap, threadManager_);
END_IMPLEMENT_TYPED_GC_NEW();

ArrayBufferAllocatorDecorator* arrayBufferDecorator_ = nullptr;

private:
IsolateWrap();

Expand Down
1 change: 1 addition & 0 deletions src/api/utils/gc-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <GCUtil.h>
#include "compiler.h"
#include "gc-container.h"
#include "gc-descriptor.h"
#include "sf-vector.h"

#include <string>
Expand Down
Loading