mem_mgr.txx 18 KB

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