diff options
author | melkov <melkov@yandex-team.ru> | 2022-02-10 16:48:13 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:13 +0300 |
commit | 438546c8737d5c1fdeb31157dcf999717d930eec (patch) | |
tree | d29d229abd2f9f889b9b7eb148d635059dc26acf /util/memory/segpool_alloc.h | |
parent | 96647fad5355ff5ef45a00a6d85c097028584ab0 (diff) | |
download | ydb-438546c8737d5c1fdeb31157dcf999717d930eec.tar.gz |
Restoring authorship annotation for <melkov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/memory/segpool_alloc.h')
-rw-r--r-- | util/memory/segpool_alloc.h | 120 |
1 files changed, 60 insertions, 60 deletions
diff --git a/util/memory/segpool_alloc.h b/util/memory/segpool_alloc.h index 1a83b7a543..e8d39b64fa 100644 --- a/util/memory/segpool_alloc.h +++ b/util/memory/segpool_alloc.h @@ -1,35 +1,35 @@ #pragma once - + #include "segmented_string_pool.h" - -/** - This is a partially implemented allocator class that uses segmented_pool - as its allocatior base. - In fact, segpool_alloc can only allocate, it can't deallocate. Therefore, - it should be used with add-only containers that are just clear()'ed in the - end of their life cycle. - - External segmented_pool object must be cleared explicitly after all - containers use it in their allocator are cleared. - Single segmented_pool object may be used for several containers. - - Use segpool_alloc_vt as allocator template parameter for container. - Example: + +/** + This is a partially implemented allocator class that uses segmented_pool + as its allocatior base. + In fact, segpool_alloc can only allocate, it can't deallocate. Therefore, + it should be used with add-only containers that are just clear()'ed in the + end of their life cycle. + + External segmented_pool object must be cleared explicitly after all + containers use it in their allocator are cleared. + Single segmented_pool object may be used for several containers. + + Use segpool_alloc_vt as allocator template parameter for container. + Example: using TSomething = THashMap<A, B, hash<A>, TEqualTo<A>, segpool_alloc_vt>; - ... - TSomething something; - something.GetNodeAllocator().pool = &alloc_pool; - - Some containers may provide no way to access their allocator. In that case, - if you want to use them with segpool_alloc_vt, you should first either - hack them, or fix segpool_alloc so that it would create ref-counted - segmented_pool object on it's own - + ... + TSomething something; + something.GetNodeAllocator().pool = &alloc_pool; + + Some containers may provide no way to access their allocator. In that case, + if you want to use them with segpool_alloc_vt, you should first either + hack them, or fix segpool_alloc so that it would create ref-counted + segmented_pool object on it's own + ! Only tested with THashMap -*/ - -template <class _Tp> -struct segpool_alloc { +*/ + +template <class _Tp> +struct segpool_alloc { using pool_type = segmented_pool<char>; pool_type* pool; using pointer = _Tp*; @@ -39,62 +39,62 @@ struct segpool_alloc { using size_type = size_t; using difference_type = ptrdiff_t; using value_type = _Tp; -#ifndef NDEBUG - ui64 pool_count, malloc_count, pool_free_count, malloc_free_count; -#endif +#ifndef NDEBUG + ui64 pool_count, malloc_count, pool_free_count, malloc_free_count; +#endif segpool_alloc() : pool(nullptr) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); - } + } segpool_alloc(pool_type* p) : pool(p) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); } - segpool_alloc(const segpool_alloc& a) + segpool_alloc(const segpool_alloc& a) : pool(a.pool) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); - } - template <class _Tp1> - segpool_alloc(const segpool_alloc<_Tp1>& a) + } + template <class _Tp1> + segpool_alloc(const segpool_alloc<_Tp1>& a) : pool(a.pool) { Y_IF_DEBUG(pool_count = malloc_count = pool_free_count = malloc_free_count = 0); } _Tp* allocate(size_t __n) { - if (!pool) { - _Tp* data = (_Tp*)malloc(__n * sizeof(_Tp)); + if (!pool) { + _Tp* data = (_Tp*)malloc(__n * sizeof(_Tp)); Y_IF_DEBUG(if (data) malloc_count++); - return data; - } + return data; + } _Tp* data = (_Tp*)pool->append(nullptr, __n * sizeof(_Tp)); Y_IF_DEBUG(pool_count++); - return data; - } - void deallocate(pointer __p, size_t /*__n*/) { - if (!pool) { + return data; + } + void deallocate(pointer __p, size_t /*__n*/) { + if (!pool) { Y_IF_DEBUG(malloc_free_count++); - free(__p); + free(__p); } else { Y_IF_DEBUG(pool_free_count++); ; } - } - ~segpool_alloc() { + } + ~segpool_alloc() { //assert(pool_count == pool_free_count && malloc_count == malloc_free_count); <- uncomment when swap() problem is solved //printf("in ~segpool_alloc: size = %u, pool_count = %" PRId64 ", malloc_count = %" PRId64 ", pool_free_count = %" PRId64 ", malloc_free_count = %" PRId64 "\n", - // sizeof(_Tp), pool_count, malloc_count, pool_free_count, malloc_free_count); + // sizeof(_Tp), pool_count, malloc_count, pool_free_count, malloc_free_count); //fflush(stdout); - } + } template <class _Tp1> struct rebind { using other = segpool_alloc<_Tp1>; - }; - size_type max_size() const { - return size_type(-1) / sizeof(_Tp); - } + }; + size_type max_size() const { + return size_type(-1) / sizeof(_Tp); + } void construct(pointer __p, const _Tp& __val) { new (__p) _Tp(__val); } @@ -102,17 +102,17 @@ struct segpool_alloc { (void)__p; /* Make MSVC happy. */ __p->~_Tp(); } -}; +}; -template <class _Tp> +template <class _Tp> inline bool operator==(const segpool_alloc<_Tp>& a1, const segpool_alloc<_Tp>& a2) { return a1.pool == a2.pool; -} - -template <class _Tp> +} + +template <class _Tp> inline bool operator!=(const segpool_alloc<_Tp>& a1, const segpool_alloc<_Tp>& a2) { return a1.pool != a2.pool; -} - +} + // Any type since it is supposed to be rebound anyway. using segpool_alloc_vt = segpool_alloc<int>; |