buffer.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package alloc
  2. import (
  3. "io"
  4. "sync"
  5. )
  6. const (
  7. defaultOffset = 16
  8. )
  9. // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
  10. // the buffer into an internal buffer pool, in order to recreate a buffer more
  11. // quickly.
  12. type Buffer struct {
  13. head []byte
  14. pool *bufferPool
  15. Value []byte
  16. offset int
  17. }
  18. // Release recycles the buffer into an internal buffer pool.
  19. func (b *Buffer) Release() {
  20. if b == nil {
  21. return
  22. }
  23. b.pool.free(b)
  24. b.head = nil
  25. b.Value = nil
  26. b.pool = nil
  27. }
  28. // Clear clears the content of the buffer, results an empty buffer with
  29. // Len() = 0.
  30. func (b *Buffer) Clear() *Buffer {
  31. b.offset = defaultOffset
  32. b.Value = b.head[b.offset:b.offset]
  33. return b
  34. }
  35. // AppendBytes appends one or more bytes to the end of the buffer.
  36. func (b *Buffer) AppendBytes(bytes ...byte) *Buffer {
  37. b.Value = append(b.Value, bytes...)
  38. return b
  39. }
  40. // Append appends a byte array to the end of the buffer.
  41. func (b *Buffer) Append(data []byte) *Buffer {
  42. b.Value = append(b.Value, data...)
  43. return b
  44. }
  45. // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
  46. // no more than 16 bytes.
  47. func (b *Buffer) Prepend(data []byte) *Buffer {
  48. b.SliceBack(len(data))
  49. copy(b.Value, data)
  50. return b
  51. }
  52. // Bytes returns the content bytes of this Buffer.
  53. func (b *Buffer) Bytes() []byte {
  54. return b.Value
  55. }
  56. // Slice cuts the buffer at the given position.
  57. func (b *Buffer) Slice(from, to int) *Buffer {
  58. b.Value = b.Value[from:to]
  59. return b
  60. }
  61. // SliceFrom cuts the buffer at the given position.
  62. func (b *Buffer) SliceFrom(from int) *Buffer {
  63. b.Value = b.Value[from:]
  64. return b
  65. }
  66. // SliceBack extends the Buffer to its front by offset bytes.
  67. // Caller must ensure cumulated offset is no more than 16.
  68. func (b *Buffer) SliceBack(offset int) *Buffer {
  69. newoffset := b.offset - offset
  70. if newoffset < 0 {
  71. newoffset = 0
  72. }
  73. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  74. b.offset = newoffset
  75. return b
  76. }
  77. // Len returns the length of the buffer content.
  78. func (b *Buffer) Len() int {
  79. if b == nil {
  80. return 0
  81. }
  82. return len(b.Value)
  83. }
  84. func (b *Buffer) IsEmpty() bool {
  85. return b.Len() == 0
  86. }
  87. // IsFull returns true if the buffer has no more room to grow.
  88. func (b *Buffer) IsFull() bool {
  89. return len(b.Value) == cap(b.Value)
  90. }
  91. // Write implements Write method in io.Writer.
  92. func (b *Buffer) Write(data []byte) (int, error) {
  93. b.Append(data)
  94. return len(data), nil
  95. }
  96. // Read implements io.Reader.Read().
  97. func (b *Buffer) Read(data []byte) (int, error) {
  98. if b.Len() == 0 {
  99. return 0, io.EOF
  100. }
  101. nBytes := copy(data, b.Value)
  102. if nBytes == b.Len() {
  103. b.Value = b.Value[:0]
  104. } else {
  105. b.Value = b.Value[nBytes:]
  106. }
  107. return nBytes, nil
  108. }
  109. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  110. begin := b.Len()
  111. b.Value = b.Value[:cap(b.Value)]
  112. nBytes, err := reader.Read(b.Value[begin:])
  113. b.Value = b.Value[:begin+nBytes]
  114. return nBytes, err
  115. }
  116. type bufferPool struct {
  117. chain chan []byte
  118. allocator *sync.Pool
  119. }
  120. func newBufferPool(bufferSize, poolSize int) *bufferPool {
  121. pool := &bufferPool{
  122. chain: make(chan []byte, poolSize),
  123. allocator: &sync.Pool{
  124. New: func() interface{} { return make([]byte, bufferSize) },
  125. },
  126. }
  127. for i := 0; i < poolSize/2; i++ {
  128. pool.chain <- make([]byte, bufferSize)
  129. }
  130. return pool
  131. }
  132. func (p *bufferPool) allocate() *Buffer {
  133. var b []byte
  134. select {
  135. case b = <-p.chain:
  136. default:
  137. b = p.allocator.Get().([]byte)
  138. }
  139. return &Buffer{
  140. head: b,
  141. pool: p,
  142. Value: b[defaultOffset:],
  143. offset: defaultOffset,
  144. }
  145. }
  146. func (p *bufferPool) free(buffer *Buffer) {
  147. rawBuffer := buffer.head
  148. if rawBuffer == nil {
  149. return
  150. }
  151. select {
  152. case p.chain <- rawBuffer:
  153. default:
  154. p.allocator.Put(rawBuffer)
  155. }
  156. }
  157. var smallPool = newBufferPool(1024, 64)
  158. var mediumPool = newBufferPool(8*1024, 128)
  159. var largePool = newBufferPool(64*1024, 64)
  160. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  161. func NewSmallBuffer() *Buffer {
  162. return smallPool.allocate()
  163. }
  164. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  165. func NewBuffer() *Buffer {
  166. return mediumPool.allocate()
  167. }
  168. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  169. func NewLargeBuffer() *Buffer {
  170. return largePool.allocate()
  171. }