buffer.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Bytes returns the content bytes of this Buffer.
  53. func (b *Buffer) Bytes() []byte {
  54. return b.Value
  55. }
  56. // Slice cuts the buffer at the given position.
  57. func (b *Buffer) Slice(from, to int) *Buffer {
  58. b.Value = b.Value[from:to]
  59. return b
  60. }
  61. // SliceFrom cuts the buffer at the given position.
  62. func (b *Buffer) SliceFrom(from int) *Buffer {
  63. b.Value = b.Value[from:]
  64. return b
  65. }
  66. // SliceBack extends the Buffer to its front by offset bytes.
  67. // Caller must ensure cumulated offset is no more than 16.
  68. func (b *Buffer) SliceBack(offset int) *Buffer {
  69. newoffset := b.offset - offset
  70. if newoffset < 0 {
  71. newoffset = 0
  72. }
  73. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  74. b.offset = newoffset
  75. return b
  76. }
  77. // Len returns the length of the buffer content.
  78. func (b *Buffer) Len() int {
  79. if b == nil {
  80. return 0
  81. }
  82. return len(b.Value)
  83. }
  84. // IsFull returns true if the buffer has no more room to grow.
  85. func (b *Buffer) IsFull() bool {
  86. return len(b.Value) == cap(b.Value)
  87. }
  88. // Write implements Write method in io.Writer.
  89. func (b *Buffer) Write(data []byte) (int, error) {
  90. b.Append(data)
  91. return len(data), nil
  92. }
  93. // Read implements io.Reader.Read().
  94. func (b *Buffer) Read(data []byte) (int, error) {
  95. if b.Len() == 0 {
  96. return 0, io.EOF
  97. }
  98. nBytes := copy(data, b.Value)
  99. if nBytes == b.Len() {
  100. b.Value = b.Value[:0]
  101. } else {
  102. b.Value = b.Value[nBytes:]
  103. }
  104. return nBytes, nil
  105. }
  106. type bufferPool struct {
  107. chain chan []byte
  108. allocator *sync.Pool
  109. }
  110. func newBufferPool(bufferSize, poolSize int) *bufferPool {
  111. pool := &bufferPool{
  112. chain: make(chan []byte, poolSize),
  113. allocator: &sync.Pool{
  114. New: func() interface{} { return make([]byte, bufferSize) },
  115. },
  116. }
  117. for i := 0; i < poolSize/2; i++ {
  118. pool.chain <- make([]byte, bufferSize)
  119. }
  120. return pool
  121. }
  122. func (p *bufferPool) allocate() *Buffer {
  123. var b []byte
  124. select {
  125. case b = <-p.chain:
  126. default:
  127. b = p.allocator.Get().([]byte)
  128. }
  129. return &Buffer{
  130. head: b,
  131. pool: p,
  132. Value: b[defaultOffset:],
  133. offset: defaultOffset,
  134. }
  135. }
  136. func (p *bufferPool) free(buffer *Buffer) {
  137. select {
  138. case p.chain <- buffer.head:
  139. default:
  140. p.allocator.Put(buffer.head)
  141. }
  142. }
  143. var smallPool = newBufferPool(1024, 64)
  144. var mediumPool = newBufferPool(8*1024, 128)
  145. var largePool = newBufferPool(64*1024, 64)
  146. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  147. func NewSmallBuffer() *Buffer {
  148. return smallPool.allocate()
  149. }
  150. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  151. func NewBuffer() *Buffer {
  152. return mediumPool.allocate()
  153. }
  154. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  155. func NewLargeBuffer() *Buffer {
  156. return largePool.allocate()
  157. }