-
Notifications
You must be signed in to change notification settings - Fork 375
Boundary tests and custom allocator #1145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
libraries/multi-index-lru/include/userver/multi-index-lru/impl/mempool.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <cstddef> | ||
| #include <cassert> | ||
| #include <iostream> | ||
|
|
||
| USERVER_NAMESPACE_BEGIN | ||
|
|
||
| namespace multi_index_lru { | ||
|
|
||
| static std::size_t align_up(std::size_t size) noexcept { | ||
| constexpr std::size_t alignment = alignof(std::max_align_t); | ||
| return (size + alignment - 1) & ~(alignment - 1); | ||
| } | ||
|
|
||
| class FixedPool { | ||
| public: | ||
| explicit FixedPool(std::size_t capacity, std::size_t elem_size) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explicit лишний |
||
| : capacity_(capacity) | ||
| , element_size_(align_up(elem_size)) | ||
| , storage_(nullptr) | ||
| , free_head_(nullptr) { | ||
|
|
||
| if (capacity_ == 0) { | ||
| return; | ||
| } | ||
|
|
||
| std::size_t total_size = capacity_ * element_size_; | ||
|
|
||
| storage_ = static_cast<char*>(::operator new(total_size)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нельзя ли в списках инициализации это сделать? и в умный пойнтер обернуть? |
||
|
|
||
| char* current = storage_; | ||
| free_head_ = nullptr; | ||
|
|
||
| for (std::size_t i = 0; i < capacity_; ++i) { | ||
|
|
||
| void** next_ptr = reinterpret_cast<void**>(current); | ||
| *next_ptr = free_head_; | ||
| free_head_ = current; | ||
|
|
||
| current += element_size_; | ||
| } | ||
| } | ||
|
|
||
| ~FixedPool() { | ||
| ::operator delete(storage_); | ||
| } | ||
|
|
||
| FixedPool(const FixedPool&) = delete; | ||
| FixedPool& operator=(const FixedPool&) = delete; | ||
|
|
||
| void* allocate() { | ||
| if (!free_head_) { | ||
| std::cerr << "ERROR: FixedPool out of memory! capacity=" << capacity_ << std::endl; | ||
| throw std::bad_alloc(); | ||
| } | ||
|
|
||
| void* block = free_head_; | ||
|
|
||
| void** next_ptr = static_cast<void**>(block); | ||
| free_head_ = *next_ptr; | ||
|
|
||
| return block; | ||
| } | ||
|
|
||
| void deallocate(void* ptr) noexcept { | ||
| if (!ptr) { | ||
| return; | ||
| } | ||
|
|
||
| char* char_ptr = static_cast<char*>(ptr); | ||
| char* storage_end = storage_ + capacity_ * element_size_; | ||
|
|
||
| if (char_ptr < storage_ || char_ptr >= storage_end) { | ||
| std::cerr << "ERROR: FixedPool deallocating pointer not from pool!" << std::endl; | ||
| std::cerr << " ptr: " << ptr << " is outside [" | ||
| << static_cast<void*>(storage_) << ", " | ||
| << static_cast<void*>(storage_end) << ")" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| void** next_ptr = static_cast<void**>(ptr); | ||
| *next_ptr = free_head_; | ||
| free_head_ = ptr; | ||
| } | ||
|
|
||
| std::size_t capacity() const noexcept { return capacity_; } | ||
| std::size_t element_size() const noexcept { return element_size_; } | ||
|
|
||
| private: | ||
| std::size_t capacity_; | ||
| std::size_t element_size_; | ||
| char* storage_; | ||
| void* free_head_; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class FixedPoolAllocator { | ||
| public: | ||
| using value_type = T; | ||
| using size_type = std::size_t; | ||
| using difference_type = std::ptrdiff_t; | ||
| using propagate_on_container_move_assignment = std::true_type; | ||
| using is_always_equal = std::false_type; | ||
|
|
||
| FixedPoolAllocator() noexcept | ||
| : pool_(nullptr) {} | ||
|
|
||
| explicit FixedPoolAllocator(std::size_t capacity) noexcept | ||
| : FixedPoolAllocator(capacity, sizeof(T)) {} | ||
|
|
||
| FixedPoolAllocator(std::size_t capacity, std::size_t element_size) noexcept | ||
| : pool_(std::make_shared<FixedPool>(capacity, element_size)) {} | ||
|
|
||
| FixedPoolAllocator(const FixedPoolAllocator& other) = delete; | ||
|
|
||
| FixedPoolAllocator(FixedPoolAllocator&& other) = delete; | ||
|
|
||
| template <typename U> | ||
| FixedPoolAllocator(const FixedPoolAllocator<U>& other) noexcept { | ||
| pool_ = std::make_shared<FixedPool>( | ||
| other.pool_->capacity(), | ||
| sizeof(T) | ||
| ); | ||
| } | ||
|
|
||
| ~FixedPoolAllocator() {} | ||
|
|
||
| T* allocate(size_type n) { | ||
| if (n != 1) { | ||
| return static_cast<T*>(::operator new(n * sizeof(T))); | ||
| } | ||
|
|
||
| if (!pool_) { | ||
| std::cerr << " ERROR: pool_ is null!" << std::endl; | ||
| throw std::bad_alloc(); | ||
| } | ||
|
|
||
| void* ptr = pool_->allocate(); | ||
|
|
||
| T* result = static_cast<T*>(ptr); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| void deallocate(T* ptr, size_type n) noexcept { | ||
|
|
||
| if (n != 1) { | ||
| ::operator delete(ptr); | ||
| return; | ||
| } | ||
|
|
||
| if (pool_) { | ||
| pool_->deallocate(ptr); | ||
| } | ||
| } | ||
|
|
||
| template <typename U> struct rebind { using other = FixedPoolAllocator<U>; }; | ||
|
|
||
| template <typename U> | ||
| bool operator==(const FixedPoolAllocator<U>& other) const noexcept { | ||
| bool eq = (pool_ == other.pool_); | ||
| return eq; | ||
| } | ||
|
|
||
| template <typename U> | ||
| bool operator!=(const FixedPoolAllocator<U>& other) const noexcept { | ||
| return !(*this == other); | ||
| } | ||
|
|
||
| std::size_t get_element_size() const noexcept { | ||
| return pool_ ? pool_->element_size() : 0; | ||
| } | ||
|
|
||
| private: | ||
| template <typename U> friend class FixedPoolAllocator; | ||
|
|
||
| std::shared_ptr<FixedPool> pool_; | ||
| }; | ||
|
|
||
| } // namespace multi_index_lru | ||
|
|
||
| USERVER_NAMESPACE_END | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тоже люблю статические функции, но лучше положить в анонимный неймспейс