diff --git a/go.mod b/go.mod index 11d2ccd..81b866e 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,8 @@ module github.com/ortuman/nuke -go 1.21.7 +go 1.21.8 -require github.com/stretchr/testify v1.8.4 +require github.com/stretchr/testify v1.9.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index fa4b6e6..60ce688 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/monotonic_arena.go b/monotonic_arena.go index 29a17bd..f708a63 100644 --- a/monotonic_arena.go +++ b/monotonic_arena.go @@ -26,8 +26,8 @@ func (s *monotonicBuffer) alloc(size, alignment uintptr) (unsafe.Pointer, bool) s.ptr = unsafe.Pointer(unsafe.SliceData(buf)) } alignOffset := uintptr(0) - for alignedPtr := uintptr(s.ptr) + s.offset; alignedPtr%alignment != 0; alignedPtr++ { - alignOffset++ + if delta := (uintptr(s.ptr) + s.offset) % alignment; delta > 0 { + alignOffset = alignment - delta } allocSize := size + alignOffset @@ -37,15 +37,7 @@ func (s *monotonicBuffer) alloc(size, alignment uintptr) (unsafe.Pointer, bool) ptr := unsafe.Pointer(uintptr(s.ptr) + s.offset + alignOffset) s.offset += allocSize - // This piece of code will be translated into a runtime.memclrNoHeapPointers - // invocation by the compiler, which is an assembler optimized implementation. - // Architecture specific code can be found at src/runtime/memclr_$GOARCH.s - // in Go source (since https://codereview.appspot.com/137880043). - b := unsafe.Slice((*byte)(ptr), size) - - for i := range b { - b[i] = 0 - } + clear(unsafe.Slice((*byte)(ptr), size)) return ptr, true } @@ -67,7 +59,7 @@ func (s *monotonicBuffer) availableBytes() uintptr { // NewMonotonicArena creates a new monotonic arena with a specified number of buffers and a buffer size. func NewMonotonicArena(bufferSize, bufferCount int) Arena { - a := &monotonicArena{} + a := &monotonicArena{buffers: make([]*monotonicBuffer, 0, bufferCount)} for i := 0; i < bufferCount; i++ { a.buffers = append(a.buffers, newMonotonicBuffer(bufferSize)) } @@ -76,7 +68,7 @@ func NewMonotonicArena(bufferSize, bufferCount int) Arena { // Alloc satisfies the Arena interface. func (a *monotonicArena) Alloc(size, alignment uintptr) unsafe.Pointer { - for i := 0; i < len(a.buffers); i++ { + for i := range a.buffers { ptr, ok := a.buffers[i].alloc(size, alignment) if ok { return ptr @@ -87,7 +79,7 @@ func (a *monotonicArena) Alloc(size, alignment uintptr) unsafe.Pointer { // Reset satisfies the Arena interface. func (a *monotonicArena) Reset(release bool) { - for _, s := range a.buffers { - s.reset(release) + for i := range a.buffers { + a.buffers[i].reset(release) } }