BufferAllocator.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. struct BufferAllocator_pvt;
  16. #define Allocator_Provider_CONTEXT_TYPE struct BufferAllocator_pvt
  17. #include "exception/Except.h"
  18. #include "memory/BufferAllocator.h"
  19. #include "util/Bits.h"
  20. #include "util/Identity.h"
  21. #include <stdint.h>
  22. /**
  23. * TODO(cjd): addOnFreeJob adds a job which is only run when the root allocator is freed
  24. * and it needs to be run when the allocator which called it, or any of that allocator's
  25. * ancestors is freed, not just the root.
  26. */
  27. #define ALIGNMENT __BIGGEST_ALIGNMENT__
  28. /** Internal state for Allocator. */
  29. struct BufferAllocator_pvt
  30. {
  31. /** Pointer to the beginning of the buffer. */
  32. char* const basePointer;
  33. /** Pointer to the place in the buffer to allocate the next block of memory. */
  34. char* pointer;
  35. /** Pointer to the end of the buffer. */
  36. char* const endPointer;
  37. struct Except* onOOM;
  38. Identity
  39. };
  40. /**
  41. * Get a pointer which is aligned on memory boundries.
  42. *
  43. * @param pointer the location where the pointer should be.
  44. * @param alignedOn how big the word is that the boundry should be aligned on.
  45. */
  46. #define getAligned(pointer, alignedOn) \
  47. ((char*) ((uintptr_t)( ((char*)(pointer)) + (alignedOn) - 1) & ~ ((alignedOn) - 1)))
  48. /** @see Allocator_malloc() */
  49. static void* allocatorMalloc(struct BufferAllocator_pvt* context, unsigned long length)
  50. {
  51. Identity_check(context);
  52. char* pointer = getAligned(context->pointer, ALIGNMENT);
  53. char* endOfAlloc = pointer + length;
  54. if (endOfAlloc >= context->endPointer) {
  55. Except_throw(context->onOOM, "BufferAllocator ran out of memory.");
  56. }
  57. if (endOfAlloc < context->pointer) {
  58. Except_throw(context->onOOM, "BufferAllocator integer overflow.");
  59. }
  60. context->pointer = endOfAlloc;
  61. return (void*) pointer;
  62. }
  63. static void* provideMemory(struct BufferAllocator_pvt* context,
  64. struct Allocator_Allocation* original,
  65. unsigned long size,
  66. struct Allocator* group)
  67. {
  68. if (original == NULL) {
  69. return allocatorMalloc(context, size);
  70. }
  71. if (((char*)original) + original->size == context->pointer) {
  72. // This is reallocating the last allocation.
  73. // clear the allocation then let allocatorMalloc() recreate it.
  74. context->pointer = (char*)original;
  75. }
  76. if (size == 0) {
  77. return NULL;
  78. }
  79. void* newAlloc = allocatorMalloc(context, size);
  80. if (newAlloc != original) {
  81. Assert_true((char*)newAlloc > (char*)original + original->size);
  82. Bits_memcpy(newAlloc, original, original->size);
  83. }
  84. return newAlloc;
  85. }
  86. /** @see BufferAllocator.h */
  87. struct Allocator* BufferAllocator__new(void* buffer,
  88. unsigned long length,
  89. char* file,
  90. int line)
  91. {
  92. struct BufferAllocator_pvt stackAlloc = {
  93. .basePointer = buffer,
  94. .pointer = buffer,
  95. .endPointer = ((char*)buffer) + length
  96. };
  97. Identity_set(&stackAlloc);
  98. struct BufferAllocator_pvt* alloc =
  99. allocatorMalloc(&stackAlloc, sizeof(struct BufferAllocator_pvt));
  100. Bits_memcpy(alloc, &stackAlloc, sizeof(struct BufferAllocator_pvt));
  101. return Allocator_new(0, provideMemory, alloc, file, line);
  102. }