mem_mgr.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * \file mem_mgr.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 6-30-2014
  5. * \brief This file contains the declaration of a simple memory manager which
  6. * uses a pre-allocated buffer of size defined in call to the constructor.
  7. */
  8. // TODO: Implement fast stack allocation.
  9. #include <cstdlib>
  10. #include <stdint.h>
  11. #include <cassert>
  12. #include <vector>
  13. #include <stack>
  14. #include <map>
  15. #include <pvfmm_common.hpp>
  16. #ifndef _PVFMM_MEM_MGR_HPP_
  17. #define _PVFMM_MEM_MGR_HPP_
  18. #ifdef __INTEL_OFFLOAD
  19. #pragma offload_attribute(push,target(mic))
  20. #endif
  21. namespace pvfmm{
  22. namespace mem{
  23. /**
  24. * \brief Identify each type uniquely.
  25. */
  26. template <class T>
  27. class TypeTraits{
  28. public:
  29. static inline uintptr_t ID();
  30. static inline bool IsPOD();
  31. };
  32. /**
  33. * \brief MemoryManager class declaration.
  34. */
  35. class MemoryManager{
  36. public:
  37. static const char init_mem_val=42;
  38. /**
  39. * \brief Header data for each memory block.
  40. */
  41. struct MemHead{
  42. size_t n_indx;
  43. size_t n_elem;
  44. uintptr_t type_id;
  45. uintptr_t type_size;
  46. unsigned char check_sum;
  47. };
  48. /**
  49. * \brief Constructor for MemoryManager.
  50. */
  51. MemoryManager(size_t N);
  52. /**
  53. * \brief Constructor for MemoryManager.
  54. */
  55. ~MemoryManager();
  56. static inline MemHead* GetMemHead(void* p);
  57. inline void* malloc(const size_t& n_elem=1, const size_t& type_size=sizeof(char)) const;
  58. inline void free(void* p) const;
  59. void print() const;
  60. static void test();
  61. private:
  62. // Private constructor
  63. MemoryManager();
  64. // Private copy constructor
  65. MemoryManager(const MemoryManager& m);
  66. // Check all free memory equals init_mem_val
  67. void Check() const;
  68. /**
  69. * \brief Node structure for a doubly linked list, representing free and
  70. * occupied memory blocks. Blocks are split, merged or state is changed
  71. * between free and occupied in O(1) time given the pointer to the MemNode.
  72. */
  73. struct MemNode{
  74. bool free;
  75. size_t size;
  76. char* mem_ptr;
  77. size_t prev, next;
  78. std::multimap<size_t, size_t>::iterator it;
  79. };
  80. /**
  81. * \brief Return index of one of the available MemNodes from node_stack or
  82. * create new MemNode by resizing node_buff.
  83. */
  84. inline size_t new_node() const;
  85. /**
  86. * \brief Add node index for now available MemNode to node_stack.
  87. */
  88. inline void delete_node(size_t indx) const;
  89. char* buff; // pointer to memory buffer.
  90. size_t buff_size; // total buffer size in bytes.
  91. size_t n_dummy_indx; // index of first (dummy) MemNode in link list.
  92. mutable std::vector<MemNode> node_buff; // storage for MemNode objects, this can only grow.
  93. mutable std::stack<size_t> node_stack; // stack of available free MemNodes from node_buff.
  94. mutable std::multimap<size_t, size_t> free_map; // pair (MemNode.size, MemNode_id) for all free MemNodes.
  95. mutable omp_lock_t omp_lock; // openmp lock to prevent concurrent changes.
  96. };
  97. /** A global MemoryManager object. This is the default for aligned_new and
  98. * aligned_free */
  99. extern MemoryManager glbMemMgr;
  100. /**
  101. * \brief Aligned allocation as an alternative to new. Uses placement new to
  102. * construct objects.
  103. */
  104. template <class T>
  105. inline T* aligned_new(size_t n_elem=1, const MemoryManager* mem_mgr=&glbMemMgr);
  106. /**
  107. * \brief Aligned de-allocation as an alternative to delete. Calls the object
  108. * destructors. Not sure which destructor is called for virtual classes, this
  109. * is why we also match the TypeTraits<T>::ID()
  110. */
  111. template <class T>
  112. inline void aligned_delete(T* A, const MemoryManager* mem_mgr=&glbMemMgr);
  113. /**
  114. * \brief Wrapper to memcpy. Also checks if source and destination pointers are
  115. * the same.
  116. */
  117. inline void * memcopy(void * destination, const void * source, size_t num);
  118. }//end namespace
  119. }//end namespace
  120. #include <mem_mgr.txx>
  121. #ifdef __INTEL_OFFLOAD
  122. #pragma offload_attribute(pop)
  123. #endif
  124. #endif //_PVFMM_MEM_MGR_HPP_