lldb.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2014 The lldb Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package lldb implements a low level database engine. The database model used
  5. // could be considered a specific implementation of some small(est)
  6. // intersection of models listed in [1]. As a settled term is lacking, it'll be
  7. // called here a 'Virtual memory model' (VMM).
  8. //
  9. // Changelog
  10. //
  11. // 2016-07-24: v1.0.4 brings some performance improvements.
  12. //
  13. // 2016-07-22: v1.0.3 brings some small performance improvements.
  14. //
  15. // 2016-07-12: v1.0.2 now uses packages from cznic/internal.
  16. //
  17. // 2016-07-12: v1.0.1 adds a license for testdata/fortunes.txt.
  18. //
  19. // 2016-07-11: First standalone release v1.0.0 of the package previously
  20. // published as experimental (github.com/cznic/exp/lldb).
  21. //
  22. // Filers
  23. //
  24. // A Filer is an abstraction of storage. A Filer may be a part of some process'
  25. // virtual address space, an OS file, a networked, remote file etc. Persistence
  26. // of the storage is optional, opaque to VMM and it is specific to a concrete
  27. // Filer implementation.
  28. //
  29. // Space management
  30. //
  31. // Mechanism to allocate, reallocate (resize), deallocate (and later reclaim
  32. // the unused) contiguous parts of a Filer, called blocks. Blocks are
  33. // identified and referred to by a handle, an int64.
  34. //
  35. // BTrees
  36. //
  37. // In addition to the VMM like services, lldb provides volatile and
  38. // non-volatile BTrees. Keys and values of a BTree are limited in size to 64kB
  39. // each (a bit more actually). Support for larger keys/values, if desired, can
  40. // be built atop a BTree to certain limits.
  41. //
  42. // Handles vs pointers
  43. //
  44. // A handle is the abstracted storage counterpart of a memory address. There
  45. // is one fundamental difference, though. Resizing a block never results in a
  46. // change to the handle which refers to the resized block, so a handle is more
  47. // akin to an unique numeric id/key. Yet it shares one property of pointers -
  48. // handles can be associated again with blocks after the original handle block
  49. // was deallocated. In other words, a handle uniqueness domain is the state of
  50. // the database and is not something comparable to e.g. an ever growing
  51. // numbering sequence.
  52. //
  53. // Also, as with memory pointers, dangling handles can be created and blocks
  54. // overwritten when such handles are used. Using a zero handle to refer to a
  55. // block will not panic; however, the resulting error is effectively the same
  56. // exceptional situation as dereferencing a nil pointer.
  57. //
  58. // Blocks
  59. //
  60. // Allocated/used blocks, are limited in size to only a little bit more than
  61. // 64kB. Bigger semantic entities/structures must be built in lldb's client
  62. // code. The content of a block has no semantics attached, it's only a fully
  63. // opaque `[]byte`.
  64. //
  65. // Scalars
  66. //
  67. // Use of "scalars" applies to EncodeScalars, DecodeScalars and Collate. Those
  68. // first two "to bytes" and "from bytes" functions are suggested for handling
  69. // multi-valued Allocator content items and/or keys/values of BTrees (using
  70. // Collate for keys). Types called "scalar" are:
  71. //
  72. // nil (the typeless one)
  73. // bool
  74. // all integral types: [u]int8, [u]int16, [u]int32, [u]int, [u]int64
  75. // all floating point types: float32, float64
  76. // all complex types: complex64, complex128
  77. // []byte (64kB max)
  78. // string (64kb max)
  79. //
  80. // Specific implementations
  81. //
  82. // Included are concrete implementations of some of the VMM interfaces included
  83. // to ease serving simple client code or for testing and possibly as an
  84. // example. More details in the documentation of such implementations.
  85. //
  86. // [1]: http://en.wikipedia.org/wiki/Database_model
  87. package lldb
  88. const (
  89. fltSz = 0x70 // size of the FLT
  90. maxShort = 251
  91. maxRq = 65787
  92. maxFLTRq = 4112
  93. maxHandle = 1<<56 - 1
  94. atomLen = 16
  95. tagUsedLong = 0xfc
  96. tagUsedRelocated = 0xfd
  97. tagFreeShort = 0xfe
  98. tagFreeLong = 0xff
  99. tagNotCompressed = 0
  100. tagCompressed = 1
  101. )
  102. // Content size n -> blocksize in atoms.
  103. func n2atoms(n int) int {
  104. if n > maxShort {
  105. n += 2
  106. }
  107. return (n+1)/16 + 1
  108. }
  109. // Content size n -> number of padding zeros.
  110. func n2padding(n int) int {
  111. if n > maxShort {
  112. n += 2
  113. }
  114. return 15 - (n+1)&15
  115. }
  116. // Handle <-> offset
  117. func h2off(h int64) int64 { return (h + 6) * 16 }
  118. func off2h(off int64) int64 { return off/16 - 6 }
  119. // Get a 7B int64 from b
  120. func b2h(b []byte) (h int64) {
  121. for _, v := range b[:7] {
  122. h = h<<8 | int64(v)
  123. }
  124. return
  125. }
  126. // Put a 7B int64 into b
  127. func h2b(b []byte, h int64) []byte {
  128. for i := range b[:7] {
  129. b[i], h = byte(h>>48), h<<8
  130. }
  131. return b
  132. }
  133. // Content length N (must be in [252, 65787]) to long used block M field.
  134. func n2m(n int) (m int) {
  135. return n % 0x10000
  136. }
  137. // Long used block M (must be in [0, 65535]) field to content length N.
  138. func m2n(m int) (n int) {
  139. if m <= maxShort {
  140. m += 0x10000
  141. }
  142. return m
  143. }
  144. func bpack(a []byte) []byte {
  145. if cap(a) > len(a) {
  146. return append([]byte(nil), a...)
  147. }
  148. return a
  149. }