buffer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package alloc
  2. import (
  3. "sync"
  4. )
  5. // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
  6. // the buffer into an internal buffer pool, in order to recreate a buffer more
  7. // quickly.
  8. type Buffer struct {
  9. head []byte
  10. pool *bufferPool
  11. Value []byte
  12. }
  13. // Release recycles the buffer into an internal buffer pool.
  14. func (b *Buffer) Release() {
  15. b.pool.free(b)
  16. b.head = nil
  17. b.Value = nil
  18. b.pool = nil
  19. }
  20. // Clear clears the content of the buffer, results an empty buffer with
  21. // Len() = 0.
  22. func (b *Buffer) Clear() *Buffer {
  23. b.Value = b.head[:0]
  24. return b
  25. }
  26. // AppendBytes appends one or more bytes to the end of the buffer.
  27. func (b *Buffer) AppendBytes(bytes ...byte) *Buffer {
  28. b.Value = append(b.Value, bytes...)
  29. return b
  30. }
  31. // Append appends a byte array to the end of the buffer.
  32. func (b *Buffer) Append(data []byte) *Buffer {
  33. b.Value = append(b.Value, data...)
  34. return b
  35. }
  36. func (b *Buffer) Bytes() []byte {
  37. return b.Value
  38. }
  39. // Slice cuts the buffer at the given position.
  40. func (b *Buffer) Slice(from, to int) *Buffer {
  41. b.Value = b.Value[from:to]
  42. return b
  43. }
  44. // SliceFrom cuts the buffer at the given position.
  45. func (b *Buffer) SliceFrom(from int) *Buffer {
  46. b.Value = b.Value[from:]
  47. return b
  48. }
  49. // Len returns the length of the buffer content.
  50. func (b *Buffer) Len() int {
  51. return len(b.Value)
  52. }
  53. // IsFull returns true if the buffer has no more room to grow.
  54. func (b *Buffer) IsFull() bool {
  55. return len(b.Value) == cap(b.Value)
  56. }
  57. // Write implements Write method in io.Writer.
  58. func (b *Buffer) Write(data []byte) (int, error) {
  59. b.Append(data)
  60. return len(data), nil
  61. }
  62. type bufferPool struct {
  63. chain chan []byte
  64. allocator *sync.Pool
  65. }
  66. func newBufferPool(bufferSize, poolSize int) *bufferPool {
  67. pool := &bufferPool{
  68. chain: make(chan []byte, poolSize),
  69. allocator: &sync.Pool{
  70. New: func() interface{} { return make([]byte, bufferSize) },
  71. },
  72. }
  73. for i := 0; i < poolSize; i++ {
  74. pool.chain <- make([]byte, bufferSize)
  75. }
  76. return pool
  77. }
  78. func (p *bufferPool) allocate() *Buffer {
  79. var b []byte
  80. select {
  81. case b = <-p.chain:
  82. default:
  83. b = p.allocator.Get().([]byte)
  84. }
  85. return &Buffer{
  86. head: b,
  87. pool: p,
  88. Value: b,
  89. }
  90. }
  91. func (p *bufferPool) free(buffer *Buffer) {
  92. select {
  93. case p.chain <- buffer.head:
  94. default:
  95. p.allocator.Put(buffer.head)
  96. }
  97. }
  98. var smallPool = newBufferPool(1024, 256)
  99. var mediumPool = newBufferPool(8*1024, 512)
  100. var largePool = newBufferPool(64*1024, 128)
  101. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  102. func NewSmallBuffer() *Buffer {
  103. return smallPool.allocate()
  104. }
  105. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  106. func NewBuffer() *Buffer {
  107. return mediumPool.allocate()
  108. }
  109. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  110. func NewLargeBuffer() *Buffer {
  111. return largePool.allocate()
  112. }