Skip to content

Commit e30ccb7

Browse files
committed
memory: use unsafe.Add to align unsafe memory instead of uintptr conversion
This allows removing the `nolint:govet` statement which the vanilla `go vet` doesn't acknowledge to begin with. Signed-off-by: Timo Beckers <timo@isovalent.com>
1 parent 0e35955 commit e30ccb7

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

memory_unsafe.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,10 @@ func allocate(size int) (unsafe.Pointer, error) {
118118
// Allocate a new slice and store a pointer to its backing array.
119119
alloc := unsafe.Pointer(unsafe.SliceData(make([]byte, size)))
120120

121-
// nolint:govet
122-
//
123121
// Align the pointer to a page boundary within the allocation. This may alias
124-
// the initial pointer if it was already page-aligned. Ignore govet warnings
125-
// since we're calling [runtime.KeepAlive] on the original Go memory.
126-
aligned := unsafe.Pointer(internal.Align(uintptr(alloc), uintptr(os.Getpagesize())))
127-
runtime.KeepAlive(alloc)
122+
// the initial pointer if it was already page-aligned.
123+
aligned := internal.Align(uintptr(alloc), uintptr(os.Getpagesize()))
124+
alloc = unsafe.Add(alloc, aligned-uintptr(alloc))
128125

129126
// Return an aligned pointer into the backing array, losing the original
130127
// reference. The runtime.SetFinalizer docs specify that its argument 'must be
@@ -142,7 +139,7 @@ func allocate(size int) (unsafe.Pointer, error) {
142139
// pointer separately, which severely complicates finalizer setup and makes it
143140
// prone to human error. For now, just bump the pointer and treat it as the
144141
// new and only reference to the backing array.
145-
return aligned, nil
142+
return alloc, nil
146143
}
147144

148145
// mapmap memory-maps the given file descriptor at the given address and sets a

0 commit comments

Comments
 (0)