buffer.go 2.7 KB

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