summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/internal/rt/pool.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/bytedance/sonic/internal/rt/pool.go')
-rw-r--r--vendor/github.com/bytedance/sonic/internal/rt/pool.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/vendor/github.com/bytedance/sonic/internal/rt/pool.go b/vendor/github.com/bytedance/sonic/internal/rt/pool.go
new file mode 100644
index 000000000..60545b1ff
--- /dev/null
+++ b/vendor/github.com/bytedance/sonic/internal/rt/pool.go
@@ -0,0 +1,31 @@
+package rt
+
+import (
+ "unsafe"
+)
+
+type SlicePool struct {
+ pool unsafe.Pointer
+ len int
+ index int
+ typ uintptr
+}
+
+func NewPool(typ *GoType, size int) SlicePool {
+ return SlicePool{pool: newarray(typ, size), len: size, typ: uintptr(unsafe.Pointer(typ))}
+}
+
+func (self *SlicePool) GetSlice(size int) unsafe.Pointer {
+ // pool is full, fallback to normal alloc
+ if size > self.Remain() {
+ return newarray(AsGoType(self.typ), size)
+ }
+
+ ptr := PtrAdd(self.pool, uintptr(self.index)* AsGoType(self.typ).Size)
+ self.index += size
+ return ptr
+}
+
+func (self *SlicePool) Remain() int {
+ return self.len - self.index
+}