aboutsummaryrefslogtreecommitdiff
path: root/dht/slab.go
blob: a8b40181282fd21363d996633ad824d8e9b470c8 (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
package dht

// Slab memory allocation

// Initialise the slab as a channel of blocks, allocating them as required and
// pushing them back on the slab. This reduces garbage collection.
type slab chan []byte

func newSlab(blockSize int, numBlocks int) slab {
	s := make(slab, numBlocks)
	for i := 0; i < numBlocks; i++ {
		s <- make([]byte, blockSize)
	}
	return s
}

func (s slab) alloc() (x []byte) {
	return <-s
}

func (s slab) free(x []byte) {
	// Check we are using the right dimensions
	x = x[:cap(x)]
	s <- x
}