diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/memory/segmented_string_pool.h | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/memory/segmented_string_pool.h')
-rw-r--r-- | util/memory/segmented_string_pool.h | 82 |
1 files changed, 41 insertions, 41 deletions
diff --git a/util/memory/segmented_string_pool.h b/util/memory/segmented_string_pool.h index 2208ceed6d..a40aa408f5 100644 --- a/util/memory/segmented_string_pool.h +++ b/util/memory/segmented_string_pool.h @@ -1,39 +1,39 @@ #pragma once #include <util/system/align.h> -#include <util/system/yassert.h> -#include <util/system/defaults.h> +#include <util/system/yassert.h> +#include <util/system/defaults.h> #include <util/generic/noncopyable.h> -#include <util/generic/vector.h> +#include <util/generic/vector.h> #include <util/generic/strbuf.h> - + #include <memory> -#include <cstdio> -#include <cstdlib> - +#include <cstdio> +#include <cstdlib> + /* * Non-reallocated storage for the objects of POD type */ template <class T, class Alloc = std::allocator<T>> -class segmented_pool: TNonCopyable { +class segmented_pool: TNonCopyable { protected: - Alloc seg_allocator; + Alloc seg_allocator; struct seg_inf { - T* data; // allocated chunk + T* data; // allocated chunk size_t _size; // size of allocated chunk in sizeof(T)-units size_t freepos; // offset to free chunk's memory in bytes - seg_inf() + seg_inf() : data(nullptr) - , _size(0) - , freepos(0) - { - } + , _size(0) + , freepos(0) + { + } seg_inf(T* d, size_t sz) - : data(d) - , _size(sz) - , freepos(0) - { - } + : data(d) + , _size(sz) + , freepos(0) + { + } }; using seg_container = TVector<seg_inf>; using seg_iterator = typename seg_container::iterator; @@ -53,20 +53,20 @@ protected: if (curseg == segs.end() || curseg->_size < last_free) { segs.push_back(seg_inf(seg_allocator.allocate(last_free), last_free)); if (Y_UNLIKELY(Name)) - printf("Pool \"%s\" was increased by %" PRISZT " bytes to %" PRISZT " Mb.\n", Name, last_free * sizeof(T), capacity() / 0x100000); + printf("Pool \"%s\" was increased by %" PRISZT " bytes to %" PRISZT " Mb.\n", Name, last_free * sizeof(T), capacity() / 0x100000); curseg = segs.end() - 1; } Y_ASSERT(curseg->freepos == 0); Y_ASSERT(curseg->_size >= last_free); } } - + public: explicit segmented_pool(size_t segsz, const char* name = nullptr) - : segment_size(segsz) - , last_free(0) - , last_ins_size(0) - , Name(name) + : segment_size(segsz) + , last_free(0) + , last_ins_size(0) + , Name(name) { curseg = segs.begin(); } @@ -74,23 +74,23 @@ public: clear(); } /* src - array of objects, len - count of elements in array */ - T* append(const T* src, size_t len) { + T* append(const T* src, size_t len) { check_capacity(len); - ui8* rv = (ui8*)curseg->data + curseg->freepos; + ui8* rv = (ui8*)curseg->data + curseg->freepos; last_ins_size = sizeof(T) * len; if (src) memcpy(rv, src, last_ins_size); curseg->freepos += last_ins_size, last_free -= len; return (T*)rv; } - T* append() { + T* append() { T* obj = get_raw(); - new (obj) T(); + new (obj) T(); return obj; } - T* get_raw() { // append(0, 1) + T* get_raw() { // append(0, 1) check_capacity(1); - ui8* rv = (ui8*)curseg->data + curseg->freepos; + ui8* rv = (ui8*)curseg->data + curseg->freepos; last_ins_size = sizeof(T); curseg->freepos += last_ins_size, last_free -= 1; return (T*)rv; @@ -100,7 +100,7 @@ public: } bool contains(const T* ptr) const { for (seg_const_iterator i = segs.begin(), ie = segs.end(); i != ie; ++i) - if ((char*)ptr >= (char*)i->data && (char*)ptr < (char*)i->data + i->freepos) + if ((char*)ptr >= (char*)i->data && (char*)ptr < (char*)i->data + i->freepos) return true; return false; } @@ -146,10 +146,10 @@ public: } }; -class segmented_string_pool: public segmented_pool<char> { +class segmented_string_pool: public segmented_pool<char> { private: using _Base = segmented_pool<char>; - + public: segmented_string_pool() : segmented_string_pool(1024 * 1024) @@ -157,14 +157,14 @@ public: } explicit segmented_string_pool(size_t segsz) - : _Base(segsz) - { - } - char* append(const char* src) { + : _Base(segsz) + { + } + char* append(const char* src) { Y_ASSERT(src); return _Base::append(src, strlen(src) + 1); } - char* append(const char* src, size_t len) { + char* append(const char* src, size_t len) { char* rv = _Base::append(nullptr, len + 1); if (src) memcpy(rv, src, len); @@ -185,7 +185,7 @@ public: }; template <typename T, typename C> -inline T* pool_push(segmented_pool<C>& pool, const T* v) { +inline T* pool_push(segmented_pool<C>& pool, const T* v) { static_assert(sizeof(C) == 1, "only char type supported"); size_t len = SizeOf(v); C* buf = pool.append(nullptr, AlignUp(len)); |