github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/arena/arena.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package arena 15 16 // Allocator pre-allocates memory to reduce memory allocation cost. 17 // It is not thread-safe. 18 type Allocator interface { 19 // Alloc allocates memory with 0 len and capacity cap. 20 Alloc(capacity int) []byte 21 22 // AllocWithLen allocates memory with length and capacity. 23 AllocWithLen(length int, capacity int) []byte 24 25 // Reset resets arena offset. 26 // Make sure all the allocated memory are not used any more. 27 Reset() 28 } 29 30 // SimpleAllocator is a simple implementation of ArenaAllocator. 31 type SimpleAllocator struct { 32 arena []byte 33 off int 34 } 35 36 type stdAllocator struct { 37 } 38 39 func (a *stdAllocator) Alloc(capacity int) []byte { 40 return make([]byte, 0, capacity) 41 } 42 43 func (a *stdAllocator) AllocWithLen(length int, capacity int) []byte { 44 return make([]byte, length, capacity) 45 } 46 47 func (a *stdAllocator) Reset() { 48 } 49 50 var _ Allocator = &stdAllocator{} 51 52 // StdAllocator implements Allocator but do not pre-allocate memory. 53 var StdAllocator = &stdAllocator{} 54 55 // NewAllocator creates an Allocator with a specified capacity. 56 func NewAllocator(capacity int) *SimpleAllocator { 57 return &SimpleAllocator{arena: make([]byte, 0, capacity)} 58 } 59 60 // Alloc implements Allocator.AllocBytes interface. 61 func (s *SimpleAllocator) Alloc(capacity int) []byte { 62 if s.off+capacity < cap(s.arena) { 63 slice := s.arena[s.off:s.off : s.off+capacity] 64 s.off += capacity 65 return slice 66 } 67 68 return make([]byte, 0, capacity) 69 } 70 71 // AllocWithLen implements Allocator.AllocWithLen interface. 72 func (s *SimpleAllocator) AllocWithLen(length int, capacity int) []byte { 73 slice := s.Alloc(capacity) 74 return slice[:length:capacity] 75 } 76 77 // Reset implements Allocator.Reset interface. 78 func (s *SimpleAllocator) Reset() { 79 s.off = 0 80 }