Memory.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <new>
  25. #include "Types.h"
  26. #include "Assert.h"
  27. #include "Allocator.h"
  28. namespace crown
  29. {
  30. CE_EXPORT Allocator& default_allocator();
  31. /// @defgroup Memory Memory
  32. namespace memory
  33. {
  34. /// Value used to fill unused memory
  35. const uint32_t PADDING_VALUE = 0xFFFFFFFFu;
  36. /// Constructs the initial default allocators.
  37. /// @note
  38. /// Has to be called before anything else during the engine startup.
  39. CE_EXPORT void init();
  40. /// Destroys the allocators created with memory::init().
  41. /// @note
  42. /// Should be the last call of the program.
  43. CE_EXPORT void shutdown();
  44. /// Returns the pointer @a p aligned to the desired @a align byte
  45. inline void* align_top(void* p, size_t align)
  46. {
  47. CE_ASSERT(align >= 1, "Alignment must be > 1");
  48. CE_ASSERT(align % 2 == 0 || align == 1, "Alignment must be a power of two");
  49. uintptr_t ptr = (uintptr_t)p;
  50. const size_t mod = ptr % align;
  51. if (mod)
  52. {
  53. ptr += align - mod;
  54. }
  55. return (void*) ptr;
  56. }
  57. /// Respects standard behaviour when calling on NULL @a ptr
  58. template <typename T>
  59. inline void call_destructor_and_deallocate(Allocator& a, T* ptr)
  60. {
  61. if (!ptr)
  62. return;
  63. ptr->~T();
  64. a.deallocate(ptr);
  65. }
  66. /// Allocates memory with @a allocator for the given @a T type
  67. /// and calls constructor on it.
  68. /// @note
  69. /// @a allocator must be a reference to an existing allocator.
  70. #define CE_NEW(allocator, T) new ((allocator).allocate(sizeof(T), CE_ALIGNOF(T))) T
  71. /// Calls destructor on @a ptr and deallocates memory using the
  72. /// given @a allocator.
  73. /// @note
  74. /// @a allocator must be a reference to an existing allocator.
  75. #define CE_DELETE(allocator, ptr) crown::memory::call_destructor_and_deallocate(allocator, ptr)
  76. } // namespace memory
  77. } // namespace crown