aboutsummaryrefslogtreecommitdiff
path: root/internal/contentenc/bpool.go
blob: c4517d33ad7018d0473f27012a96bbea4b1b7221 (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
32
33
34
35
36
37
38
39
package contentenc

import (
	"log"
	"sync"
)

// bPool is a byte slice pool
type bPool struct {
	sync.Pool
	sliceLen int
}

func newBPool(sliceLen int) bPool {
	return bPool{
		Pool: sync.Pool{
			New: func() interface{} { return make([]byte, sliceLen) },
		},
		sliceLen: sliceLen,
	}
}

// Put grows the slice "s" to its maximum capacity and puts it into the pool.
func (b *bPool) Put(s []byte) {
	s = s[:cap(s)]
	if len(s) != b.sliceLen {
		log.Panicf("wrong len=%d, want=%d", len(s), b.sliceLen)
	}
	b.Pool.Put(s)
}

// Get returns a byte slice from the pool.
func (b *bPool) Get() (s []byte) {
	s = b.Pool.Get().([]byte)
	if len(s) != b.sliceLen {
		log.Panicf("wrong len=%d, want=%d", len(s), b.sliceLen)
	}
	return s
}