|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// Copyright 2009 The Go Authors. |
| 6 | +// |
| 7 | +// Redistribution and use in source and binary forms, with or without |
| 8 | +// modification, are permitted provided that the following conditions are |
| 9 | +// met: |
| 10 | +// |
| 11 | +// * Redistributions of source code must retain the above copyright |
| 12 | +// notice, this list of conditions and the following disclaimer. |
| 13 | +// * Redistributions in binary form must reproduce the above |
| 14 | +// copyright notice, this list of conditions and the following disclaimer |
| 15 | +// in the documentation and/or other materials provided with the |
| 16 | +// distribution. |
| 17 | +// * Neither the name of Google LLC nor the names of its |
| 18 | +// contributors may be used to endorse or promote products derived from |
| 19 | +// this software without specific prior written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + |
| 33 | +// Adapted from go/src/log/slog/internal/buffer.go |
| 34 | + |
| 35 | +package btclog |
| 36 | + |
| 37 | +import "sync" |
| 38 | + |
| 39 | +type buffer []byte |
| 40 | + |
| 41 | +// bufferPool defines a concurrent safe free list of byte slices used to provide |
| 42 | +// temporary buffers for formatting log messages prior to outputting them. |
| 43 | +var bufferPool = sync.Pool{ |
| 44 | + New: func() any { |
| 45 | + b := make([]byte, 0, 1024) |
| 46 | + return (*buffer)(&b) |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +// newBuffer returns a byte slice from the free list. A new buffer is allocated |
| 51 | +// if there are not any available on the free list. The returned byte slice |
| 52 | +// should be returned to the fee list by using the recycleBuffer function when |
| 53 | +// the caller is done with it. |
| 54 | +func newBuffer() *buffer { |
| 55 | + return bufferPool.Get().(*buffer) |
| 56 | +} |
| 57 | + |
| 58 | +// free puts the provided byte slice, which should have been obtained via the |
| 59 | +// newBuffer function, back on the free list. |
| 60 | +func (b *buffer) free() { |
| 61 | + // To reduce peak allocation, return only smaller buffers to the pool. |
| 62 | + const maxBufferSize = 16 << 10 |
| 63 | + if cap(*b) <= maxBufferSize { |
| 64 | + *b = (*b)[:0] |
| 65 | + bufferPool.Put(b) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (b *buffer) writeByte(p byte) { |
| 70 | + *b = append(*b, p) |
| 71 | +} |
| 72 | + |
| 73 | +func (b *buffer) writeBytes(p []byte) { |
| 74 | + *b = append(*b, p...) |
| 75 | +} |
| 76 | + |
| 77 | +func (b *buffer) writeString(s string) { |
| 78 | + *b = append(*b, s...) |
| 79 | +} |
0 commit comments