mem_mgr.txx 18 KB

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