mem_mgr.hpp 4.1 KB

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