| 185 | } |
| 186 | |
| 187 | func (b *buffer) Slice(start, end int) Buffer { |
| 188 | if b.rootBuf == nil { |
| 189 | panic("Cannot slice freed buffer") |
| 190 | } |
| 191 | |
| 192 | data := b.data[start:end] // access the data to check slice bounds |
| 193 | |
| 194 | if len(data) == 0 { |
| 195 | return emptyBuffer{} |
| 196 | } |
| 197 | if len(data) == len(b.data) { |
| 198 | b.Ref() |
| 199 | return b |
| 200 | } |
| 201 | // We are creating a new reference (view) to a portion of the root buffer's |
| 202 | // data. Therefore, we must increment the reference count of the root buffer |
| 203 | // to ensure the underlying data is not freed while this view is still in |
| 204 | // use. |
| 205 | b.rootBuf.Ref() |
| 206 | s := newBuffer() |
| 207 | s.data = data |
| 208 | s.rootBuf = b.rootBuf |
| 209 | s.refs.Store(1) |
| 210 | return s |
| 211 | } |
| 212 | |
| 213 | func (b *buffer) split(n int) (Buffer, Buffer) { |
| 214 | if b.rootBuf == nil || b.rootBuf.refs.Add(1) <= 1 { |