github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/storage/slice.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package storage 12 13 import "unsafe" 14 15 func nonZeroingMakeByteSlice(len int) []byte { 16 ptr := mallocgc(uintptr(len), nil, false) 17 return (*[MaxArrayLen]byte)(ptr)[:len:len] 18 } 19 20 // Replacement for C.GoBytes which does not zero initialize the returned slice 21 // before overwriting it. 22 // 23 // TODO(peter): Remove when go1.11 is released which has a similar change to 24 // C.GoBytes. 25 func gobytes(ptr unsafe.Pointer, len int) []byte { 26 if len == 0 { 27 return make([]byte, 0) 28 } 29 x := nonZeroingMakeByteSlice(len) 30 src := (*[MaxArrayLen]byte)(ptr)[:len:len] 31 copy(x, src) 32 return x 33 }