pool.go raw
1 package rt
2
3 import (
4 "unsafe"
5 )
6
7 type SlicePool struct {
8 pool unsafe.Pointer
9 len int
10 index int
11 typ uintptr
12 }
13
14 func NewPool(typ *GoType, size int) SlicePool {
15 return SlicePool{pool: newarray(typ, size), len: size, typ: uintptr(unsafe.Pointer(typ))}
16 }
17
18 func (self *SlicePool) GetSlice(size int) unsafe.Pointer {
19 // pool is full, fallback to normal alloc
20 if size > self.Remain() {
21 return newarray(AsGoType(self.typ), size)
22 }
23
24 ptr := PtrAdd(self.pool, uintptr(self.index)* AsGoType(self.typ).Size)
25 self.index += size
26 return ptr
27 }
28
29 func (self *SlicePool) Remain() int {
30 return self.len - self.index
31 }
32