Files
smpp-tester/pdu/bufpool.go
PhatPhuckDave 4ea320c784 Make bufpool use uint
I mean we can't have a pool of negative size?
2024-07-22 23:13:34 +02:00

49 lines
991 B
Go

package pdu
import (
"sync"
)
type BufferPoolManager struct {
pools map[uint]*sync.Pool
mu sync.Mutex
}
func NewBufferPoolManager() *BufferPoolManager {
return &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
}
}
func (bpm *BufferPoolManager) GetBuffer(size uint) *([]uint8) {
bpm.mu.Lock()
pool, exists := bpm.pools[size]
if !exists {
pool = &sync.Pool{
New: func() interface{} {
buf := make([]uint8, size)
return &buf
},
}
bpm.pools[size] = pool
}
bpm.mu.Unlock()
return pool.Get().(*[]uint8)
}
func (bpm *BufferPoolManager) PutBuffer(buf *([]uint8)) {
size := uint(len(*buf))
bpm.mu.Lock()
pool, exists := bpm.pools[size]
if !exists {
bpm.mu.Unlock()
return
}
bpm.mu.Unlock()
for i := range *buf {
(*buf)[i] = 0
}
pool.Put(buf)
}