github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/pool/checked_bytes.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package pool 22 23 import "github.com/m3db/m3/src/x/checked" 24 25 type checkedBytesPool struct { 26 bytesPool BytesPool 27 pool BucketizedObjectPool 28 } 29 30 // NewBytesPoolFn is a function to construct a new bytes pool 31 type NewBytesPoolFn func(sizes []Bucket) BytesPool 32 33 // NewCheckedBytesPool creates a new checked bytes pool 34 func NewCheckedBytesPool( 35 sizes []Bucket, 36 opts ObjectPoolOptions, 37 newBackingBytesPool NewBytesPoolFn, 38 ) CheckedBytesPool { 39 return &checkedBytesPool{ 40 bytesPool: newBackingBytesPool(sizes), 41 pool: NewBucketizedObjectPool(sizes, opts), 42 } 43 } 44 45 func (p *checkedBytesPool) BytesPool() BytesPool { 46 return p.bytesPool 47 } 48 49 func (p *checkedBytesPool) Init() { 50 opts := checked.NewBytesOptions(). 51 SetFinalizer(p) 52 53 p.bytesPool.Init() 54 p.pool.Init(func(capacity int) interface{} { 55 value := p.bytesPool.Get(capacity) 56 return checked.NewBytes(value, opts) 57 }) 58 } 59 60 func (p *checkedBytesPool) Get(capacity int) checked.Bytes { 61 return p.pool.Get(capacity).(checked.Bytes) 62 } 63 64 func (p *checkedBytesPool) FinalizeBytes(bytes checked.Bytes) { 65 bytes.IncRef() 66 bytes.Resize(0) 67 capacity := bytes.Cap() 68 bytes.DecRef() 69 p.pool.Put(bytes, capacity) 70 } 71 72 // AppendByteChecked appends a byte to a byte slice getting a new slice from 73 // the CheckedBytesPool if the slice is at capacity 74 func AppendByteChecked( 75 bytes checked.Bytes, 76 b byte, 77 pool CheckedBytesPool, 78 ) ( 79 result checked.Bytes, 80 swapped bool, 81 ) { 82 orig := bytes 83 84 if bytes.Len() == bytes.Cap() { 85 newBytes := pool.Get(bytes.Cap() * 2) 86 87 // Inc the ref to read/write to it 88 newBytes.IncRef() 89 newBytes.Resize(bytes.Len()) 90 91 copy(newBytes.Bytes(), bytes.Bytes()) 92 93 bytes = newBytes 94 } 95 96 bytes.Append(b) 97 98 result = bytes 99 swapped = orig != bytes 100 101 if swapped { 102 // No longer holding reference from the inc 103 result.DecRef() 104 } 105 106 return 107 }