allocators.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*************************************************************************/
  2. /* allocators.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef ALLOCATORS_H
  31. #define ALLOCATORS_H
  32. #include "core/os/memory.h"
  33. template <int PREALLOC_COUNT = 64, int MAX_HANDS = 8>
  34. class BalloonAllocator {
  35. enum {
  36. USED_FLAG = (1 << 30),
  37. USED_MASK = USED_FLAG - 1
  38. };
  39. struct Balloon {
  40. Balloon *next;
  41. Balloon *prev;
  42. uint32_t hand;
  43. };
  44. struct Hand {
  45. int used;
  46. int allocated;
  47. Balloon *first;
  48. Balloon *last;
  49. };
  50. Hand hands[MAX_HANDS];
  51. public:
  52. void *alloc(size_t p_size) {
  53. size_t max = (1 << MAX_HANDS);
  54. ERR_FAIL_COND_V(p_size > max, NULL);
  55. unsigned int hand = 0;
  56. while (p_size > (size_t)(1 << hand))
  57. ++hand;
  58. Hand &h = hands[hand];
  59. if (h.used == h.allocated) {
  60. for (int i = 0; i < PREALLOC_COUNT; i++) {
  61. Balloon *b = (Balloon *)memalloc(sizeof(Balloon) + (1 << hand));
  62. b->hand = hand;
  63. if (h.last) {
  64. b->prev = h.last;
  65. h.last->next = b;
  66. h.last = b;
  67. } else {
  68. b->prev = NULL;
  69. h.last = b;
  70. h.first = b;
  71. }
  72. }
  73. h.last->next = NULL;
  74. h.allocated += PREALLOC_COUNT;
  75. }
  76. Balloon *pick = h.last;
  77. ERR_FAIL_COND_V((pick->hand & USED_FLAG), NULL);
  78. // remove last
  79. h.last = h.last->prev;
  80. h.last->next = NULL;
  81. pick->next = h.first;
  82. h.first->prev = pick;
  83. pick->prev = NULL;
  84. h.first = pick;
  85. h.used++;
  86. pick->hand |= USED_FLAG;
  87. return (void *)(pick + 1);
  88. }
  89. void free(void *p_ptr) {
  90. Balloon *b = (Balloon *)p_ptr;
  91. b -= 1;
  92. ERR_FAIL_COND(!(b->hand & USED_FLAG));
  93. b->hand = b->hand & USED_MASK; // not used
  94. int hand = b->hand;
  95. Hand &h = hands[hand];
  96. if (b == h.first)
  97. h.first = b->next;
  98. if (b->prev)
  99. b->prev->next = b->next;
  100. if (b->next)
  101. b->next->prev = b->prev;
  102. if (h.last != b) {
  103. h.last->next = b;
  104. b->prev = h.last;
  105. b->next = NULL;
  106. h.last = b;
  107. }
  108. h.used--;
  109. if (h.used <= (h.allocated - (PREALLOC_COUNT * 2))) { // this is done to ensure no alloc/free is done constantly
  110. for (int i = 0; i < PREALLOC_COUNT; i++) {
  111. ERR_CONTINUE(h.last->hand & USED_FLAG);
  112. Balloon *new_last = h.last->prev;
  113. if (new_last)
  114. new_last->next = NULL;
  115. memfree(h.last);
  116. h.last = new_last;
  117. }
  118. h.allocated -= PREALLOC_COUNT;
  119. }
  120. }
  121. BalloonAllocator() {
  122. for (int i = 0; i < MAX_HANDS; i++) {
  123. hands[i].allocated = 0;
  124. hands[i].used = 0;
  125. hands[i].first = NULL;
  126. hands[i].last = NULL;
  127. }
  128. }
  129. void clear() {
  130. for (int i = 0; i < MAX_HANDS; i++) {
  131. while (hands[i].first) {
  132. Balloon *b = hands[i].first;
  133. hands[i].first = b->next;
  134. memfree(b);
  135. }
  136. hands[i].allocated = 0;
  137. hands[i].used = 0;
  138. hands[i].first = NULL;
  139. hands[i].last = NULL;
  140. }
  141. }
  142. ~BalloonAllocator() {
  143. clear();
  144. }
  145. };
  146. #endif // ALLOCATORS_H