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 "golang.org/x/exp/constraints" 8 : 9 : // align returns the next value greater than or equal to offset that's divisible 10 : // by val. 11 1 : func align[T constraints.Integer](offset, val T) T { 12 1 : return (offset + val - 1) & ^(val - 1) 13 1 : } 14 : 15 : // alignWithZeroes aligns the provided offset to val, and writing zeroes to any 16 : // bytes in buf between the old offset and new aligned offset. This provides 17 : // determinism when reusing memory that has not been zeroed. 18 1 : func alignWithZeroes[T constraints.Integer](buf []byte, offset, val T) T { 19 1 : aligned := align[T](offset, val) 20 1 : for i := offset; i < aligned; i++ { 21 0 : buf[i] = 0 22 0 : } 23 1 : return aligned 24 : } 25 : 26 : const ( 27 : align16 = 2 28 : align32 = 4 29 : align64 = 8 30 : ) 31 : 32 : // When multiplying or dividing by align{16,32,64} using signed integers, it's 33 : // faster to shift to the left to multiply or shift to the right to divide. (The 34 : // compiler optimization is limited to unsigned integers.) The below constants 35 : // define the shift amounts corresponding to the above align constants. (eg, 36 : // alignNShift = log(alignN)). 37 : // 38 : // TODO(jackson): Consider updating usages to use uints? They can be less 39 : // ergonomic. 40 : const ( 41 : align16Shift = 1 42 : align32Shift = 2 43 : align64Shift = 3 44 : )