General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
6.6 KiB

пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
  1. #pragma once
  2. #include "gp_config.hpp"
  3. #include "gp/buffer.hpp"
  4. #include "gp/array.hpp"
  5. #include "gp/integer_math.hpp"
  6. #include <type_traits>
  7. #include <gp/algorithm/tmp_manip.hpp>
  8. #include <gp/algorithm/modifiers.hpp>
  9. #include <gp/allocator/dummy.hpp>
  10. namespace gp{
  11. template<typename page_allocator = gp::dummy_allocator, size_t max_msb = 24, size_t align = 8>
  12. class buddy{
  13. struct twig {
  14. bool used : 1;
  15. bool used_children : 1;
  16. twig(uint8_t src) {
  17. used = 1 & src;
  18. used_children = 2 & src;
  19. }
  20. operator uint8_t() {
  21. return 1 * used + 2 * used_children;
  22. }
  23. };
  24. struct bundle {
  25. uint8_t a : 2;
  26. uint8_t b : 2;
  27. uint8_t c : 2;
  28. uint8_t d : 2;
  29. bundle() {
  30. a = 0; b = 0; c = 0; d = 0;
  31. }
  32. };
  33. page_allocator allocator;
  34. gp::buffer<char> data;
  35. const size_t max_depth;
  36. const size_t twig_explore_length;
  37. static constexpr size_t max_theoric_depth = max_msb - gp::math::msb(align);
  38. static constexpr size_t required_twigs = (1 << (max_theoric_depth + 1)) - 1;
  39. /**
  40. * ((max allocatable size - min allocatable size) ** 2 - 1) / 4 twigs in a bundle
  41. **/
  42. static constexpr size_t span_size = required_twigs / 4 + (required_twigs % 4 != 0);
  43. gp::array<bundle, span_size> stack;
  44. twig get_twig(size_t idx) const {
  45. auto far = idx / 4;
  46. auto local = idx % 4;
  47. switch(local) {
  48. case 0:
  49. return stack[far].a;
  50. case 1:
  51. return stack[far].b;
  52. case 2:
  53. return stack[far].c;
  54. case 3:
  55. return stack[far].d;
  56. }
  57. }
  58. void set_twig(size_t idx, twig v) {
  59. auto far = idx / 4;
  60. auto local = idx % 4;
  61. auto& group = stack[far];
  62. switch(local) {
  63. case 0:
  64. group.a = v;
  65. return;
  66. case 1:
  67. group.b = v;
  68. return;
  69. case 2:
  70. group.c = v;
  71. return;
  72. case 3:
  73. group.d = v;
  74. return;
  75. }
  76. }
  77. constexpr size_t size_to_depth(size_t sz) {
  78. size_t pow2 = gp::math::msb(sz) - gp::math::msb(align);
  79. return gp::clamp(
  80. (size_t)0 ,
  81. max_depth - pow2,
  82. max_depth
  83. );
  84. }
  85. constexpr size_t depth_to_size(size_t depth) {
  86. return 1 << (max_depth - depth + gp::math::msb(align));
  87. }
  88. constexpr size_t get_left(size_t index) const {
  89. return ((index + 1) << 1) - 1;
  90. }
  91. constexpr size_t get_right(size_t index) const {
  92. return ((index + 1) << 1);
  93. }
  94. template<typename function>
  95. void all_under(size_t index, function func) {
  96. size_t left = get_left(index);
  97. size_t right = get_right(index);
  98. all_under(left, func);
  99. all_under(right, func);
  100. func(left);
  101. func(right);
  102. }
  103. template<typename function>
  104. void all_over(size_t index, function func) {
  105. if(index != 0) {
  106. size_t parent = ((index + 1) >> 1) - 1;
  107. func(parent);
  108. if(parent != 0)
  109. all_over(parent, func);
  110. }
  111. }
  112. template<typename function>
  113. bool is_any_child(size_t index, function func) const {
  114. size_t left = get_left(index);
  115. size_t right = get_right(index);
  116. if(left < twig_explore_length && right < twig_explore_length) {
  117. if(func(left)) return true;
  118. if(func(right)) return true;
  119. if(is_any_child(left, func)) return true;
  120. if(is_any_child(right, func)) return true;
  121. }
  122. return false;
  123. }
  124. static constexpr size_t no_twig = -1;
  125. size_t find_free_twig(size_t depth, size_t root = 0, size_t explored = 0) const {
  126. auto v = get_twig(root);
  127. if(depth == explored) {
  128. if(v.used || v.used_children)
  129. {
  130. return no_twig;
  131. } else {
  132. return root;
  133. }
  134. } else {
  135. if(v.used)
  136. {
  137. return no_twig;
  138. }
  139. ++explored;
  140. auto ret = find_free_twig(depth, get_right(root), explored);
  141. if(ret != no_twig)
  142. {
  143. return ret;
  144. }
  145. ret = find_free_twig(depth, get_left(root), explored);
  146. if(ret != no_twig)
  147. {
  148. return ret;
  149. }
  150. }
  151. return no_twig;
  152. }
  153. size_t find_used_twig(size_t offset, size_t root = 0, size_t explored = 0) {
  154. auto v = get_twig(root);
  155. if(v.used && offset == 0)
  156. {
  157. return root;
  158. }
  159. ++explored;
  160. if(explored > max_depth) return no_twig;
  161. size_t cut = (1 << (max_depth + gp::math::log2(align))) >> explored;
  162. if(offset >= cut)
  163. {
  164. return find_used_twig(offset-cut, get_right(root), explored);
  165. } else {
  166. return find_used_twig(offset, get_left(root), explored);
  167. }
  168. }
  169. static bool empty_node(const buddy* me, size_t node) {
  170. gp_config::assertion(node < me->twig_explore_length, "bad emptyness test");
  171. auto p = me->get_twig(node);
  172. return !(
  173. p.used | p.used_children
  174. );
  175. }
  176. public:
  177. buddy()
  178. : data(gp::buffer<char>(nullptr,nullptr))
  179. , max_depth(0)
  180. , twig_explore_length(1 << max_depth)
  181. {}
  182. buddy(size_t sz)
  183. : data(nullptr,nullptr)
  184. , max_depth(gp::math::msb(sz)-gp::math::msb(align))
  185. , twig_explore_length(1 << max_depth)
  186. {
  187. if(sz!=0 && (sz & (sz - 1)) == 0)
  188. {
  189. auto v=allocator.allocate(sz);
  190. if(v!=nullptr)
  191. {
  192. if((reinterpret_cast<intptr_t>(v) % align) ==0)
  193. {
  194. data=gp::buffer<char>(reinterpret_cast<char*>(v),reinterpret_cast<char*>(v)+sz);
  195. }
  196. else
  197. {
  198. allocator.deallocate(v);
  199. }
  200. }
  201. }
  202. }
  203. buddy(char* pos,size_t sz)
  204. : data(pos,pos+sz)
  205. , max_depth(gp::math::msb(sz)-gp::math::msb(align))
  206. , twig_explore_length(1 << max_depth)
  207. {
  208. }
  209. void* allocate(size_t sz)
  210. {
  211. auto depth = size_to_depth(sz);
  212. auto index = find_free_twig(depth);
  213. if(index == no_twig)
  214. {
  215. return nullptr;
  216. }
  217. auto pot = reinterpret_cast<char*>(
  218. (index - (1 << depth) + 1)*depth_to_size(depth)
  219. + reinterpret_cast<intptr_t>(&*data.begin())
  220. );
  221. if(!data.contains(pot)) {
  222. return nullptr;
  223. }
  224. all_over(index, [&](size_t idx){
  225. auto t = get_twig(idx);
  226. t.used_children = true;
  227. set_twig(idx, t);
  228. });
  229. auto t = get_twig(index);
  230. t.used = true;
  231. set_twig(index, t);
  232. return pot;
  233. }
  234. constexpr bool try_reallocate(void*, size_t) {
  235. return false;
  236. }
  237. bool deallocate(void* ptr)
  238. {
  239. if(data.contains((char*)ptr))
  240. {
  241. size_t integral_offset = reinterpret_cast<intptr_t>(ptr) - reinterpret_cast<intptr_t>(&*data.begin());
  242. auto index = find_used_twig(integral_offset);
  243. if(index == no_twig)
  244. {
  245. return false;
  246. }
  247. twig v = get_twig(index);
  248. v.used = false;
  249. v.used_children = false;
  250. set_twig(index, v);
  251. all_over(index, [&](size_t idx){
  252. auto l = get_twig(get_left(idx));
  253. auto r = get_twig(get_right(idx));
  254. set_twig(idx, 2*(l.used | l.used_children | r.used | r.used_children));
  255. });
  256. return true;
  257. }
  258. return false;
  259. }
  260. bool empty() const {
  261. buddy* addr = (buddy*)this;
  262. auto prepred = not_fn(&buddy::empty_node);
  263. auto pred = bind_front(prepred, addr);
  264. return empty_node(addr, 0) && !is_any_child(0, pred);
  265. }
  266. ~buddy()
  267. {
  268. allocator.deallocate(data.begin().data);
  269. }
  270. };
  271. }