github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/sstable/unsafe.go (about)

     1  // Copyright 2018 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 sstable
     6  
     7  import (
     8  	"unsafe"
     9  
    10  	"github.com/cockroachdb/pebble/internal/manual"
    11  )
    12  
    13  func getBytes(ptr unsafe.Pointer, length int) []byte {
    14  	return (*[manual.MaxArrayLen]byte)(ptr)[:length:length]
    15  }
    16  
    17  func decodeVarint(ptr unsafe.Pointer) (uint32, unsafe.Pointer) {
    18  	if a := *((*uint8)(ptr)); a < 128 {
    19  		return uint32(a),
    20  			unsafe.Pointer(uintptr(ptr) + 1)
    21  	} else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
    22  		return uint32(b)<<7 | uint32(a),
    23  			unsafe.Pointer(uintptr(ptr) + 2)
    24  	} else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
    25  		return uint32(c)<<14 | uint32(b)<<7 | uint32(a),
    26  			unsafe.Pointer(uintptr(ptr) + 3)
    27  	} else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
    28  		return uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a),
    29  			unsafe.Pointer(uintptr(ptr) + 4)
    30  	} else {
    31  		d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
    32  		return uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a),
    33  			unsafe.Pointer(uintptr(ptr) + 5)
    34  	}
    35  }