github.com/dgraph-io/ristretto@v0.1.2-0.20240116140435-c67e07994f91/z/mremap_nosize.go (about) 1 //go:build (arm64 || arm) && linux 2 // +build arm64 arm 3 // +build linux 4 5 /* 6 * Copyright 2020 Dgraph Labs, Inc. and Contributors 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package z 22 23 import ( 24 "reflect" 25 "unsafe" 26 27 "golang.org/x/sys/unix" 28 ) 29 30 // mremap is a Linux-specific system call to remap pages in memory. This can be used in place of munmap + mmap. 31 func mremap(data []byte, size int) ([]byte, error) { 32 //nolint:lll 33 // taken from <https://github.com/torvalds/linux/blob/f8394f232b1eab649ce2df5c5f15b0e528c92091/include/uapi/linux/mman.h#L8> 34 const MREMAP_MAYMOVE = 0x1 35 36 header := (*reflect.SliceHeader)(unsafe.Pointer(&data)) 37 // For ARM64, the second return argument for SYS_MREMAP is inconsistent (prior allocated size) with 38 // other architectures, which return the size allocated 39 mmapAddr, _, errno := unix.Syscall6( 40 unix.SYS_MREMAP, 41 header.Data, 42 uintptr(header.Len), 43 uintptr(size), 44 uintptr(MREMAP_MAYMOVE), 45 0, 46 0, 47 ) 48 if errno != 0 { 49 return nil, errno 50 } 51 52 header.Data = mmapAddr 53 header.Cap = size 54 header.Len = size 55 return data, nil 56 }