mem_mgr.txx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #include <omp.h>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <cassert>
  5. #include <algorithm>
  6. #include <type_traits>
  7. #include SCTL_INCLUDE(profile.hpp)
  8. namespace SCTL_NAMESPACE {
  9. #ifdef SCTL_MEMDEBUG
  10. template <class ValueType> inline ConstIterator<ValueType>::ConstIterator(const ValueType* base_, difference_type len_, bool dynamic_alloc) {
  11. this->base = (char*)base_;
  12. this->len = len_ * (Long)sizeof(ValueType);
  13. this->offset = 0;
  14. SCTL_ASSERT_MSG((uintptr_t)(this->base + this->offset) % alignof(ValueType) == 0, "invalid alignment during pointer type conversion.");
  15. if (dynamic_alloc) {
  16. MemoryManager::MemHead& mh = *&MemoryManager::GetMemHead((char*)this->base);
  17. MemoryManager::CheckMemHead(mh);
  18. alloc_ctr = mh.alloc_ctr;
  19. mem_head = &mh;
  20. } else
  21. mem_head = nullptr;
  22. }
  23. template <class ValueType> inline void ConstIterator<ValueType>::IteratorAssertChecks(Long j) const {
  24. //const auto& base = this->base;
  25. const auto& offset = this->offset + j * (Long)sizeof(ValueType);
  26. const auto& len = this->len;
  27. const auto& mem_head = this->mem_head;
  28. const auto& alloc_ctr = this->alloc_ctr;
  29. if (*this == NullIterator<ValueType>()) SCTL_WARN("dereferencing a nullptr is undefined.");
  30. SCTL_ASSERT_MSG(offset >= 0 && offset + (Long)sizeof(ValueType) <= len, "access to pointer [B" << (offset < 0 ? "" : "+") << offset << ",B" << (offset + (Long)sizeof(ValueType) < 0 ? "" : "+") << offset + (Long)sizeof(ValueType) << ") is outside of the range [B,B+" << len << ").");
  31. if (mem_head) {
  32. MemoryManager::MemHead& mh = *(MemoryManager::MemHead*)(mem_head);
  33. SCTL_ASSERT_MSG(mh.alloc_ctr == alloc_ctr, "invalid memory address or corrupted memory.");
  34. }
  35. }
  36. template <class ValueType> inline typename ConstIterator<ValueType>::reference ConstIterator<ValueType>::operator*() const {
  37. this->IteratorAssertChecks();
  38. return *(ValueType*)(base + offset);
  39. }
  40. template <class ValueType> inline typename ConstIterator<ValueType>::pointer ConstIterator<ValueType>::operator->() const {
  41. this->IteratorAssertChecks();
  42. return (ValueType*)(base + offset);
  43. }
  44. template <class ValueType> inline typename ConstIterator<ValueType>::reference ConstIterator<ValueType>::operator[](difference_type j) const {
  45. this->IteratorAssertChecks(j);
  46. return *(ValueType*)(base + offset + j * (Long)sizeof(ValueType));
  47. }
  48. template <class ValueType> inline typename Iterator<ValueType>::reference Iterator<ValueType>::operator*() const {
  49. this->IteratorAssertChecks();
  50. return *(ValueType*)(this->base + this->offset);
  51. }
  52. template <class ValueType> inline typename Iterator<ValueType>::value_type* Iterator<ValueType>::operator->() const {
  53. this->IteratorAssertChecks();
  54. return (ValueType*)(this->base + this->offset);
  55. }
  56. template <class ValueType> inline typename Iterator<ValueType>::reference Iterator<ValueType>::operator[](difference_type j) const {
  57. this->IteratorAssertChecks(j);
  58. return *(ValueType*)(this->base + this->offset + j * (Long)sizeof(ValueType));
  59. }
  60. #endif
  61. inline MemoryManager::MemoryManager(Long N) {
  62. buff_size = N;
  63. { // Allocate buff
  64. SCTL_ASSERT(SCTL_MEM_ALIGN <= 0x8000);
  65. Long alignment = SCTL_MEM_ALIGN - 1;
  66. char* base_ptr = (char*)::malloc(N + 2 + alignment);
  67. SCTL_ASSERT_MSG(base_ptr, "memory allocation failed.");
  68. buff = (char*)((uintptr_t)(base_ptr + 2 + alignment) & ~(uintptr_t)alignment);
  69. ((uint16_t*)buff)[-1] = (uint16_t)(buff - base_ptr);
  70. }
  71. { // Initialize to init_mem_val
  72. #ifdef SCTL_MEMDEBUG
  73. #pragma omp parallel for
  74. for (Long i = 0; i < buff_size; i++) {
  75. buff[i] = init_mem_val;
  76. }
  77. #endif
  78. }
  79. n_dummy_indx = new_node();
  80. Long n_indx = new_node();
  81. MemNode& n_dummy = node_buff[n_dummy_indx - 1];
  82. MemNode& n = node_buff[n_indx - 1];
  83. n_dummy.size = 0;
  84. n_dummy.free = false;
  85. n_dummy.prev = 0;
  86. n_dummy.next = n_indx;
  87. n_dummy.mem_ptr = &buff[0];
  88. SCTL_ASSERT(n_indx);
  89. n.size = N;
  90. n.free = true;
  91. n.prev = n_dummy_indx;
  92. n.next = 0;
  93. n.mem_ptr = &buff[0];
  94. n.it = free_map.insert(std::make_pair(N, n_indx));
  95. //omp_init_lock(&omp_lock);
  96. }
  97. inline MemoryManager::~MemoryManager() {
  98. Check();
  99. MemNode* n_dummy = &node_buff[n_dummy_indx - 1];
  100. MemNode* n = &node_buff[n_dummy->next - 1];
  101. if (!n->free || n->size != buff_size || node_stack.size() != node_buff.size() - 2 || !system_malloc.empty()) {
  102. SCTL_WARN("memory leak detected.");
  103. }
  104. //omp_destroy_lock(&omp_lock);
  105. { // free buff
  106. SCTL_ASSERT(buff);
  107. ::free(buff - ((uint16_t*)buff)[-1]);
  108. buff = nullptr;
  109. }
  110. }
  111. inline MemoryManager::MemHead& MemoryManager::GetMemHead(char* I) {
  112. SCTL_ASSERT_MSG(I != nullptr, "nullptr exception.");
  113. static uintptr_t alignment = SCTL_MEM_ALIGN - 1;
  114. static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
  115. return *(MemHead*)(((char*)I) - header_size);
  116. }
  117. inline void MemoryManager::CheckMemHead(const MemHead& mem_head) { // Verify header check_sum
  118. #ifdef SCTL_MEMDEBUG
  119. Long check_sum = 0;
  120. const unsigned char* base_ = (const unsigned char*)&mem_head;
  121. for (Integer i = 0; i < (Integer)sizeof(MemHead); i++) {
  122. check_sum += base_[i];
  123. }
  124. check_sum -= mem_head.check_sum;
  125. check_sum = check_sum & ((1UL << (8 * sizeof(mem_head.check_sum))) - 1);
  126. SCTL_ASSERT_MSG(check_sum == mem_head.check_sum, "invalid memory address or corrupted memory.");
  127. #endif
  128. }
  129. inline Iterator<char> MemoryManager::malloc(const Long n_elem, const Long type_size, const MemHead::TypeID type_id) const {
  130. if (!n_elem) return NullIterator<char>();
  131. static uintptr_t alignment = SCTL_MEM_ALIGN - 1;
  132. static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
  133. Long size = n_elem * type_size + header_size;
  134. size = (uintptr_t)(size + alignment) & ~(uintptr_t)alignment;
  135. char* base = nullptr;
  136. static Long alloc_ctr = 0;
  137. Long head_alloc_ctr, n_indx;
  138. #pragma omp critical(SCTL_MEM_MGR_CRIT)
  139. {
  140. //mutex_lock.lock();
  141. //omp_set_lock(&omp_lock);
  142. alloc_ctr++;
  143. head_alloc_ctr = alloc_ctr;
  144. std::multimap<Long, Long>::iterator it = free_map.lower_bound(size);
  145. n_indx = (it != free_map.end() ? it->second : 0);
  146. if (n_indx) { // Allocate from buff
  147. Long n_free_indx = (it->first > size ? new_node() : 0);
  148. MemNode& n = node_buff[n_indx - 1];
  149. assert(n.size == it->first);
  150. assert(n.it == it);
  151. assert(n.free);
  152. if (n_free_indx) { // Create a node for the remaining free part.
  153. MemNode& n_free = node_buff[n_free_indx - 1];
  154. n_free = n;
  155. n_free.size -= size;
  156. n_free.mem_ptr = (char*)n_free.mem_ptr + size;
  157. { // Insert n_free to the link list
  158. n_free.prev = n_indx;
  159. if (n_free.next) {
  160. Long n_next_indx = n_free.next;
  161. MemNode& n_next = node_buff[n_next_indx - 1];
  162. n_next.prev = n_free_indx;
  163. }
  164. n.next = n_free_indx;
  165. }
  166. assert(n_free.free); // Insert n_free to free map
  167. n_free.it = free_map.insert(std::make_pair(n_free.size, n_free_indx));
  168. n.size = size; // Update n
  169. }
  170. n.free = false;
  171. free_map.erase(it);
  172. base = n.mem_ptr;
  173. }
  174. //omp_unset_lock(&omp_lock);
  175. //mutex_lock.unlock();
  176. }
  177. if (!base) { // Use system malloc
  178. char* p = (char*)::malloc(size + 2 + alignment + end_padding);
  179. SCTL_ASSERT_MSG(p, "memory allocation failed.");
  180. #ifdef SCTL_MEMDEBUG
  181. #pragma omp critical(SCTL_MEM_MGR_CRIT)
  182. { // system_malloc.insert(p)
  183. //mutex_lock.lock();
  184. //omp_set_lock(&omp_lock);
  185. system_malloc.insert(p);
  186. //omp_unset_lock(&omp_lock);
  187. //mutex_lock.unlock();
  188. }
  189. { // set p[*] to init_mem_val
  190. #pragma omp parallel for
  191. for (Long i = 0; i < (Long)(size + 2 + alignment + end_padding); i++) p[i] = init_mem_val;
  192. }
  193. #endif
  194. { // base <-- align(p)
  195. base = (char*)((uintptr_t)(p + 2 + alignment) & ~(uintptr_t)alignment);
  196. ((uint16_t*)base)[-1] = (uint16_t)(base - p);
  197. }
  198. }
  199. { // Check out-of-bounds write
  200. #ifdef SCTL_MEMDEBUG
  201. if (n_indx) {
  202. #pragma omp parallel for
  203. for (Long i = 0; i < size; i++) SCTL_ASSERT_MSG(base[i] == init_mem_val, "memory corruption detected.");
  204. }
  205. #endif
  206. }
  207. MemHead& mem_head = *(MemHead*)base;
  208. { // Set mem_head
  209. #ifdef SCTL_MEMDEBUG
  210. for (Integer i = 0; i < (Integer)sizeof(MemHead); i++) base[i] = init_mem_val;
  211. #endif
  212. mem_head.n_indx = n_indx;
  213. mem_head.n_elem = n_elem;
  214. mem_head.type_size = type_size;
  215. mem_head.alloc_ctr = head_alloc_ctr;
  216. mem_head.type_id = type_id;
  217. }
  218. { // Set header check_sum
  219. #ifdef SCTL_MEMDEBUG
  220. Long check_sum = 0;
  221. unsigned char* base_ = (unsigned char*)base;
  222. mem_head.check_sum = 0;
  223. for (Integer i = 0; i < (Integer)sizeof(MemHead); i++) check_sum += base_[i];
  224. check_sum = check_sum & ((1UL << (8 * sizeof(mem_head.check_sum))) - 1);
  225. mem_head.check_sum = check_sum;
  226. #endif
  227. }
  228. Profile::Add_MEM(n_elem * type_size);
  229. #ifdef SCTL_MEMDEBUG
  230. return Iterator<char>(base + header_size, n_elem * type_size, true);
  231. #else
  232. return base + header_size;
  233. #endif
  234. }
  235. inline void MemoryManager::free(Iterator<char> p) const {
  236. if (p == NullIterator<char>()) return;
  237. static uintptr_t alignment = SCTL_MEM_ALIGN - 1;
  238. static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
  239. SCTL_UNUSED(header_size);
  240. MemHead& mem_head = GetMemHead(&p[0]);
  241. Long n_indx = mem_head.n_indx;
  242. Long n_elem = mem_head.n_elem;
  243. Long type_size = mem_head.type_size;
  244. char* base = (char*)&mem_head;
  245. { // Verify header check_sum; set array to init_mem_val
  246. #ifdef SCTL_MEMDEBUG
  247. CheckMemHead(mem_head);
  248. Long size = mem_head.n_elem * mem_head.type_size;
  249. #pragma omp parallel for
  250. for (Long i = 0; i < size; i++) p[i] = init_mem_val;
  251. for (Integer i = 0; i < (Integer)sizeof(MemHead); i++) base[i] = init_mem_val;
  252. #endif
  253. }
  254. if (n_indx == 0) { // Use system free
  255. assert(base < &buff[0] || base >= &buff[buff_size]);
  256. char* p_;
  257. { // p_ <-- unalign(base)
  258. p_ = (char*)((uintptr_t)base - ((uint16_t*)base)[-1]);
  259. }
  260. #ifdef SCTL_MEMDEBUG
  261. { // Check out-of-bounds write
  262. base[-1] = init_mem_val;
  263. base[-2] = init_mem_val;
  264. Long size = n_elem * type_size + header_size;
  265. size = (uintptr_t)(size + alignment) & ~(uintptr_t)alignment;
  266. #pragma omp parallel for
  267. for (Long i = 0; i < (Long)(size + 2 + alignment + end_padding); i++) {
  268. SCTL_ASSERT_MSG(p_[i] == init_mem_val, "memory corruption detected.");
  269. }
  270. }
  271. #pragma omp critical(SCTL_MEM_MGR_CRIT)
  272. if (buff != nullptr) { // system_malloc.erase(p_)
  273. //mutex_lock.lock();
  274. //omp_set_lock(&omp_lock);
  275. SCTL_ASSERT_MSG(system_malloc.erase(p_) == 1, "double free or corruption.");
  276. //omp_unset_lock(&omp_lock);
  277. //mutex_lock.unlock();
  278. }
  279. #endif
  280. ::free(p_);
  281. } else {
  282. #ifdef SCTL_MEMDEBUG
  283. { // Check out-of-bounds write
  284. MemNode& n = node_buff[n_indx - 1];
  285. char* base = n.mem_ptr;
  286. #pragma omp parallel for
  287. for (Long i = 0; i < n.size; i++) {
  288. SCTL_ASSERT_MSG(base[i] == init_mem_val, "memory corruption detected.");
  289. }
  290. }
  291. #endif
  292. assert(n_indx <= (Long)node_buff.size());
  293. #pragma omp critical(SCTL_MEM_MGR_CRIT)
  294. {
  295. //mutex_lock.lock();
  296. //omp_set_lock(&omp_lock);
  297. MemNode& n = node_buff[n_indx - 1];
  298. assert(!n.free && n.size > 0 && n.mem_ptr == base);
  299. if (n.prev != 0 && node_buff[n.prev - 1].free) {
  300. Long n_prev_indx = n.prev;
  301. MemNode& n_prev = node_buff[n_prev_indx - 1];
  302. n.size += n_prev.size;
  303. n.mem_ptr = n_prev.mem_ptr;
  304. n.prev = n_prev.prev;
  305. free_map.erase(n_prev.it);
  306. delete_node(n_prev_indx);
  307. if (n.prev) {
  308. node_buff[n.prev - 1].next = n_indx;
  309. }
  310. }
  311. if (n.next != 0 && node_buff[n.next - 1].free) {
  312. Long n_next_indx = n.next;
  313. MemNode& n_next = node_buff[n_next_indx - 1];
  314. n.size += n_next.size;
  315. n.next = n_next.next;
  316. free_map.erase(n_next.it);
  317. delete_node(n_next_indx);
  318. if (n.next) {
  319. node_buff[n.next - 1].prev = n_indx;
  320. }
  321. }
  322. n.free = true; // Insert n to free_map
  323. n.it = free_map.insert(std::make_pair(n.size, n_indx));
  324. //omp_unset_lock(&omp_lock);
  325. //mutex_lock.unlock();
  326. }
  327. }
  328. Profile::Add_MEM(-n_elem * type_size);
  329. }
  330. inline void MemoryManager::print() const {
  331. if (!buff_size) return;
  332. #pragma omp critical(SCTL_MEM_MGR_CRIT)
  333. {
  334. //mutex_lock.lock();
  335. //omp_set_lock(&omp_lock);
  336. Long size = 0;
  337. Long largest_size = 0;
  338. MemNode* n = &node_buff[n_dummy_indx - 1];
  339. std::cout << "\n|";
  340. while (n->next) {
  341. n = &node_buff[n->next - 1];
  342. if (n->free) {
  343. std::cout << ' ';
  344. largest_size = std::max(largest_size, n->size);
  345. } else {
  346. std::cout << '#';
  347. size += n->size;
  348. }
  349. }
  350. std::cout << "| allocated=" << round(size * 1000.0 / buff_size) / 10 << "%";
  351. std::cout << " largest_free=" << round(largest_size * 1000.0 / buff_size) / 10 << "%\n";
  352. //omp_unset_lock(&omp_lock);
  353. //mutex_lock.unlock();
  354. }
  355. }
  356. inline void MemoryManager::test() {
  357. Long M = 2000000000;
  358. { // With memory manager
  359. Long N = (Long)(M * sizeof(double) * 1.1);
  360. double tt;
  361. Iterator<double> tmp;
  362. std::cout << "With memory manager: ";
  363. MemoryManager memgr(N);
  364. for (Integer j = 0; j < 3; j++) {
  365. tmp = (Iterator<double>)memgr.malloc(M * sizeof(double));
  366. SCTL_ASSERT(tmp != NullIterator<double>());
  367. tt = omp_get_wtime();
  368. #pragma omp parallel for
  369. for (Long i = 0; i < M; i += 64) tmp[i] = (double)i;
  370. tt = omp_get_wtime() - tt;
  371. std::cout << tt << ' ';
  372. memgr.free((Iterator<char>)tmp);
  373. }
  374. std::cout << '\n';
  375. }
  376. { // Without memory manager
  377. double tt;
  378. double* tmp;
  379. std::cout << "Without memory manager: ";
  380. for (Integer j = 0; j < 3; j++) {
  381. tmp = (double*)::malloc(M * sizeof(double));
  382. SCTL_ASSERT(tmp != nullptr);
  383. tt = omp_get_wtime();
  384. #pragma omp parallel for
  385. for (Long i = 0; i < M; i += 64) tmp[i] = (double)i;
  386. tt = omp_get_wtime() - tt;
  387. std::cout << tt << ' ';
  388. ::free(tmp);
  389. }
  390. std::cout << '\n';
  391. }
  392. }
  393. inline void MemoryManager::Check() const {
  394. #ifdef SCTL_MEMDEBUG
  395. // print();
  396. #pragma omp critical(SCTL_MEM_MGR_CRIT)
  397. {
  398. //mutex_lock.lock();
  399. //omp_set_lock(&omp_lock);
  400. MemNode* curr_node = &node_buff[n_dummy_indx - 1];
  401. while (curr_node->next) {
  402. curr_node = &node_buff[curr_node->next - 1];
  403. if (curr_node->free) {
  404. char* base = curr_node->mem_ptr;
  405. #pragma omp parallel for
  406. for (Long i = 0; i < curr_node->size; i++) {
  407. SCTL_ASSERT_MSG(base[i] == init_mem_val, "memory corruption detected.");
  408. }
  409. }
  410. }
  411. //omp_unset_lock(&omp_lock);
  412. //mutex_lock.unlock();
  413. }
  414. #endif
  415. }
  416. inline Long MemoryManager::new_node() const {
  417. if (node_stack.empty()) {
  418. node_buff.resize(node_buff.size() + 1);
  419. node_stack.push(node_buff.size());
  420. }
  421. Long indx = node_stack.top();
  422. node_stack.pop();
  423. assert(indx);
  424. return indx;
  425. }
  426. inline void MemoryManager::delete_node(Long indx) const {
  427. assert(indx);
  428. assert(indx <= (Long)node_buff.size());
  429. MemNode& n = node_buff[indx - 1];
  430. n.free = false;
  431. n.size = 0;
  432. n.prev = 0;
  433. n.next = 0;
  434. n.mem_ptr = nullptr;
  435. node_stack.push(indx);
  436. }
  437. template <class ValueType> inline Iterator<ValueType> aligned_new(Long n_elem, const MemoryManager* mem_mgr) {
  438. if (!n_elem) return NullIterator<ValueType>();
  439. static MemoryManager def_mem_mgr(0);
  440. if (!mem_mgr) mem_mgr = &def_mem_mgr;
  441. Iterator<ValueType> A = (Iterator<ValueType>)mem_mgr->malloc(n_elem, sizeof(ValueType), typeid(ValueType).hash_code());
  442. SCTL_ASSERT_MSG(A != NullIterator<ValueType>(), "memory allocation failed.");
  443. if (!std::is_trivial<ValueType>::value) { // Call constructors
  444. // printf("%s\n", __PRETTY_FUNCTION__);
  445. #pragma omp parallel for schedule(static)
  446. for (Long i = 0; i < n_elem; i++) {
  447. ValueType* Ai = new (&A[i]) ValueType();
  448. assert(Ai == (&A[i]));
  449. SCTL_UNUSED(Ai);
  450. }
  451. } else {
  452. #ifdef SCTL_MEMDEBUG
  453. static Long random_init_val = 1;
  454. Iterator<char> A_ = (Iterator<char>)A;
  455. #pragma omp parallel for schedule(static)
  456. for (Long i = 0; i < n_elem * (Long)sizeof(ValueType); i++) {
  457. A_[i] = random_init_val + i;
  458. }
  459. random_init_val += n_elem * sizeof(ValueType);
  460. #endif
  461. }
  462. return A;
  463. }
  464. template <class ValueType> inline void aligned_delete(Iterator<ValueType> A, const MemoryManager* mem_mgr) {
  465. if (A == NullIterator<ValueType>()) return;
  466. if (!std::is_trivial<ValueType>::value) { // Call destructors
  467. // printf("%s\n", __PRETTY_FUNCTION__);
  468. MemoryManager::MemHead& mem_head = MemoryManager::GetMemHead((char*)&A[0]);
  469. #ifdef SCTL_MEMDEBUG
  470. MemoryManager::CheckMemHead(mem_head);
  471. SCTL_ASSERT_MSG(mem_head.type_id==typeid(ValueType).hash_code(), "pointer to aligned_delete has different type than what was used in aligned_new.");
  472. #endif
  473. Long n_elem = mem_head.n_elem;
  474. for (Long i = 0; i < n_elem; i++) {
  475. A[i].~ValueType();
  476. }
  477. } else {
  478. #ifdef SCTL_MEMDEBUG
  479. MemoryManager::MemHead& mem_head = MemoryManager::GetMemHead((char*)&A[0]);
  480. MemoryManager::CheckMemHead(mem_head);
  481. SCTL_ASSERT_MSG(mem_head.type_id==typeid(ValueType).hash_code(), "pointer to aligned_delete has different type than what was used in aligned_new.");
  482. Long size = mem_head.n_elem * mem_head.type_size;
  483. Iterator<char> A_ = (Iterator<char>)A;
  484. #pragma omp parallel for
  485. for (Long i = 0; i < size; i++) {
  486. A_[i] = 0;
  487. }
  488. #endif
  489. }
  490. static MemoryManager def_mem_mgr(0);
  491. if (!mem_mgr) mem_mgr = &def_mem_mgr;
  492. mem_mgr->free((Iterator<char>)A);
  493. }
  494. template <class ValueType> inline Iterator<ValueType> memcopy(Iterator<ValueType> destination, ConstIterator<ValueType> source, Long num) {
  495. if (destination != source && num) {
  496. #ifdef SCTL_MEMDEBUG
  497. SCTL_UNUSED(destination[num - 1]);
  498. SCTL_UNUSED(source[num - 1] );
  499. #endif
  500. if (std::is_trivially_copyable<ValueType>::value) {
  501. memcpy((void*)&destination[0], (const void*)&source[0], num * sizeof(ValueType));
  502. } else {
  503. for (Long i = 0; i < num; i++) destination[i] = source[i];
  504. }
  505. }
  506. return destination;
  507. }
  508. template <class ValueType> inline Iterator<ValueType> memset(Iterator<ValueType> ptr, int value, Long num) {
  509. if (num) {
  510. #ifdef SCTL_MEMDEBUG
  511. SCTL_UNUSED(ptr[0] );
  512. SCTL_UNUSED(ptr[num - 1]);
  513. #endif
  514. SCTL_ASSERT(std::is_trivially_copyable<ValueType>::value);
  515. ::memset((void*)&ptr[0], value, num * sizeof(ValueType));
  516. }
  517. return ptr;
  518. }
  519. } // end namespace