buffer.go 3.6 KB

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