lzham_mem.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // File: lzham_mem.h
  2. // See Copyright Notice and license at the end of include/lzham.h
  3. #pragma once
  4. namespace lzham
  5. {
  6. void lzham_mem_init();
  7. void* lzham_malloc(size_t size, size_t* pActual_size = NULL);
  8. void* lzham_realloc(void* p, size_t size, size_t* pActual_size = NULL, bool movable = true);
  9. void lzham_free(void* p);
  10. size_t lzham_msize(void* p);
  11. template<typename T>
  12. inline T* lzham_new()
  13. {
  14. T* p = static_cast<T*>(lzham_malloc(sizeof(T)));
  15. if (!p) return NULL;
  16. if (LZHAM_IS_SCALAR_TYPE(T))
  17. return p;
  18. return helpers::construct(p);
  19. }
  20. template<typename T, typename A>
  21. inline T* lzham_new(const A& init0)
  22. {
  23. T* p = static_cast<T*>(lzham_malloc(sizeof(T)));
  24. if (!p) return NULL;
  25. return new (static_cast<void*>(p)) T(init0);
  26. }
  27. template<typename T, typename A, typename B>
  28. inline T* lzham_new(const A& init0, const B& init1)
  29. {
  30. T* p = static_cast<T*>(lzham_malloc(sizeof(T)));
  31. if (!p) return NULL;
  32. return new (static_cast<void*>(p)) T(init0, init1);
  33. }
  34. template<typename T, typename A, typename B, typename C>
  35. inline T* lzham_new(const A& init0, const B& init1, const C& init2)
  36. {
  37. T* p = static_cast<T*>(lzham_malloc(sizeof(T)));
  38. if (!p) return NULL;
  39. return new (static_cast<void*>(p)) T(init0, init1, init2);
  40. }
  41. template<typename T, typename A, typename B, typename C, typename D>
  42. inline T* lzham_new(const A& init0, const B& init1, const C& init2, const D& init3)
  43. {
  44. T* p = static_cast<T*>(lzham_malloc(sizeof(T)));
  45. if (!p) return NULL;
  46. return new (static_cast<void*>(p)) T(init0, init1, init2, init3);
  47. }
  48. template<typename T>
  49. inline T* lzham_new_array(uint32 num)
  50. {
  51. if (!num) num = 1;
  52. uint8* q = static_cast<uint8*>(lzham_malloc(LZHAM_MIN_ALLOC_ALIGNMENT + sizeof(T) * num));
  53. if (!q)
  54. return NULL;
  55. T* p = reinterpret_cast<T*>(q + LZHAM_MIN_ALLOC_ALIGNMENT);
  56. reinterpret_cast<uint32*>(p)[-1] = num;
  57. reinterpret_cast<uint32*>(p)[-2] = ~num;
  58. if (!LZHAM_IS_SCALAR_TYPE(T))
  59. {
  60. helpers::construct_array(p, num);
  61. }
  62. return p;
  63. }
  64. template<typename T>
  65. inline void lzham_delete(T* p)
  66. {
  67. if (p)
  68. {
  69. if (!LZHAM_IS_SCALAR_TYPE(T))
  70. {
  71. helpers::destruct(p);
  72. }
  73. lzham_free(p);
  74. }
  75. }
  76. template<typename T>
  77. inline void lzham_delete_array(T* p)
  78. {
  79. if (p)
  80. {
  81. const uint32 num = reinterpret_cast<uint32*>(p)[-1];
  82. const uint32 num_check = reinterpret_cast<uint32*>(p)[-2];
  83. LZHAM_ASSERT(num && (num == ~num_check));
  84. if (num == ~num_check)
  85. {
  86. if (!LZHAM_IS_SCALAR_TYPE(T))
  87. {
  88. helpers::destruct_array(p, num);
  89. }
  90. lzham_free(reinterpret_cast<uint8*>(p) - LZHAM_MIN_ALLOC_ALIGNMENT);
  91. }
  92. }
  93. }
  94. void lzham_print_mem_stats();
  95. } // namespace lzham