mem_mgr.hpp 4.0 KB

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