node.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  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. #ifndef __NODE_DTC_H
  17. #define __NODE_DTC_H
  18. #include <stdint.h>
  19. #include "namespace.h"
  20. #include "global.h"
  21. #include "node_set.h"
  22. #include "node_index.h"
  23. DTC_BEGIN_NAMESPACE
  24. class NGInfo;
  25. class NodeIndex;
  26. class Node {
  27. public:
  28. Node(NODE_SET *ns = NULL, int idx = 0) : _owner(ns), _index(idx)
  29. {
  30. }
  31. Node(const Node &n) : _owner(n._owner), _index(n._index)
  32. {
  33. }
  34. ~Node()
  35. {
  36. }
  37. public:
  38. int get_index(void)
  39. {
  40. return _index;
  41. }
  42. NODE_SET *Owner()
  43. {
  44. return _owner;
  45. }
  46. /* attribute op*/
  47. NODE_ID_T &lru_prev()
  48. {
  49. NODE_ID_T *p = node_lru();
  50. return p[LRU_PREV];
  51. }
  52. NODE_ID_T &lru_next()
  53. {
  54. NODE_ID_T *p = node_lru();
  55. return p[LRU_NEXT];
  56. }
  57. NODE_ID_T &next_node_id()
  58. {
  59. return _owner->next_node_id(_index);
  60. }
  61. NODE_ID_T node_id()
  62. {
  63. return _owner->node_id(_index);
  64. }
  65. MEM_HANDLE_T &vd_handle()
  66. {
  67. return _owner->vd_handle(_index);
  68. }
  69. /* return time-marker time */
  70. unsigned int Time()
  71. {
  72. return (unsigned int)vd_handle();
  73. }
  74. /* dirty flag*/
  75. bool is_dirty() const
  76. {
  77. return _owner->is_dirty(_index);
  78. }
  79. void set_dirty()
  80. {
  81. return _owner->set_dirty(_index);
  82. }
  83. void clr_dirty()
  84. {
  85. return _owner->clr_dirty(_index);
  86. }
  87. public:
  88. /* used for timelist */
  89. Node Next()
  90. {
  91. return from_id(lru_next());
  92. }
  93. Node Prev()
  94. {
  95. return from_id(lru_prev());
  96. }
  97. /* used for hash */
  98. Node next_node(void)
  99. {
  100. return from_id(next_node_id());
  101. }
  102. /* for copyable */
  103. Node &operator=(const Node &n)
  104. {
  105. _owner = n._owner;
  106. _index = n._index;
  107. return *this;
  108. }
  109. int operator!() const
  110. {
  111. return _owner == NULL || _index >= NODE_GROUP_INCLUDE_NODES;
  112. }
  113. int operator!=(Node &node)
  114. {
  115. return _owner != node.Owner() || _index != node.get_index();
  116. }
  117. int operator==(Node &node)
  118. {
  119. return _owner == node.Owner() && _index == node.get_index();
  120. }
  121. int not_in_lru_list()
  122. {
  123. return lru_prev() == node_id() || lru_next() == node_id();
  124. }
  125. static Node Empty(void)
  126. {
  127. Node node;
  128. return node;
  129. }
  130. private:
  131. /* init or delete this */
  132. int Reset()
  133. {
  134. next_node_id() = INVALID_NODE_ID;
  135. lru_prev() = node_id();
  136. lru_next() = node_id();
  137. clr_dirty();
  138. return 0;
  139. }
  140. int Release()
  141. {
  142. _owner->release_node(*this);
  143. Reset();
  144. _owner = NULL;
  145. _index = 0;
  146. return 0;
  147. }
  148. static inline Node from_id(NODE_ID_T id)
  149. {
  150. return I_SEARCH(id);
  151. }
  152. private:
  153. // [0] = prev, [1] = next
  154. NODE_ID_T *node_lru()
  155. {
  156. return _owner->node_lru(_index);
  157. }
  158. private:
  159. NODE_SET *_owner;
  160. int _index;
  161. public:
  162. /* friend class */
  163. friend class NGInfo;
  164. friend class NodeIndex;
  165. friend struct node_set;
  166. };
  167. DTC_END_NAMESPACE
  168. #endif