github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/pool/types.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 provides implementations for object pooling. 22 package pool 23 24 import ( 25 "github.com/m3db/m3/src/x/checked" 26 "github.com/m3db/m3/src/x/instrument" 27 ) 28 29 // Allocator allocates an object for a pool. 30 type Allocator func() interface{} 31 32 // ObjectPool provides a pool for objects. 33 type ObjectPool interface { 34 // Init initializes the pool. 35 Init(alloc Allocator) 36 37 // Get provides an object from the pool. 38 Get() interface{} 39 40 // Put returns an object to the pool. 41 Put(obj interface{}) 42 } 43 44 // OnPoolAccessErrorFn is a function to call when a pool access error occurs, 45 // such as get or put before the pool is initialized. 46 type OnPoolAccessErrorFn func(err error) 47 48 // ObjectPoolOptions provides options for an object pool. 49 type ObjectPoolOptions interface { 50 // SetSize sets the size of the object pool. 51 SetSize(value int) ObjectPoolOptions 52 53 // Size returns the size of the object pool. 54 Size() int 55 56 // SetDynamic creates a dynamically-sized, non-preallocated pool. 57 SetDynamic(value bool) ObjectPoolOptions 58 59 // Dynamic returns if the pool is dynamic. 60 Dynamic() bool 61 62 // SetRefillLowWatermark sets the refill low watermark value between [0, 1), 63 // if zero then no refills occur. 64 SetRefillLowWatermark(value float64) ObjectPoolOptions 65 66 // RefillLowWatermark returns the refill low watermark value between [0, 1), 67 // if zero then no refills occur. 68 RefillLowWatermark() float64 69 70 // SetRefillHighWatermark sets the refill high watermark value between [0, 1), 71 // if less or equal to low watermark then no refills occur. 72 SetRefillHighWatermark(value float64) ObjectPoolOptions 73 74 // RefillLowWatermark returns the refill low watermark value between [0, 1), 75 // if less or equal to low watermark then no refills occur. 76 RefillHighWatermark() float64 77 78 // SetInstrumentOptions sets the instrument options. 79 SetInstrumentOptions(value instrument.Options) ObjectPoolOptions 80 81 // InstrumentOptions returns the instrument options. 82 InstrumentOptions() instrument.Options 83 84 // SetOnPoolAccessErrorFn sets the on pool access error callback, by 85 // default this is a panic. 86 SetOnPoolAccessErrorFn(value OnPoolAccessErrorFn) ObjectPoolOptions 87 88 // OnPoolAccessErrorFn returns the on pool access error callback, by 89 // default this is a panic. 90 OnPoolAccessErrorFn() OnPoolAccessErrorFn 91 } 92 93 // Bucket specifies a pool bucket. 94 type Bucket struct { 95 // Capacity is the size of each element in the bucket. 96 Capacity int 97 98 // Count is the number of fixed elements in the bucket. 99 Count Size 100 101 // Options is an optional override to specify options to use for a bucket, 102 // specify nil to use the options specified to the bucketized pool 103 // constructor for this bucket. 104 Options ObjectPoolOptions 105 } 106 107 // BucketByCapacity is a sortable collection of pool buckets. 108 type BucketByCapacity []Bucket 109 110 func (x BucketByCapacity) Len() int { 111 return len(x) 112 } 113 114 func (x BucketByCapacity) Swap(i, j int) { 115 x[i], x[j] = x[j], x[i] 116 } 117 118 func (x BucketByCapacity) Less(i, j int) bool { 119 return x[i].Capacity < x[j].Capacity 120 } 121 122 // BucketizedAllocator allocates an object for a bucket given its capacity. 123 type BucketizedAllocator func(capacity int) interface{} 124 125 // BucketizedObjectPool is a bucketized pool of objects. 126 type BucketizedObjectPool interface { 127 // Init initializes the pool. 128 Init(alloc BucketizedAllocator) 129 130 // Get provides an object from the pool. 131 Get(capacity int) interface{} 132 133 // Put returns an object to the pool, given the object capacity. 134 Put(obj interface{}, capacity int) 135 } 136 137 // BytesPool provides a pool for variable size buffers. 138 type BytesPool interface { 139 // Init initializes the pool. 140 Init() 141 142 // Get provides a buffer from the pool. 143 Get(capacity int) []byte 144 145 // Put returns a buffer to the pool. 146 Put(buffer []byte) 147 } 148 149 // CheckedBytesPool provides a checked pool for variable size buffers. 150 type CheckedBytesPool interface { 151 // Init initializes the pool. 152 Init() 153 154 // Get provides a buffer from the pool, to return it to the pool simply 155 // increment it immediately, continue to increment and decrement through 156 // use and when decremented to zero and finalized it will return itself 157 // to the pool. The pool uses the finalizer on the checked.Bytes so be sure 158 // not to override it. 159 Get(capacity int) checked.Bytes 160 161 // BytesPool returns the underlying bytes pool used, useful if required 162 // to retrieve when only the checked bytes pool is accessible. 163 BytesPool() BytesPool 164 } 165 166 // FloatsPool provides a pool for variable-sized float64 slices. 167 type FloatsPool interface { 168 // Init initializes the pool. 169 Init() 170 171 // Get provides an float64 slice from the pool. 172 Get(capacity int) []float64 173 174 // Put returns an float64 slice to the pool. 175 Put(value []float64) 176 }