buffer.go 3.0 KB

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