buffer.go 3.2 KB

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