Skip to content

Commit

Permalink
Fix alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
CannibalVox committed Apr 10, 2023
1 parent 27d0f19 commit 8b05e8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
20 changes: 10 additions & 10 deletions alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
23 changes: 15 additions & 8 deletions allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import "unsafe"

type allocatorPage struct {
remainingSize int
nextOffset int
size int
nextOffset int
size int

buffer unsafe.Pointer
}
Expand All @@ -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,
}
Expand All @@ -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
}

Expand All @@ -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)
Expand Down

0 comments on commit 8b05e8f

Please sign in to comment.