github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/unsafekit/reference.go (about)

     1  // Copyright 2020 Insolar Network Ltd.
     2  // All rights reserved.
     3  // This material is licensed under the Insolar License version 1.0,
     4  // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
     5  
     6  package unsafekit
     7  
     8  /*
     9  
    10  // WARNING! You MUST make sure that (v) stays alive while the resulting longbits.ByteString is in use.
    11  func WrapLocalRef(v *reference.Local) (r longbits.ByteString) {
    12  	if v == nil {
    13  		return ""
    14  	}
    15  	KeepAliveWhile((unsafe.Pointer)(v), func(p unsafe.Pointer) uintptr {
    16  		r = wrapUnsafePtr(uintptr(p), unsafe.Sizeof(*v))
    17  		return 0
    18  	})
    19  	return
    20  }
    21  
    22  // WARNING! You MUST make sure that (v) stays alive while the resulting longbits.ByteString is in use.
    23  func WrapGlobalRef(v *reference.Global) (r longbits.ByteString) {
    24  	if v == nil {
    25  		return ""
    26  	}
    27  	KeepAliveWhile((unsafe.Pointer)(v), func(p unsafe.Pointer) uintptr {
    28  		r = wrapUnsafePtr(uintptr(p), unsafe.Sizeof(*v))
    29  		return 0
    30  	})
    31  	return
    32  }
    33  
    34  // WARNING! This function has different guarantees based on (s) origin:
    35  // 1) When (s) is made by wrapping another type - it satisfies Unsafe Rule (1) Conversion of a *T1 to Pointer to *T2.
    36  //    You are safe.
    37  //
    38  // 2) When (s) is made by wrapping []byte or string - it violates Unsafe Rule (6) Conversion of SliceHeader/StringHeader
    39  //    And YOU MUST make sure that the origin stays alive while the result is in use.
    40  //
    41  func UnwrapAsLocalRef(s longbits.ByteString) *reference.Local {
    42  	switch len(s) {
    43  	case 0:
    44  		return nil
    45  	case reference.LocalBinarySize:
    46  		r := (*reference.Local)(_unwrapUnsafe(s))
    47  		runtime.KeepAlive(s)
    48  		return r
    49  	default:
    50  		panic("illegal value")
    51  	}
    52  }
    53  
    54  // WARNING! This function has different guarantees based on (s) origin:
    55  // 1) When (s) is made by wrapping another type - it satisfies Unsafe Rule (1) Conversion of a *T1 to Pointer to *T2.
    56  //    You are safe.
    57  //
    58  // 2) When (s) is made by wrapping []byte or string - it violates Unsafe Rule (6) Conversion of SliceHeader/StringHeader
    59  //    And YOU MUST make sure that the origin stays alive while the result is in use.
    60  //
    61  func UnwrapAsGlobalRef(s longbits.ByteString) *reference.Global {
    62  	switch len(s) {
    63  	case 0:
    64  		return nil
    65  	case reference.GlobalBinarySize:
    66  		r := (*reference.Global)(_unwrapUnsafe(s))
    67  		runtime.KeepAlive(s)
    68  		return r
    69  	default:
    70  		panic("illegal value")
    71  	}
    72  }
    73  
    74   */