Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Go back to manage startup data garbage colection
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Jan 19, 2022
1 parent 2075982 commit a09cb03
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
5 changes: 1 addition & 4 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ func NewIsolate(opts ...createOptions) *Isolate {
var cOptions C.IsolateOptions

if params.startupData != nil {
// p := C.CString(string(params.startupData.data))
cOptions.snapshot_blob_data = params.startupData.data
// defer C.free(unsafe.Pointer(p))
cOptions.snapshot_blob_raw_size = params.startupData.raw_size
cOptions.snapshot_blob = params.startupData.ptr
}

iso := &Isolate{
Expand Down
17 changes: 9 additions & 8 deletions snapshot_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ const (
)

type StartupData struct {
data *C.char
raw_size C.int
index C.size_t
ptr *C.RtnSnapshotBlob
index C.size_t
}

func (s *StartupData) Dispose() {
if s.ptr != nil {
C.SnapshotBlobDelete(s.ptr)
}
}

type SnapshotCreator struct {
Expand Down Expand Up @@ -70,11 +75,7 @@ func (s *SnapshotCreator) Create(functionCode FunctionCodeHandling) (*StartupDat
s.ctx.ptr = nil
s.iso.ptr = nil

raw_size := rtn.raw_size
// data := C.GoBytes(unsafe.Pointer(rtn.data), raw_size)
// defer C.free(unsafe.Pointer(rtn.data))

return &StartupData{data: rtn.data, raw_size: raw_size, index: s.index}, nil
return &StartupData{ptr: rtn, index: s.index}, nil
}

func (s *SnapshotCreator) Dispose() {
Expand Down
4 changes: 3 additions & 1 deletion snapshot_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestCreateSnapshot(t *testing.T) {

data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
fatalIf(t, err)
defer data.Dispose()

iso := v8.NewIsolate(v8.WithStartupData(data))
defer iso.Dispose()
Expand Down Expand Up @@ -59,8 +60,9 @@ func TestCreateSnapshotErrorAfterSuccessfullCreate(t *testing.T) {
err := snapshotCreator.AddContext(snapshotCreatoCtx)
fatalIf(t, err)

_, err = snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
data, err := snapshotCreator.Create(v8.FunctionCodeHandlingKlear)
fatalIf(t, err)
defer data.Dispose()

err = snapshotCreator.AddContext(snapshotCreatoCtx)
if err == nil {
Expand Down
26 changes: 13 additions & 13 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ IsolatePtr NewIsolate(IsolateOptions options) {
params.array_buffer_allocator = default_allocator;

StartupData* startup_data;
if (options.snapshot_blob_data) {
startup_data = new StartupData{options.snapshot_blob_data,
options.snapshot_blob_raw_size};
if (options.snapshot_blob != nullptr) {
startup_data = new StartupData{options.snapshot_blob->data,
options.snapshot_blob->raw_size};
params.snapshot_blob = startup_data;
} else {
startup_data = nullptr;
Expand Down Expand Up @@ -308,25 +308,25 @@ size_t AddContext(SnapshotCreatorPtr snapshotCreator, ContextPtr ctx) {
return snapshotCreator->AddContext(local_ctx);
}

RtnSnapshotBlob CreateBlob(SnapshotCreatorPtr snapshotCreator,
RtnSnapshotBlob* CreateBlob(SnapshotCreatorPtr snapshotCreator,
ContextPtr ctx,
int function_code_handling) {
RtnSnapshotBlob rtn = {};
ContextFree(ctx);
// kKeep - keeps any compiled functions
// kClear - does not keep any compiled functions
StartupData startup_data = snapshotCreator->CreateBlob(
SnapshotCreator::FunctionCodeHandling(function_code_handling));

int length = startup_data.raw_size;

// char* data = (char*)malloc(length);
// memcpy(data, startup_data.data, length);

rtn.data = startup_data.data;
rtn.raw_size = length;
RtnSnapshotBlob* sb = new RtnSnapshotBlob;
sb->data = startup_data.data;
sb->raw_size = startup_data.raw_size;
delete snapshotCreator;
return rtn;
return sb;
}

void SnapshotBlobDelete(RtnSnapshotBlob* ptr) {
delete[] ptr->data;
delete ptr;
}

/********** Exceptions & Errors **********/
Expand Down
6 changes: 3 additions & 3 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ typedef struct {
} CompileOptions;

typedef struct {
const char* snapshot_blob_data;
int snapshot_blob_raw_size;
RtnSnapshotBlob* snapshot_blob;
} IsolateOptions;

typedef struct {
Expand Down Expand Up @@ -157,10 +156,11 @@ extern void IsolateTerminateExecution(IsolatePtr ptr);
extern int IsolateIsExecutionTerminating(IsolatePtr ptr);
extern IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr);

extern void SnapshotBlobDelete(RtnSnapshotBlob* ptr);
extern RtnSnapshotCreator NewSnapshotCreator();
extern void DeleteSnapshotCreator(SnapshotCreatorPtr snapshotCreator);
extern size_t AddContext(SnapshotCreatorPtr snapshotCreator, ContextPtr ctx);
extern RtnSnapshotBlob CreateBlob(SnapshotCreatorPtr snapshotCreator,
extern RtnSnapshotBlob* CreateBlob(SnapshotCreatorPtr snapshotCreator,
ContextPtr ctx,
int function_code_handling);

Expand Down

0 comments on commit a09cb03

Please sign in to comment.