memory.cpp 4.1 KB

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