aboutsummaryrefslogtreecommitdiff
path: root/internal/contentenc/bpool.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2017-06-29 22:05:23 +0200
committerJakob Unterwurzacher2017-06-29 23:44:32 +0200
commit80676c685fe2b52ce0c48a7d9d922895d0c583b8 (patch)
treebb88c7335bf96ba8e61a42f136241bfc687053cd /internal/contentenc/bpool.go
parent3d32bcd37b751a1ed089bbe07c1341ab8d7b9773 (diff)
contentenc: add safer "bPool" pool variant; add pBlockPool
bPool verifies the lengths of slices going in and out. Also, add a plaintext block pool - pBlockPool - and use it for decryption.
Diffstat (limited to 'internal/contentenc/bpool.go')
-rw-r--r--internal/contentenc/bpool.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/internal/contentenc/bpool.go b/internal/contentenc/bpool.go
new file mode 100644
index 0000000..c4517d3
--- /dev/null
+++ b/internal/contentenc/bpool.go
@@ -0,0 +1,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
+}