pool.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package util
  2. import (
  3. "io"
  4. "net"
  5. )
  6. var PoolSize = 16
  7. type Recyclable interface {
  8. Recycle()
  9. }
  10. type BLLReader struct {
  11. *ListItem[Buffer]
  12. pos int
  13. }
  14. func (r *BLLReader) CanRead() bool {
  15. return r.ListItem != nil && !r.IsRoot()
  16. }
  17. func (r *BLLReader) Skip(n int) (err error) {
  18. for r.CanRead() {
  19. l := r.Value.Len() - r.pos
  20. if l > n {
  21. r.pos += n
  22. return
  23. }
  24. n -= l
  25. r.ListItem = r.Next
  26. r.pos = 0
  27. }
  28. return io.EOF
  29. }
  30. func (r *BLLReader) ReadByte() (b byte, err error) {
  31. for r.CanRead() {
  32. l := r.Value.Len() - r.pos
  33. if l > 0 {
  34. b = r.Value[r.pos]
  35. r.pos++
  36. return
  37. }
  38. r.ListItem = r.Next
  39. r.pos = 0
  40. }
  41. return 0, io.EOF
  42. }
  43. func (r *BLLReader) LEB128Unmarshal() (uint, int, error) {
  44. v := uint(0)
  45. n := 0
  46. for i := 0; i < 8; i++ {
  47. b, err := r.ReadByte()
  48. if err != nil {
  49. return 0, 0, err
  50. }
  51. v |= (uint(b&0b01111111) << (i * 7))
  52. n++
  53. if (b & 0b10000000) == 0 {
  54. break
  55. }
  56. }
  57. return v, n, nil
  58. }
  59. func (r *BLLReader) ReadBE(n int) (be uint32, err error) {
  60. for i := 0; i < n; i++ {
  61. b, err := r.ReadByte()
  62. if err != nil {
  63. return 0, err
  64. }
  65. be += uint32(b) << ((n - i - 1) << 3)
  66. }
  67. return
  68. }
  69. func (r *BLLReader) ReadN(n int) (result net.Buffers) {
  70. for r.CanRead() {
  71. l := r.Value.Len() - r.pos
  72. if l > n {
  73. result = append(result, r.Value[r.pos:r.pos+n])
  74. r.pos += n
  75. return
  76. }
  77. result = append(result, r.Value[r.pos:])
  78. n -= l
  79. r.ListItem = r.Next
  80. r.pos = 0
  81. }
  82. return
  83. }
  84. func (r *BLLReader) WriteNTo(n int, result *net.Buffers) (actual int) {
  85. actual = n
  86. for r.CanRead() {
  87. l := r.Value.Len() - r.pos
  88. if l > n {
  89. *result = append(*result, r.Value[r.pos:r.pos+n])
  90. r.pos += n
  91. return
  92. }
  93. *result = append(*result, r.Value[r.pos:])
  94. n -= l
  95. r.ListItem = r.Next
  96. r.pos = 0
  97. }
  98. return actual - n
  99. }
  100. func (r *BLLReader) GetOffset() int {
  101. return r.pos
  102. }
  103. type BLLsReader struct {
  104. *ListItem[*BLL]
  105. BLLReader
  106. }
  107. func (r *BLLsReader) CanRead() bool {
  108. return r.ListItem != nil && !r.IsRoot()
  109. }
  110. func (r *BLLsReader) ReadByte() (b byte, err error) {
  111. if r.BLLReader.CanRead() {
  112. b, err = r.BLLReader.ReadByte()
  113. if err == nil {
  114. return
  115. }
  116. }
  117. r.ListItem = r.Next
  118. if !r.CanRead() {
  119. return 0, io.EOF
  120. }
  121. r.BLLReader = *r.Value.NewReader()
  122. return r.BLLReader.ReadByte()
  123. }
  124. type BLLs struct {
  125. List[*BLL]
  126. ByteLength int
  127. }
  128. func (list *BLLs) PushValue(item *BLL) {
  129. if list == nil {
  130. return
  131. }
  132. list.List.PushValue(item)
  133. list.ByteLength += item.ByteLength
  134. }
  135. func (list *BLLs) Push(item *ListItem[Buffer]) {
  136. if list == nil {
  137. return
  138. }
  139. if list.List.Length == 0 {
  140. var bll BLL
  141. bll.Push(item)
  142. list.PushValue(&bll)
  143. } else {
  144. list.Pre.Value.Push(item)
  145. list.ByteLength += item.Value.Len()
  146. }
  147. }
  148. func (list *BLLs) ToList() (result [][][]byte) {
  149. list.Range(func(bll *BLL) bool {
  150. result = append(result, bll.ToBuffers())
  151. return true
  152. })
  153. return
  154. }
  155. func (list *BLLs) ToBuffers() (result net.Buffers) {
  156. list.Range(func(bll *BLL) bool {
  157. result = append(result, bll.ToBuffers()...)
  158. return true
  159. })
  160. return
  161. }
  162. func (list *BLLs) ToBytes() (result []byte) {
  163. list.Range(func(bll *BLL) bool {
  164. result = append(result, bll.ToBytes()...)
  165. return true
  166. })
  167. return
  168. }
  169. func (list *BLLs) Recycle() {
  170. list.Range(func(bll *BLL) bool {
  171. bll.Recycle()
  172. return true
  173. })
  174. list.Clear()
  175. list.ByteLength = 0
  176. }
  177. func (list *BLLs) NewReader() *BLLsReader {
  178. return &BLLsReader{list.Next, *list.Next.Value.NewReader()}
  179. }
  180. // ByteLinkList
  181. type BLL struct {
  182. List[Buffer]
  183. ByteLength int
  184. }
  185. func (list *BLL) NewReader() *BLLReader {
  186. return &BLLReader{list.Next, 0}
  187. }
  188. // func (list *BLL) Concat(list2 BLL) {
  189. // list.Tail.Next = list2.Head
  190. // list.Tail = list2.Tail
  191. // list.Length += list2.Length
  192. // list.ByteLength += list2.ByteLength
  193. // }
  194. func (list *BLL) Push(item *ListItem[Buffer]) {
  195. if list == nil {
  196. return
  197. }
  198. list.List.Push(item)
  199. list.ByteLength += item.Value.Len()
  200. }
  201. func (list *BLL) Shift() (item *ListItem[Buffer]) {
  202. if list == nil || list.Length == 0 {
  203. return
  204. }
  205. item = list.List.Shift()
  206. list.ByteLength -= item.Value.Len()
  207. return
  208. }
  209. func (list *BLL) Clear() {
  210. list.List.Clear()
  211. list.ByteLength = 0
  212. }
  213. func (list *BLL) ToBuffers() (result net.Buffers) {
  214. list.Range(func(item Buffer) bool {
  215. result = append(result, item)
  216. return true
  217. })
  218. return
  219. }
  220. func (list *BLL) WriteTo(w io.Writer) (int64, error) {
  221. t := list.ToBuffers()
  222. return t.WriteTo(w)
  223. }
  224. func (list *BLL) ToBytes() (b []byte) {
  225. b = make([]byte, 0, list.ByteLength)
  226. list.Range(func(item Buffer) bool {
  227. b = append(b, item...)
  228. return true
  229. })
  230. return
  231. }
  232. // 全部回收掉
  233. func (list *BLL) Recycle() {
  234. list.List.Recycle()
  235. list.ByteLength = 0
  236. }
  237. func (list *BLL) GetByte(index int) (b byte) {
  238. list.Range(func(item Buffer) bool {
  239. l := item.Len()
  240. if index < l {
  241. b = item[index]
  242. return false
  243. }
  244. index -= l
  245. return true
  246. })
  247. return
  248. }
  249. func (list *BLL) GetUint24(index int) uint32 {
  250. return list.GetUintN(index, 3)
  251. }
  252. func (list *BLL) GetUintN(index int, n int) (result uint32) {
  253. for i := 0; i < n; i++ {
  254. result += uint32(list.GetByte(index+i)) << ((n - i - 1) << 3)
  255. }
  256. return
  257. }
  258. // func (b *Buffer) ToBuffers() (result net.Buffers) {
  259. // for p := b.Next; p != &b.ListItem; p = p.Next {
  260. // result = append(result, p.Value)
  261. // }
  262. // return
  263. // }
  264. type BytesPool []List[Buffer]
  265. // 获取来自真实内存的切片的——假内存块,即只回收外壳
  266. func (p BytesPool) GetShell(b []byte) (item *ListItem[Buffer]) {
  267. if len(p) == 0 {
  268. return &ListItem[Buffer]{Value: b}
  269. }
  270. item = p[0].PoolShift()
  271. item.Value = b
  272. item.reset = true
  273. return
  274. }
  275. func (p BytesPool) Get(size int) (item *ListItem[Buffer]) {
  276. for i := 1; i < len(p); i++ {
  277. if level := 1 << i; level >= size {
  278. if item = p[i].PoolShift(); cap(item.Value) > 0 {
  279. item.Value = item.Value.SubBuf(0, size)
  280. } else {
  281. item.Value = make(Buffer, size, level)
  282. }
  283. return
  284. }
  285. }
  286. // Pool 中没有就无法回收
  287. if item == nil {
  288. item = &ListItem[Buffer]{
  289. Value: make(Buffer, size),
  290. reset: true,
  291. }
  292. }
  293. return
  294. }
  295. type Pool[T any] List[T]
  296. func (p *Pool[T]) Get() (item *ListItem[T]) {
  297. return (*List[T])(p).PoolShift()
  298. }