buffer.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. newoffset := b.offset - len(data)
  57. if newoffset < 0 {
  58. newoffset = 0
  59. }
  60. copy(b.head[newoffset:], data)
  61. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  62. b.offset = newoffset
  63. return b
  64. }
  65. func (b *Buffer) Bytes() []byte {
  66. return b.Value
  67. }
  68. // Slice cuts the buffer at the given position.
  69. func (b *Buffer) Slice(from, to int) *Buffer {
  70. b.Value = b.Value[from:to]
  71. return b
  72. }
  73. // SliceFrom cuts the buffer at the given position.
  74. func (b *Buffer) SliceFrom(from int) *Buffer {
  75. b.Value = b.Value[from:]
  76. return b
  77. }
  78. // Len returns the length of the buffer content.
  79. func (b *Buffer) Len() int {
  80. return len(b.Value)
  81. }
  82. // IsFull returns true if the buffer has no more room to grow.
  83. func (b *Buffer) IsFull() bool {
  84. return len(b.Value) == cap(b.Value)
  85. }
  86. // Write implements Write method in io.Writer.
  87. func (b *Buffer) Write(data []byte) (int, error) {
  88. b.Append(data)
  89. return len(data), nil
  90. }
  91. func (b *Buffer) Read(data []byte) (int, error) {
  92. if b.Len() == 0 {
  93. return 0, io.EOF
  94. }
  95. nBytes := copy(data, b.Value)
  96. if nBytes == b.Len() {
  97. b.Value = b.Value[:0]
  98. } else {
  99. b.Value = b.Value[nBytes:]
  100. }
  101. return nBytes, nil
  102. }
  103. type bufferPool struct {
  104. chain chan []byte
  105. allocator *sync.Pool
  106. }
  107. func newBufferPool(bufferSize, poolSize int) *bufferPool {
  108. pool := &bufferPool{
  109. chain: make(chan []byte, poolSize),
  110. allocator: &sync.Pool{
  111. New: func() interface{} { return make([]byte, bufferSize) },
  112. },
  113. }
  114. for i := 0; i < poolSize; i++ {
  115. pool.chain <- make([]byte, bufferSize)
  116. }
  117. return pool
  118. }
  119. func (p *bufferPool) allocate() *Buffer {
  120. var b []byte
  121. select {
  122. case b = <-p.chain:
  123. default:
  124. b = p.allocator.Get().([]byte)
  125. }
  126. return &Buffer{
  127. head: b,
  128. pool: p,
  129. Value: b[DefaultOffset:],
  130. offset: DefaultOffset,
  131. }
  132. }
  133. func (p *bufferPool) free(buffer *Buffer) {
  134. select {
  135. case p.chain <- buffer.head:
  136. default:
  137. p.allocator.Put(buffer.head)
  138. }
  139. }
  140. var smallPool = newBufferPool(1024, 256)
  141. var mediumPool = newBufferPool(8*1024, 512)
  142. var largePool = newBufferPool(64*1024, 128)
  143. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  144. func NewSmallBuffer() *Buffer {
  145. return smallPool.allocate()
  146. }
  147. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  148. func NewBuffer() *Buffer {
  149. return mediumPool.allocate()
  150. }
  151. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  152. func NewLargeBuffer() *Buffer {
  153. return largePool.allocate()
  154. }