Skip to content

Commit

Permalink
chore: Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed Apr 11, 2022
1 parent 6c88bd3 commit d4a1446
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
12 changes: 2 additions & 10 deletions memory/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ func (a *ResourceAllocator) allocator() memory.Allocator {
}

type GcAllocator struct {
mem *ResourceAllocator
mem Allocator
}

func NewGcAllocator(mem *ResourceAllocator) *GcAllocator {
func NewGcAllocator(mem Allocator) *GcAllocator {
return &GcAllocator{
mem: mem,
}
Expand Down Expand Up @@ -300,14 +300,6 @@ func (a *GcAllocator) Free(b []byte) {

func (a *GcAllocator) Account(size int) error { return a.mem.Account(size) }

func (a *GcAllocator) Allocated() int64 {
return a.mem.Allocated()
}

func (a *GcAllocator) MaxAllocated() int64 {
return a.mem.MaxAllocated()
}

// Manager will manage the memory allowed for the Allocator.
// The Allocator may use the Manager to request additional memory or to
// give back memory that is currently in use by the Allocator
Expand Down
22 changes: 12 additions & 10 deletions memory/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func TestAllocator_GC_Allocate(t *testing.T) {
mem := arrowmemory.NewCheckedAllocator(memory.DefaultAllocator)
defer mem.AssertSize(t, 0)

allocator := memory.NewGcAllocator(memory.NewResourceAllocator(mem))
b := allocator.Allocate(64)
allocator := memory.NewResourceAllocator(mem)
gc := memory.NewGcAllocator(allocator)
b := gc.Allocate(64)

assert.Equal(t, 64, mem.CurrentAlloc(), "unexpected memory allocation.")
if want, got := int64(64), allocator.Allocated(); want != got {
Expand All @@ -41,9 +42,9 @@ func TestAllocator_GC_Allocate(t *testing.T) {
t.Fatalf("unexpected max allocated count -want/+got\n\t- %d\n\t+ %d", want, got)
}

allocator.Free(b)
gc.Free(b)

RunGC(allocator)
RunGC(gc)
mem.AssertSize(t, 0)
if want, got := int64(0), allocator.Allocated(); want != got {
t.Fatalf("unexpected allocated count -want/+got\n\t- %d\n\t+ %d", want, got)
Expand All @@ -57,8 +58,9 @@ func TestAllocator_GC_Reallocate(t *testing.T) {
mem := arrowmemory.NewCheckedAllocator(memory.DefaultAllocator)
defer mem.AssertSize(t, 0)

allocator := memory.NewGcAllocator(memory.NewResourceAllocator(mem))
b := allocator.Allocate(64)
allocator := memory.NewResourceAllocator(mem)
gc := memory.NewGcAllocator(allocator)
b := gc.Allocate(64)

assert.Equal(t, 64, mem.CurrentAlloc(), "unexpected memory allocation.")
if want, got := int64(64), allocator.Allocated(); want != got {
Expand All @@ -68,7 +70,7 @@ func TestAllocator_GC_Reallocate(t *testing.T) {
t.Fatalf("unexpected max allocated count -want/+got\n\t- %d\n\t+ %d", want, got)
}

b = allocator.Reallocate(128, b)
b = gc.Reallocate(128, b)

assert.Equal(t, 128, mem.CurrentAlloc(), "unexpected memory allocation.")
if want, got := int64(128), allocator.Allocated(); want != got {
Expand All @@ -78,9 +80,9 @@ func TestAllocator_GC_Reallocate(t *testing.T) {
t.Fatalf("unexpected max allocated count -want/+got\n\t- %d\n\t+ %d", want, got)
}

allocator.Free(b)
gc.Free(b)

RunGC(allocator)
RunGC(gc)
mem.AssertSize(t, 0)
if want, got := int64(0), allocator.Allocated(); want != got {
t.Fatalf("unexpected allocated count -want/+got\n\t- %d\n\t+ %d", want, got)
Expand Down Expand Up @@ -190,7 +192,7 @@ func TestAllocator_MaxAfterFree(t *testing.T) {

func TestAllocator_Limit(t *testing.T) {
maxLimit := int64(64)
allocator := memory.NewResourceAllocator(&memory.ResourceAllocator{Limit: &maxLimit})
allocator := &memory.ResourceAllocator{Limit: &maxLimit}
if err := allocator.Account(64); err != nil {
t.Fatalf("unexpected error: %s", err)
}
Expand Down

0 comments on commit d4a1446

Please sign in to comment.