temp_allocator.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "allocator.h"
  25. #include "memory.h"
  26. namespace crown
  27. {
  28. /// Allocates from a fixed-size buffer.
  29. /// When the internal memory is exhausted or the allocation size exceeds
  30. /// the remainig internal storage, the backing allocator is used instead.
  31. /// The memory is automatically freed when the allocator is destroyed.
  32. ///
  33. /// @ingroup Memory
  34. template <size_t SIZE>
  35. class TempAllocator : public Allocator
  36. {
  37. public:
  38. /// Uses the @a backing allocator when internal memory is exahusted
  39. /// or the allocation size exceeds the remaining storage.
  40. TempAllocator(Allocator& backing = default_allocator());
  41. ~TempAllocator();
  42. /// @copydoc Allocator::allocate()
  43. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
  44. /// Does nothing, the memory is automatically freed when the
  45. /// allocator is destroyed.
  46. void deallocate(void* data);
  47. /// @copydoc Allocator::allocated_size()
  48. size_t allocated_size();
  49. private:
  50. Allocator& _backing;
  51. char* _begin;
  52. char* _end;
  53. char* _cur;
  54. size_t _chunk_size;
  55. char _buffer[SIZE];
  56. };
  57. typedef TempAllocator<64> TempAllocator64;
  58. typedef TempAllocator<128> TempAllocator128;
  59. typedef TempAllocator<256> TempAllocator256;
  60. typedef TempAllocator<512> TempAllocator512;
  61. typedef TempAllocator<1024> TempAllocator1024;
  62. typedef TempAllocator<2048> TempAllocator2048;
  63. typedef TempAllocator<4096> TempAllocator4096;
  64. template <size_t SIZE>
  65. inline TempAllocator<SIZE>::TempAllocator(Allocator& backing)
  66. : _backing(backing)
  67. , _begin(&_buffer[0])
  68. , _end(&_buffer[SIZE - 1])
  69. , _cur(&_buffer[0])
  70. , _chunk_size(4 * 1024)
  71. {
  72. *(void**) _begin = 0;
  73. _cur += sizeof(void*);
  74. }
  75. template <size_t SIZE>
  76. inline TempAllocator<SIZE>::~TempAllocator()
  77. {
  78. union { char* as_char; void** as_dvoid; };
  79. as_char = _buffer;
  80. void *p = *(void **)as_dvoid;
  81. while (p)
  82. {
  83. void *next = *(void **)p;
  84. _backing.deallocate(p);
  85. p = next;
  86. }
  87. }
  88. template <size_t SIZE>
  89. inline void* TempAllocator<SIZE>::allocate(size_t size, size_t align)
  90. {
  91. _cur = (char*) memory::align_top(_cur, align);
  92. if (size > size_t(_end - _cur))
  93. {
  94. uint32_t to_allocate = sizeof(void*) + size + align;
  95. if (to_allocate < _chunk_size)
  96. {
  97. to_allocate = _chunk_size;
  98. }
  99. _chunk_size *= 2;
  100. void *p = _backing.allocate(to_allocate);
  101. *(void **)_begin = p;
  102. _cur = _begin = (char*) p;
  103. _end = _begin + to_allocate;
  104. *(void**) _begin = 0;
  105. _cur += sizeof(void*);
  106. memory::align_top(p, align);
  107. }
  108. void *result = _cur;
  109. _cur += size;
  110. return result;
  111. }
  112. template <size_t SIZE>
  113. inline void TempAllocator<SIZE>::deallocate(void* /*data*/)
  114. {
  115. }
  116. template <size_t SIZE>
  117. inline size_t TempAllocator<SIZE>::allocated_size()
  118. {
  119. return 0;
  120. }
  121. } // namespace crown