github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/smart/smartutil.go (about) 1 /* 2 Copyright 2018 The OpenEBS Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package smart 18 19 import ( 20 "encoding/binary" 21 "math/bits" 22 "unsafe" 23 ) 24 25 //intSize is the size in bytes (converted to integer) of 0 26 const intSize int = int(unsafe.Sizeof(0)) 27 28 // A ByteOrder specifies how to convert byte sequences 29 // into 16-, 32-, or 64-bit unsigned integers. 30 var ( 31 NativeEndian binary.ByteOrder 32 ) 33 34 // init determines native endianness of a system 35 func init() { 36 i := 0x1 37 b := (*[intSize]byte)(unsafe.Pointer(&i)) 38 if b[0] == 1 { 39 // LittleEndian is the little-endian implementation of ByteOrder 40 NativeEndian = binary.LittleEndian 41 } else { 42 // BigEndian is the Big-endian implementation of ByteOrder 43 NativeEndian = binary.BigEndian 44 } 45 } 46 47 // ErrorCollector Struct is a struct for map of errors 48 type ErrorCollector struct { 49 errors map[string]error 50 } 51 52 // NewErrorCollector returns a pointer to the ErrorCollector 53 func NewErrorCollector() *ErrorCollector { 54 return &ErrorCollector{errors: map[string]error{}} 55 } 56 57 // Collect function is used to collect errors corresponding to the keys given to it 58 func (c *ErrorCollector) Collect(key string, e error) bool { 59 if e != nil { 60 c.errors[key] = e 61 return true 62 } 63 return false 64 } 65 66 // Error is used to return all the collected errors as a map 67 func (c *ErrorCollector) Error() (errorMap map[string]error) { 68 return c.errors 69 } 70 71 // MSignificantBit finds the most significant bit set in a uint 72 func MSignificantBit(i uint) int { 73 if i == 0 { 74 return 0 75 } 76 return bits.Len(i) - 1 77 }