summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/internal/rt/pool.go
blob: 60545b1ffe0bf390768b7e12cc25ce240233a275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
}