1
0

Allocator_pvt.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. struct Allocator_Allocation_pvt {
  36. struct Allocator_Allocation pub;
  37. struct Allocator_Allocation_pvt* next;
  38. long lineNum;
  39. const char* fileName;
  40. };
  41. Assert_compileTime(!(sizeof(struct Allocator_Allocation_pvt) % __BIGGEST_ALIGNMENT__));
  42. /** Singly linked list of allocators. */
  43. struct Allocator_List;
  44. struct Allocator_List {
  45. struct Allocator_pvt* alloc;
  46. struct Allocator_List* next;
  47. };
  48. struct Allocator_Adoptions {
  49. struct Allocator_List* parents;
  50. struct Allocator_List* children;
  51. };
  52. /** Internal state for Allocator. */
  53. struct Allocator_pvt
  54. {
  55. /** This allocator. */
  56. struct Allocator pub;
  57. /**
  58. * A linked list of the allocations made with this allocator.
  59. * These are all freed when the allocator is freed.
  60. */
  61. struct Allocator_Allocation_pvt* allocations;
  62. /** A linked list of jobs which must be done when this allocator is freed. */
  63. struct Allocator_OnFreeJob_pvt* onFree;
  64. /** The parent of this allocator, self-pointer if this is a root allocator. */
  65. struct Allocator_pvt* parent;
  66. /**
  67. * When this allocator is freed, lastSibling->nextSibling will be set to nextSibling
  68. * removing this allocator from the linked list.
  69. */
  70. struct Allocator_pvt* lastSibling;
  71. /** A pointer to the next allocator which is a child of the same parent. */
  72. struct Allocator_pvt* nextSibling;
  73. /** The first child allocator, this will be freed when this allocator is freed. */
  74. struct Allocator_pvt* firstChild;
  75. /** The root allocator with additional tree-global data. */
  76. struct Allocator_FirstCtx* rootAlloc;
  77. /** The number of bytes allocated by *this* allocator (but not it's children). */
  78. unsigned long allocatedHere;
  79. /**
  80. * If this allocator is neither an adopted parent nor an adopted child, this field is NULL,
  81. * Otherwise it is a linked list of adopted parents and children of this allocator.
  82. * The structure here is allocated by THIS ALLOCATOR, not by it's parent or child.
  83. */
  84. struct Allocator_Adoptions* adoptions;
  85. #ifdef Allocator_USE_CANARIES
  86. /** The canary for allocations made with this allocator constant to allow varification. */
  87. unsigned long canary;
  88. /** The canary which will be used for the next allocator, mutable. */
  89. unsigned long nextCanary;
  90. #endif
  91. /** For checking structure integrity. */
  92. Identity
  93. };
  94. /** The first ("genesis") allocator, not a child of any other allocator. */
  95. struct Allocator_FirstCtx
  96. {
  97. /** The context for the first allocator. */
  98. struct Allocator_pvt context;
  99. Allocator_Provider provider;
  100. Allocator_Provider_CONTEXT_TYPE* providerContext;
  101. /** The number of bytes which can be allocated by this allocator and all of its family. */
  102. int64_t spaceAvailable;
  103. /** The number of bytes which can be allocated total. */
  104. int64_t maxSpace;
  105. Identity
  106. };
  107. #endif