mem_mgr.txx 18 KB

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