buffer.go 3.1 KB

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