-
Notifications
You must be signed in to change notification settings - Fork 391
Cascaded allocator #2113
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
base: master
Are you sure you want to change the base?
Cascaded allocator #2113
Changes from 36 commits
bd320b1
37dc8f4
c61b9be
6166f88
32d3d1f
e2a5511
6835890
150eb7b
089af45
ac87bd4
b71f82d
34abb6e
cf39680
78e7165
e5e55bc
00016bf
912d9db
833bc87
40675b0
148734d
6e9a17c
d065069
eba85a4
1e1862d
0f26ecb
4a7fcd0
cf686b3
4c27ab0
8210ac0
e8d9b51
63e543f
d4e7d14
874d9cb
92066a1
4a9c114
8450156
0c8067c
cc4053c
ac5f611
fec93be
42b409e
a837b5d
c9ead44
021ef62
92c3499
b84e0c4
58da8c3
818c631
76ceba9
c19e903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| #include "ace/ACE.h" | ||
| #include "ace/OS_NS_string.h" | ||
| #include <cstring> | ||
| #include <memory> | ||
|
|
||
| ACE_BEGIN_VERSIONED_NAMESPACE_DECL | ||
|
|
||
|
|
@@ -177,6 +178,227 @@ ACE_Dynamic_Cached_Allocator<ACE_LOCK>::free (void * ptr) | |
| this->free_list_.add ((ACE_Cached_Mem_Pool_Node<char> *) ptr); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::ACE_Cascaded_Dynamic_Cached_Allocator | ||
| (size_t initial_n_chunks, size_t chunk_size) | ||
| : initial_n_chunks_ (initial_n_chunks), | ||
| chunk_size_ (chunk_size), | ||
| chunk_sum_ (0) | ||
| { | ||
| ACE_ASSERT (this->chunk_size_ > 0); | ||
|
|
||
| comb_alloc_ptr tmp; | ||
| // If ACE_NEW fails, the hierarchy_ will be reconstructed when malloc API is called. | ||
| ACE_NEW (tmp, comb_alloc_type(this->initial_n_chunks_, this->chunk_size_)); | ||
smithAchang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Consider the exception of vector push_back call | ||
| std::unique_ptr<comb_alloc_type> smart_ptr(tmp); | ||
| // Has strong exception safety guarantee for call of push_back. | ||
| this->hierarchy_.push_back (smart_ptr.get()); | ||
| if (0 == this->hierarchy_.size()) | ||
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Increase the chunk sum if all points having potential risk of exception is passed. | ||
| this->chunk_sum_ += smart_ptr->pool_depth(); | ||
|
|
||
| smart_ptr.release(); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::~ACE_Cascaded_Dynamic_Cached_Allocator () | ||
| { | ||
| for (size_t h = 0; h < this->hierarchy_.size(); h++) | ||
| { | ||
| delete this->hierarchy_[h]; | ||
| } | ||
|
|
||
| this->hierarchy_.clear(); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> void * | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::malloc (size_t nbytes) | ||
| { | ||
| // Check if size requested fits within pre-determined size. | ||
| if (nbytes > this->chunk_size_) | ||
| return nullptr; | ||
|
|
||
| ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); | ||
|
|
||
| void * ptr = nullptr; | ||
|
|
||
| for (size_t h = 0; h < this->hierarchy_.size(); h++) | ||
| { | ||
| ptr = this->hierarchy_[h]->malloc(nbytes); | ||
| if(ptr != nullptr) | ||
| break; | ||
| } | ||
|
|
||
| if(ptr == nullptr) | ||
| { | ||
| comb_alloc_ptr tmp; | ||
| ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * (1 << this->hierarchy_.size()), | ||
| this->chunk_size_), | ||
| nullptr); | ||
|
|
||
| // Consider the exception of vector push_back call | ||
| std::unique_ptr<comb_alloc_type> smart_ptr(tmp); | ||
| const auto old_size = this->hierarchy_.size(); | ||
| // Has strong exception safety guarantee for call of push_back. | ||
| this->hierarchy_.push_back(smart_ptr.get()); | ||
| if (old_size == this->hierarchy_.size()) | ||
| { | ||
| return nullptr; | ||
| } | ||
|
|
||
| // Increase the chunk sum if all points having potential risk of exception is passed. | ||
| this->chunk_sum_ += smart_ptr->pool_depth(); | ||
| ptr = smart_ptr->malloc(nbytes); | ||
|
|
||
| smart_ptr.release(); | ||
| } | ||
|
|
||
| return ptr; | ||
| } | ||
|
|
||
| template <class ACE_LOCK> void * | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::calloc (size_t nbytes, | ||
| char initial_value) | ||
| { | ||
| // Check if size requested fits within pre-determined size. | ||
| if (nbytes > this->chunk_size_) | ||
| return nullptr; | ||
|
|
||
| // No need any lock. | ||
| void *ptr = malloc(nbytes); | ||
| if (ptr != nullptr) | ||
| ACE_OS::memset (ptr, initial_value, this->chunk_size_); | ||
|
|
||
| return ptr; | ||
| } | ||
|
|
||
| template <class ACE_LOCK> void * | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::calloc (size_t, size_t, char) | ||
| { | ||
| ACE_NOTSUP_RETURN (nullptr); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> void | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::free (void * ptr) | ||
| { | ||
| ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); | ||
|
|
||
| ACE_ASSERT (this->hierarchy_.size() > 0); | ||
|
|
||
| // Use first allocator as a free chunk manager for all allocators when chunk freed. | ||
| if (ptr != nullptr && this->hierarchy_.size() > 0) | ||
| this->hierarchy_[0]->free(ptr); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::remove () | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::bind (const char *, void *, int) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::trybind (const char *, void *&) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::find (const char *, void *&) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::find (const char *) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::unbind (const char *) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::unbind (const char *, void *&) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::sync (ssize_t, int) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::sync (void *, size_t, int) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::protect (ssize_t, int) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> int | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::protect (void *, size_t, int) | ||
| { | ||
| ACE_NOTSUP_RETURN (-1); | ||
| } | ||
|
|
||
| template <class ACE_LOCK> size_t | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::pool_depth () | ||
| { | ||
| ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, 0)); | ||
|
|
||
| size_t pool_depth = 0; | ||
|
|
||
| for (size_t h = 0; h < this->hierarchy_.size(); h++) | ||
| { | ||
| pool_depth += this->hierarchy_[h]->pool_depth(); | ||
| } | ||
|
|
||
| return pool_depth; | ||
| } | ||
|
|
||
| template <class ACE_LOCK> void | ||
| ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::dump () const | ||
| { | ||
| #if defined (ACE_HAS_DUMP) | ||
| ACE_TRACE ("ACE_Cascaded_Dynamic_Cached_Allocator<ACE_LOCK>::dump"); | ||
|
|
||
| ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this)); | ||
| ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("initial_n_chunks_ = %u\n"), this->initial_n_chunks_)); | ||
| ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("chunk_size_ = %u\n"), this->chunk_size_)); | ||
| ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("chunk_sum_ = %u\n"), this->chunk_sum_)); | ||
| ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("hierarchy_ size = %u\n"), this->hierarchy_.size())); | ||
|
|
||
| for (size_t h = 0; h < this->hierarchy_.size(); h++) | ||
| { | ||
| this->hierarchy_[h]->dump(); | ||
| ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); | ||
| } | ||
|
|
||
| ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP)); | ||
| #endif /* ACE_HAS_DUMP */ | ||
| } | ||
|
|
||
| ACE_ALLOC_HOOK_DEFINE_Tmcc (ACE_Malloc_T) | ||
|
|
||
| template <class MALLOC> void * | ||
|
|
||
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.
initialize tmp
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.
the temp var will be assigned or call flow is ended by
ACE_NEWmacroShould it be initialized?
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.
the temp var will be assigned or call flow is ended by
ACE_NEWmacroShould it be initialized?