buffer.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Package alloc provides a light-weight memory allocation mechanism.
  2. package alloc
  3. import (
  4. "io"
  5. "github.com/v2ray/v2ray-core/common/serial"
  6. )
  7. const (
  8. defaultOffset = 16
  9. )
  10. // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
  11. // the buffer into an internal buffer pool, in order to recreate a buffer more
  12. // quickly.
  13. type Buffer struct {
  14. head []byte
  15. pool *BufferPool
  16. Value []byte
  17. offset int
  18. }
  19. func CreateBuffer(container []byte, parent *BufferPool) *Buffer {
  20. b := new(Buffer)
  21. b.head = container
  22. b.pool = parent
  23. b.Value = b.head[defaultOffset:]
  24. b.offset = defaultOffset
  25. return b
  26. }
  27. // Release recycles the buffer into an internal buffer pool.
  28. func (b *Buffer) Release() {
  29. if b == nil || b.head == nil {
  30. return
  31. }
  32. if b.pool != nil {
  33. b.pool.Free(b)
  34. }
  35. b.head = nil
  36. b.Value = nil
  37. b.pool = nil
  38. }
  39. // Clear clears the content of the buffer, results an empty buffer with
  40. // Len() = 0.
  41. func (b *Buffer) Clear() *Buffer {
  42. b.offset = defaultOffset
  43. b.Value = b.head[b.offset:b.offset]
  44. return b
  45. }
  46. func (b *Buffer) Reset() *Buffer {
  47. b.offset = defaultOffset
  48. b.Value = b.head
  49. return b
  50. }
  51. // AppendBytes appends one or more bytes to the end of the buffer.
  52. func (b *Buffer) AppendBytes(bytes ...byte) *Buffer {
  53. b.Value = append(b.Value, bytes...)
  54. return b
  55. }
  56. // Append appends a byte array to the end of the buffer.
  57. func (b *Buffer) Append(data []byte) *Buffer {
  58. b.Value = append(b.Value, data...)
  59. return b
  60. }
  61. // AppendString appends a given string to the end of the buffer.
  62. func (b *Buffer) AppendString(s string) *Buffer {
  63. b.Value = append(b.Value, s...)
  64. return b
  65. }
  66. func (b *Buffer) AppendUint16(v uint16) *Buffer {
  67. b.Value = serial.Uint16ToBytes(v, b.Value)
  68. return b
  69. }
  70. func (b *Buffer) AppendUint32(v uint32) *Buffer {
  71. b.Value = serial.Uint32ToBytes(v, b.Value)
  72. return b
  73. }
  74. // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
  75. // no more than 16 bytes.
  76. func (b *Buffer) Prepend(data []byte) *Buffer {
  77. b.SliceBack(len(data))
  78. copy(b.Value, data)
  79. return b
  80. }
  81. func (b *Buffer) PrependBytes(data ...byte) *Buffer {
  82. return b.Prepend(data)
  83. }
  84. func (b *Buffer) PrependUint16(v uint16) *Buffer {
  85. b.SliceBack(2)
  86. serial.Uint16ToBytes(v, b.Value[:0])
  87. return b
  88. }
  89. func (b *Buffer) PrependUint32(v uint32) *Buffer {
  90. b.SliceBack(4)
  91. serial.Uint32ToBytes(v, b.Value[:0])
  92. return b
  93. }
  94. // Bytes returns the content bytes of this Buffer.
  95. func (b *Buffer) Bytes() []byte {
  96. return b.Value
  97. }
  98. // Slice cuts the buffer at the given position.
  99. func (b *Buffer) Slice(from, to int) *Buffer {
  100. b.offset += from
  101. b.Value = b.Value[from:to]
  102. return b
  103. }
  104. // SliceFrom cuts the buffer at the given position.
  105. func (b *Buffer) SliceFrom(from int) *Buffer {
  106. b.offset += from
  107. b.Value = b.Value[from:]
  108. return b
  109. }
  110. // SliceBack extends the Buffer to its front by offset bytes.
  111. // Caller must ensure cumulated offset is no more than 16.
  112. func (b *Buffer) SliceBack(offset int) *Buffer {
  113. newoffset := b.offset - offset
  114. if newoffset < 0 {
  115. panic("Negative buffer offset.")
  116. }
  117. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  118. b.offset = newoffset
  119. return b
  120. }
  121. // Len returns the length of the buffer content.
  122. func (b *Buffer) Len() int {
  123. if b == nil {
  124. return 0
  125. }
  126. return len(b.Value)
  127. }
  128. func (b *Buffer) IsEmpty() bool {
  129. return b.Len() == 0
  130. }
  131. // IsFull returns true if the buffer has no more room to grow.
  132. func (b *Buffer) IsFull() bool {
  133. return len(b.Value) == cap(b.Value)
  134. }
  135. // Write implements Write method in io.Writer.
  136. func (b *Buffer) Write(data []byte) (int, error) {
  137. b.Append(data)
  138. return len(data), nil
  139. }
  140. // Read implements io.Reader.Read().
  141. func (b *Buffer) Read(data []byte) (int, error) {
  142. if b.Len() == 0 {
  143. return 0, io.EOF
  144. }
  145. nBytes := copy(data, b.Value)
  146. if nBytes == b.Len() {
  147. b.Clear()
  148. } else {
  149. b.Value = b.Value[nBytes:]
  150. b.offset += nBytes
  151. }
  152. return nBytes, nil
  153. }
  154. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  155. begin := b.Len()
  156. b.Value = b.Value[:cap(b.Value)]
  157. nBytes, err := reader.Read(b.Value[begin:])
  158. if err == nil {
  159. b.Value = b.Value[:begin+nBytes]
  160. }
  161. return nBytes, err
  162. }
  163. func (b *Buffer) String() string {
  164. return string(b.Value)
  165. }
  166. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  167. func NewSmallBuffer() *Buffer {
  168. return smallPool.Allocate()
  169. }
  170. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  171. func NewBuffer() *Buffer {
  172. return mediumPool.Allocate()
  173. }
  174. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  175. func NewLargeBuffer() *Buffer {
  176. return largePool.Allocate()
  177. }
  178. func NewBufferWithSize(size int) *Buffer {
  179. if size <= SmallBufferSize {
  180. return NewSmallBuffer()
  181. }
  182. if size <= BufferSize {
  183. return NewBuffer()
  184. }
  185. return NewLargeBuffer()
  186. }
  187. func NewLocalBuffer(size int) *Buffer {
  188. return CreateBuffer(make([]byte, size), nil)
  189. }