MallocAllocator_pvt.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 MallocAllocator_pvt_H
  16. #define MallocAllocator_pvt_H
  17. #include "memory/MallocAllocator.h"
  18. #include "util/Identity.h"
  19. #include <stdint.h>
  20. struct MallocAllocator_pvt;
  21. struct MallocAllocator_OnFreeJob;
  22. struct MallocAllocator_OnFreeJob {
  23. struct Allocator_OnFreeJob generic;
  24. struct MallocAllocator_pvt* alloc;
  25. struct MallocAllocator_OnFreeJob* next;
  26. /* prevent async jobs from being called multiple times, nonzero = done */
  27. int done;
  28. const char* file;
  29. int line;
  30. };
  31. struct MallocAllocator_Allocation;
  32. struct MallocAllocator_Allocation {
  33. struct MallocAllocator_Allocation* next;
  34. size_t size;
  35. #ifdef MallocAllocator_USE_CANARIES
  36. long beginCanary;
  37. #endif
  38. };
  39. /** Singly linked list of allocators. */
  40. struct MallocAllocator_List;
  41. struct MallocAllocator_List {
  42. struct MallocAllocator_pvt* alloc;
  43. struct MallocAllocator_List* next;
  44. };
  45. struct MallocAllocator_Adoptions {
  46. struct MallocAllocator_List* parents;
  47. struct MallocAllocator_List* children;
  48. };
  49. /** Internal state for Allocator. */
  50. struct MallocAllocator_pvt
  51. {
  52. /** This allocator. */
  53. struct Allocator pub;
  54. /**
  55. * A linked list of the allocations made with this allocator.
  56. * These are all freed when the allocator is freed.
  57. */
  58. struct MallocAllocator_Allocation* allocations;
  59. /** A linked list of jobs which must be done when this allocator is freed. */
  60. struct MallocAllocator_OnFreeJob* onFree;
  61. /**
  62. * When this allocator is freed, lastSibling->nextSibling will be set to nextSibling
  63. * removing this allocator from the linked list.
  64. * GOTCHYA: if this is the first sibling, lastSibling will point to the parent and
  65. * in that case, lastSibling->firstChild becomes nextSibling.
  66. */
  67. struct MallocAllocator_pvt* lastSibling;
  68. /** A pointer to the next allocator which is a child of the same parent. */
  69. struct MallocAllocator_pvt* nextSibling;
  70. /** The first child allocator, this will be freed when this allocator is freed. */
  71. struct MallocAllocator_pvt* firstChild;
  72. /** The root allocator with additional tree-global data. */
  73. struct MallocAllocator_FirstCtx* rootAlloc;
  74. /** The number of bytes allocated by *this* allocator (but not it's children). */
  75. unsigned long allocatedHere;
  76. /**
  77. * If this allocator is neither an adopted parent nor an adopted child, this field is NULL,
  78. * Otherwise it is a linked list of adopted parents and children of this allocator.
  79. * The structure here is allocated by THIS ALLOCATOR, not by it's parent or child.
  80. */
  81. struct MallocAllocator_Adoptions* adoptions;
  82. /** This is the location where the allocator was created. */
  83. const char* identFile;
  84. int identLine;
  85. /** If true then this allocator is in the process of being freed. */
  86. int freeing;
  87. #ifdef MallocAllocator_USE_CANARIES
  88. /** The canary for allocations made with this allocator constant to allow varification. */
  89. long canary;
  90. /** The canary which will be used for the next allocator, mutable. */
  91. long nextCanary;
  92. #endif
  93. /** For checking structure integrity. */
  94. Identity
  95. };
  96. /** The first ("genesis") allocator, not a child of any other allocator. */
  97. struct MallocAllocator_FirstCtx
  98. {
  99. /** The context for the first allocator. */
  100. struct MallocAllocator_pvt context;
  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. size_t maxSpace;
  105. };
  106. #endif