github.com/primecitizens/pcz/std@v0.2.1/core/mem/alias.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2018 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package mem 9 10 // alias implements memory alaising tests. 11 // This code also exists as golang.org/x/crypto/internal/alias. 12 13 import ( 14 "unsafe" 15 ) 16 17 // AnyOverlap reports whether x and y share memory at any (not necessarily 18 // corresponding) index. The memory beyond the slice length is ignored. 19 func AnyOverlap(x, y []byte) bool { 20 return len(x) > 0 && len(y) > 0 && 21 uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) && 22 uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1])) 23 } 24 25 // InexactOverlap reports whether x and y share memory at any non-corresponding 26 // index. The memory beyond the slice length is ignored. Note that x and y can 27 // have different lengths and still not have any inexact overlap. 28 // 29 // InexactOverlap can be used to implement the requirements of the crypto/cipher 30 // AEAD, Block, BlockMode and Stream interfaces. 31 func InexactOverlap(x, y []byte) bool { 32 if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] { 33 return false 34 } 35 return AnyOverlap(x, y) 36 }