zeroshade commented on code in PR #289:
URL: https://github.com/apache/arrow-go/pull/289#discussion_r1966328267
##########
arrow/memory/mallocator/mallocator.go:
##########
@@ -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))
} 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]
Review Comment:
Ah weird. I always thought it was an absolute capacity for some reason...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]