BufferAllocator.c 4.0 KB

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