-
Notifications
You must be signed in to change notification settings - Fork 16
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
fix(arrow/memory): Align allocations always #289
Open
lidavidm
wants to merge
4
commits into
apache:main
Choose a base branch
from
lidavidm:gh-282
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,21 +19,20 @@ package mallocator | |
|
||
// #include <stdlib.h> | ||
// #include <string.h> | ||
// | ||
// void* realloc_and_initialize(void* ptr, size_t old_len, size_t new_len) { | ||
// void* new_ptr = realloc(ptr, new_len); | ||
// if (new_ptr && new_len > old_len) { | ||
// memset(new_ptr + old_len, 0, new_len - old_len); | ||
// } | ||
// return new_ptr; | ||
// } | ||
import "C" | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"unsafe" | ||
) | ||
|
||
func roundToPowerOf2(v, round uintptr) uintptr { | ||
forceCarry := round - 1 | ||
truncateMask := ^forceCarry | ||
return (v + forceCarry) & truncateMask | ||
} | ||
|
||
// Mallocator is an allocator which defers to libc malloc. | ||
// | ||
// The primary reason to use this is when exporting data across the C Data | ||
|
@@ -45,9 +44,22 @@ import ( | |
// The build tag 'mallocator' will also make this the default allocator. | ||
type Mallocator struct { | ||
allocatedBytes uint64 | ||
// We want to align allocations, but since we only get/return []byte, | ||
// we need to remember the "real" address for Free somehow | ||
realAllocations sync.Map | ||
alignment int | ||
} | ||
|
||
func NewMallocator() *Mallocator { return &Mallocator{} } | ||
func NewMallocator() *Mallocator { return &Mallocator{alignment: 64} } | ||
|
||
func NewMallocatorWithAlignment(alignment int) *Mallocator { | ||
if alignment < 1 { | ||
panic("mallocator: invalid alignment (must be positive)") | ||
} else if alignment > 1 && (alignment&(alignment-1)) != 0 { | ||
panic("mallocator: invalid alignment (must be power of 2)") | ||
} | ||
return &Mallocator{alignment: alignment} | ||
} | ||
|
||
func (alloc *Mallocator) Allocate(size int) []byte { | ||
// Use calloc to zero-initialize memory. | ||
|
@@ -58,28 +70,44 @@ func (alloc *Mallocator) Allocate(size int) []byte { | |
if size < 0 { | ||
panic("mallocator: negative size") | ||
} | ||
ptr, err := C.calloc(C.size_t(size), 1) | ||
paddedSize := C.size_t(size) + C.size_t(alloc.alignment) | ||
ptr, err := C.calloc(paddedSize, 1) | ||
if err != nil { | ||
// under some circumstances and allocation patterns, we can end up in a scenario | ||
// where for some reason calloc return ENOMEM even though there is definitely memory | ||
// available for use. So we attempt to fallback to simply doing malloc + memset in | ||
// this case. If malloc returns a nil pointer, then we know we're out of memory | ||
// and will surface the error. | ||
if ptr = C.malloc(C.size_t(size)); ptr == nil { | ||
if ptr = C.malloc(paddedSize); ptr == nil { | ||
panic(err) | ||
} | ||
C.memset(ptr, 0, C.size_t(size)) | ||
C.memset(ptr, 0, C.size_t(paddedSize)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
} else if ptr == nil { | ||
panic("mallocator: out of memory") | ||
} | ||
|
||
buf := unsafe.Slice((*byte)(ptr), paddedSize) | ||
aligned := roundToPowerOf2(uintptr(ptr), uintptr(alloc.alignment)) | ||
alloc.realAllocations.Store(aligned, uintptr(ptr)) | ||
atomic.AddUint64(&alloc.allocatedBytes, uint64(size)) | ||
return unsafe.Slice((*byte)(ptr), size) | ||
|
||
if uintptr(ptr) != aligned { | ||
shift := aligned - uintptr(ptr) | ||
return buf[shift : uintptr(size)+shift : uintptr(size)+shift] | ||
zeroshade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return buf[:size:size] | ||
} | ||
|
||
func (alloc *Mallocator) Free(b []byte) { | ||
sz := len(b) | ||
C.free(getPtr(b)) | ||
ptr := getPtr(b) | ||
realAddr, loaded := alloc.realAllocations.LoadAndDelete(uintptr(ptr)) | ||
if !loaded { | ||
// double-free? | ||
return | ||
} | ||
realPtr := unsafe.Pointer(realAddr.(uintptr)) | ||
C.free(realPtr) | ||
// Subtract sh.Len via two's complement (since atomic doesn't offer subtract) | ||
atomic.AddUint64(&alloc.allocatedBytes, ^(uint64(sz) - 1)) | ||
} | ||
|
@@ -88,20 +116,16 @@ func (alloc *Mallocator) Reallocate(size int, b []byte) []byte { | |
if size < 0 { | ||
panic("mallocator: negative size") | ||
} | ||
cp := cap(b) | ||
ptr, err := C.realloc_and_initialize(getPtr(b), C.size_t(cp), C.size_t(size)) | ||
if err != nil { | ||
panic(err) | ||
} else if ptr == nil && size != 0 { | ||
panic("mallocator: out of memory") | ||
} | ||
delta := size - len(b) | ||
if delta >= 0 { | ||
atomic.AddUint64(&alloc.allocatedBytes, uint64(delta)) | ||
} else { | ||
atomic.AddUint64(&alloc.allocatedBytes, ^(uint64(-delta) - 1)) | ||
|
||
if cap(b) >= size { | ||
diff := size - len(b) | ||
atomic.AddUint64(&alloc.allocatedBytes, uint64(diff)) | ||
return b[:size] | ||
} | ||
return unsafe.Slice((*byte)(ptr), size) | ||
newBuf := alloc.Allocate(size) | ||
copy(newBuf, b) | ||
alloc.Free(b) | ||
return newBuf | ||
} | ||
|
||
func (alloc *Mallocator) AllocatedBytes() int64 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C.size_t(size + alloc.alignment)
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.