Skip to content
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

feat: sync gc #2640

Closed
wants to merge 2 commits into from
Closed
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
70 changes: 62 additions & 8 deletions gnovm/pkg/gnolang/alloc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package gnolang

import "reflect"
import (
"fmt"
"reflect"
"runtime"
"sync/atomic"
)

// Keeps track of in-memory allocations.
// In the future, allocations within realm boundaries will be
Expand Down Expand Up @@ -102,8 +107,8 @@
return
}

alloc.bytes += size
if alloc.bytes > alloc.maxBytes {
atomic.AddInt64(&alloc.bytes, size)
if atomic.LoadInt64(&alloc.bytes) > alloc.maxBytes {

Check warning on line 111 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L110-L111

Added lines #L110 - L111 were not covered by tests
panic("allocation limit exceeded")
}
}
Expand Down Expand Up @@ -190,21 +195,50 @@

func (alloc *Allocator) NewString(s string) StringValue {
alloc.AllocateString(int64(len(s)))
return StringValue(s)
ss := StringValue(s)

if alloc != nil {
runtime.SetFinalizer(&ss, func(ss *StringValue) {
atomic.AddInt64(&alloc.bytes, -int64(len(*ss)))
fmt.Printf("deleted string: current bytes used %+v\n", atomic.LoadInt64(&alloc.bytes))
})

Check warning on line 204 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L201-L204

Added lines #L201 - L204 were not covered by tests
}

return ss
}

func (alloc *Allocator) NewListArray(n int) *ArrayValue {
alloc.AllocateListArray(int64(n))
return &ArrayValue{

arr := &ArrayValue{
List: make([]TypedValue, n),
}

if alloc != nil {
runtime.SetFinalizer(arr, func(arr *ArrayValue) {
atomic.AddInt64(&alloc.bytes, -int64(len(arr.List)))
fmt.Printf("deleted list array: current bytes used %+v\n", atomic.LoadInt64(&alloc.bytes))
})

Check warning on line 221 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L218-L221

Added lines #L218 - L221 were not covered by tests
}

return arr
}

func (alloc *Allocator) NewDataArray(n int) *ArrayValue {
alloc.AllocateDataArray(int64(n))
return &ArrayValue{

arr := &ArrayValue{
Data: make([]byte, n),
}

if alloc != nil {
runtime.SetFinalizer(arr, func(arr *ArrayValue) {
atomic.AddInt64(&alloc.bytes, -int64(len(arr.Data)))
fmt.Printf("deleted data array: current bytes used %+v\n", atomic.LoadInt64(&alloc.bytes))
})

Check warning on line 238 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L235-L238

Added lines #L235 - L238 were not covered by tests
}

return arr
}

func (alloc *Allocator) NewArrayFromData(data []byte) *ArrayValue {
Expand Down Expand Up @@ -256,14 +290,34 @@
// NOTE: fields must be allocated (e.g. from NewStructFields)
func (alloc *Allocator) NewStruct(fields []TypedValue) *StructValue {
alloc.AllocateStruct()
return &StructValue{

strct := &StructValue{
Fields: fields,
}

if alloc != nil {
runtime.SetFinalizer(strct, func(strct *StructValue) {
atomic.AddInt64(&alloc.bytes, -allocStruct)
fmt.Printf("deleted struct: current bytes used %+v\n", atomic.LoadInt64(&alloc.bytes))
})

Check warning on line 302 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L299-L302

Added lines #L299 - L302 were not covered by tests
}

return strct
}

func (alloc *Allocator) NewStructFields(fields int) []TypedValue {
alloc.AllocateStructFields(int64(fields))
return make([]TypedValue, fields)

ffs := make([]TypedValue, fields)

if alloc != nil {
runtime.SetFinalizer(&ffs, func(ffs *[]TypedValue) {
atomic.AddInt64(&alloc.bytes, allocStructField*-int64(len(*ffs)))
fmt.Printf("deleted fields: current bytes used %+v\n", atomic.LoadInt64(&alloc.bytes))
})

Check warning on line 317 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L314-L317

Added lines #L314 - L317 were not covered by tests
}

return ffs
}

// NOTE: fields will be allocated.
Expand Down
Loading