aboutsummaryrefslogtreecommitdiffstats
path: root/util/draft/memory.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/draft/memory.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/draft/memory.h')
-rw-r--r--util/draft/memory.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/util/draft/memory.h b/util/draft/memory.h
new file mode 100644
index 0000000000..0a9722bb36
--- /dev/null
+++ b/util/draft/memory.h
@@ -0,0 +1,40 @@
+#pragma once
+
+#include <util/system/defaults.h>
+
+#include <algorithm>
+#include <functional>
+#include <utility>
+
+template <class T>
+inline bool IsZero(const T* begin, const T* end) {
+ return std::find_if(begin, end, [](const T& other) { return other != T(0); }) == end;
+}
+
+template <size_t Size>
+inline bool IsZero(const char* p) {
+ size_t sizeInUI64 = Size / 8;
+ const char* pEndUi64 = p + sizeInUI64 * 8;
+ if (sizeInUI64 && !IsZero<ui64>((const ui64*)p, (const ui64*)pEndUi64))
+ return false;
+ return IsZero(pEndUi64, p + Size);
+}
+
+#define IS_ZERO_INTSZ(INT) \
+ template <> \
+ inline bool IsZero<sizeof(INT)>(const char* p) { \
+ return (*(INT*)p) == INT(0); \
+ }
+
+IS_ZERO_INTSZ(ui8)
+IS_ZERO_INTSZ(ui16)
+IS_ZERO_INTSZ(ui32)
+IS_ZERO_INTSZ(ui64)
+
+#undef IS_ZERO_INTSZ
+
+// If you want to use this to check all fields in a struct make sure it's w/o holes or #pragma pack(1)
+template <class T>
+bool IsZero(const T& t) {
+ return IsZero<sizeof(T)>((const char*)&t);
+}