| // Copyright 2016 The etcd Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package main |
| |
| import ( |
| "unsafe" |
| ) |
| |
| func unsafeAdd(base unsafe.Pointer, offset uintptr) unsafe.Pointer { |
| return unsafe.Pointer(uintptr(base) + offset) |
| } |
| |
| func unsafeIndex(base unsafe.Pointer, offset uintptr, elemsz uintptr, n int) unsafe.Pointer { |
| return unsafe.Pointer(uintptr(base) + offset + uintptr(n)*elemsz) |
| } |
| |
| func unsafeByteSlice(base unsafe.Pointer, offset uintptr, i, j int) []byte { |
| // See: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices |
| // |
| // This memory is not allocated from C, but it is unmanaged by Go's |
| // garbage collector and should behave similarly, and the compiler |
| // should produce similar code. Note that this conversion allows a |
| // subslice to begin after the base address, with an optional offset, |
| // while the URL above does not cover this case and only slices from |
| // index 0. However, the wiki never says that the address must be to |
| // the beginning of a C allocation (or even that malloc was used at |
| // all), so this is believed to be correct. |
| return (*[pageMaxAllocSize]byte)(unsafeAdd(base, offset))[i:j:j] |
| } |