LCOV - code coverage report
Current view: top level - pebble/internal/aligned - aligned.go (source / functions) Hit Total Coverage
Test: 2024-09-07 08:16Z fc6eab06 - tests + meta.lcov Lines: 8 9 88.9 %
Date: 2024-09-07 08:17:11 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2024 The LevelDB-Go and Pebble Authors. All rights reserved. Use
       2             : // of this source code is governed by a BSD-style license that can be found in
       3             : // the LICENSE file.
       4             : 
       5             : package aligned
       6             : 
       7             : import (
       8             :         "fmt"
       9             :         "unsafe"
      10             : )
      11             : 
      12             : // ByteSlice allocates a new byte slice of length n, ensuring the address of the
      13             : // beginning of the slice is word aligned. Go does not guarantee that a simple
      14             : // make([]byte, n) is aligned. In practice it often is, especially for larger n,
      15             : // but small n can often be misaligned.
      16           1 : func ByteSlice(n int) []byte {
      17           1 :         a := make([]uint64, (n+7)/8)
      18           1 :         b := unsafe.Slice((*byte)(unsafe.Pointer(&a[0])), n)
      19           1 : 
      20           1 :         // Verify alignment.
      21           1 :         ptr := uintptr(unsafe.Pointer(&b[0]))
      22           1 :         if ptr%unsafe.Sizeof(int(0)) != 0 {
      23           0 :                 panic(fmt.Sprintf("allocated []uint64 slice not %d-aligned: pointer %p", unsafe.Sizeof(int(0)), &b[0]))
      24             :         }
      25           1 :         return b
      26             : }

Generated by: LCOV version 1.14