summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-mempool
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mempool')
-rw-r--r--vendor/codeberg.org/gruf/go-mempool/LICENSE9
-rw-r--r--vendor/codeberg.org/gruf/go-mempool/README.md3
-rw-r--r--vendor/codeberg.org/gruf/go-mempool/pool.go99
3 files changed, 111 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-mempool/LICENSE b/vendor/codeberg.org/gruf/go-mempool/LICENSE
new file mode 100644
index 000000000..d6f08d0ab
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-mempool/LICENSE
@@ -0,0 +1,9 @@
+MIT License
+
+Copyright (c) gruf
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/codeberg.org/gruf/go-mempool/README.md b/vendor/codeberg.org/gruf/go-mempool/README.md
new file mode 100644
index 000000000..af4cb6770
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-mempool/README.md
@@ -0,0 +1,3 @@
+# go-mempool
+
+very simple memory pool implementation \ No newline at end of file
diff --git a/vendor/codeberg.org/gruf/go-mempool/pool.go b/vendor/codeberg.org/gruf/go-mempool/pool.go
new file mode 100644
index 000000000..e5ff6ba3d
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-mempool/pool.go
@@ -0,0 +1,99 @@
+package mempool
+
+import (
+ "unsafe"
+)
+
+const DefaultDirtyFactor = 128
+
+// Pool provides a type-safe form
+// of UnsafePool using generics.
+//
+// Note it is NOT safe for concurrent
+// use, you must protect it yourself!
+type Pool[T any] struct {
+
+ // New is an optionally provided
+ // allocator used when no value
+ // is available for use in pool.
+ New func() T
+
+ // Reset is an optionally provided
+ // value resetting function called
+ // on passed value to Put().
+ Reset func(T)
+
+ UnsafePool
+}
+
+func (p *Pool[T]) Get() T {
+ if ptr := p.UnsafePool.Get(); ptr != nil {
+ return *(*T)(ptr)
+ } else if p.New != nil {
+ return p.New()
+ }
+ var z T
+ return z
+}
+
+func (p *Pool[T]) Put(t T) {
+ if p.Reset != nil {
+ p.Reset(t)
+ }
+ ptr := unsafe.Pointer(&t)
+ p.UnsafePool.Put(ptr)
+}
+
+// UnsafePool provides an incredibly
+// simple memory pool implementation
+// that stores ptrs to memory values,
+// and regularly flushes internal pool
+// structures according to DirtyFactor.
+//
+// Note it is NOT safe for concurrent
+// use, you must protect it yourself!
+type UnsafePool struct {
+
+ // DirtyFactor determines the max
+ // number of $dirty count before
+ // pool is garbage collected. Where:
+ // $dirty = len(current) - len(victim)
+ DirtyFactor int
+
+ current []unsafe.Pointer
+ victim []unsafe.Pointer
+}
+
+func (p *UnsafePool) Get() unsafe.Pointer {
+ // First try current list.
+ if len(p.current) > 0 {
+ ptr := p.current[len(p.current)-1]
+ p.current = p.current[:len(p.current)-1]
+ return ptr
+ }
+
+ // Fallback to victim.
+ if len(p.victim) > 0 {
+ ptr := p.victim[len(p.victim)-1]
+ p.victim = p.victim[:len(p.victim)-1]
+ return ptr
+ }
+
+ return nil
+}
+
+func (p *UnsafePool) Put(ptr unsafe.Pointer) {
+ p.current = append(p.current, ptr)
+
+ // Get dirty factor.
+ df := p.DirtyFactor
+ if df == 0 {
+ df = DefaultDirtyFactor
+ }
+
+ if len(p.current)-len(p.victim) > df {
+ // Garbage collection!
+ p.victim = p.current
+ p.current = nil
+ }
+}