MakeContiguous ensures the write buffer consists of exactly one contiguous single slice of the provided length and returns the slice.
(length int)
| 36 | // MakeContiguous ensures the write buffer consists of exactly one contiguous single slice of the provided length |
| 37 | // and returns the slice. |
| 38 | func (b *WriteBuffer) MakeContiguous(length int) []byte { |
| 39 | b.Reset() |
| 40 | |
| 41 | b.mu.Lock() |
| 42 | defer b.mu.Unlock() |
| 43 | |
| 44 | var v []byte |
| 45 | |
| 46 | switch { |
| 47 | case length <= typicalContiguousAllocator.chunkSize: |
| 48 | // most commonly used allocator for default chunk size with max 8MB |
| 49 | b.alloc = typicalContiguousAllocator |
| 50 | v = b.allocChunk()[0:length] |
| 51 | |
| 52 | case length <= maxContiguousAllocator.chunkSize: |
| 53 | b.alloc = maxContiguousAllocator |
| 54 | v = b.allocChunk()[0:length] |
| 55 | |
| 56 | default: |
| 57 | v = make([]byte, length) |
| 58 | } |
| 59 | |
| 60 | b.inner.Slices = [][]byte{v} |
| 61 | |
| 62 | return v |
| 63 | } |
| 64 | |
| 65 | // Reset resets buffer back to empty. |
| 66 | func (b *WriteBuffer) Reset() { |