gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

alias.go (1148B)


      1 // Copyright 2018 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 //go:build !purego
      6 // +build !purego
      7 
      8 // Package alias implements memory aliasing tests.
      9 package alias
     10 
     11 import "unsafe"
     12 
     13 // AnyOverlap reports whether x and y share memory at any (not necessarily
     14 // corresponding) index. The memory beyond the slice length is ignored.
     15 func AnyOverlap(x, y []byte) bool {
     16 	return len(x) > 0 && len(y) > 0 &&
     17 		uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) &&
     18 		uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1]))
     19 }
     20 
     21 // InexactOverlap reports whether x and y share memory at any non-corresponding
     22 // index. The memory beyond the slice length is ignored. Note that x and y can
     23 // have different lengths and still not have any inexact overlap.
     24 //
     25 // InexactOverlap can be used to implement the requirements of the crypto/cipher
     26 // AEAD, Block, BlockMode and Stream interfaces.
     27 func InexactOverlap(x, y []byte) bool {
     28 	if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
     29 		return false
     30 	}
     31 	return AnyOverlap(x, y)
     32 }