github.com/fiatjaf/generic-ristretto@v0.0.1/z/mremap_linux_arm64.go (about)

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