memory.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "memory.h"
  24. #include "allocator.h"
  25. #include "mutex.h"
  26. // //-----------------------------------------------------------------------------
  27. // void* operator new(size_t) throw (std::bad_alloc)
  28. // {
  29. // CE_ASSERT(false, "operator new forbidden");
  30. // return NULL;
  31. // }
  32. // //-----------------------------------------------------------------------------
  33. // void* operator new[](size_t) throw (std::bad_alloc)
  34. // {
  35. // CE_ASSERT(false, "operator new[] forbidden");
  36. // return NULL;
  37. // }
  38. // //-----------------------------------------------------------------------------
  39. // void operator delete(void*) throw ()
  40. // {
  41. // CE_ASSERT(false, "operator delete forbidden");
  42. // }
  43. // //-----------------------------------------------------------------------------
  44. // void operator delete[](void*) throw ()
  45. // {
  46. // CE_ASSERT(false, "operator delete[] forbidden");
  47. // }
  48. namespace crown
  49. {
  50. namespace memory
  51. {
  52. /// Allocator based on C malloc().
  53. class HeapAllocator : public Allocator
  54. {
  55. public:
  56. HeapAllocator()
  57. : _allocated_size(0)
  58. , _allocation_count(0)
  59. {
  60. }
  61. ~HeapAllocator()
  62. {
  63. CE_ASSERT(_allocation_count == 0 && allocated_size() == 0,
  64. "Missing %d deallocations causing a leak of %ld bytes", _allocation_count, allocated_size());
  65. }
  66. /// @copydoc Allocator::allocate()
  67. void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN)
  68. {
  69. ScopedMutex sm(_mutex);
  70. size_t actual_size = actual_allocation_size(size, align);
  71. Header* h = (Header*)malloc(actual_size);
  72. h->size = actual_size;
  73. void* data = memory::align_top(h + 1, align);
  74. pad(h, data);
  75. _allocated_size += actual_size;
  76. _allocation_count++;
  77. return data;
  78. }
  79. /// @copydoc Allocator::deallocate()
  80. void deallocate(void* data)
  81. {
  82. ScopedMutex sm(_mutex);
  83. if (!data)
  84. return;
  85. Header* h = header(data);
  86. _allocated_size -= h->size;
  87. _allocation_count--;
  88. free(h);
  89. }
  90. /// @copydoc Allocator::allocated_size()
  91. size_t allocated_size()
  92. {
  93. ScopedMutex sm(_mutex);
  94. return _allocated_size;
  95. }
  96. /// Returns the size in bytes of the block of memory pointed by @a data
  97. size_t get_size(void* data)
  98. {
  99. ScopedMutex sm(_mutex);
  100. Header* h = header(data);
  101. return h->size;
  102. }
  103. private:
  104. // Holds the number of bytes of an allocation
  105. struct Header
  106. {
  107. uint32_t size;
  108. };
  109. //-----------------------------------------------------------------------------
  110. size_t actual_allocation_size(size_t size, size_t align)
  111. {
  112. return size + align + sizeof(Header);
  113. }
  114. //-----------------------------------------------------------------------------
  115. Header* header(void* data)
  116. {
  117. uint32_t* ptr = (uint32_t*)data;
  118. ptr--;
  119. while (*ptr == memory::PADDING_VALUE)
  120. {
  121. ptr--;
  122. }
  123. return (Header*)ptr;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void* data(Header* header, size_t align)
  127. {
  128. return memory::align_top(header + 1, align);
  129. }
  130. //-----------------------------------------------------------------------------
  131. void pad(Header* header, void* data)
  132. {
  133. uint32_t* p = (uint32_t*)(header + 1);
  134. while (p != data)
  135. {
  136. *p = memory::PADDING_VALUE;
  137. p++;
  138. }
  139. }
  140. private:
  141. Mutex _mutex;
  142. size_t _allocated_size;
  143. uint32_t _allocation_count;
  144. };
  145. } // namespace memory
  146. namespace memory_globals
  147. {
  148. using namespace memory;
  149. // Create default allocators
  150. char _buffer[1024];
  151. HeapAllocator* _default_allocator = NULL;
  152. void init()
  153. {
  154. _default_allocator = new (_buffer) HeapAllocator();
  155. }
  156. void shutdown()
  157. {
  158. _default_allocator->~HeapAllocator();
  159. }
  160. } // namespace memory_globals
  161. Allocator& default_allocator()
  162. {
  163. return *memory_globals::_default_allocator;
  164. }
  165. } // namespace crown