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 colblk 6 : 7 : import ( 8 : "unsafe" 9 : 10 : "golang.org/x/exp/constraints" 11 : ) 12 : 13 : // align returns the next value greater than or equal to offset that's divisible 14 : // by val. 15 1 : func align[T constraints.Integer](offset, val T) T { 16 1 : return (offset + val - 1) & ^(val - 1) 17 1 : } 18 : 19 : // alignWithZeroes aligns the provided offset to val, and writing zeroes to any 20 : // bytes in buf between the old offset and new aligned offset. This provides 21 : // determinism when reusing memory that has not been zeroed. 22 1 : func alignWithZeroes[T constraints.Integer](buf []byte, offset, val T) T { 23 1 : aligned := align[T](offset, val) 24 1 : for i := offset; i < aligned; i++ { 25 1 : buf[i] = 0 26 1 : } 27 1 : return aligned 28 : } 29 : 30 : const ( 31 : align16 = 2 32 : align32 = 4 33 : align64 = 8 34 : ) 35 : 36 : // When multiplying or dividing by align{16,32,64} using signed integers, it's 37 : // faster to shift to the left to multiply or shift to the right to divide. (The 38 : // compiler optimization is limited to unsigned integers.) The below constants 39 : // define the shift amounts corresponding to the above align constants. (eg, 40 : // alignNShift = log(alignN)). 41 : // 42 : // TODO(jackson): Consider updating usages to use uints? They can be less 43 : // ergonomic. 44 : const ( 45 : align16Shift = 1 46 : align32Shift = 2 47 : align64Shift = 3 48 : ) 49 : 50 : // TODO(jackson): A subsequent Go release will remove the ability to call these 51 : // runtime functions. We should consider asm implementations that we maintain 52 : // within the crlib repo. 53 : // 54 : // See https://github.com/golang/go/issues/67401 55 : 56 : //go:linkname memmove runtime.memmove 57 : func memmove(to, from unsafe.Pointer, n uintptr) 58 : 59 : //go:linkname mallocgc runtime.mallocgc 60 : func mallocgc(size uintptr, typ unsafe.Pointer, needzero bool) unsafe.Pointer