Skip to content

Commit

Permalink
fix(arrow/memory/internal/cgoalloc): Remove usage of reflect.SliceHea…
Browse files Browse the repository at this point in the history
…der (#194)

### Rationale for this change
Removing deprecated usage of `reflect.SliceHeader` and using
`unsafe.SliceData` and `unsafe.Slice` instead.

### Are these changes tested?
yes

### Are there any user-facing changes?
No

Fixes #40
  • Loading branch information
zeroshade authored Nov 21, 2024
1 parent e445486 commit fb174ba
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions arrow/memory/internal/cgoalloc/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package cgoalloc
// #include "allocator.h"
import "C"
import (
"reflect"
"unsafe"
)

Expand All @@ -35,20 +34,14 @@ type CGOMemPool = C.ArrowMemoryPool
// CgoPoolAlloc allocates a block of memory of length 'size' using the memory
// pool that is passed in.
func CgoPoolAlloc(pool CGOMemPool, size int) []byte {
var ret []byte
if size == 0 {
return ret
return []byte{}
}

var out *C.uint8_t
C.arrow_pool_allocate(pool, C.int64_t(size), (**C.uint8_t)(unsafe.Pointer(&out)))

s := (*reflect.SliceHeader)(unsafe.Pointer(&ret))
s.Data = uintptr(unsafe.Pointer(out))
s.Len = size
s.Cap = size

return ret
return unsafe.Slice((*byte)(unsafe.Pointer(out)), size)
}

// CgoPoolRealloc calls 'reallocate' on the block of memory passed in which must
Expand All @@ -59,16 +52,10 @@ func CgoPoolRealloc(pool CGOMemPool, size int, b []byte) []byte {
}

oldSize := C.int64_t(len(b))
data := (*C.uint8_t)(unsafe.Pointer(&b[0]))
data := (*C.uint8_t)(unsafe.SliceData(b))
C.arrow_pool_reallocate(pool, oldSize, C.int64_t(size), &data)

var ret []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&ret))
s.Data = uintptr(unsafe.Pointer(data))
s.Len = size
s.Cap = size

return ret
return unsafe.Slice((*byte)(unsafe.Pointer(data)), size)
}

// CgoPoolFree uses the indicated memory pool to free a block of memory. The
Expand Down

0 comments on commit fb174ba

Please sign in to comment.