buffer.go 4.8 KB

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