Allocator_pvt.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Allocator_pvt_H
  16. #define Allocator_pvt_H
  17. #include "memory/Allocator.h"
  18. #include "util/Identity.h"
  19. #include "util/Assert.h"
  20. #include <stdint.h>
  21. struct Allocator_pvt;
  22. struct Allocator_OnFreeJob_pvt;
  23. struct Allocator_OnFreeJob_pvt {
  24. struct Allocator_OnFreeJob pub;
  25. struct Allocator_pvt* alloc;
  26. struct Allocator_OnFreeJob_pvt* next;
  27. /* prevent async jobs from being called multiple times, nonzero = called */
  28. int called;
  29. int line;
  30. const char* file;
  31. /** Set by allocator. */
  32. Identity
  33. };
  34. struct Allocator_Allocation_pvt;
  35. #define Allocator_Allocation_pvt_SIZE_NOPAD ( \
  36. Allocator_Allocation_SIZE + \
  37. __SIZEOF_POINTER__ + \
  38. __SIZEOF_LONG__ + \
  39. __SIZEOF_POINTER__ \
  40. )
  41. struct Allocator_Allocation_pvt {
  42. struct Allocator_Allocation pub;
  43. struct Allocator_Allocation_pvt* next;
  44. #if Allocator_Allocation_pvt_SIZE_NOPAD < __BIGGEST_ALIGNMENT__
  45. uint8_t _pad[__BIGGEST_ALIGNMENT__ - Allocator_Allocation_pvt_SIZE_NOPAD];
  46. #define Allocator_Allocation_pvt_SIZE __BIGGEST_ALIGNMENT__
  47. #else
  48. #define Allocator_Allocation_pvt_SIZE Allocator_Allocation_pvt_SIZE_NOPAD
  49. #endif
  50. long lineNum;
  51. const char* fileName;
  52. };
  53. Assert_compileTime(sizeof(struct Allocator_Allocation_pvt) == Allocator_Allocation_pvt_SIZE);
  54. Assert_compileTime(!(Allocator_Allocation_pvt_SIZE % __BIGGEST_ALIGNMENT__));
  55. /** Singly linked list of allocators. */
  56. struct Allocator_List;
  57. struct Allocator_List {
  58. struct Allocator_pvt* alloc;
  59. struct Allocator_List* next;
  60. };
  61. struct Allocator_Adoptions {
  62. struct Allocator_List* parents;
  63. struct Allocator_List* children;
  64. };
  65. /** Internal state for Allocator. */
  66. struct Allocator_pvt
  67. {
  68. /** This allocator. */
  69. struct Allocator pub;
  70. /**
  71. * A linked list of the allocations made with this allocator.
  72. * These are all freed when the allocator is freed.
  73. */
  74. struct Allocator_Allocation_pvt* allocations;
  75. /** A linked list of jobs which must be done when this allocator is freed. */
  76. struct Allocator_OnFreeJob_pvt* onFree;
  77. /** The parent of this allocator, self-pointer if this is a root allocator. */
  78. struct Allocator_pvt* parent;
  79. /**
  80. * When this allocator is freed, lastSibling->nextSibling will be set to nextSibling
  81. * removing this allocator from the linked list.
  82. */
  83. struct Allocator_pvt* lastSibling;
  84. /** A pointer to the next allocator which is a child of the same parent. */
  85. struct Allocator_pvt* nextSibling;
  86. /** The first child allocator, this will be freed when this allocator is freed. */
  87. struct Allocator_pvt* firstChild;
  88. /** The root allocator with additional tree-global data. */
  89. struct Allocator_FirstCtx* rootAlloc;
  90. /** The number of bytes allocated by *this* allocator (but not it's children). */
  91. unsigned long allocatedHere;
  92. /**
  93. * If this allocator is neither an adopted parent nor an adopted child, this field is NULL,
  94. * Otherwise it is a linked list of adopted parents and children of this allocator.
  95. * The structure here is allocated by THIS ALLOCATOR, not by it's parent or child.
  96. */
  97. struct Allocator_Adoptions* adoptions;
  98. #ifdef Allocator_USE_CANARIES
  99. /** The canary for allocations made with this allocator constant to allow varification. */
  100. unsigned long canary;
  101. /** The canary which will be used for the next allocator, mutable. */
  102. unsigned long nextCanary;
  103. #endif
  104. /** For checking structure integrity. */
  105. Identity
  106. };
  107. /** The first ("genesis") allocator, not a child of any other allocator. */
  108. struct Allocator_FirstCtx
  109. {
  110. /** The context for the first allocator. */
  111. struct Allocator_pvt context;
  112. Allocator_Provider provider;
  113. Allocator_Provider_CONTEXT_TYPE* providerContext;
  114. /** The number of bytes which can be allocated by this allocator and all of its family. */
  115. int64_t spaceAvailable;
  116. /** The number of bytes which can be allocated total. */
  117. int64_t maxSpace;
  118. Identity
  119. };
  120. #endif