Allocator_pvt.h 4.0 KB

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