lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/xbytes/alloc.go (about) 1 // Copyright (C) 2017-2018 Nexedi SA and Contributors. 2 // Kirill Smelkov <kirr@nexedi.com> 3 // 4 // This program is free software: you can Use, Study, Modify and Redistribute 5 // it under the terms of the GNU General Public License version 3, or (at your 6 // option) any later version, as published by the Free Software Foundation. 7 // 8 // You can also Link and Combine this program with other software covered by 9 // the terms of any of the Free Software licenses or any of the Open Source 10 // Initiative approved licenses and Convey the resulting work. Corresponding 11 // source of such a combination shall include the source code for all other 12 // software used. 13 // 14 // This program is distributed WITHOUT ANY WARRANTY; without even the implied 15 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16 // 17 // See COPYING file for full licensing terms. 18 // See https://www.nexedi.com/licensing for rationale and options. 19 20 // (re)allocation routines for []byte. 21 22 package xbytes 23 24 import ( 25 "lab.nexedi.com/kirr/go123/xmath" 26 ) 27 28 // Grow increases length of byte slice by n elements. 29 // 30 // If there is not enough capacity the slice is reallocated and copied. 31 // The memory for grown elements is not initialized. 32 func Grow(b []byte, n int) []byte { 33 ln := len(b) + n 34 if ln <= cap(b) { 35 return b[:ln] 36 } 37 38 bb := make([]byte, ln, xmath.CeilPow2(uint64(ln))) 39 copy(bb, b) 40 return bb 41 } 42 43 // MakeRoom makes sure cap(b) - len(b) >= n. 44 // 45 // If there is not enough capacity the slice is reallocated and copied. 46 // Length of the slice remains unchanged. 47 func MakeRoom(b []byte, n int) []byte { 48 ln := len(b) + n 49 if ln <= cap(b) { 50 return b 51 } 52 53 bb := make([]byte, len(b), xmath.CeilPow2(uint64(ln))) 54 copy(bb, b) 55 return bb 56 } 57 58 // Resize resizes byte slice to be of length n. 59 // 60 // If slice length is increased and there is not enough capacity, the slice is reallocated and copied. 61 // The memory for grown elements, if any, is not initialized. 62 func Resize(b []byte, n int) []byte { 63 if cap(b) >= n { 64 return b[:n] 65 } 66 67 bb := make([]byte, n, xmath.CeilPow2(uint64(n))) 68 copy(bb, b) 69 return bb 70 } 71 72 73 // Realloc resizes byte slice to be of length n not preserving content. 74 // 75 // If slice length is increased and there is not enough capacity, the slice is reallocated but not copied. 76 // The memory for all elements becomes uninitialized. 77 // 78 // NOTE semantic is different from C realloc(3) where content is preserved. 79 // NOTE use Resize when you need to preserve slice content. 80 func Realloc(b []byte, n int) []byte { 81 return Realloc64(b, int64(n)) 82 } 83 84 // Realloc64 is the same as Realloc but for size typed as int64. 85 func Realloc64(b []byte, n int64) []byte { 86 if int64(cap(b)) >= n { 87 return b[:n] 88 } 89 90 return make([]byte, n, xmath.CeilPow2(uint64(n))) 91 }