From 8b05e8f31008070c6ef4ad07e90e07d8c6049e59 Mon Sep 17 00:00:00 2001 From: Stephen Baynham Date: Mon, 10 Apr 2023 15:03:12 -0700 Subject: [PATCH] Fix alignment --- alloc_test.go | 20 ++++++++++---------- allocator.go | 23 +++++++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/alloc_test.go b/alloc_test.go index 907416d..6b735e0 100644 --- a/alloc_test.go +++ b/alloc_test.go @@ -11,9 +11,9 @@ func TestBasicFunc(t *testing.T) { require.NotNil(t, alloc.Malloc(1000)) require.Len(t, alloc.basePages, 1) - require.Equal(t, 1101, alloc.basePages[0].nextOffset) + require.Equal(t, 1112, alloc.basePages[0].nextOffset) require.Equal(t, 4096, alloc.basePages[0].size) - require.Equal(t, 2995, alloc.basePages[0].remainingSize) + require.Equal(t, 2984, alloc.basePages[0].remainingSize) ReturnAlloc(alloc) @@ -34,13 +34,13 @@ func TestMultiPage(t *testing.T) { require.Len(t, alloc.basePages, 2) require.Len(t, alloc.standaloneAllocs, 0) - require.Equal(t, 3600, alloc.basePages[0].nextOffset) + require.Equal(t, 3616, alloc.basePages[0].nextOffset) require.Equal(t, 4096, alloc.basePages[0].size) - require.Equal(t, 496, alloc.basePages[0].remainingSize) + require.Equal(t, 480, alloc.basePages[0].remainingSize) - require.Equal(t, 900, alloc.basePages[1].nextOffset) + require.Equal(t, 904, alloc.basePages[1].nextOffset) require.Equal(t, 4096, alloc.basePages[1].size) - require.Equal(t, 3196, alloc.basePages[1].remainingSize) + require.Equal(t, 3192, alloc.basePages[1].remainingSize) ReturnAlloc(alloc) @@ -60,9 +60,9 @@ func TestSlightlyTooBigStandalone(t *testing.T) { require.Len(t, alloc.basePages, 1) require.Len(t, alloc.standaloneAllocs, 1) - require.Equal(t, 3600, alloc.basePages[0].nextOffset) + require.Equal(t, 3616, alloc.basePages[0].nextOffset) require.Equal(t, 4096, alloc.basePages[0].size) - require.Equal(t, 496, alloc.basePages[0].remainingSize) + require.Equal(t, 480, alloc.basePages[0].remainingSize) ReturnAlloc(alloc) @@ -82,9 +82,9 @@ func TestCouldBeStandaloneButFit(t *testing.T) { require.Len(t, alloc.basePages, 1) require.Len(t, alloc.standaloneAllocs, 0) - require.Equal(t, 3900, alloc.basePages[0].nextOffset) + require.Equal(t, 3912, alloc.basePages[0].nextOffset) require.Equal(t, 4096, alloc.basePages[0].size) - require.Equal(t, 196, alloc.basePages[0].remainingSize) + require.Equal(t, 184, alloc.basePages[0].remainingSize) ReturnAlloc(alloc) diff --git a/allocator.go b/allocator.go index 3f008fd..134d560 100644 --- a/allocator.go +++ b/allocator.go @@ -8,8 +8,8 @@ import "unsafe" type allocatorPage struct { remainingSize int - nextOffset int - size int + nextOffset int + size int buffer unsafe.Pointer } @@ -18,8 +18,8 @@ func createPage(size int) *allocatorPage { ptr := C.malloc(C.size_t(size)) return &allocatorPage{ remainingSize: size, - nextOffset: 0, - size: size, + nextOffset: 0, + size: size, buffer: ptr, } @@ -40,17 +40,24 @@ func (p *allocatorPage) NextPtr(size int) unsafe.Pointer { } ptr := unsafe.Add(p.buffer, p.nextOffset) + oldOffset := p.nextOffset + p.nextOffset += size - p.remainingSize -= size + alignment := p.nextOffset % 8 + if alignment != 0 { + p.nextOffset = p.nextOffset - (p.nextOffset % 8) + 8 + } + + p.remainingSize -= p.nextOffset - oldOffset return ptr } type Allocator struct { - basePageSize int + basePageSize int considerStandaloneSize int - basePages []*allocatorPage + basePages []*allocatorPage standaloneAllocs []unsafe.Pointer } @@ -72,7 +79,7 @@ func (a *Allocator) Malloc(size int) unsafe.Pointer { } func (a *Allocator) CString(str string) unsafe.Pointer { - strByteLen := len(str)+1 + strByteLen := len(str) + 1 ptr := a.Malloc(strByteLen) ptrSlice := ([]byte)(unsafe.Slice((*byte)(ptr), strByteLen)) copy(ptrSlice, str)