NewBuffer creates a new Buffer from the given data, initializing the reference counter to 1. The data will then be returned to the given pool when all references to the returned Buffer are released. As a special case to avoid additional allocations, if the given buffer pool is nil, the returned buff
(data *[]byte, pool BufferPool)
| 105 | // |
| 106 | // Note that the backing array of the given data is not copied. |
| 107 | func NewBuffer(data *[]byte, pool BufferPool) Buffer { |
| 108 | // Use the buffer's capacity instead of the length, otherwise buffers may |
| 109 | // not be reused under certain conditions. For example, if a large buffer |
| 110 | // is acquired from the pool, but fewer bytes than the buffering threshold |
| 111 | // are written to it, the buffer will not be returned to the pool. |
| 112 | if pool == nil || IsBelowBufferPoolingThreshold(cap(*data)) { |
| 113 | return (SliceBuffer)(*data) |
| 114 | } |
| 115 | b := newBuffer() |
| 116 | b.origData = data |
| 117 | b.data = *data |
| 118 | b.pool = pool |
| 119 | b.rootBuf = b |
| 120 | b.refs.Store(1) |
| 121 | return b |
| 122 | } |
| 123 | |
| 124 | // Copy creates a new Buffer from the given data, initializing the reference |
| 125 | // counter to 1. |