diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/streams/zc_memory_input | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/streams/zc_memory_input')
-rw-r--r-- | library/cpp/streams/zc_memory_input/ya.make | 12 | ||||
-rw-r--r-- | library/cpp/streams/zc_memory_input/zc_memory_input.cpp | 1 | ||||
-rw-r--r-- | library/cpp/streams/zc_memory_input/zc_memory_input.h | 48 |
3 files changed, 61 insertions, 0 deletions
diff --git a/library/cpp/streams/zc_memory_input/ya.make b/library/cpp/streams/zc_memory_input/ya.make new file mode 100644 index 0000000000..bc94d6f1ed --- /dev/null +++ b/library/cpp/streams/zc_memory_input/ya.make @@ -0,0 +1,12 @@ +LIBRARY() + +OWNER( + pg + g:util +) + +SRCS( + zc_memory_input.cpp +) + +END() diff --git a/library/cpp/streams/zc_memory_input/zc_memory_input.cpp b/library/cpp/streams/zc_memory_input/zc_memory_input.cpp new file mode 100644 index 0000000000..682099a239 --- /dev/null +++ b/library/cpp/streams/zc_memory_input/zc_memory_input.cpp @@ -0,0 +1 @@ +#include "zc_memory_input.h" diff --git a/library/cpp/streams/zc_memory_input/zc_memory_input.h b/library/cpp/streams/zc_memory_input/zc_memory_input.h new file mode 100644 index 0000000000..c939d8e426 --- /dev/null +++ b/library/cpp/streams/zc_memory_input/zc_memory_input.h @@ -0,0 +1,48 @@ +#pragma once + +#include <util/stream/mem.h> +#include <util/system/defaults.h> +#include <util/generic/yexception.h> + +/// Zero-copy memory input with fixed read +class TZCMemoryInput: public TMemoryInput { +public: + TZCMemoryInput() { + } + + TZCMemoryInput(const char* dataPtr, size_t size) + : TMemoryInput(dataPtr, size) + { + } + + TZCMemoryInput(TMemoryInput& rhs) + : TMemoryInput(rhs.Buf(), rhs.Avail()) + { + } + + /// if there's 'size' data read it, otherwise just return false + Y_FORCE_INLINE bool ReadFixed(const char*& buf, size_t size) { + if (Avail() >= size) { + buf = Buf(); + Reset(Buf() + size, Avail() - size); + return true; + } + return false; + } + + template <class T> + Y_FORCE_INLINE T LoadPOD() { + const char* buf = nullptr; + if (!ReadFixed(buf, sizeof(T))) + ythrow yexception() << "TZCMemoryInput::LoadPOD failed: not enough data (" + << Avail() << " of " << sizeof(T) << " bytes)"; + T res; + memcpy(&res, buf, sizeof(T)); + return res; + } + + template <class T> + Y_FORCE_INLINE void ReadPOD(T& x) { + x = LoadPOD<T>(); + } +}; |