allocators.h 4.7 KB

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