buffer.go 3.7 KB

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