open-vault/vendor/github.com/ugorji/go/codec/helper_unsafe.go

54 lines
972 B
Go
Raw Normal View History

2016-09-30 13:50:46 +00:00
// +build unsafe
2015-06-29 21:50:55 +00:00
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
2015-08-19 01:12:51 +00:00
// Use of this source code is governed by a MIT license found in the LICENSE file.
2015-06-29 21:50:55 +00:00
package codec
import (
2017-07-18 14:15:54 +00:00
"runtime"
2015-06-29 21:50:55 +00:00
"unsafe"
)
// This file has unsafe variants of some helper methods.
2017-07-18 14:15:54 +00:00
// NOTE: See helper_not_unsafe.go for the usage information.
2015-06-29 21:50:55 +00:00
type unsafeString struct {
Data uintptr
Len int
}
2016-04-26 00:18:04 +00:00
type unsafeSlice struct {
2015-06-29 21:50:55 +00:00
Data uintptr
Len int
Cap int
}
func stringView(v []byte) string {
2015-10-07 20:10:00 +00:00
if len(v) == 0 {
return ""
}
2016-04-26 00:18:04 +00:00
bx := (*unsafeSlice)(unsafe.Pointer(&v))
sx := unsafeString{bx.Data, bx.Len}
return *(*string)(unsafe.Pointer(&sx))
2015-06-29 21:50:55 +00:00
}
func bytesView(v string) []byte {
2015-10-07 20:10:00 +00:00
if len(v) == 0 {
return zeroByteSlice
}
2016-04-26 00:18:04 +00:00
sx := (*unsafeString)(unsafe.Pointer(&v))
bx := unsafeSlice{sx.Data, sx.Len, sx.Len}
return *(*[]byte)(unsafe.Pointer(&bx))
2015-06-29 21:50:55 +00:00
}
2017-07-18 14:15:54 +00:00
func keepAlive4BytesView(v string) {
runtime.KeepAlive(v)
}
func keepAlive4StringView(v []byte) {
runtime.KeepAlive(v)
}