allocators.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*************************************************************************/
  2. /* allocators.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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)) ++hand;
  56. Hand &h=hands[hand];
  57. if (h.used==h.allocated) {
  58. for(int i=0;i<PREALLOC_COUNT;i++) {
  59. Balloon *b = (Balloon*)memalloc(sizeof(Balloon)+(1<<hand));
  60. b->hand=hand;
  61. if (h.last) {
  62. b->prev=h.last;
  63. h.last->next=b;
  64. h.last=b;
  65. } else {
  66. b->prev=NULL;
  67. h.last=b;
  68. h.first=b;
  69. }
  70. }
  71. h.last->next=NULL;
  72. h.allocated+=PREALLOC_COUNT;
  73. }
  74. Balloon *pick=h.last;
  75. ERR_FAIL_COND_V( (pick->hand&USED_FLAG), NULL );
  76. // remove last
  77. h.last=h.last->prev;
  78. h.last->next=NULL;
  79. pick->next=h.first;
  80. h.first->prev=pick;
  81. pick->prev=NULL;
  82. h.first=pick;
  83. h.used++;
  84. pick->hand|=USED_FLAG;
  85. return (void*)(pick+1);
  86. }
  87. void free(void* p_ptr) {
  88. Balloon *b=(Balloon*)p_ptr;
  89. b-=1;
  90. ERR_FAIL_COND(!(b->hand&USED_FLAG) );
  91. b->hand=b->hand&USED_MASK; // not used
  92. int hand=b->hand;
  93. Hand &h=hands[hand];
  94. if (b==h.first)
  95. h.first=b->next;
  96. if (b->prev)
  97. b->prev->next=b->next;
  98. if (b->next)
  99. b->next->prev=b->prev;
  100. if (h.last!=b) {
  101. h.last->next=b;
  102. b->prev=h.last;
  103. b->next=NULL;
  104. h.last=b;
  105. }
  106. h.used--;
  107. if (h.used<=(h.allocated-(PREALLOC_COUNT*2))) { // this is done to ensure no alloc/free is done constantly
  108. for(int i=0;i<PREALLOC_COUNT;i++) {
  109. ERR_CONTINUE( h.last->hand& USED_FLAG );
  110. Balloon *new_last=h.last->prev;
  111. if (new_last)
  112. new_last->next=NULL;
  113. memfree( h.last );
  114. h.last=new_last;
  115. }
  116. h.allocated-=PREALLOC_COUNT;
  117. }
  118. }
  119. BalloonAllocator() {
  120. for(int i=0;i<MAX_HANDS;i++) {
  121. hands[i].allocated=0;
  122. hands[i].used=0;
  123. hands[i].first=NULL;
  124. hands[i].last=NULL;
  125. }
  126. }
  127. void clear() {
  128. for(int i=0;i<MAX_HANDS;i++) {
  129. while(hands[i].first) {
  130. Balloon *b=hands[i].first;
  131. hands[i].first=b->next;
  132. memfree(b);
  133. }
  134. hands[i].allocated=0;
  135. hands[i].used=0;
  136. hands[i].first=NULL;
  137. hands[i].last=NULL;
  138. }
  139. }
  140. ~BalloonAllocator() {
  141. clear();
  142. }
  143. };
  144. #endif // ALLOCATORS_H