aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/mem_copy.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/generic/mem_copy.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/generic/mem_copy.h')
-rw-r--r--util/generic/mem_copy.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/util/generic/mem_copy.h b/util/generic/mem_copy.h
new file mode 100644
index 0000000000..b68c852953
--- /dev/null
+++ b/util/generic/mem_copy.h
@@ -0,0 +1,55 @@
+#pragma once
+
+#include "typetraits.h"
+
+#include <util/system/types.h>
+
+#include <cstring>
+
+template <class T>
+using TIfPOD = std::enable_if_t<TTypeTraits<T>::IsPod, T*>;
+
+template <class T>
+using TIfNotPOD = std::enable_if_t<!TTypeTraits<T>::IsPod, T*>;
+
+template <class T>
+static inline TIfPOD<T> MemCopy(T* to, const T* from, size_t n) noexcept {
+ if (n) {
+ memcpy(to, from, n * sizeof(T));
+ }
+
+ return to;
+}
+
+template <class T>
+static inline TIfNotPOD<T> MemCopy(T* to, const T* from, size_t n) {
+ for (size_t i = 0; i < n; ++i) {
+ to[i] = from[i];
+ }
+
+ return to;
+}
+
+template <class T>
+static inline TIfPOD<T> MemMove(T* to, const T* from, size_t n) noexcept {
+ if (n) {
+ memmove(to, from, n * sizeof(T));
+ }
+
+ return to;
+}
+
+template <class T>
+static inline TIfNotPOD<T> MemMove(T* to, const T* from, size_t n) {
+ if (to <= from || to >= from + n) {
+ return MemCopy(to, from, n);
+ }
+
+ //copy backwards
+ while (n) {
+ to[n - 1] = from[n - 1];
+ --n;
+ }
+
+ return to;
+}