arena.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package skl
  17. import (
  18. "sync/atomic"
  19. "unsafe"
  20. "github.com/dgraph-io/badger/v4/y"
  21. )
  22. const (
  23. offsetSize = int(unsafe.Sizeof(uint32(0)))
  24. // Always align nodes on 64-bit boundaries, even on 32-bit architectures,
  25. // so that the node.value field is 64-bit aligned. This is necessary because
  26. // node.getValueOffset uses atomic.LoadUint64, which expects its input
  27. // pointer to be 64-bit aligned.
  28. nodeAlign = int(unsafe.Sizeof(uint64(0))) - 1
  29. )
  30. // Arena should be lock-free.
  31. type Arena struct {
  32. n atomic.Uint32
  33. buf []byte
  34. }
  35. // newArena returns a new arena.
  36. func newArena(n int64) *Arena {
  37. // Don't store data at position 0 in order to reserve offset=0 as a kind
  38. // of nil pointer.
  39. out := &Arena{buf: make([]byte, n)}
  40. out.n.Store(1)
  41. return out
  42. }
  43. func (s *Arena) size() int64 {
  44. return int64(s.n.Load())
  45. }
  46. // putNode allocates a node in the arena. The node is aligned on a pointer-sized
  47. // boundary. The arena offset of the node is returned.
  48. func (s *Arena) putNode(height int) uint32 {
  49. // Compute the amount of the tower that will never be used, since the height
  50. // is less than maxHeight.
  51. unusedSize := (maxHeight - height) * offsetSize
  52. // Pad the allocation with enough bytes to ensure pointer alignment.
  53. l := uint32(MaxNodeSize - unusedSize + nodeAlign)
  54. n := s.n.Add(l)
  55. y.AssertTruef(int(n) <= len(s.buf),
  56. "Arena too small, toWrite:%d newTotal:%d limit:%d",
  57. l, n, len(s.buf))
  58. // Return the aligned offset.
  59. m := (n - l + uint32(nodeAlign)) & ^uint32(nodeAlign)
  60. return m
  61. }
  62. // Put will *copy* val into arena. To make better use of this, reuse your input
  63. // val buffer. Returns an offset into buf. User is responsible for remembering
  64. // size of val. We could also store this size inside arena but the encoding and
  65. // decoding will incur some overhead.
  66. func (s *Arena) putVal(v y.ValueStruct) uint32 {
  67. l := v.EncodedSize()
  68. n := s.n.Add(l)
  69. y.AssertTruef(int(n) <= len(s.buf),
  70. "Arena too small, toWrite:%d newTotal:%d limit:%d",
  71. l, n, len(s.buf))
  72. m := n - l
  73. v.Encode(s.buf[m:])
  74. return m
  75. }
  76. func (s *Arena) putKey(key []byte) uint32 {
  77. l := uint32(len(key))
  78. n := s.n.Add(l)
  79. y.AssertTruef(int(n) <= len(s.buf),
  80. "Arena too small, toWrite:%d newTotal:%d limit:%d",
  81. l, n, len(s.buf))
  82. // m is the offset where you should write.
  83. // n = new len - key len give you the offset at which you should write.
  84. m := n - l
  85. // Copy to buffer from m:n
  86. y.AssertTrue(len(key) == copy(s.buf[m:n], key))
  87. return m
  88. }
  89. // getNode returns a pointer to the node located at offset. If the offset is
  90. // zero, then the nil node pointer is returned.
  91. func (s *Arena) getNode(offset uint32) *node {
  92. if offset == 0 {
  93. return nil
  94. }
  95. return (*node)(unsafe.Pointer(&s.buf[offset]))
  96. }
  97. // getKey returns byte slice at offset.
  98. func (s *Arena) getKey(offset uint32, size uint16) []byte {
  99. return s.buf[offset : offset+uint32(size)]
  100. }
  101. // getVal returns byte slice at offset. The given size should be just the value
  102. // size and should NOT include the meta bytes.
  103. func (s *Arena) getVal(offset uint32, size uint32) (ret y.ValueStruct) {
  104. ret.Decode(s.buf[offset : offset+size])
  105. return
  106. }
  107. // getNodeOffset returns the offset of node in the arena. If the node pointer is
  108. // nil, then the zero offset is returned.
  109. func (s *Arena) getNodeOffset(nd *node) uint32 {
  110. if nd == nil {
  111. return 0
  112. }
  113. return uint32(uintptr(unsafe.Pointer(nd)) - uintptr(unsafe.Pointer(&s.buf[0])))
  114. }