From bd320b1ec25af41352e702ae327d7fffb8329c92 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sun, 3 Sep 2023 15:24:47 +0800 Subject: [PATCH 01/72] can compiled successfully on Windows Plat --- ACE/ace/Malloc_T.cpp | 164 +++++++++++++++++++++++++++++++++++++++++++ ACE/ace/Malloc_T.h | 99 +++++++++++++++++++++++++- ACE/ace/Malloc_T.inl | 23 ++++++ 3 files changed, 285 insertions(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 8f1467423601a..67ea53de97a64 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -177,6 +177,170 @@ ACE_Dynamic_Cached_Allocator::free (void * ptr) this->free_list_.add ((ACE_Cached_Mem_Pool_Node *) ptr); } +template +ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_Allocator + (size_t initial_n_chunks, size_t chunk_size) + : initial_n_chunks_ (initial_n_chunks), + chunk_size_ (chunk_size) +{ + ACE_ASSERT (chunk_size > 0); + + comb_alloc_ptr tmp; + ACE_NEW (tmp, comb_alloc_type(this->initial_n_chunks_, this->chunk_size_)); + hierarchy_.push_back(tmp); +} + +template +ACE_Cascaded_Dynamic_Cached_Allocator::~ACE_Cascaded_Dynamic_Cached_Allocator () +{ + for (size_t c = 0; + c < hierarchy_.size(); + c++) + { + delete hierarchy_[c]; + } + + hierarchy_.clear(); + this->initial_n_chunks_ = 0; + this->chunk_size_ = 0; +} + +template void * +ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) +{ + // Check if size requested fits within pre-determined size. + if (nbytes > chunk_size_) + return nullptr; + + ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); + + void * ptr = nullptr; + + for (size_t c = 0; + c < hierarchy_.size(); + c++) + { + ptr = hierarchy_[c]->malloc(nbytes); + if(ptr != nullptr) + break; + } + + if(ptr == nullptr) + { + comb_alloc_ptr tmp; + ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); + hierarchy_.push_back(tmp); + ptr = tmp->malloc(nbytes); + } + + + return ptr; +} + +template void * +ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t nbytes, + char initial_value) +{ + // Check if size requested fits within pre-determined size. + if (nbytes > chunk_size_) + return 0; + + // No need any lock + void *ptr = malloc(nbytes); + if (ptr != nullptr) + ACE_OS::memset (ptr, initial_value, chunk_size_); + + return ptr; +} + +template void * +ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t, size_t, char) +{ + ACE_NOTSUP_RETURN (0); +} + +template void +ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) +{ + ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); + + if (ptr != nullptr) + hierarchy_[0]->free(ptr); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::remove () +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::bind (const char *, void *, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::trybind (const char *, void *&) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::find (const char *, void *&) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::find (const char *) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::unbind (const char *) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::unbind (const char *, void *&) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::sync (ssize_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::sync (void *, size_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::protect (ssize_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template int +ACE_Cascaded_Dynamic_Cached_Allocator::protect (void *, size_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template void +ACE_Cascaded_Dynamic_Cached_Allocator::dump () const +{ +#if defined (ACE_HAS_DUMP) +#endif /* ACE_HAS_DUMP */ +} + ACE_ALLOC_HOOK_DEFINE_Tmcc (ACE_Malloc_T) template void * diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 62775b798cbdb..518ccb5276953 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -5,7 +5,8 @@ * @file Malloc_T.h * * @author Douglas C. Schmidt and - * Irfan Pyarali + * Irfan Pyarali and + smithAchang */ //========================================================================== @@ -15,6 +16,7 @@ #include "ace/Malloc.h" /* Need ACE_Control_Block */ #include "ace/Malloc_Base.h" /* Need ACE_Allocator */ +#include "ace/Vector_T.h" /* Need ACE_Vector */ #if !defined (ACE_LACKS_PRAGMA_ONCE) # pragma once @@ -203,6 +205,101 @@ class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator size_t chunk_size_; }; +/** + * @class ACE_Cascaded_Dynamic_Cached_Allocator + * + * @brief A fixed-size-based allocator that caches blocks for quicker access, + * but with a hierarchy of cascaded, nested allocators + * + * This class enables caching of dynamically allocated, + * fixed-size chunks. Notice that the chunk_size + * must be greater than or equal to sizeof (void*) for + * this to work properly. + * + * Notice that when the latest allocator is empty, the allocator will create a fresh + * ACE_Dynamic_Cached_Allocator allocator again with + * init_n_chunks* the sum of current allocators as it's constructor parameter, + * so all the allocators will form a cascaded hierarchy. + + * This class can be configured flexibly with different types of + * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and @a + * ACE_Process_Mutex constructor API. + * + * @sa ACE_Dynamic_Cached_Allocator + */ +template +class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator +{ +public: + /// Create a cached memory pool with @a n_chunks chunks + /// each with @a chunk_size size. + ACE_Cascaded_Dynamic_Cached_Allocator (size_t initial_n_chunks, size_t chunk_size); + + /// Clear things up. + ~ACE_Cascaded_Dynamic_Cached_Allocator (); + + /** + * Get a chunk of memory from free list cache. Note that @a nbytes is + * only checked to make sure that it's less or equal to @a chunk_size, + * and is otherwise ignored since malloc() always returns a pointer to an + * item of @a chunk_size size. + */ + virtual void *malloc (size_t nbytes); + + /** + * Get a chunk of memory from free list cache, giving them + * @a initial_value. Note that @a nbytes is only checked to make sure + * that it's less or equal to @a chunk_size, and is otherwise ignored + * since calloc() always returns a pointer to an item of @a chunk_size. + */ + virtual void *calloc (size_t nbytes, char initial_value = '\0'); + + /// This method is a no-op and just returns 0 since the free list + /// only works with fixed sized entities. + virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0'); + + /// Return a chunk of memory back to free list cache. + virtual void free (void *ptr); + + /// These methods are no-ops. + virtual int remove (); + virtual int bind (const char *name, void *pointer, int duplicates = 0); + virtual int trybind (const char *name, void *&pointer); + virtual int find (const char *name, void *&pointer); + virtual int find (const char *name); + virtual int unbind (const char *name); + virtual int unbind (const char *name, void *&pointer); + virtual int sync (ssize_t len = -1, int flags = MS_SYNC); + virtual int sync (void *addr, size_t len, int flags = MS_SYNC); + virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); + virtual int protect (void *addr, size_t len, int prot = PROT_RDWR); + virtual void dump () const; + + /// Return the number of chunks available in the hierarchy. + size_t pool_depth (); + + /// Returns a reference to the lock used to provide mutual exclusion to + /// the allocator hierarchy. + ACE_LOCK &mutex (); + +private: + // Useful STL-style traits. + typedef typename ACE_Dynamic_Cached_Allocator comb_alloc_type; + typedef typename comb_alloc_type* comb_alloc_ptr; + + /// Synchronization variable for API. + ACE_LOCK mutex_; + + /// Remember how we allocate the memory so we can clear things up later. + ACE_Vector hierarchy_; + + /// Remember the size of initial n_chunks for creating fresh allocator in future. + size_t initial_n_chunks_; + + /// Remember the size of our chunks for creating fresh allocator in future. + size_t chunk_size_; +}; + /** * @class ACE_Allocator_Adapter * diff --git a/ACE/ace/Malloc_T.inl b/ACE/ace/Malloc_T.inl index c66d0edfc69c5..d86ae72e757dc 100644 --- a/ACE/ace/Malloc_T.inl +++ b/ACE/ace/Malloc_T.inl @@ -38,6 +38,29 @@ ACE_Dynamic_Cached_Allocator::pool_depth () return this->free_list_.size (); } +template ACE_INLINE size_t +ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () +{ + ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, 0)); + + size_t pool_depth = 0; + + for (size_t c = 0; + c < hierarchy_.size(); + c++) + { + pool_depth += hierarchy_[c]->pool_depth(); + } + + return pool_depth; +} + +template ACE_INLINE ACE_LOCK & +ACE_Cascaded_Dynamic_Cached_Allocator::mutex () +{ + return this->mutex_; +} + template ACE_INLINE int ACE_Malloc_T::ref_counter () { From 37dc8f45b7a446096ab91f4ff133418f210fc081 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sun, 3 Sep 2023 21:39:05 +0800 Subject: [PATCH 02/72] =?UTF-8?q?Allocator=5FCascaded=5FTest.cpp=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/tests/Allocator_Cascaded_Test.cpp | 86 +++++++++++++++++++++++++++ ACE/tests/tests.mpc | 8 +++ 2 files changed, 94 insertions(+) create mode 100644 ACE/tests/Allocator_Cascaded_Test.cpp diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp new file mode 100644 index 0000000000000..a170c960e0a45 --- /dev/null +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -0,0 +1,86 @@ +// ============================================================================ +// ============================================================================ +// +// = LIBRARY +// tests +// +// = DESCRIPTION +// This program tests the basic APIs supported in +// , and demonstrates how to use it. +// +// = AUTHOR +// Smith Achang +// +// ============================================================================ + +#include "test_config.h" +#include "ace/Synch_Traits.h" +#include "ace/Malloc_T.h" +#include "ace/Null_Mutex.h" + +#define ACE_TEST_EXCEPTION_RETURN(expression, message) \ +do \ +{ \ + if (expression) \ + { \ + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1); \ + } \ +} \ +while (0) + +static int +run_free_lock_cascaded_allocator_test () +{ + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); + + void *ptr, *ptr1, *ptr2; + size_t initial_n_chunks = 1; + size_t chunk_size = sizeof(void*); + + ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); + + ptr = alloc.calloc (1, 1, chunk_size); + + ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for ccalloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); + + ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, " initial pool depth must be one\n"); + + size_t nbytes = chunk_size; + + ptr = alloc.malloc(nbytes); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 0, " initial pool depth must be zero\n"); + alloc.free(ptr); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, " initial pool depth must be one after free\n"); + + ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); + ptr = alloc.malloc (nbytes); + ptr1 = alloc.malloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must be one after alloc twice\n"); + + + ptr2 = alloc.calloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (*(ACE_UINT64*)ptr2 != 0, " calloc call will clear the memory to zero\n"); + + + return 0; +} + +int +run_main (int, ACE_TCHAR *argv[]) +{ + ACE_UNUSED_ARG (argv); + + ACE_START_TEST (ACE_TEXT ("ACE_Cascaded_Dynamic_Cached_Allocator_Test")); + + int retval = 0; + + + // Run the tests for each type of ordering. + retval = run_free_lock_cascaded_allocator_test (); + + + ACE_END_TEST; + + + return retval; +} diff --git a/ACE/tests/tests.mpc b/ACE/tests/tests.mpc index c98a938e1844a..bed7f8e117d22 100644 --- a/ACE/tests/tests.mpc +++ b/ACE/tests/tests.mpc @@ -138,6 +138,14 @@ project(ACE Test) : acetest { } } +project(Allocator Cascaded Test) : acetest { + avoids += ace_for_tao + exename = Allocator_Cascaded_Test + Source_Files { + Allocator_Cascaded_Test.cpp + } +} + project(Aio Platform Test) : acetest { exename = Aio_Platform_Test Source_Files { From c61b9be44e0af8c12e4173d38ad110d50d3516b6 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Mon, 4 Sep 2023 19:43:42 +0800 Subject: [PATCH 03/72] add the 2*sum(Allocator of hierarchy) testing case --- ACE/ace/Malloc_T.h | 5 +++-- ACE/tests/Allocator_Cascaded_Test.cpp | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 518ccb5276953..878f830e6d1f3 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -17,6 +17,7 @@ #include "ace/Malloc.h" /* Need ACE_Control_Block */ #include "ace/Malloc_Base.h" /* Need ACE_Allocator */ #include "ace/Vector_T.h" /* Need ACE_Vector */ +#include "ace/Null_Mutex.h" /* Need ACE_Null_Mutex */ #if !defined (ACE_LACKS_PRAGMA_ONCE) # pragma once @@ -212,12 +213,12 @@ class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator * but with a hierarchy of cascaded, nested allocators * * This class enables caching of dynamically allocated, - * fixed-size chunks. Notice that the chunk_size + * fixed-size chunks. Notice that the @a chunk_size * must be greater than or equal to sizeof (void*) for * this to work properly. * * Notice that when the latest allocator is empty, the allocator will create a fresh - * ACE_Dynamic_Cached_Allocator allocator again with + * @a ACE_Dynamic_Cached_Allocator allocator again with * init_n_chunks* the sum of current allocators as it's constructor parameter, * so all the allocators will form a cascaded hierarchy. diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index a170c960e0a45..638e379123542 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -14,9 +14,8 @@ // ============================================================================ #include "test_config.h" -#include "ace/Synch_Traits.h" +s #include "ace/Malloc_T.h" -#include "ace/Null_Mutex.h" #define ACE_TEST_EXCEPTION_RETURN(expression, message) \ do \ @@ -39,7 +38,7 @@ run_free_lock_cascaded_allocator_test () ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); - ptr = alloc.calloc (1, 1, chunk_size); + ptr = alloc.calloc (1, sizeof(void*)); ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for ccalloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); @@ -55,12 +54,18 @@ run_free_lock_cascaded_allocator_test () ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); ptr = alloc.malloc (nbytes); ptr1 = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must be one after alloc twice\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must can support to alloc twice\n"); ptr2 = alloc.calloc (nbytes); ACE_TEST_EXCEPTION_RETURN (*(ACE_UINT64*)ptr2 != 0, " calloc call will clear the memory to zero\n"); + alloc.free (ptr); + alloc.free (ptr1); + alloc.free (ptr2); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + 2 * initial_n_chunks), + " cascaded pool depth must be three after freed all malloc ptrs\n"); + return 0; } From 6166f88580867886c85ff807926bdd67486e1097 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Mon, 4 Sep 2023 19:54:08 +0800 Subject: [PATCH 04/72] bin/fuzz.pl find trailing whitespace --- ACE/ace/Malloc_T.h | 8 ++++---- ACE/ace/Malloc_T.inl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 878f830e6d1f3..cbc8df14254aa 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -5,7 +5,7 @@ * @file Malloc_T.h * * @author Douglas C. Schmidt and - * Irfan Pyarali and + * Irfan Pyarali and smithAchang */ //========================================================================== @@ -217,11 +217,11 @@ class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator * must be greater than or equal to sizeof (void*) for * this to work properly. * - * Notice that when the latest allocator is empty, the allocator will create a fresh + * Notice that when the latest allocator is empty, the allocator will create a fresh * @a ACE_Dynamic_Cached_Allocator allocator again with * init_n_chunks* the sum of current allocators as it's constructor parameter, * so all the allocators will form a cascaded hierarchy. - + * This class can be configured flexibly with different types of * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and @a * ACE_Process_Mutex constructor API. @@ -231,7 +231,7 @@ class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator template class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator { -public: +public: /// Create a cached memory pool with @a n_chunks chunks /// each with @a chunk_size size. ACE_Cascaded_Dynamic_Cached_Allocator (size_t initial_n_chunks, size_t chunk_size); diff --git a/ACE/ace/Malloc_T.inl b/ACE/ace/Malloc_T.inl index d86ae72e757dc..f5a85348fecd6 100644 --- a/ACE/ace/Malloc_T.inl +++ b/ACE/ace/Malloc_T.inl @@ -55,7 +55,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () return pool_depth; } -template ACE_INLINE ACE_LOCK & +template ACE_INLINE ACE_LOCK & ACE_Cascaded_Dynamic_Cached_Allocator::mutex () { return this->mutex_; From 32d3d1f40c19e20afd155c51d9fd33ace931cf46 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Mon, 4 Sep 2023 19:57:22 +0800 Subject: [PATCH 05/72] bin/fuzz.pl find trailing whitespace 2 --- ACE/ace/Malloc_T.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 67ea53de97a64..8daa633f5ab04 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -228,7 +228,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) if(ptr == nullptr) { comb_alloc_ptr tmp; - ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); + ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); hierarchy_.push_back(tmp); ptr = tmp->malloc(nbytes); } From e2a551125f80ef6fdaf292bcb5915b389c9204cb Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Mon, 4 Sep 2023 22:50:43 +0800 Subject: [PATCH 06/72] compiler error --- ACE/tests/Allocator_Cascaded_Test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 638e379123542..4d9477d38e91b 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -14,7 +14,6 @@ // ============================================================================ #include "test_config.h" -s #include "ace/Malloc_T.h" #define ACE_TEST_EXCEPTION_RETURN(expression, message) \ From 150eb7baa09139771fc919462dcc9092c04737fe Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Mon, 4 Sep 2023 23:14:00 +0800 Subject: [PATCH 07/72] use std::vector --- ACE/ace/Malloc_T.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index cbc8df14254aa..486eadf1843ab 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -16,7 +16,6 @@ #include "ace/Malloc.h" /* Need ACE_Control_Block */ #include "ace/Malloc_Base.h" /* Need ACE_Allocator */ -#include "ace/Vector_T.h" /* Need ACE_Vector */ #include "ace/Null_Mutex.h" /* Need ACE_Null_Mutex */ #if !defined (ACE_LACKS_PRAGMA_ONCE) @@ -27,6 +26,8 @@ #include "ace/Free_List.h" #include "ace/Guard_T.h" +#include + ACE_BEGIN_VERSIONED_NAMESPACE_DECL /** @@ -285,14 +286,14 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator private: // Useful STL-style traits. - typedef typename ACE_Dynamic_Cached_Allocator comb_alloc_type; + typedef typename ACE_Dynamic_Cached_Allocator comb_alloc_type; typedef typename comb_alloc_type* comb_alloc_ptr; /// Synchronization variable for API. ACE_LOCK mutex_; /// Remember how we allocate the memory so we can clear things up later. - ACE_Vector hierarchy_; + std::vector hierarchy_; /// Remember the size of initial n_chunks for creating fresh allocator in future. size_t initial_n_chunks_; From 089af451de14d2e966dc2cc4cbb5261dfa3db7d9 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 5 Sep 2023 10:47:33 +0800 Subject: [PATCH 08/72] linux compiling error & fix issue --- ACE/ace/Malloc_T.h | 4 ++-- ACE/tests/Allocator_Cascaded_Test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 486eadf1843ab..dff2f0494de4c 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -286,8 +286,8 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator private: // Useful STL-style traits. - typedef typename ACE_Dynamic_Cached_Allocator comb_alloc_type; - typedef typename comb_alloc_type* comb_alloc_ptr; + typedef ACE_Dynamic_Cached_Allocator comb_alloc_type; + typedef comb_alloc_type* comb_alloc_ptr; /// Synchronization variable for API. ACE_LOCK mutex_; diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 4d9477d38e91b..3293f243b3253 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -57,7 +57,7 @@ run_free_lock_cascaded_allocator_test () ptr2 = alloc.calloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (*(ACE_UINT64*)ptr2 != 0, " calloc call will clear the memory to zero\n"); + ACE_TEST_EXCEPTION_RETURN (*static_cast(ptr2) != 0, " calloc call will clear the memory to zero\n"); alloc.free (ptr); alloc.free (ptr1); From ac87bd4d591a5b08a279346c1260d632668cdadf Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 5 Sep 2023 15:10:53 +0800 Subject: [PATCH 09/72] according the routine of test framework --- ACE/tests/Allocator_Cascaded_Test.cpp | 11 +++-------- ACE/tests/run_test.lst | 1 + ACE/tests/tests.mpc | 14 +++++++------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 3293f243b3253..ec2d9094c6e7a 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -13,8 +13,8 @@ // // ============================================================================ -#include "test_config.h" #include "ace/Malloc_T.h" +#include "test_config.h" #define ACE_TEST_EXCEPTION_RETURN(expression, message) \ do \ @@ -70,21 +70,16 @@ run_free_lock_cascaded_allocator_test () } int -run_main (int, ACE_TCHAR *argv[]) +run_main (int, ACE_TCHAR *[]) { - ACE_UNUSED_ARG (argv); - - ACE_START_TEST (ACE_TEXT ("ACE_Cascaded_Dynamic_Cached_Allocator_Test")); + ACE_START_TEST (ACE_TEXT ("Allocator_Cascaded_Test")); int retval = 0; - // Run the tests for each type of ordering. retval = run_free_lock_cascaded_allocator_test (); - ACE_END_TEST; - return retval; } diff --git a/ACE/tests/run_test.lst b/ACE/tests/run_test.lst index c8b018e3eb6cd..118c1ce71dd72 100644 --- a/ACE/tests/run_test.lst +++ b/ACE/tests/run_test.lst @@ -16,6 +16,7 @@ ACE_Init_Test: MFC ACE_Test Aio_Platform_Test +Allocator_Cascaded_Test Arg_Shifter_Test ARGV_Test Array_Map_Test diff --git a/ACE/tests/tests.mpc b/ACE/tests/tests.mpc index bed7f8e117d22..5e216233b1e66 100644 --- a/ACE/tests/tests.mpc +++ b/ACE/tests/tests.mpc @@ -138,18 +138,18 @@ project(ACE Test) : acetest { } } -project(Allocator Cascaded Test) : acetest { - avoids += ace_for_tao - exename = Allocator_Cascaded_Test +project(Aio Platform Test) : acetest { + exename = Aio_Platform_Test Source_Files { - Allocator_Cascaded_Test.cpp + Aio_Platform_Test.cpp } } -project(Aio Platform Test) : acetest { - exename = Aio_Platform_Test +project(Allocator Cascaded Test) : acetest { + avoids += ace_for_tao + exename = Allocator_Cascaded_Test Source_Files { - Aio_Platform_Test.cpp + Allocator_Cascaded_Test.cpp } } From b71f82dcd46b0522e596b3fb7bd97f92fbc0dc20 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 5 Sep 2023 16:42:01 +0800 Subject: [PATCH 10/72] key design comment for free API --- ACE/ace/Malloc_T.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 8daa633f5ab04..d36376f552016 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -264,6 +264,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) { ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); + // Use first allocator as a free chunk manager for all allocators when chunk freed. if (ptr != nullptr) hierarchy_[0]->free(ptr); } From 34abb6e3ed153d94265e02bf6dd0354f1646ff19 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 5 Sep 2023 17:17:42 +0800 Subject: [PATCH 11/72] fix space indent & const member field --- ACE/ace/Malloc_T.cpp | 31 ++++++++++++++----------------- ACE/ace/Malloc_T.h | 4 ++-- ACE/ace/Malloc_T.inl | 6 +++--- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index d36376f552016..0242b20b4d4cc 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -194,15 +194,13 @@ template ACE_Cascaded_Dynamic_Cached_Allocator::~ACE_Cascaded_Dynamic_Cached_Allocator () { for (size_t c = 0; - c < hierarchy_.size(); - c++) + c < hierarchy_.size(); + c++) { delete hierarchy_[c]; } hierarchy_.clear(); - this->initial_n_chunks_ = 0; - this->chunk_size_ = 0; } template void * @@ -217,23 +215,22 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) void * ptr = nullptr; for (size_t c = 0; - c < hierarchy_.size(); - c++) + c < hierarchy_.size(); + c++) { - ptr = hierarchy_[c]->malloc(nbytes); - if(ptr != nullptr) - break; + ptr = hierarchy_[c]->malloc(nbytes); + if(ptr != nullptr) + break; } if(ptr == nullptr) { - comb_alloc_ptr tmp; - ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); - hierarchy_.push_back(tmp); - ptr = tmp->malloc(nbytes); + comb_alloc_ptr tmp; + ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); + hierarchy_.push_back(tmp); + ptr = tmp->malloc(nbytes); } - return ptr; } @@ -243,12 +240,12 @@ ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t nbytes, { // Check if size requested fits within pre-determined size. if (nbytes > chunk_size_) - return 0; + return nullptr; - // No need any lock + // No need any lock. void *ptr = malloc(nbytes); if (ptr != nullptr) - ACE_OS::memset (ptr, initial_value, chunk_size_); + ACE_OS::memset (ptr, initial_value, chunk_size_); return ptr; } diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index dff2f0494de4c..ca50b4b99b394 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -296,10 +296,10 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator std::vector hierarchy_; /// Remember the size of initial n_chunks for creating fresh allocator in future. - size_t initial_n_chunks_; + const size_t initial_n_chunks_; /// Remember the size of our chunks for creating fresh allocator in future. - size_t chunk_size_; + const size_t chunk_size_; }; /** diff --git a/ACE/ace/Malloc_T.inl b/ACE/ace/Malloc_T.inl index f5a85348fecd6..0c8060e1cdeca 100644 --- a/ACE/ace/Malloc_T.inl +++ b/ACE/ace/Malloc_T.inl @@ -46,10 +46,10 @@ ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () size_t pool_depth = 0; for (size_t c = 0; - c < hierarchy_.size(); - c++) + c < hierarchy_.size(); + c++) { - pool_depth += hierarchy_[c]->pool_depth(); + pool_depth += hierarchy_[c]->pool_depth(); } return pool_depth; From cf396803734afe0a158f7c492b29360c2ca7a03a Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 5 Sep 2023 17:35:08 +0800 Subject: [PATCH 12/72] using instead of typedef, will be fitter for template declaration --- ACE/ace/Malloc_T.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index ca50b4b99b394..3341eb69c141d 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -286,7 +286,7 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator private: // Useful STL-style traits. - typedef ACE_Dynamic_Cached_Allocator comb_alloc_type; + using comb_alloc_type = ACE_Dynamic_Cached_Allocator; typedef comb_alloc_type* comb_alloc_ptr; /// Synchronization variable for API. From 78e7165db4c0dd3e9356af3a494f7b4d52f72c7c Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 5 Sep 2023 17:39:21 +0800 Subject: [PATCH 13/72] remove duplicate empty lines --- ACE/tests/Allocator_Cascaded_Test.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index ec2d9094c6e7a..99c99d8be8ca6 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -55,7 +55,6 @@ run_free_lock_cascaded_allocator_test () ptr1 = alloc.malloc (nbytes); ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must can support to alloc twice\n"); - ptr2 = alloc.calloc (nbytes); ACE_TEST_EXCEPTION_RETURN (*static_cast(ptr2) != 0, " calloc call will clear the memory to zero\n"); @@ -65,7 +64,6 @@ run_free_lock_cascaded_allocator_test () ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + 2 * initial_n_chunks), " cascaded pool depth must be three after freed all malloc ptrs\n"); - return 0; } From e5e55bc4f8b8334b5e41a2a36a5b596c8583a725 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 5 Sep 2023 17:55:43 +0800 Subject: [PATCH 14/72] free protected when malloc in constructor failed --- ACE/ace/Malloc_T.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 0242b20b4d4cc..6810b97823512 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -218,7 +218,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) c < hierarchy_.size(); c++) { - ptr = hierarchy_[c]->malloc(nbytes); + ptr = hierarchy_[c]->malloc(nbytes); if(ptr != nullptr) break; } @@ -262,8 +262,8 @@ ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); // Use first allocator as a free chunk manager for all allocators when chunk freed. - if (ptr != nullptr) - hierarchy_[0]->free(ptr); + if (ptr != nullptr && hierarchy_.size () > 0) + hierarchy_[0]->free(ptr); } template int From 00016bfc4b50ad8f64edc77f575fc9716c727e34 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 5 Sep 2023 20:44:49 +0800 Subject: [PATCH 15/72] 1. add assert for free API 2. using instead of typedef --- ACE/ace/Malloc_T.cpp | 4 +++- ACE/ace/Malloc_T.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 6810b97823512..c692d4688a14e 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -183,7 +183,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All : initial_n_chunks_ (initial_n_chunks), chunk_size_ (chunk_size) { - ACE_ASSERT (chunk_size > 0); + ACE_ASSERT (chunk_size_ > 0); comb_alloc_ptr tmp; ACE_NEW (tmp, comb_alloc_type(this->initial_n_chunks_, this->chunk_size_)); @@ -261,6 +261,8 @@ ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) { ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); + ACE_ASSERT (hierarchy_.size () > 0); + // Use first allocator as a free chunk manager for all allocators when chunk freed. if (ptr != nullptr && hierarchy_.size () > 0) hierarchy_[0]->free(ptr); diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 3341eb69c141d..415854d857c48 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -287,7 +287,7 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator private: // Useful STL-style traits. using comb_alloc_type = ACE_Dynamic_Cached_Allocator; - typedef comb_alloc_type* comb_alloc_ptr; + using comb_alloc_ptr = comb_alloc_type*; /// Synchronization variable for API. ACE_LOCK mutex_; From 912d9db0af859d1d66dac78bc88fda1d81323150 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Wed, 6 Sep 2023 09:15:03 +0800 Subject: [PATCH 16/72] add comments for ACE_NEW fail in constructor --- ACE/ace/Malloc_T.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index c692d4688a14e..bf1657cfab37a 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -186,6 +186,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All ACE_ASSERT (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_)); hierarchy_.push_back(tmp); } From 40675b0bab2922c310280e23e091ff351d98a19a Mon Sep 17 00:00:00 2001 From: smitAchang Date: Thu, 7 Sep 2023 09:30:26 +0800 Subject: [PATCH 17/72] fix ""One line, indent is always 2 spaces --- ACE/ace/Malloc_T.cpp | 22 ++++----- ACE/ace/Malloc_T.h | 2 +- ACE/ace/Malloc_T.inl | 2 +- ACE/tests/Allocator_Cascaded_Test.cpp | 66 +++++++++++++-------------- 4 files changed, 46 insertions(+), 46 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index bf1657cfab37a..804b7b5bc5511 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -198,7 +198,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::~ACE_Cascaded_Dynamic_Cached_Al c < hierarchy_.size(); c++) { - delete hierarchy_[c]; + delete hierarchy_[c]; } hierarchy_.clear(); @@ -219,17 +219,17 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) c < hierarchy_.size(); c++) { - ptr = hierarchy_[c]->malloc(nbytes); - if(ptr != nullptr) - break; + ptr = hierarchy_[c]->malloc(nbytes); + if(ptr != nullptr) + break; } if(ptr == nullptr) { - comb_alloc_ptr tmp; - ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); - hierarchy_.push_back(tmp); - ptr = tmp->malloc(nbytes); + comb_alloc_ptr tmp; + ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); + hierarchy_.push_back(tmp); + ptr = tmp->malloc(nbytes); } return ptr; @@ -241,12 +241,12 @@ ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t nbytes, { // Check if size requested fits within pre-determined size. if (nbytes > chunk_size_) - return nullptr; + return nullptr; // No need any lock. void *ptr = malloc(nbytes); if (ptr != nullptr) - ACE_OS::memset (ptr, initial_value, chunk_size_); + ACE_OS::memset (ptr, initial_value, chunk_size_); return ptr; } @@ -266,7 +266,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) // Use first allocator as a free chunk manager for all allocators when chunk freed. if (ptr != nullptr && hierarchy_.size () > 0) - hierarchy_[0]->free(ptr); + hierarchy_[0]->free(ptr); } template int diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 415854d857c48..55823d036fb67 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -6,7 +6,7 @@ * * @author Douglas C. Schmidt and * Irfan Pyarali and - smithAchang + * smithAchang */ //========================================================================== diff --git a/ACE/ace/Malloc_T.inl b/ACE/ace/Malloc_T.inl index 0c8060e1cdeca..dbe99f7f49246 100644 --- a/ACE/ace/Malloc_T.inl +++ b/ACE/ace/Malloc_T.inl @@ -49,7 +49,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () c < hierarchy_.size(); c++) { - pool_depth += hierarchy_[c]->pool_depth(); + pool_depth += hierarchy_[c]->pool_depth(); } return pool_depth; diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 99c99d8be8ca6..275356d00f708 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -16,55 +16,55 @@ #include "ace/Malloc_T.h" #include "test_config.h" -#define ACE_TEST_EXCEPTION_RETURN(expression, message) \ -do \ -{ \ - if (expression) \ - { \ - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1); \ - } \ -} \ +#define ACE_TEST_EXCEPTION_RETURN(expression, message) \ +do \ +{ \ + if (expression) \ + { \ + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1);\ + } \ +} \ while (0) static int run_free_lock_cascaded_allocator_test () { - ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - void *ptr, *ptr1, *ptr2; - size_t initial_n_chunks = 1; - size_t chunk_size = sizeof(void*); + void *ptr, *ptr1, *ptr2; + size_t initial_n_chunks = 1; + size_t chunk_size = sizeof(void*); - ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); + ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); - ptr = alloc.calloc (1, sizeof(void*)); + ptr = alloc.calloc (1, sizeof(void*)); - ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for ccalloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); + ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for ccalloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); - ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, " initial pool depth must be one\n"); + ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, " initial pool depth must be one\n"); - size_t nbytes = chunk_size; + size_t nbytes = chunk_size; - ptr = alloc.malloc(nbytes); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 0, " initial pool depth must be zero\n"); - alloc.free(ptr); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, " initial pool depth must be one after free\n"); + ptr = alloc.malloc(nbytes); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 0, " initial pool depth must be zero\n"); + alloc.free(ptr); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, " initial pool depth must be one after free\n"); - ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); - ptr = alloc.malloc (nbytes); - ptr1 = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must can support to alloc twice\n"); + ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); + ptr = alloc.malloc (nbytes); + ptr1 = alloc.malloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must can support to alloc twice\n"); - ptr2 = alloc.calloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (*static_cast(ptr2) != 0, " calloc call will clear the memory to zero\n"); + ptr2 = alloc.calloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (*static_cast(ptr2) != 0, " calloc call will clear the memory to zero\n"); - alloc.free (ptr); - alloc.free (ptr1); - alloc.free (ptr2); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + 2 * initial_n_chunks), - " cascaded pool depth must be three after freed all malloc ptrs\n"); + alloc.free (ptr); + alloc.free (ptr1); + alloc.free (ptr2); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + 2 * initial_n_chunks), + " cascaded pool depth must be three after freed all malloc ptrs\n"); - return 0; + return 0; } int From 6e9a17cba6b0e35c00502f4175e7065d90ebe71a Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 8 Sep 2023 20:37:11 +0800 Subject: [PATCH 18/72] for g++ -fno-implicit-templates consideration --- ACE/ace/Malloc_T.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 55823d036fb67..4e7dd298f5d62 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -233,6 +233,10 @@ template class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator { public: + // Useful STL-style traits. + using comb_alloc_type = ACE_Dynamic_Cached_Allocator; + using comb_alloc_ptr = comb_alloc_type*; + /// Create a cached memory pool with @a n_chunks chunks /// each with @a chunk_size size. ACE_Cascaded_Dynamic_Cached_Allocator (size_t initial_n_chunks, size_t chunk_size); @@ -285,10 +289,6 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator ACE_LOCK &mutex (); private: - // Useful STL-style traits. - using comb_alloc_type = ACE_Dynamic_Cached_Allocator; - using comb_alloc_ptr = comb_alloc_type*; - /// Synchronization variable for API. ACE_LOCK mutex_; From d065069962be5fcb08808a67f61155f0e4143137 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 16 Sep 2023 11:29:23 +0800 Subject: [PATCH 19/72] implement the dump API --- ACE/ace/Malloc_T.cpp | 15 ++++++++++++++- ACE/ace/Malloc_T.h | 4 +++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 804b7b5bc5511..3e7b646ec326e 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -338,7 +338,20 @@ ACE_Cascaded_Dynamic_Cached_Allocator::protect (void *, size_t, int) template void ACE_Cascaded_Dynamic_Cached_Allocator::dump () const { -#if defined (ACE_HAS_DUMP) +#if defined (ACE_HAS_DUMP) + ACE_TRACE ("ACE_Cascaded_Dynamic_Cached_Allocator::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_)); + + for (size_t c = 0; c < hierarchy_.size (); c++) + { + hierarchy_[c]->dump (); + ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); + } + + ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP)); #endif /* ACE_HAS_DUMP */ } diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 4e7dd298f5d62..0eebd30a635a7 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -267,6 +267,9 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator /// Return a chunk of memory back to free list cache. virtual void free (void *ptr); + /// Dump the state of this object. + virtual void dump () const; + /// These methods are no-ops. virtual int remove (); virtual int bind (const char *name, void *pointer, int duplicates = 0); @@ -279,7 +282,6 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator virtual int sync (void *addr, size_t len, int flags = MS_SYNC); virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); virtual int protect (void *addr, size_t len, int prot = PROT_RDWR); - virtual void dump () const; /// Return the number of chunks available in the hierarchy. size_t pool_depth (); From eba85a441323ae42b1380a46d4b431c7217ae022 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 16 Sep 2023 14:17:44 +0800 Subject: [PATCH 20/72] fix for statement of dump API --- ACE/ace/Malloc_T.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 3e7b646ec326e..375bfc3a63a29 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -345,7 +345,9 @@ ACE_Cascaded_Dynamic_Cached_Allocator::dump () const 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_)); - for (size_t c = 0; c < hierarchy_.size (); c++) + for (size_t c = 0; + c < hierarchy_.size (); + c++) { hierarchy_[c]->dump (); ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); From 1e1862db23278f7040e753cc82e3055606191440 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 16 Sep 2023 15:08:06 +0800 Subject: [PATCH 21/72] fix trailing whitespace --- ACE/ace/Malloc_T.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 375bfc3a63a29..347f793bada6b 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -338,7 +338,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::protect (void *, size_t, int) template void ACE_Cascaded_Dynamic_Cached_Allocator::dump () const { -#if defined (ACE_HAS_DUMP) +#if defined (ACE_HAS_DUMP) ACE_TRACE ("ACE_Cascaded_Dynamic_Cached_Allocator::dump"); ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this)); From 0f26ecb227907727f0ac6f804c3d9d27c2d232cf Mon Sep 17 00:00:00 2001 From: smitAchang Date: Mon, 18 Sep 2023 08:50:38 +0800 Subject: [PATCH 22/72] add hierarchy_.size () for dump API --- ACE/ace/Malloc_T.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 347f793bada6b..bf05a524880b7 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -344,6 +344,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::dump () const 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 ("hierarchy_ size = %u\n"), this->hierarchy_.size ())); for (size_t c = 0; c < hierarchy_.size (); From 4a7fcd0a5d14dbf887480f79d298e8622de0ef7f Mon Sep 17 00:00:00 2001 From: smitAchang Date: Fri, 22 Sep 2023 17:21:29 +0800 Subject: [PATCH 23/72] =?UTF-8?q?1=E3=80=81fix=20some=20coding=20style=20u?= =?UTF-8?q?sing=20this=20keyword=20to=20distinguish=20vars=202=E3=80=81mak?= =?UTF-8?q?e=20simple=20for=20statement=20at=20one=20line=203=E3=80=81add?= =?UTF-8?q?=20the=20pool=5Fsum=20API=20for=20component=20transparency=204?= =?UTF-8?q?=E3=80=81move=20pool=5Fdepth=20API=20from=20INL=20file=20to=20s?= =?UTF-8?q?ource=20file=20for=20its=20complexity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.cpp | 63 +++++++++++++++++++++++++++----------------- ACE/ace/Malloc_T.h | 6 +++++ ACE/ace/Malloc_T.inl | 13 ++------- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index bf05a524880b7..c5cd60160a2e5 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -181,45 +181,44 @@ template ACE_Cascaded_Dynamic_Cached_Allocator::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_size_ (chunk_size), + chunk_sum_ (0) { - ACE_ASSERT (chunk_size_ > 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_)); - hierarchy_.push_back(tmp); + // Increase the chunk sum. + this->chunk_sum_ += tmp->pool_depth (); + this->hierarchy_.push_back(tmp); } template ACE_Cascaded_Dynamic_Cached_Allocator::~ACE_Cascaded_Dynamic_Cached_Allocator () { - for (size_t c = 0; - c < hierarchy_.size(); - c++) + for (size_t h = 0; h < this->hierarchy_.size(); h++) { - delete hierarchy_[c]; + delete this->hierarchy_[h]; } - hierarchy_.clear(); + this->hierarchy_.clear(); } template void * ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) { // Check if size requested fits within pre-determined size. - if (nbytes > chunk_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 c = 0; - c < hierarchy_.size(); - c++) + for (size_t h = 0; h < this->hierarchy_.size(); h++) { - ptr = hierarchy_[c]->malloc(nbytes); + ptr = this->hierarchy_[h]->malloc(nbytes); if(ptr != nullptr) break; } @@ -227,8 +226,10 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) if(ptr == nullptr) { comb_alloc_ptr tmp; - ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * hierarchy_.size(), this->chunk_size_), nullptr); - hierarchy_.push_back(tmp); + ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * this->hierarchy_.size(), this->chunk_size_), nullptr); + // Increase the chunk sum. + this->chunk_sum_ += tmp->pool_depth (); + this->hierarchy_.push_back(tmp); ptr = tmp->malloc(nbytes); } @@ -240,13 +241,13 @@ ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t nbytes, char initial_value) { // Check if size requested fits within pre-determined size. - if (nbytes > chunk_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, chunk_size_); + ACE_OS::memset (ptr, initial_value, this->chunk_size_); return ptr; } @@ -262,11 +263,11 @@ ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) { ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); - ACE_ASSERT (hierarchy_.size () > 0); + ACE_ASSERT (this->hierarchy_.size () > 0); // Use first allocator as a free chunk manager for all allocators when chunk freed. - if (ptr != nullptr && hierarchy_.size () > 0) - hierarchy_[0]->free(ptr); + if (ptr != nullptr && this->hierarchy_.size () > 0) + this->hierarchy_[0]->free(ptr); } template int @@ -335,6 +336,21 @@ ACE_Cascaded_Dynamic_Cached_Allocator::protect (void *, size_t, int) ACE_NOTSUP_RETURN (-1); } +template size_t +ACE_Cascaded_Dynamic_Cached_Allocator::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 void ACE_Cascaded_Dynamic_Cached_Allocator::dump () const { @@ -344,13 +360,12 @@ ACE_Cascaded_Dynamic_Cached_Allocator::dump () const 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 c = 0; - c < hierarchy_.size (); - c++) + for (size_t h = 0; h < this->hierarchy_.size (); h++) { - hierarchy_[c]->dump (); + this->hierarchy_[h]->dump (); ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); } diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 0eebd30a635a7..32e0ea4ed9f9f 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -286,6 +286,9 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator /// Return the number of chunks available in the hierarchy. size_t pool_depth (); + /// Return the sum of chunks including used and freed in the hierarchy. + size_t pool_sum (); + /// Returns a reference to the lock used to provide mutual exclusion to /// the allocator hierarchy. ACE_LOCK &mutex (); @@ -302,6 +305,9 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator /// Remember the size of our chunks for creating fresh allocator in future. const size_t chunk_size_; + + /// Remember the sum of our chunks including used and freed + size_t chunk_sum_; }; /** diff --git a/ACE/ace/Malloc_T.inl b/ACE/ace/Malloc_T.inl index dbe99f7f49246..e9e4c49054f10 100644 --- a/ACE/ace/Malloc_T.inl +++ b/ACE/ace/Malloc_T.inl @@ -39,20 +39,11 @@ ACE_Dynamic_Cached_Allocator::pool_depth () } template ACE_INLINE size_t -ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () +ACE_Cascaded_Dynamic_Cached_Allocator::pool_sum () { ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, 0)); - size_t pool_depth = 0; - - for (size_t c = 0; - c < hierarchy_.size(); - c++) - { - pool_depth += hierarchy_[c]->pool_depth(); - } - - return pool_depth; + return this->chunk_sum_; } template ACE_INLINE ACE_LOCK & From cf686b38b7705bcc6c70e5e5556322469ec3506b Mon Sep 17 00:00:00 2001 From: smitAchang Date: Fri, 22 Sep 2023 17:49:28 +0800 Subject: [PATCH 24/72] add the pool_sum api --- ACE/tests/Allocator_Cascaded_Test.cpp | 36 +++++++++++++++++---------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 275356d00f708..154a544a2fea1 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -31,29 +31,39 @@ run_free_lock_cascaded_allocator_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); + const size_t initial_n_chunks = 1; + const size_t chunk_size = sizeof(void*); + void *ptr, *ptr1, *ptr2; - size_t initial_n_chunks = 1; - size_t chunk_size = sizeof(void*); + size_t nbytes = chunk_size; + size_t chunk_sum, old_chunk_sum; ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); + chunk_sum = alloc.pool_sum (); + ACE_TEST_EXCEPTION_RETURN (chunk_sum != initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); + ACE_DEBUG ((LM_INFO, "%C will test unsupported API ...\n", __func__)); ptr = alloc.calloc (1, sizeof(void*)); - - ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for ccalloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); - - ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, " initial pool depth must be one\n"); - - size_t nbytes = chunk_size; + ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); + ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, " initial pool depth must keep unchanged for call of unsupported API\n"); ptr = alloc.malloc(nbytes); ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 0, " initial pool depth must be zero\n"); alloc.free(ptr); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, " initial pool depth must be one after free\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, " initial pool depth must be initial_n_chunks after free\n"); ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); - ptr = alloc.malloc (nbytes); + ptr = alloc.malloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, " pool must return valid ptr, cascaded pool must support to alloc more times firstly\n"); + ptr1 = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must can support to alloc twice\n"); + ACE_TEST_EXCEPTION_RETURN (ptr1 == nullptr, " pool must return valid ptr, cascaded pool must support to alloc more times secondly\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must support to alloc twice\n"); + + old_chunk_sum = chunk_sum; + chunk_sum = alloc.pool_sum (); + ACE_TEST_EXCEPTION_RETURN (chunk_sum < old_chunk_sum, " cascaded pool sum must be bigger than that of initial pool\n"); + ACE_TEST_EXCEPTION_RETURN (chunk_sum != (3*initial_n_chunks), " cascaded pool sum must be as expected, pool has been enlarged\n"); ptr2 = alloc.calloc (nbytes); ACE_TEST_EXCEPTION_RETURN (*static_cast(ptr2) != 0, " calloc call will clear the memory to zero\n"); @@ -61,8 +71,8 @@ run_free_lock_cascaded_allocator_test () alloc.free (ptr); alloc.free (ptr1); alloc.free (ptr2); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + 2 * initial_n_chunks), - " cascaded pool depth must be three after freed all malloc ptrs\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + (2 * initial_n_chunks)), + " cascaded pool depth must be three after having freed all malloc ptrs\n"); return 0; } From 939a2a2c4ac6eab22af3efabd69b7c2fc7b07418 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 23 Sep 2023 11:22:41 +0800 Subject: [PATCH 25/72] can compile successfully --- ACE/ace/Malloc_T.cpp | 255 ++++++++++++++++++++++++++ ACE/ace/Malloc_T.h | 106 +++++++++++ ACE/ace/Malloc_T.inl | 6 + ACE/tests/Allocator_Cascaded_Test.cpp | 58 ++++++ 4 files changed, 425 insertions(+) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index c5cd60160a2e5..f1f0d42cc7b83 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -373,6 +373,261 @@ ACE_Cascaded_Dynamic_Cached_Allocator::dump () const #endif /* ACE_HAS_DUMP */ } +template +ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based_Allocator +(size_t initial_n_chunks, size_t chunk_size, size_t min_initial_n_chunks) + : initial_n_chunks_ (initial_n_chunks), + chunk_size_ (chunk_size), + min_initial_n_chunks_ (min_initial_n_chunks) +{ + 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_)); + this->hierarchy_.push_back (tmp); +} + +template +ACE_Cascaded_Multi_Size_Based_Allocator::~ACE_Cascaded_Multi_Size_Based_Allocator () +{ + for (size_t h = 0; h < this->hierarchy_.size (); h++) + { + delete this->hierarchy_[h]; + } + + this->hierarchy_.clear (); +} + +template +void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) +{ + // Check if size requested fits within pre-determined size. + if (nbytes > this->chunk_size_) + return nullptr; + + // Use Binary Search to find minimal pos that value is bigger than nbytes. + size_t m = 0; + size_t l = 0; + size_t h = 0; + + // Must has initial value When hierarchy_ is empty + size_t chunk_size = this->chunk_size_; + + while (l <= h) + { + m = (l + h) / 2; + chunk_size = this->chunk_size_ << m; + if (chunk_size >= nbytes) + { + // End loop + if (m == 0) + break; + + h = m - 1; + } + else + { + l = m + 1; + } + } + + // Not in hierarchy or less than nbytes when search + while (chunk_size < nbytes) + { + chunk_size <<= 1; + ++m; + } + + if (m > ACE_OCTET_MAX) + return nullptr; + + ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); + + if (m < this->hierarchy_.size () && this->hierarchy_[m] != nullptr) + { + void* ptr = this->hierarchy_[m]->malloc(nbytes); + if (ptr == nullptr) + return nullptr; + + *static_cast (ptr) = static_cast (m); + return static_cast (ptr) + sizeof (ACE_UINT8); + } + + // The found pos maybe nullptr or beyond the current hierarchy_ size. + if (m >= this->hierarchy_.size()) + { + this->hierarchy_.resize (m + 1, nullptr); + } + + const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; + comb_alloc_ptr newly_alloc; + ACE_NEW_RETURN (newly_alloc, + comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, + chunk_size + sizeof(ACE_UINT8)), + nullptr + ); + + this->hierarchy_[m] = newly_alloc; + void* ptr = newly_alloc->malloc (nbytes); + if (ptr == nullptr) + return nullptr; + + *static_cast (ptr) = static_cast (m); + return static_cast (ptr) + sizeof (ACE_UINT8); +} + +template +void* ACE_Cascaded_Multi_Size_Based_Allocator::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 +void* ACE_Cascaded_Multi_Size_Based_Allocator::calloc (size_t, size_t, char) +{ + ACE_NOTSUP_RETURN (0); +} + +template +void ACE_Cascaded_Multi_Size_Based_Allocator::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 +int ACE_Cascaded_Multi_Size_Based_Allocator::remove () +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::bind (const char*, void*, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::trybind (const char*, void*&) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::find (const char*, void*&) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::find (const char*) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::unbind (const char*) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::unbind (const char*, void*&) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::sync (ssize_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::sync (void*, size_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::protect (ssize_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +int ACE_Cascaded_Multi_Size_Based_Allocator::protect (void*, size_t, int) +{ + ACE_NOTSUP_RETURN (-1); +} + +template +size_t ACE_Cascaded_Multi_Size_Based_Allocator::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 +size_t ACE_Cascaded_Multi_Size_Based_Allocator::pool_sum () +{ + ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, 0)); + + size_t pool_sum = 0; + + for (size_t h = 0; h < this->hierarchy_.size (); h++) + { + pool_sum += this->hierarchy_[h]->pool_sum (); + } + + return pool_sum; +} + +template +void ACE_Cascaded_Multi_Size_Based_Allocator::dump () const +{ +#if defined(ACE_HAS_DUMP) + ACE_TRACE ("ACE_Cascaded_Dynamic_Cached_Allocator::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 ("min_initial_n_chunks_ = %u\n"), this->min_initial_n_chunks_)); + ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("chunk_size_ = %u\n"), this->chunk_size_)); + 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 void * diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 32e0ea4ed9f9f..6d0dfdc4cca33 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -310,6 +310,112 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator size_t chunk_sum_; }; +/** + * @class ACE_Cascaded_Multi_Size_Based_Allocator + * + * @brief A allocator nests a vector of various size-based allocator forming a allocator hierarchy. + * that caches blocks for quicker access, + * but with a hierarchy of cascaded, nested allocators + * + * This class enables caching of dynamically allocated, + * fixed-size chunks. Notice that the @a chunk_size + * must be greater than or equal to sizeof (void*) for + * this to work properly. + * + * Notice that when the latest allocator is empty, the allocator will create a fresh + * @a ACE_Cascaded_Dynamic_Cached_Allocator allocator again with + * init_n_chunks* the sum of current allocators as it's constructor parameter, + * so all the allocators will form a cascaded hierarchy. + + * This class can be configured flexibly with different types of + * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and @a + * ACE_Process_Mutex constructor API. + * + * @sa ACE_Cascaded_Dynamic_Cached_Allocator + */ +template +class ACE_Cascaded_Multi_Size_Based_Allocator : public ACE_Allocator +{ +public: + // Useful STL-style traits. + using comb_alloc_type = ACE_Cascaded_Dynamic_Cached_Allocator; + using comb_alloc_ptr = comb_alloc_type*; + + /// Create a cached memory pool with @a initial_n_chunks chunks + /// each with @a chunk_size size as its initial capacity. + /// When create a fresh allocator, it's constructor parameter initial_n_chunks + /// muse be greater than or equal to @a min_initial_n_chunks + ACE_Cascaded_Multi_Size_Based_Allocator (size_t initial_n_chunks, size_t chunk_size, size_t min_initial_n_chunks = 1); + + /// Clear things up. + ~ACE_Cascaded_Multi_Size_Based_Allocator (); + + /** + * Get a chunk of memory from free list cache. Note that @a nbytes is + * only checked to make sure that it's less or equal to @a chunk_size, + * and is otherwise ignored since malloc() always returns a pointer to an + * item of @a chunk_size size. + */ + virtual void* malloc (size_t nbytes); + + /** + * Get a chunk of memory from free list cache, giving them + * @a initial_value. Note that @a nbytes is only checked to make sure + * that it's less or equal to @a chunk_size, and is otherwise ignored + * since calloc() always returns a pointer to an item of @a chunk_size. + */ + virtual void* calloc (size_t nbytes, char initial_value = '\0'); + + /// This method is a no-op and just returns 0 since the free list + /// only works with fixed sized entities. + virtual void* calloc (size_t n_elem, size_t elem_size, char initial_value = '\0'); + + /// Return a chunk of memory back to free list cache. + virtual void free (void* ptr); + + /// Dump the state of this object. + virtual void dump () const; + + /// These methods are no-ops. + virtual int remove (); + virtual int bind (const char* name, void* pointer, int duplicates = 0); + virtual int trybind (const char* name, void*& pointer); + virtual int find (const char* name, void*& pointer); + virtual int find (const char* name); + virtual int unbind (const char* name); + virtual int unbind (const char* name, void*& pointer); + virtual int sync (ssize_t len = -1, int flags = MS_SYNC); + virtual int sync (void* addr, size_t len, int flags = MS_SYNC); + virtual int protect (ssize_t len = -1, int prot = PROT_RDWR); + virtual int protect (void* addr, size_t len, int prot = PROT_RDWR); + + /// Return the number of chunks available in the hierarchy. + size_t pool_depth (); + + /// Return the sum of chunks including used and freed in the hierarchy. + size_t pool_sum (); + + /// Returns a reference to the lock used to provide mutual exclusion to + /// the allocator hierarchy. + ACE_LOCK& mutex (); + +private: + /// Synchronization variable for API. + ACE_LOCK mutex_; + + /// Remember how we allocate the memory so we can clear things up later. + std::vector hierarchy_; + + /// Remember the size of initial n_chunks for creating fresh allocator in future. + const size_t initial_n_chunks_; + + /// Remember the min size of initial n_chunks for creating fresh allocator in future. + const size_t min_initial_n_chunks_; + + /// Remember the size of our chunks for creating fresh allocator in future. + const size_t chunk_size_; +}; + /** * @class ACE_Allocator_Adapter * diff --git a/ACE/ace/Malloc_T.inl b/ACE/ace/Malloc_T.inl index e9e4c49054f10..a2f71a64de8fa 100644 --- a/ACE/ace/Malloc_T.inl +++ b/ACE/ace/Malloc_T.inl @@ -52,6 +52,12 @@ ACE_Cascaded_Dynamic_Cached_Allocator::mutex () return this->mutex_; } +template +ACE_INLINE ACE_LOCK& ACE_Cascaded_Multi_Size_Based_Allocator::mutex () +{ + return this->mutex_; +} + template ACE_INLINE int ACE_Malloc_T::ref_counter () { diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 154a544a2fea1..7ca848a603faa 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -77,6 +77,64 @@ run_free_lock_cascaded_allocator_test () return 0; } +static int +run_free_lock_cascaded_multi_size_based_allocator_test () +{ + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); + + const size_t initial_n_chunks = 2; + const size_t chunk_size = sizeof (void*); + + void *ptr, *ptr1, *ptr2; + size_t nbytes = chunk_size; + size_t chunk_sum, old_chunk_sum; + char initial_value = '\0'; + + ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, sizeof (void*)); + chunk_sum = alloc.pool_sum (); + ACE_TEST_EXCEPTION_RETURN (chunk_sum != initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); + + ACE_DEBUG ((LM_INFO, "%C will test unsupported API ...\n", __func__)); + ptr = alloc.calloc (1, sizeof (void*), initial_value); + ACE_TEST_EXCEPTION_RETURN ( + ptr != nullptr, " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, + " initial pool depth must keep unchanged for call of unsupported API\n"); + + ptr = alloc.malloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 0, " initial pool depth must be zero\n"); + alloc.free (ptr); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, + " initial pool depth must be initial_n_chunks after free\n"); + + ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); + ptr = alloc.malloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, + " pool must return valid ptr, cascaded pool must support to alloc more times firstly\n"); + + ptr1 = alloc.malloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (ptr1 == nullptr, + " pool must return valid ptr, cascaded pool must support to alloc more times secondly\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must support to alloc twice\n"); + + old_chunk_sum = chunk_sum; + chunk_sum = alloc.pool_sum (); + ACE_TEST_EXCEPTION_RETURN (chunk_sum < old_chunk_sum, " cascaded pool sum must be bigger than that of initial pool\n"); + ACE_TEST_EXCEPTION_RETURN (chunk_sum != (3 * initial_n_chunks), + " cascaded pool sum must be as expected, pool has been enlarged\n"); + + ptr2 = alloc.calloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (*static_cast (ptr2) != 0, " calloc call will clear the memory to zero\n"); + + alloc.free (ptr); + alloc.free (ptr1); + alloc.free (ptr2); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + (2 * initial_n_chunks)), + " cascaded pool depth must be three after having freed all malloc ptrs\n"); + + return 0; +} + int run_main (int, ACE_TCHAR *[]) { From 4c27ab08e2781f2210a69a21b1be7c3fb16f3591 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 23 Sep 2023 11:31:36 +0800 Subject: [PATCH 26/72] =?UTF-8?q?1=E3=80=81fix=20constructor=20description?= =?UTF-8?q?=202=E3=80=81modify=20the=20test=20case=20to=20use=20lock=20str?= =?UTF-8?q?ategy,=20that=20will=20more=20likely=20find=20some=20compiling?= =?UTF-8?q?=20error=20than=20free=20lock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.h | 8 ++++---- ACE/tests/Allocator_Cascaded_Test.cpp | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 32e0ea4ed9f9f..64efc7ea4849c 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -210,7 +210,7 @@ class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator /** * @class ACE_Cascaded_Dynamic_Cached_Allocator * - * @brief A fixed-size-based allocator that caches blocks for quicker access, + * @brief A size-based allocator that caches blocks for quicker access, * but with a hierarchy of cascaded, nested allocators * * This class enables caching of dynamically allocated, @@ -224,8 +224,8 @@ class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator * so all the allocators will form a cascaded hierarchy. * This class can be configured flexibly with different types of - * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and @a - * ACE_Process_Mutex constructor API. + * ACE_LOCK strategies that support the @a ACE_Thread_Mutex and + * @a ACE_Process_Mutex constructor API. * * @sa ACE_Dynamic_Cached_Allocator */ @@ -237,7 +237,7 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator using comb_alloc_type = ACE_Dynamic_Cached_Allocator; using comb_alloc_ptr = comb_alloc_type*; - /// Create a cached memory pool with @a n_chunks chunks + /// Create a cached memory pool with @a initial_n_chunks chunks /// each with @a chunk_size size. ACE_Cascaded_Dynamic_Cached_Allocator (size_t initial_n_chunks, size_t chunk_size); diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 154a544a2fea1..52aa23d31333c 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -37,13 +37,14 @@ run_free_lock_cascaded_allocator_test () void *ptr, *ptr1, *ptr2; size_t nbytes = chunk_size; size_t chunk_sum, old_chunk_sum; + char initial_value = '\0'; - ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); + ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); chunk_sum = alloc.pool_sum (); ACE_TEST_EXCEPTION_RETURN (chunk_sum != initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); ACE_DEBUG ((LM_INFO, "%C will test unsupported API ...\n", __func__)); - ptr = alloc.calloc (1, sizeof(void*)); + ptr = alloc.calloc (1, sizeof (void*), initial_value); ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, " initial pool depth must keep unchanged for call of unsupported API\n"); From 66eb2543a75d76d2cd61addd29acf0006b265665 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 23 Sep 2023 14:33:00 +0800 Subject: [PATCH 27/72] ACE_NEW* macro function should be split by empty line --- ACE/ace/Malloc_T.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index f1f0d42cc7b83..84c84f0a5b8ab 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -385,6 +385,7 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based 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_)); + this->hierarchy_.push_back (tmp); } @@ -405,22 +406,23 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) // Check if size requested fits within pre-determined size. if (nbytes > this->chunk_size_) return nullptr; - + + // Will be assigned by lately Binary Search process. + size_t chunk_size; + // Use Binary Search to find minimal pos that value is bigger than nbytes. size_t m = 0; size_t l = 0; - size_t h = 0; - - // Must has initial value When hierarchy_ is empty - size_t chunk_size = this->chunk_size_; + size_t h = this->hierarchy_.size(); while (l <= h) { m = (l + h) / 2; chunk_size = this->chunk_size_ << m; + if (chunk_size >= nbytes) { - // End loop + // End loop. if (m == 0) break; @@ -432,7 +434,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) } } - // Not in hierarchy or less than nbytes when search + // Not in hierarchy or less than nbytes when Binary Search. while (chunk_size < nbytes) { chunk_size <<= 1; @@ -444,7 +446,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); - if (m < this->hierarchy_.size () && this->hierarchy_[m] != nullptr) + if (m < this->hierarchy_.size() && this->hierarchy_[m] != nullptr) { void* ptr = this->hierarchy_[m]->malloc(nbytes); if (ptr == nullptr) From 8210ac09d94843273a9c95763652e8aead3a9807 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 23 Sep 2023 14:42:36 +0800 Subject: [PATCH 28/72] =?UTF-8?q?1=E3=80=81split=20ACE=5FNEW*=20with=20emp?= =?UTF-8?q?ty=20line,=20the=20macro=20function=20may=20end=20the=20flow=20?= =?UTF-8?q?2=E3=80=81fix=20some=20code=20style?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/MPC | 1 + ACE/ace/Malloc_T.cpp | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 160000 ACE/MPC diff --git a/ACE/MPC b/ACE/MPC new file mode 160000 index 0000000000000..307f1fc30267d --- /dev/null +++ b/ACE/MPC @@ -0,0 +1 @@ +Subproject commit 307f1fc30267dde08c28d5665716530ce7ff0abd diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index c5cd60160a2e5..e2b36c446fd4e 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -189,8 +189,9 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All 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_)); - // Increase the chunk sum. - this->chunk_sum_ += tmp->pool_depth (); + + // Increase the chunk sum if succeed. + this->chunk_sum_ += tmp->pool_depth(); this->hierarchy_.push_back(tmp); } @@ -227,8 +228,9 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) { comb_alloc_ptr tmp; ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * this->hierarchy_.size(), this->chunk_size_), nullptr); - // Increase the chunk sum. - this->chunk_sum_ += tmp->pool_depth (); + + // Increase the chunk sum if succeed. + this->chunk_sum_ += tmp->pool_depth(); this->hierarchy_.push_back(tmp); ptr = tmp->malloc(nbytes); } @@ -263,10 +265,10 @@ ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) { ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); - ACE_ASSERT (this->hierarchy_.size () > 0); + 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) + if (ptr != nullptr && this->hierarchy_.size() > 0) this->hierarchy_[0]->free(ptr); } @@ -343,9 +345,9 @@ ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () size_t pool_depth = 0; - for (size_t h = 0; h < this->hierarchy_.size (); h++) + for (size_t h = 0; h < this->hierarchy_.size(); h++) { - pool_depth += this->hierarchy_[h]->pool_depth (); + pool_depth += this->hierarchy_[h]->pool_depth(); } return pool_depth; @@ -361,11 +363,11 @@ ACE_Cascaded_Dynamic_Cached_Allocator::dump () const 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 ())); + ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("hierarchy_ size = %u\n"), this->hierarchy_.size())); - for (size_t h = 0; h < this->hierarchy_.size (); h++) + for (size_t h = 0; h < this->hierarchy_.size(); h++) { - this->hierarchy_[h]->dump (); + this->hierarchy_[h]->dump(); ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); } From 8c1ee7704fbc64dc0408108687f6e705915cd4ef Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 23 Sep 2023 18:11:16 +0800 Subject: [PATCH 29/72] add Test Case --- ACE/ace/Malloc_T.cpp | 24 ++-- ACE/tests/Allocator_Cascaded_Test.cpp | 187 +++++++++++++++++++++----- 2 files changed, 166 insertions(+), 45 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 3872aac7aba71..fd57175075336 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -396,7 +396,8 @@ ACE_Cascaded_Multi_Size_Based_Allocator::~ACE_Cascaded_Multi_Size_Base { for (size_t h = 0; h < this->hierarchy_.size (); h++) { - delete this->hierarchy_[h]; + if (this->hierarchy_[h] != nullptr) + delete this->hierarchy_[h]; } this->hierarchy_.clear (); @@ -405,10 +406,6 @@ ACE_Cascaded_Multi_Size_Based_Allocator::~ACE_Cascaded_Multi_Size_Base template void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) { - // Check if size requested fits within pre-determined size. - if (nbytes > this->chunk_size_) - return nullptr; - // Will be assigned by lately Binary Search process. size_t chunk_size; @@ -509,9 +506,14 @@ void ACE_Cascaded_Multi_Size_Based_Allocator::free (void* ptr) 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); + if (ptr != nullptr) + { + void* const hdr_ptr = static_cast (ptr) - sizeof (ACE_UINT8); + const size_t h = *static_cast (hdr_ptr); + + if (h < this->hierarchy_.size () && this->hierarchy_[h] != nullptr) + this->hierarchy_[h]->free (ptr); + } } template @@ -589,7 +591,8 @@ size_t ACE_Cascaded_Multi_Size_Based_Allocator::pool_depth () for (size_t h = 0; h < this->hierarchy_.size (); h++) { - pool_depth += this->hierarchy_[h]->pool_depth (); + if (this->hierarchy_[h] != nullptr) + pool_depth += this->hierarchy_[h]->pool_depth (); } return pool_depth; @@ -604,7 +607,8 @@ size_t ACE_Cascaded_Multi_Size_Based_Allocator::pool_sum () for (size_t h = 0; h < this->hierarchy_.size (); h++) { - pool_sum += this->hierarchy_[h]->pool_sum (); + if (this->hierarchy_[h] != nullptr) + pool_sum += this->hierarchy_[h]->pool_sum (); } return pool_sum; diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index a5eaff1e2b4b0..7cb99de29bd84 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -15,6 +15,7 @@ #include "ace/Malloc_T.h" #include "test_config.h" +#include #define ACE_TEST_EXCEPTION_RETURN(expression, message) \ do \ @@ -26,6 +27,19 @@ do \ } \ while (0) +#define ACE_ASSERT_RETURN(expression, message) \ +do \ +{ \ + if (!(expression)) \ + { \ + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1); \ + } \ +} \ +while (0) + +#define DELTA(level, initial_chunks, min_initial_chunks) \ + (initial_chunks >> level) > min_initial_chunks ? (initial_chunks >> level) : min_initial_chunks + static int run_free_lock_cascaded_allocator_test () { @@ -79,59 +93,158 @@ run_free_lock_cascaded_allocator_test () } static int -run_free_lock_cascaded_multi_size_based_allocator_test () +run_cascaded_multi_size_based_allocator_basic_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - const size_t initial_n_chunks = 2; - const size_t chunk_size = sizeof (void*); + const size_t initial_n_chunks = 11; + const size_t min_initial_n_chunks = 2; + const size_t chunk_size = sizeof (void*) + 5; + const size_t nbytes = chunk_size; + std::vector ptrs; void *ptr, *ptr1, *ptr2; - size_t nbytes = chunk_size; - size_t chunk_sum, old_chunk_sum; - char initial_value = '\0'; + size_t pool_sum, pool_depth; - ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, sizeof (void*)); - chunk_sum = alloc.pool_sum (); - ACE_TEST_EXCEPTION_RETURN (chunk_sum != initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); + + const char initial_value = '\0'; + + ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); + pool_sum = alloc.pool_sum (); + ACE_ASSERT_RETURN (pool_sum == initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); ACE_DEBUG ((LM_INFO, "%C will test unsupported API ...\n", __func__)); ptr = alloc.calloc (1, sizeof (void*), initial_value); - ACE_TEST_EXCEPTION_RETURN ( - ptr != nullptr, " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, + ACE_ASSERT_RETURN (ptr == nullptr, + " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); + + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth == initial_n_chunks, " initial pool depth must keep unchanged for call of unsupported API\n"); ptr = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 0, " initial pool depth must be zero\n"); + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth == (initial_n_chunks - 1), + " initial pool depth must decrease by one\n"); alloc.free (ptr); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, - " initial pool depth must be initial_n_chunks after free\n"); + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth == initial_n_chunks, + " initial pool depth must restore to initial_n_chunks after free\n"); - ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C will test first level cascaded allocator ...\n", __func__)); + + for (size_t i = 0; i < (2 * initial_n_chunks); ++i) + { + ptr = alloc.malloc (nbytes); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr, cascaded pool must support to alloc more times firstly\n"); + ptrs.push_back (ptr); + } + + pool_sum = alloc.pool_sum (); + ACE_ASSERT_RETURN (pool_sum == (3 * initial_n_chunks), + " cascaded pool only has two levels, so the pool sum must be 3*initial_n_chunks after alloced 2 * initial_n_chunks times\n"); + + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth == (initial_n_chunks), + " cascaded pool only has two levels, so the pool depth must be initial_n_chunks after alloced 2 * initial_n_chunks times\n"); + + for (size_t i = 0; i < ptrs.size(); ++i) + { + alloc.free (ptrs[i]); + } + + pool_sum = alloc.pool_sum (); + ACE_ASSERT_RETURN (pool_sum == (3 * initial_n_chunks), + " first cascaded allocator only has two levels, so the pool sum must be 3*initial_n_chunks after all freed\n"); + + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth == (3 * initial_n_chunks), + " first cascaded allocator only has two levels, so the pool depth must be initial_n_chunks after all freed\n"); + + return 0; +} + +static int +run_cascaded_multi_size_based_allocator_hierarchy_test () +{ + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); + + const size_t initial_n_chunks = 11; + const size_t min_initial_n_chunks = 2; + const size_t chunk_size = sizeof (void*) + 5; + + void *ptr; + size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; + size_t level = 0, delta; + size_t nbytes = chunk_size; + + + ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); + ACE_DEBUG ((LM_INFO, "%C Only test the basic malloc API ...\n", __func__)); ptr = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, - " pool must return valid ptr, cascaded pool must support to alloc more times firstly\n"); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr when requesting initial chunk_size\n"); + alloc.free (ptr); - ptr1 = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (ptr1 == nullptr, - " pool must return valid ptr, cascaded pool must support to alloc more times secondly\n"); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must support to alloc twice\n"); + ACE_DEBUG ((LM_INFO, "%C Will trigger the creation of nested allocator on next level ...\n", __func__)); + level = 1; + old_pool_sum = alloc.pool_sum (); + old_pool_depth = alloc.pool_depth (); + nbytes = chunk_size << level; + ptr = alloc.malloc (nbytes ); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr when requesting 2 * chunk_size\n"); - old_chunk_sum = chunk_sum; - chunk_sum = alloc.pool_sum (); - ACE_TEST_EXCEPTION_RETURN (chunk_sum < old_chunk_sum, " cascaded pool sum must be bigger than that of initial pool\n"); - ACE_TEST_EXCEPTION_RETURN (chunk_sum != (3 * initial_n_chunks), - " cascaded pool sum must be as expected, pool has been enlarged\n"); + pool_sum = alloc.pool_sum (); + delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); + ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), + " pool sum must increase as delta\n"); + + alloc.free (ptr); + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), + " pool depth must increase as delta\n"); - ptr2 = alloc.calloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (*static_cast (ptr2) != 0, " calloc call will clear the memory to zero\n"); + + ACE_DEBUG ((LM_INFO, "%C Will trigger the creation of allocator on more lowwer level ...\n", __func__)); + level = 11; + old_pool_sum = alloc.pool_sum (); + old_pool_depth = alloc.pool_depth (); + nbytes = chunk_size << level; + ptr = alloc.malloc (nbytes); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr when requesting chunk_size << 11\n"); + + pool_sum = alloc.pool_sum (); + delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); + ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), + " pool sum must increase as delta only created request level\n"); alloc.free (ptr); - alloc.free (ptr1); - alloc.free (ptr2); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + (2 * initial_n_chunks)), - " cascaded pool depth must be three after having freed all malloc ptrs\n"); + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), + " pool depth must increase as delta only created request level\n"); + + for (size_t i = 2; i < level; ++i) + { + old_pool_sum = alloc.pool_sum (); + old_pool_depth = alloc.pool_depth (); + nbytes = chunk_size << i; + ptr = alloc.malloc (nbytes); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr when requesting chunk_size << 11\n"); + + pool_sum = alloc.pool_sum (); + delta = DELTA (i, initial_n_chunks, min_initial_n_chunks); + ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), + " pool sum must increase as delta only created request level\n"); + + alloc.free (ptr); + pool_depth = alloc.pool_depth (); + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), + " pool depth must increase as delta only created request level\n"); + } return 0; } @@ -143,8 +256,12 @@ run_main (int, ACE_TCHAR *[]) int retval = 0; - // Run the tests for each type of ordering. - retval = run_free_lock_cascaded_allocator_test (); + ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Allocator ...\n", __func__)); + retval = run_free_lock_cascaded_allocator_test(); + + ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Multi_Size_Based_Allocator ...\n", __func__)); + retval = run_cascaded_multi_size_based_allocator_basic_test(); + retval = run_cascaded_multi_size_based_allocator_hierarchy_test (); ACE_END_TEST; From d8626e9de6234c5f0415ad3a571153c50b7c40f4 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sat, 23 Sep 2023 21:25:44 +0800 Subject: [PATCH 30/72] =?UTF-8?q?add=20Test=20Case=201=E3=80=81basic=202?= =?UTF-8?q?=E3=80=81differential=203=E3=80=81sum&depth=20increase&decrease?= =?UTF-8?q?d=20as=20design?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.cpp | 4 +- ACE/tests/Allocator_Cascaded_Test.cpp | 215 ++++++++++++++++++++++++-- 2 files changed, 201 insertions(+), 18 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index fd57175075336..e186498068085 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -379,8 +379,8 @@ template ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based_Allocator (size_t initial_n_chunks, size_t chunk_size, size_t min_initial_n_chunks) : initial_n_chunks_ (initial_n_chunks), - chunk_size_ (chunk_size), - min_initial_n_chunks_ (min_initial_n_chunks) + min_initial_n_chunks_ (min_initial_n_chunks), + chunk_size_ (chunk_size) { ACE_ASSERT (this->chunk_size_ > 0); diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 7cb99de29bd84..078cee3852f75 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -16,6 +16,8 @@ #include "ace/Malloc_T.h" #include "test_config.h" #include +#include +#include #define ACE_TEST_EXCEPTION_RETURN(expression, message) \ do \ @@ -27,7 +29,7 @@ do \ } \ while (0) -#define ACE_ASSERT_RETURN(expression, message) \ +#define ACE_ASSERT_RETURN(expression, message) \ do \ { \ if (!(expression)) \ @@ -37,8 +39,8 @@ do \ } \ while (0) -#define DELTA(level, initial_chunks, min_initial_chunks) \ - (initial_chunks >> level) > min_initial_chunks ? (initial_chunks >> level) : min_initial_chunks +#define DELTA(level, initial_n_chunks, min_initial_n_chunks) \ + (initial_n_chunks >> level) > min_initial_n_chunks ? (initial_n_chunks >> level) : min_initial_n_chunks static int run_free_lock_cascaded_allocator_test () @@ -103,7 +105,7 @@ run_cascaded_multi_size_based_allocator_basic_test () const size_t nbytes = chunk_size; std::vector ptrs; - void *ptr, *ptr1, *ptr2; + void *ptr; size_t pool_sum, pool_depth; @@ -156,11 +158,11 @@ run_cascaded_multi_size_based_allocator_basic_test () pool_sum = alloc.pool_sum (); ACE_ASSERT_RETURN (pool_sum == (3 * initial_n_chunks), - " first cascaded allocator only has two levels, so the pool sum must be 3*initial_n_chunks after all freed\n"); + " first size-based cascaded allocator only has two levels, so the pool sum must be 3*initial_n_chunks after all freed\n"); pool_depth = alloc.pool_depth (); ACE_ASSERT_RETURN (pool_depth == (3 * initial_n_chunks), - " first cascaded allocator only has two levels, so the pool depth must be initial_n_chunks after all freed\n"); + " first size-based cascaded allocator only has two levels, so the pool depth must be initial_n_chunks after all freed\n"); return 0; } @@ -206,7 +208,6 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), " pool depth must increase as delta\n"); - ACE_DEBUG ((LM_INFO, "%C Will trigger the creation of allocator on more lowwer level ...\n", __func__)); level = 11; old_pool_sum = alloc.pool_sum (); @@ -228,22 +229,202 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () for (size_t i = 2; i < level; ++i) { + std::stringstream ss; old_pool_sum = alloc.pool_sum (); old_pool_depth = alloc.pool_depth (); nbytes = chunk_size << i; ptr = alloc.malloc (nbytes); - ACE_ASSERT_RETURN (ptr != nullptr, - " pool must return valid ptr when requesting chunk_size << 11\n"); + ss << " pool must return valid ptr when requesting chunk_size: << " << nbytes << std::endl; + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); + ss.str (""); pool_sum = alloc.pool_sum (); delta = DELTA (i, initial_n_chunks, min_initial_n_chunks); - ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), - " pool sum must increase as delta only created request level\n"); + ss << " pool sum must increase as delta: " << delta << " because only created request level: " << i << std::endl; + ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), ss.str ().c_str ()); + + ss.str (""); + alloc.free (ptr); + pool_depth = alloc.pool_depth (); + ss << " pool depth must increase as delta: " << delta << " because only created request level: " << i << std::endl; + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), ss.str ().c_str ()); + } + + return 0; +} + +static int +run_cascaded_multi_size_based_allocator_hierarchy_free_test () +{ + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); + + const size_t initial_n_chunks = 11; + const size_t min_initial_n_chunks = 2; + const size_t chunk_size = sizeof (void*) + 5; + + void* ptr; + size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; + size_t level = 3, delta; + size_t nbytes = chunk_size << level; + + std::stringstream ss; + + ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); + ACE_DEBUG ((LM_INFO, "%C Only test the basic malloc API ...\n", __func__)); + ptr = alloc.malloc (nbytes); + ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << std::endl; + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); + alloc.free (ptr); + + + for (size_t i = 3; i < 6; ++i) + { + level = i; + ACE_DEBUG ((LM_INFO, "%C test level: %u size-based cascaded allocator ...\n", __func__, level)); + nbytes = chunk_size << level; + ptr = alloc.malloc (nbytes); + ss.str (""); + ss << " level: " << level + << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes + << std::endl; + ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + alloc.free (ptr); + + ACE_DEBUG ((LM_INFO, "%C test free pos API for level: %u ...\n", __func__, level)); + old_pool_depth = alloc.pool_depth (); + old_pool_sum = alloc.pool_sum (); + std::vector ptrs; + delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); + for (size_t j = 0; j < delta; ++j) + { + ptr = alloc.malloc (nbytes); + ss.str (""); + ss << " level: " << level + << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes + << " at loop: " << j << std::endl; + ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + ptrs.push_back (ptr); + } + + for (size_t k = 0; k < ptrs.size (); ++k) + alloc.free (ptrs[k]); + + ptrs.clear (); + + ACE_DEBUG ((LM_INFO, "%C test level: %u size-based cascaded allocator has freed all initial chunks, when alloc again, the pool sum must not changed ...\n", __func__, level)); + ptr = alloc.malloc (nbytes); + pool_depth = alloc.pool_depth (); + pool_sum = alloc.pool_sum (); + ss.str (""); + ss << " level: " << level << " pool depth: " << old_pool_depth <<" must keep unchanged" << std::endl; + ACE_ASSERT_RETURN (old_pool_depth == (pool_depth + 1), ss.str ().c_str ()); + + ss.str (""); + ss << " level: " << level << " pool sum: " << old_pool_sum << " must keep unchanged" << std::endl; + ACE_ASSERT_RETURN (old_pool_sum == pool_sum, ss.str ().c_str ()); + alloc.free (ptr); + } + + return 0; +} +static int +run_cascaded_multi_size_based_allocator_hierarchy_differential_test () +{ + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); + + const size_t initial_n_chunks = 11; + const size_t min_initial_n_chunks = 2; + const size_t chunk_size = sizeof (void*) + 5; + + void* ptr; + size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; + size_t level = 3, delta; + size_t nbytes = chunk_size << level; + + std::stringstream ss; + + ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); + ACE_DEBUG ((LM_INFO, "%C Only test the hierarchy differential ...\n", __func__)); + + for (size_t i = 3; i < 6; ++i) + { + level = i; + old_pool_depth = alloc.pool_depth (); + old_pool_sum = alloc.pool_sum (); + + ACE_DEBUG ((LM_INFO, "%C test level: %u size-based cascaded allocator ...\n", __func__, level)); + nbytes = chunk_size << level; + ptr = alloc.malloc (nbytes); + ss.str (""); + ss << " level: " << level + << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes + << std::endl; + ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + + if (i == 3) + { + pool_depth = alloc.pool_depth (); + pool_sum = alloc.pool_sum (); + delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); + + ss.str (""); + ss << " level: " << (level) << " must be created, pool depth must increased by " << delta << std::endl; + ACE_ASSERT_RETURN ((old_pool_depth + delta - 1) == pool_depth, ss.str ().c_str ()); + + ss.str (""); + ss << " level: " << (level) << " must be created, pool sum must increased by " << delta << std::endl; + ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ss.str ().c_str ()); + alloc.free (ptr); + } + + ACE_DEBUG ((LM_INFO, "%C test alloc bytes greater than level: %u ...\n", __func__, level)); + old_pool_depth = alloc.pool_depth (); + old_pool_sum = alloc.pool_sum (); + + const size_t next_level = level + 1; + delta = DELTA (next_level, initial_n_chunks, min_initial_n_chunks); + ptr = alloc.malloc (nbytes + 1); alloc.free (ptr); pool_depth = alloc.pool_depth (); - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), - " pool depth must increase as delta only created request level\n"); + pool_sum = alloc.pool_sum (); + + ss.str (""); + ss << " next level: " << next_level << " must be created, pool depth must increased by " << delta << std::endl; + ACE_ASSERT_RETURN ((old_pool_depth + delta) == pool_depth, ss.str ().c_str ()); + + ss.str (""); + ss << " next level: " << next_level << " must be created, pool sum must increased by " << delta << std::endl; + ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ss.str ().c_str ()); + + const size_t next_nbytes = chunk_size << next_level; + old_pool_depth = alloc.pool_depth (); + old_pool_sum = alloc.pool_sum (); + std::vector ptrs; + for (size_t j = nbytes + 1; j < next_nbytes; ++j) + { + ptr = alloc.malloc (j); + ss.str (""); + ss << " level: " << next_level + << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << j + << std::endl; + ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + ptrs.push_back (ptr); + } + + for (size_t k = 0; k < ptrs.size(); ++k) + { + alloc.free (ptrs[k]); + } + ptrs.clear (); + + ss.str (""); + ss << " next level: " << next_level << " pool depth must unchanged" << std::endl; + ACE_ASSERT_RETURN ((old_pool_depth) == pool_depth, ss.str ().c_str ()); + + ss.str (""); + ss << " next level: " << next_level << " pool sum must unchanged" << std::endl; + ACE_ASSERT_RETURN ((old_pool_sum) == pool_sum, ss.str ().c_str ()); } return 0; @@ -257,11 +438,13 @@ run_main (int, ACE_TCHAR *[]) int retval = 0; ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Allocator ...\n", __func__)); - retval = run_free_lock_cascaded_allocator_test(); + retval += run_free_lock_cascaded_allocator_test(); ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Multi_Size_Based_Allocator ...\n", __func__)); - retval = run_cascaded_multi_size_based_allocator_basic_test(); - retval = run_cascaded_multi_size_based_allocator_hierarchy_test (); + retval += run_cascaded_multi_size_based_allocator_basic_test(); + retval += run_cascaded_multi_size_based_allocator_hierarchy_test (); + retval += run_cascaded_multi_size_based_allocator_hierarchy_free_test (); + retval += run_cascaded_multi_size_based_allocator_hierarchy_differential_test (); ACE_END_TEST; From ec8de03fda313b9e985259bc17d3c86d66942758 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sat, 23 Sep 2023 21:42:46 +0800 Subject: [PATCH 31/72] remove trailing whitespace --- ACE/ace/Malloc_T.cpp | 4 ++-- ACE/tests/Allocator_Cascaded_Test.cpp | 28 +++++++++++++-------------- 2 files changed, 15 insertions(+), 17 deletions(-) mode change 100644 => 100755 ACE/ace/Malloc_T.cpp mode change 100644 => 100755 ACE/tests/Allocator_Cascaded_Test.cpp diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp old mode 100644 new mode 100755 index e186498068085..b4dbdaf2e8bfe --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -463,7 +463,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; comb_alloc_ptr newly_alloc; - ACE_NEW_RETURN (newly_alloc, + ACE_NEW_RETURN (newly_alloc, comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, chunk_size + sizeof(ACE_UINT8)), nullptr @@ -506,7 +506,7 @@ void ACE_Cascaded_Multi_Size_Based_Allocator::free (void* ptr) ACE_ASSERT (this->hierarchy_.size () > 0); - if (ptr != nullptr) + if (ptr != nullptr) { void* const hdr_ptr = static_cast (ptr) - sizeof (ACE_UINT8); const size_t h = *static_cast (hdr_ptr); diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp old mode 100644 new mode 100755 index 078cee3852f75..6a29cd3f4339e --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -108,7 +108,6 @@ run_cascaded_multi_size_based_allocator_basic_test () void *ptr; size_t pool_sum, pool_depth; - const char initial_value = '\0'; ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); @@ -117,7 +116,7 @@ run_cascaded_multi_size_based_allocator_basic_test () ACE_DEBUG ((LM_INFO, "%C will test unsupported API ...\n", __func__)); ptr = alloc.calloc (1, sizeof (void*), initial_value); - ACE_ASSERT_RETURN (ptr == nullptr, + ACE_ASSERT_RETURN (ptr == nullptr, " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); pool_depth = alloc.pool_depth (); @@ -126,7 +125,7 @@ run_cascaded_multi_size_based_allocator_basic_test () ptr = alloc.malloc (nbytes); pool_depth = alloc.pool_depth (); - ACE_ASSERT_RETURN (pool_depth == (initial_n_chunks - 1), + ACE_ASSERT_RETURN (pool_depth == (initial_n_chunks - 1), " initial pool depth must decrease by one\n"); alloc.free (ptr); pool_depth = alloc.pool_depth (); @@ -135,7 +134,7 @@ run_cascaded_multi_size_based_allocator_basic_test () ACE_DEBUG ((LM_INFO, "%C will test first level cascaded allocator ...\n", __func__)); - for (size_t i = 0; i < (2 * initial_n_chunks); ++i) + for (size_t i = 0; i < (2 * initial_n_chunks); ++i) { ptr = alloc.malloc (nbytes); ACE_ASSERT_RETURN (ptr != nullptr, @@ -150,7 +149,7 @@ run_cascaded_multi_size_based_allocator_basic_test () pool_depth = alloc.pool_depth (); ACE_ASSERT_RETURN (pool_depth == (initial_n_chunks), " cascaded pool only has two levels, so the pool depth must be initial_n_chunks after alloced 2 * initial_n_chunks times\n"); - + for (size_t i = 0; i < ptrs.size(); ++i) { alloc.free (ptrs[i]); @@ -200,12 +199,12 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () pool_sum = alloc.pool_sum (); delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); - ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), + ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), " pool sum must increase as delta\n"); - + alloc.free (ptr); pool_depth = alloc.pool_depth (); - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), " pool depth must increase as delta\n"); ACE_DEBUG ((LM_INFO, "%C Will trigger the creation of allocator on more lowwer level ...\n", __func__)); @@ -219,15 +218,15 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () pool_sum = alloc.pool_sum (); delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); - ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), + ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), " pool sum must increase as delta only created request level\n"); alloc.free (ptr); pool_depth = alloc.pool_depth (); - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), " pool depth must increase as delta only created request level\n"); - for (size_t i = 2; i < level; ++i) + for (size_t i = 2; i < level; ++i) { std::stringstream ss; old_pool_sum = alloc.pool_sum (); @@ -247,7 +246,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () alloc.free (ptr); pool_depth = alloc.pool_depth (); ss << " pool depth must increase as delta: " << delta << " because only created request level: " << i << std::endl; - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), ss.str ().c_str ()); + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), ss.str ().c_str ()); } return 0; @@ -276,8 +275,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); alloc.free (ptr); - - for (size_t i = 3; i < 6; ++i) + for (size_t i = 3; i < 6; ++i) { level = i; ACE_DEBUG ((LM_INFO, "%C test level: %u size-based cascaded allocator ...\n", __func__, level)); @@ -421,7 +419,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss.str (""); ss << " next level: " << next_level << " pool depth must unchanged" << std::endl; ACE_ASSERT_RETURN ((old_pool_depth) == pool_depth, ss.str ().c_str ()); - + ss.str (""); ss << " next level: " << next_level << " pool sum must unchanged" << std::endl; ACE_ASSERT_RETURN ((old_pool_sum) == pool_sum, ss.str ().c_str ()); From 59e5c7ae8411d42d08f864794d32a1ce050393a1 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sat, 23 Sep 2023 22:43:49 +0800 Subject: [PATCH 32/72] add class description --- ACE/ace/Malloc_T.h | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) mode change 100644 => 100755 ACE/ace/Malloc_T.h diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h old mode 100644 new mode 100755 index 280b5f3d5eb05..61fb43c4c2cb0 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -313,18 +313,18 @@ class ACE_Cascaded_Dynamic_Cached_Allocator : public ACE_Allocator /** * @class ACE_Cascaded_Multi_Size_Based_Allocator * - * @brief A allocator nests a vector of various size-based allocator forming a allocator hierarchy. - * that caches blocks for quicker access, - * but with a hierarchy of cascaded, nested allocators + * @brief A allocator nests a vector of various size-based allocators using @a ACE_Cascaded_Dynamic_Cached_Allocator + * forming a allocator hierarchy, so, it will has 'infinite' space as your host. + * This allocator will decrease the complicated management overhead of @a ACE_Malloc + * when it has a very long linked list, but with various, random chunk size by a cost of O(1). * - * This class enables caching of dynamically allocated, - * fixed-size chunks. Notice that the @a chunk_size - * must be greater than or equal to sizeof (void*) for + * Notice that the @a chunk_size must be greater than or equal to sizeof (void*) for * this to work properly. * - * Notice that when the latest allocator is empty, the allocator will create a fresh + * Notice when requested memory space can't be meet, the allocator will create a fresh * @a ACE_Cascaded_Dynamic_Cached_Allocator allocator again with - * init_n_chunks* the sum of current allocators as it's constructor parameter, + * init_n_chunks as its constructor parameter, but decreased according to its level order, + * chunk_size as its constructor parameter, but increased according to its level order, * so all the allocators will form a cascaded hierarchy. * This class can be configured flexibly with different types of @@ -341,11 +341,15 @@ class ACE_Cascaded_Multi_Size_Based_Allocator : public ACE_Allocator using comb_alloc_type = ACE_Cascaded_Dynamic_Cached_Allocator; using comb_alloc_ptr = comb_alloc_type*; - /// Create a cached memory pool with @a initial_n_chunks chunks - /// each with @a chunk_size size as its initial capacity. - /// When create a fresh allocator, it's constructor parameter initial_n_chunks - /// muse be greater than or equal to @a min_initial_n_chunks - ACE_Cascaded_Multi_Size_Based_Allocator (size_t initial_n_chunks, size_t chunk_size, size_t min_initial_n_chunks = 1); + /// Create a cached memory pool with @a initial_n_chunks and @a initial_chunk_size as pool starting base + /// and @a min_initial_n_chunks as a adjustable threshold. + /// When creating a fresh @a ACE_Cascaded_Dynamic_Cached_Allocator, its constructor parameter initial_n_chunks + /// will be decreased by @a initial_chunk_size is be shifted right according to its level order, + /// but initial_n_chunks must be greater than or equal to @a min_initial_n_chunks, + /// and its another constructor parameter chunk_size will be increased + /// by @a initial_chunk_size is be shifted left according to its level order e.g. greater than previous level + + ACE_Cascaded_Multi_Size_Based_Allocator (size_t initial_n_chunks, size_t initial_chunk_size, size_t min_initial_n_chunks = 1); /// Clear things up. ~ACE_Cascaded_Multi_Size_Based_Allocator (); From 1eeef7aeec7a82a9df53ee72ed4fc222f9472d71 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sun, 24 Sep 2023 08:58:03 +0800 Subject: [PATCH 33/72] =?UTF-8?q?1=E3=80=81fix=20wchar=20compiling=20error?= =?UTF-8?q?=202=E3=80=81dump=20API=20trace=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.cpp | 9 +++++--- ACE/tests/Allocator_Cascaded_Test.cpp | 32 +++++++++++++-------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index b4dbdaf2e8bfe..e332a65090af4 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -618,7 +618,7 @@ template void ACE_Cascaded_Multi_Size_Based_Allocator::dump () const { #if defined(ACE_HAS_DUMP) - ACE_TRACE ("ACE_Cascaded_Dynamic_Cached_Allocator::dump"); + ACE_TRACE ("ACE_Cascaded_Multi_Size_Based_Allocator::dump"); ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this)); ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("initial_n_chunks_ = %u\n"), this->initial_n_chunks_)); @@ -628,8 +628,11 @@ void ACE_Cascaded_Multi_Size_Based_Allocator::dump () const for (size_t h = 0; h < this->hierarchy_.size (); h++) { - this->hierarchy_[h]->dump (); - ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); + if (this->hierarchy_[h] != nullptr) + { + this->hierarchy_[h]->dump (); + ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); + } } ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP)); diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 6a29cd3f4339e..4fc146ebac39c 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -234,19 +234,19 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () nbytes = chunk_size << i; ptr = alloc.malloc (nbytes); ss << " pool must return valid ptr when requesting chunk_size: << " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); + ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ss.str (""); pool_sum = alloc.pool_sum (); delta = DELTA (i, initial_n_chunks, min_initial_n_chunks); ss << " pool sum must increase as delta: " << delta << " because only created request level: " << i << std::endl; - ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), ss.str ().c_str ()); + ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ss.str (""); alloc.free (ptr); pool_depth = alloc.pool_depth (); ss << " pool depth must increase as delta: " << delta << " because only created request level: " << i << std::endl; - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), ss.str ().c_str ()); + ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); } return 0; @@ -272,7 +272,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () ACE_DEBUG ((LM_INFO, "%C Only test the basic malloc API ...\n", __func__)); ptr = alloc.malloc (nbytes); ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); + ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); alloc.free (ptr); for (size_t i = 3; i < 6; ++i) @@ -285,7 +285,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); alloc.free (ptr); ACE_DEBUG ((LM_INFO, "%C test free pos API for level: %u ...\n", __func__, level)); @@ -300,7 +300,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << " at loop: " << j << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ptrs.push_back (ptr); } @@ -315,11 +315,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () pool_sum = alloc.pool_sum (); ss.str (""); ss << " level: " << level << " pool depth: " << old_pool_depth <<" must keep unchanged" << std::endl; - ACE_ASSERT_RETURN (old_pool_depth == (pool_depth + 1), ss.str ().c_str ()); + ACE_ASSERT_RETURN (old_pool_depth == (pool_depth + 1), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ss.str (""); ss << " level: " << level << " pool sum: " << old_pool_sum << " must keep unchanged" << std::endl; - ACE_ASSERT_RETURN (old_pool_sum == pool_sum, ss.str ().c_str ()); + ACE_ASSERT_RETURN (old_pool_sum == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); alloc.free (ptr); } @@ -358,7 +358,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); if (i == 3) { @@ -368,11 +368,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss.str (""); ss << " level: " << (level) << " must be created, pool depth must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_depth + delta - 1) == pool_depth, ss.str ().c_str ()); + ACE_ASSERT_RETURN ((old_pool_depth + delta - 1) == pool_depth, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ss.str (""); ss << " level: " << (level) << " must be created, pool sum must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ss.str ().c_str ()); + ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); alloc.free (ptr); } @@ -389,11 +389,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss.str (""); ss << " next level: " << next_level << " must be created, pool depth must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_depth + delta) == pool_depth, ss.str ().c_str ()); + ACE_ASSERT_RETURN ((old_pool_depth + delta) == pool_depth, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ss.str (""); ss << " next level: " << next_level << " must be created, pool sum must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ss.str ().c_str ()); + ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); const size_t next_nbytes = chunk_size << next_level; old_pool_depth = alloc.pool_depth (); @@ -406,7 +406,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss << " level: " << next_level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << j << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ss.str ().c_str ()); + ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ptrs.push_back (ptr); } @@ -418,11 +418,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss.str (""); ss << " next level: " << next_level << " pool depth must unchanged" << std::endl; - ACE_ASSERT_RETURN ((old_pool_depth) == pool_depth, ss.str ().c_str ()); + ACE_ASSERT_RETURN ((old_pool_depth) == pool_depth, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ss.str (""); ss << " next level: " << next_level << " pool sum must unchanged" << std::endl; - ACE_ASSERT_RETURN ((old_pool_sum) == pool_sum, ss.str ().c_str ()); + ACE_ASSERT_RETURN ((old_pool_sum) == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); } return 0; From 7d33ad168ce1c888074c3b71dd9f2e8f3ed5a56c Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sun, 24 Sep 2023 09:15:56 +0800 Subject: [PATCH 34/72] fix codacy static code Analysis issues --- ACE/tests/Allocator_Cascaded_Test.cpp | 34 +++++++++++---------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 4fc146ebac39c..33cc191a24977 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -199,12 +199,12 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () pool_sum = alloc.pool_sum (); delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); - ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), + ACE_ASSERT_RETURN (pool_sum == (old_pool_sum + delta), " pool sum must increase as delta\n"); alloc.free (ptr); pool_depth = alloc.pool_depth (); - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), + ACE_ASSERT_RETURN (pool_depth == (old_pool_depth + delta), " pool depth must increase as delta\n"); ACE_DEBUG ((LM_INFO, "%C Will trigger the creation of allocator on more lowwer level ...\n", __func__)); @@ -218,12 +218,12 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () pool_sum = alloc.pool_sum (); delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); - ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), + ACE_ASSERT_RETURN (pool_sum == (old_pool_sum + delta), " pool sum must increase as delta only created request level\n"); alloc.free (ptr); pool_depth = alloc.pool_depth (); - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), + ACE_ASSERT_RETURN (pool_depth == (old_pool_depth + delta), " pool depth must increase as delta only created request level\n"); for (size_t i = 2; i < level; ++i) @@ -240,13 +240,13 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () pool_sum = alloc.pool_sum (); delta = DELTA (i, initial_n_chunks, min_initial_n_chunks); ss << " pool sum must increase as delta: " << delta << " because only created request level: " << i << std::endl; - ACE_ASSERT_RETURN (pool_sum = (old_pool_sum + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (pool_sum == (old_pool_sum + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); ss.str (""); alloc.free (ptr); pool_depth = alloc.pool_depth (); ss << " pool depth must increase as delta: " << delta << " because only created request level: " << i << std::endl; - ACE_ASSERT_RETURN (pool_depth = (old_pool_depth + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (pool_depth == (old_pool_depth + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); } return 0; @@ -262,10 +262,8 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () const size_t chunk_size = sizeof (void*) + 5; void* ptr; - size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; - size_t level = 3, delta; + size_t level = 3; size_t nbytes = chunk_size << level; - std::stringstream ss; ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); @@ -277,6 +275,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () for (size_t i = 3; i < 6; ++i) { + size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; level = i; ACE_DEBUG ((LM_INFO, "%C test level: %u size-based cascaded allocator ...\n", __func__, level)); nbytes = chunk_size << level; @@ -292,7 +291,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () old_pool_depth = alloc.pool_depth (); old_pool_sum = alloc.pool_sum (); std::vector ptrs; - delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); + size_t delta = DELTA (level, initial_n_chunks, min_initial_n_chunks); for (size_t j = 0; j < delta; ++j) { ptr = alloc.malloc (nbytes); @@ -335,25 +334,20 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () const size_t min_initial_n_chunks = 2; const size_t chunk_size = sizeof (void*) + 5; - void* ptr; - size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; - size_t level = 3, delta; - size_t nbytes = chunk_size << level; - - std::stringstream ss; - ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); ACE_DEBUG ((LM_INFO, "%C Only test the hierarchy differential ...\n", __func__)); for (size_t i = 3; i < 6; ++i) { - level = i; + size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth, delta; + std::stringstream ss; + size_t level = i; old_pool_depth = alloc.pool_depth (); old_pool_sum = alloc.pool_sum (); ACE_DEBUG ((LM_INFO, "%C test level: %u size-based cascaded allocator ...\n", __func__, level)); - nbytes = chunk_size << level; - ptr = alloc.malloc (nbytes); + size_t nbytes = chunk_size << level; + void* ptr = alloc.malloc (nbytes); ss.str (""); ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes From 863b05634e330288974180dee44aa66831e4a068 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Mon, 25 Sep 2023 09:20:15 +0800 Subject: [PATCH 35/72] fix cor chunk_size parameter name to initial_chunk_size --- ACE/ace/Malloc_T.cpp | 20 ++++++++------------ ACE/ace/Malloc_T.h | 4 ++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index e332a65090af4..ea08da30e6afb 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -377,16 +377,16 @@ ACE_Cascaded_Dynamic_Cached_Allocator::dump () const template ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based_Allocator -(size_t initial_n_chunks, size_t chunk_size, size_t min_initial_n_chunks) +(size_t initial_n_chunks, size_t initial_chunk_size, size_t min_initial_n_chunks) : initial_n_chunks_ (initial_n_chunks), min_initial_n_chunks_ (min_initial_n_chunks), - chunk_size_ (chunk_size) + initial_chunk_size_ (initial_chunk_size) { - ACE_ASSERT (this->chunk_size_ > 0); + ACE_ASSERT (this->initial_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_)); + ACE_NEW (tmp, comb_alloc_type (this->initial_n_chunks_, this->initial_chunk_size_)); this->hierarchy_.push_back (tmp); } @@ -417,7 +417,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) while (l <= h) { m = (l + h) / 2; - chunk_size = this->chunk_size_ << m; + chunk_size = this->initial_chunk_size_ << m; if (chunk_size >= nbytes) { @@ -481,14 +481,10 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) template void* ACE_Cascaded_Multi_Size_Based_Allocator::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_); + ACE_OS::memset (ptr, initial_value, nbytes); return ptr; } @@ -496,7 +492,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::calloc (size_t nbytes, template void* ACE_Cascaded_Multi_Size_Based_Allocator::calloc (size_t, size_t, char) { - ACE_NOTSUP_RETURN (0); + ACE_NOTSUP_RETURN (nullptr); } template @@ -623,7 +619,7 @@ void ACE_Cascaded_Multi_Size_Based_Allocator::dump () const 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 ("min_initial_n_chunks_ = %u\n"), this->min_initial_n_chunks_)); - ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("chunk_size_ = %u\n"), this->chunk_size_)); + ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("chunk_size_ = %u\n"), this->initial_chunk_size_)); ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("hierarchy_ size = %u\n"), this->hierarchy_.size ())); for (size_t h = 0; h < this->hierarchy_.size (); h++) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 61fb43c4c2cb0..fc5d24ea503f2 100755 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -416,8 +416,8 @@ class ACE_Cascaded_Multi_Size_Based_Allocator : public ACE_Allocator /// Remember the min size of initial n_chunks for creating fresh allocator in future. const size_t min_initial_n_chunks_; - /// Remember the size of our chunks for creating fresh allocator in future. - const size_t chunk_size_; + /// Remember the initial size of our chunks for creating fresh allocator in future. + const size_t initial_chunk_size_; }; /** From 10558bd498fcbc692f3e07aa9fb44f41d48846b6 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Mon, 25 Sep 2023 17:01:42 +0800 Subject: [PATCH 36/72] add calloc api test --- ACE/tests/Allocator_Cascaded_Test.cpp | 42 ++++++++++++++++++++------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 33cc191a24977..ccd9da1d50d53 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -101,8 +101,8 @@ run_cascaded_multi_size_based_allocator_basic_test () const size_t initial_n_chunks = 11; const size_t min_initial_n_chunks = 2; - const size_t chunk_size = sizeof (void*) + 5; - const size_t nbytes = chunk_size; + const size_t initial_chunk_size = sizeof (void*) + 5; + const size_t nbytes = initial_chunk_size; std::vector ptrs; void *ptr; @@ -110,7 +110,7 @@ run_cascaded_multi_size_based_allocator_basic_test () const char initial_value = '\0'; - ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); + ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, initial_chunk_size, min_initial_n_chunks); pool_sum = alloc.pool_sum (); ACE_ASSERT_RETURN (pool_sum == initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); @@ -119,6 +119,14 @@ run_cascaded_multi_size_based_allocator_basic_test () ACE_ASSERT_RETURN (ptr == nullptr, " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); + ptr = alloc.calloc (nbytes, initial_value); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr for calloc(size_t nbytes, char initial_value) call\n"); + char zeros[nbytes] = {}; + ACE_ASSERT_RETURN (ACE_OS::memcmp (ptr, zeros, nbytes) == 0, + " the memory returned by calloc(size_t nbytes, char initial_value) must all be zero!\n"); + alloc.free (ptr); + pool_depth = alloc.pool_depth (); ACE_ASSERT_RETURN (pool_depth == initial_n_chunks, " initial pool depth must keep unchanged for call of unsupported API\n"); @@ -173,26 +181,40 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () const size_t initial_n_chunks = 11; const size_t min_initial_n_chunks = 2; - const size_t chunk_size = sizeof (void*) + 5; + const size_t initial_chunk_size = sizeof (void*) + 5; void *ptr; size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; size_t level = 0, delta; - size_t nbytes = chunk_size; - + size_t nbytes = initial_chunk_size; - ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); + ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, initial_chunk_size, min_initial_n_chunks); ACE_DEBUG ((LM_INFO, "%C Only test the basic malloc API ...\n", __func__)); ptr = alloc.malloc (nbytes); ACE_ASSERT_RETURN (ptr != nullptr, " pool must return valid ptr when requesting initial chunk_size\n"); alloc.free (ptr); + ACE_DEBUG ((LM_INFO, "%C Only test the basic calloc API ...\n", __func__)); + char initial_value = '\0'; + const size_t CMP_ARRAY_LEN = initial_chunk_size + 1024; + char cmpvalues[CMP_ARRAY_LEN]; + for (nbytes = initial_chunk_size; nbytes < CMP_ARRAY_LEN; ++nbytes) + { + ACE_OS::memset (cmpvalues, initial_value, CMP_ARRAY_LEN); + ptr = alloc.calloc (nbytes, initial_value); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr when callinging calloc API wiht various valid chunk_size\n"); + ACE_ASSERT_RETURN (ACE_OS::memcmp (ptr, cmpvalues, nbytes) == 0, + " pool return memory must be the same as cmpvalues when callinging calloc API wiht various valid chunk_size\n"); + alloc.free (ptr); + } + ACE_DEBUG ((LM_INFO, "%C Will trigger the creation of nested allocator on next level ...\n", __func__)); level = 1; old_pool_sum = alloc.pool_sum (); old_pool_depth = alloc.pool_depth (); - nbytes = chunk_size << level; + nbytes = initial_chunk_size << level; ptr = alloc.malloc (nbytes ); ACE_ASSERT_RETURN (ptr != nullptr, " pool must return valid ptr when requesting 2 * chunk_size\n"); @@ -211,7 +233,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () level = 11; old_pool_sum = alloc.pool_sum (); old_pool_depth = alloc.pool_depth (); - nbytes = chunk_size << level; + nbytes = initial_chunk_size << level; ptr = alloc.malloc (nbytes); ACE_ASSERT_RETURN (ptr != nullptr, " pool must return valid ptr when requesting chunk_size << 11\n"); @@ -231,7 +253,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () std::stringstream ss; old_pool_sum = alloc.pool_sum (); old_pool_depth = alloc.pool_depth (); - nbytes = chunk_size << i; + nbytes = initial_chunk_size << i; ptr = alloc.malloc (nbytes); ss << " pool must return valid ptr when requesting chunk_size: << " << nbytes << std::endl; ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); From e8d9b510067f81fe1e952d922ce1f216a6cdce16 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Mon, 25 Sep 2023 17:09:51 +0800 Subject: [PATCH 37/72] =?UTF-8?q?1=E3=80=81fix=20unsupported=20API=20retur?= =?UTF-8?q?n=20nullptr=202=E3=80=81fix=20test=20function=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.cpp | 2 +- ACE/tests/Allocator_Cascaded_Test.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index e2b36c446fd4e..1d34e4863782c 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -257,7 +257,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t nbytes, template void * ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t, size_t, char) { - ACE_NOTSUP_RETURN (0); + ACE_NOTSUP_RETURN (nullptr); } template void diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 52aa23d31333c..7ff5773941554 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -27,7 +27,7 @@ do \ while (0) static int -run_free_lock_cascaded_allocator_test () +run_cascaded_allocator_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); @@ -86,7 +86,7 @@ run_main (int, ACE_TCHAR *[]) int retval = 0; // Run the tests for each type of ordering. - retval = run_free_lock_cascaded_allocator_test (); + retval = run_cascaded_allocator_test (); ACE_END_TEST; From f94bea9ff56219590a59e223c13c33c19b1b2e38 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Mon, 25 Sep 2023 20:43:46 +0800 Subject: [PATCH 38/72] fix malloc issue --- ACE/ace/Malloc_T.cpp | 5 ++++- ACE/tests/Allocator_Cascaded_Test.cpp | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 8402559ad993d..8b241614cecb2 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -409,6 +409,9 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) // Will be assigned by lately Binary Search process. size_t chunk_size; + // Need one octet to record hierarchy pos + nbytes += sizeof (ACE_UINT8); + // Use Binary Search to find minimal pos that value is bigger than nbytes. size_t m = 0; size_t l = 0; @@ -465,7 +468,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) comb_alloc_ptr newly_alloc; ACE_NEW_RETURN (newly_alloc, comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, - chunk_size + sizeof(ACE_UINT8)), + chunk_size), nullptr ); diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 551fee28d47f8..33c601458b109 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -199,7 +199,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () char initial_value = '\0'; const size_t CMP_ARRAY_LEN = initial_chunk_size + 1024; char cmpvalues[CMP_ARRAY_LEN]; - for (nbytes = initial_chunk_size; nbytes < CMP_ARRAY_LEN; ++nbytes) + for (nbytes = initial_chunk_size; nbytes < CMP_ARRAY_LEN; ++nbytes, ++initial_value) { ACE_OS::memset (cmpvalues, initial_value, CMP_ARRAY_LEN); ptr = alloc.calloc (nbytes, initial_value); From 1ce76a3bee7087bb4b5225c53fb954ea4eb1e4e0 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Mon, 25 Sep 2023 22:41:00 +0800 Subject: [PATCH 39/72] keep each level add header delta --- ACE/ace/Malloc_T.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 8b241614cecb2..baeee17d76cdf 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -386,7 +386,7 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based 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->initial_chunk_size_)); + ACE_NEW (tmp, comb_alloc_type (this->initial_n_chunks_, this->initial_chunk_size_ + sizeof(ACE_UINT8))); this->hierarchy_.push_back (tmp); } @@ -439,6 +439,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) // Not in hierarchy or less than nbytes when Binary Search. while (chunk_size < nbytes) { + // Equal to this->initial_chunk_size_ << (m + 1) chunk_size <<= 1; ++m; } @@ -468,7 +469,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) comb_alloc_ptr newly_alloc; ACE_NEW_RETURN (newly_alloc, comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, - chunk_size), + chunk_size + sizeof(ACE_UINT8)), nullptr ); From cca031f9c5da047ce8f4e4a688d3592047ede97f Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Mon, 25 Sep 2023 23:11:10 +0800 Subject: [PATCH 40/72] fix misspell of test cpp --- ACE/tests/Allocator_Cascaded_Test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 33c601458b109..18808badf7301 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -204,9 +204,9 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () ACE_OS::memset (cmpvalues, initial_value, CMP_ARRAY_LEN); ptr = alloc.calloc (nbytes, initial_value); ACE_ASSERT_RETURN (ptr != nullptr, - " pool must return valid ptr when callinging calloc API wiht various valid chunk_size\n"); + " pool must return valid ptr when callinging calloc API with various valid chunk_size\n"); ACE_ASSERT_RETURN (ACE_OS::memcmp (ptr, cmpvalues, nbytes) == 0, - " pool return memory must be the same as cmpvalues when callinging calloc API wiht various valid chunk_size\n"); + " pool return memory must be the same as cmpvalues when callinging calloc API with various valid chunk_size\n"); alloc.free (ptr); } From 34850b636e0157ce859828c160a47982ee6493b8 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 26 Sep 2023 09:05:26 +0800 Subject: [PATCH 41/72] remove modification of nbytes += 1 --- ACE/ace/Malloc_T.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index baeee17d76cdf..d7f798011edc1 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -386,7 +386,10 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based 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->initial_chunk_size_ + sizeof(ACE_UINT8))); + // Notice: need one octet to record hierarchy pos + ACE_NEW (tmp, comb_alloc_type (this->initial_n_chunks_, + this->initial_chunk_size_ + sizeof(ACE_UINT8)) + ); this->hierarchy_.push_back (tmp); } @@ -409,9 +412,6 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) // Will be assigned by lately Binary Search process. size_t chunk_size; - // Need one octet to record hierarchy pos - nbytes += sizeof (ACE_UINT8); - // Use Binary Search to find minimal pos that value is bigger than nbytes. size_t m = 0; size_t l = 0; @@ -467,6 +467,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; comb_alloc_ptr newly_alloc; + // Notice: need one octet to record hierarchy pos ACE_NEW_RETURN (newly_alloc, comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, chunk_size + sizeof(ACE_UINT8)), From 63e543fc626720189c910d24a616e3c37571288c Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 26 Sep 2023 17:01:07 +0800 Subject: [PATCH 42/72] add deeply test case for calloc API --- ACE/tests/Allocator_Cascaded_Test.cpp | 115 +++++++++++++++++++++----- 1 file changed, 95 insertions(+), 20 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 7ff5773941554..624dd4f6d6e5a 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -15,6 +15,7 @@ #include "ace/Malloc_T.h" #include "test_config.h" +#include #define ACE_TEST_EXCEPTION_RETURN(expression, message) \ do \ @@ -32,49 +33,123 @@ run_cascaded_allocator_test () ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); const size_t initial_n_chunks = 1; - const size_t chunk_size = sizeof(void*); + const size_t chunk_size = sizeof(void*) + 7; void *ptr, *ptr1, *ptr2; size_t nbytes = chunk_size; - size_t chunk_sum, old_chunk_sum; + size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; char initial_value = '\0'; - ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, sizeof (void*)); - chunk_sum = alloc.pool_sum (); - ACE_TEST_EXCEPTION_RETURN (chunk_sum != initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); + ACE_Cascaded_Dynamic_Cached_Allocator alloc (initial_n_chunks, chunk_size); + pool_sum = alloc.pool_sum (); + ACE_TEST_EXCEPTION_RETURN (pool_sum != initial_n_chunks, " initial pool sum must be initial_n_chunks\n"); ACE_DEBUG ((LM_INFO, "%C will test unsupported API ...\n", __func__)); - ptr = alloc.calloc (1, sizeof (void*), initial_value); - ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); - ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, " initial pool depth must keep unchanged for call of unsupported API\n"); + ptr = alloc.calloc (1, chunk_size, initial_value); + ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, + " pool must return nullptr for calloc(size_t n_elem, size_t elem_size, char initial_value) call\n"); + ACE_TEST_EXCEPTION_RETURN(alloc.pool_depth() != initial_n_chunks, + " initial pool depth must keep unchanged for call of unsupported API\n"); + + ACE_DEBUG ((LM_INFO, "%C will test supported calloc API ...\n", __func__)); + ptr = alloc.calloc (nbytes + 1, initial_value); + ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, + " pool must return nullptr for calloc call with bigger nbytes parameter\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, + " initial pool depth must keep unchanged for call of unsupported API\n"); + + ptr = alloc.calloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, " pool must return valid ptr for calloc call with normal nbytes\n"); + ACE_TEST_EXCEPTION_RETURN (*static_cast (ptr) != 0, " calloc call will clear the memory to zero\n"); + alloc.free (ptr); + + ACE_DEBUG ((LM_INFO, "%C will test supported malloc API ...\n", __func__)); + ptr = alloc.malloc (nbytes + 1); + ACE_TEST_EXCEPTION_RETURN (ptr != nullptr, + " pool must return nullptr for malloc call with bigger nbytes parameter\n"); ptr = alloc.malloc(nbytes); + ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, + " pool must return valid ptr for calloc call with normal nbytes\n"); ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 0, " initial pool depth must be zero\n"); alloc.free(ptr); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, " initial pool depth must be initial_n_chunks after free\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != initial_n_chunks, + " initial pool depth must be initial_n_chunks after free\n"); ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator ...\n", __func__)); ptr = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, " pool must return valid ptr, cascaded pool must support to alloc more times firstly\n"); + ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, + " pool must return valid ptr, cascaded pool must support to alloc more times firstly\n"); ptr1 = alloc.malloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (ptr1 == nullptr, " pool must return valid ptr, cascaded pool must support to alloc more times secondly\n"); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, " cascaded pool depth must support to alloc twice\n"); - - old_chunk_sum = chunk_sum; - chunk_sum = alloc.pool_sum (); - ACE_TEST_EXCEPTION_RETURN (chunk_sum < old_chunk_sum, " cascaded pool sum must be bigger than that of initial pool\n"); - ACE_TEST_EXCEPTION_RETURN (chunk_sum != (3*initial_n_chunks), " cascaded pool sum must be as expected, pool has been enlarged\n"); - - ptr2 = alloc.calloc (nbytes); - ACE_TEST_EXCEPTION_RETURN (*static_cast(ptr2) != 0, " calloc call will clear the memory to zero\n"); + ACE_TEST_EXCEPTION_RETURN (ptr1 == nullptr, + " pool must return valid ptr, cascaded pool must support to alloc more times secondly\n"); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, + " cascaded pool depth must support to alloc twice\n"); + + old_pool_depth = alloc.pool_depth(); + ptr2 = alloc.malloc (nbytes); + ACE_TEST_EXCEPTION_RETURN (ptr2 == nullptr, + " pool must return valid ptr, cascaded pool must support to alloc more times thirdly\n"); + pool_depth = alloc.pool_depth(); + ACE_TEST_EXCEPTION_RETURN (pool_depth != ((2*initial_n_chunks) - old_pool_depth - 1), + " cascaded pool depth must support to alloc three times\n"); + + old_pool_sum = pool_sum; + pool_sum = alloc.pool_sum (); + ACE_TEST_EXCEPTION_RETURN (pool_sum < old_pool_sum, + " cascaded pool sum must be bigger than that of initial pool\n"); + ACE_TEST_EXCEPTION_RETURN (pool_sum != (initial_n_chunks + (2 * initial_n_chunks)), + " cascaded pool sum must be as expected, pool has been enlarged\n"); alloc.free (ptr); alloc.free (ptr1); alloc.free (ptr2); + ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != (initial_n_chunks + (2 * initial_n_chunks)), " cascaded pool depth must be three after having freed all malloc ptrs\n"); + ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator deeply ...\n", __func__)); + old_pool_sum = alloc.pool_sum (); + const size_t totalAllocSum = 8 * 1024; + char cmpvalues[chunk_size]; + char initial_cmp_value = initial_value; + std::vector ptrs; + for (long i = 0; i < totalAllocSum; ++i, ++initial_cmp_value) + { + ACE_OS::memset (cmpvalues, initial_cmp_value, chunk_size); + ptr = alloc.calloc (nbytes, initial_cmp_value); + ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, + " pool must return valid ptr for deeply calloc api test with normal nbytes\n"); + ACE_TEST_EXCEPTION_RETURN (ACE_OS::memcmp (ptr, cmpvalues, chunk_size) != 0, + " calloc call must set the memory content as expected\n"); + ptrs.push_back (ptr); + } + + pool_sum = alloc.pool_sum(); + ACE_TEST_EXCEPTION_RETURN (pool_sum <= old_pool_sum, + " pool sum must greater than old pool sum after alloc many chunks for deeply test\n"); + + for (size_t i = 0; i < ptrs.size (); ++i) + { + alloc.free (ptrs[i]); + } + + pool_depth = alloc.pool_depth(); + ACE_TEST_EXCEPTION_RETURN (pool_depth != pool_sum, + " pool depth must equal to pool sum after all chunks has been freed for deeply test\n"); + + for (long i = 0; i < totalAllocSum; ++i, ++initial_cmp_value) + { + ACE_OS::memset (cmpvalues, initial_cmp_value, chunk_size); + ptr = alloc.calloc (nbytes, initial_cmp_value); + ACE_TEST_EXCEPTION_RETURN (ptr == nullptr, + " pool must return valid ptr for deeply calloc api test2 with normal nbytes\n"); + ACE_TEST_EXCEPTION_RETURN (ACE_OS::memcmp (ptr, cmpvalues, chunk_size) != 0, + " deeply calloc api test2 must set the memory content as expected\n"); + alloc.free (ptr); + } + return 0; } From 5dde2d6e09b78581d98ee461ddd3c5c0be82d3a2 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 26 Sep 2023 17:20:47 +0800 Subject: [PATCH 43/72] fix some comment code style --- ACE/ace/Malloc_T.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index d7f798011edc1..e9e3f01f086e6 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -386,7 +386,7 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based comb_alloc_ptr tmp; // If ACE_NEW fails, the hierarchy_ will be reconstructed when malloc API is called. - // Notice: need one octet to record hierarchy pos + // Notice: need one octet to record hierarchy pos. ACE_NEW (tmp, comb_alloc_type (this->initial_n_chunks_, this->initial_chunk_size_ + sizeof(ACE_UINT8)) ); @@ -439,7 +439,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) // Not in hierarchy or less than nbytes when Binary Search. while (chunk_size < nbytes) { - // Equal to this->initial_chunk_size_ << (m + 1) + // Equal to this->initial_chunk_size_ << (m + 1). chunk_size <<= 1; ++m; } @@ -467,7 +467,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; comb_alloc_ptr newly_alloc; - // Notice: need one octet to record hierarchy pos + // Notice: need one octet to record hierarchy pos. ACE_NEW_RETURN (newly_alloc, comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, chunk_size + sizeof(ACE_UINT8)), From dbac54e311bd1001d4b44ac5edcaf4ec7319bea7 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 26 Sep 2023 20:01:25 +0800 Subject: [PATCH 44/72] fix ACE_TEXT_CHAR_TO_TCHAR usage --- ACE/tests/Allocator_Cascaded_Test.cpp | 36 +++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 18808badf7301..fe09ed1cee901 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -24,7 +24,7 @@ do \ { \ if (expression) \ { \ - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1);\ + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT_CHAR_TO_TCHAR (message)), 1);\ } \ } \ while (0) @@ -34,7 +34,7 @@ do \ { \ if (!(expression)) \ { \ - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1); \ + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT_CHAR_TO_TCHAR (message)), 1); \ } \ } \ while (0) @@ -256,19 +256,19 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () nbytes = initial_chunk_size << i; ptr = alloc.malloc (nbytes); ss << " pool must return valid ptr when requesting chunk_size: << " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); ss.str (""); pool_sum = alloc.pool_sum (); delta = DELTA (i, initial_n_chunks, min_initial_n_chunks); ss << " pool sum must increase as delta: " << delta << " because only created request level: " << i << std::endl; - ACE_ASSERT_RETURN (pool_sum == (old_pool_sum + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (pool_sum == (old_pool_sum + delta), ss.str().c_str()); ss.str (""); alloc.free (ptr); pool_depth = alloc.pool_depth (); ss << " pool depth must increase as delta: " << delta << " because only created request level: " << i << std::endl; - ACE_ASSERT_RETURN (pool_depth == (old_pool_depth + delta), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (pool_depth == (old_pool_depth + delta), ss.str().c_str()); } return 0; @@ -292,7 +292,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () ACE_DEBUG ((LM_INFO, "%C Only test the basic malloc API ...\n", __func__)); ptr = alloc.malloc (nbytes); ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); alloc.free (ptr); for (size_t i = 3; i < 6; ++i) @@ -306,7 +306,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); alloc.free (ptr); ACE_DEBUG ((LM_INFO, "%C test free pos API for level: %u ...\n", __func__, level)); @@ -321,7 +321,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << " at loop: " << j << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); ptrs.push_back (ptr); } @@ -336,11 +336,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () pool_sum = alloc.pool_sum (); ss.str (""); ss << " level: " << level << " pool depth: " << old_pool_depth <<" must keep unchanged" << std::endl; - ACE_ASSERT_RETURN (old_pool_depth == (pool_depth + 1), ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (old_pool_depth == (pool_depth + 1), ss.str().c_str()); ss.str (""); ss << " level: " << level << " pool sum: " << old_pool_sum << " must keep unchanged" << std::endl; - ACE_ASSERT_RETURN (old_pool_sum == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (old_pool_sum == pool_sum, ss.str().c_str()); alloc.free (ptr); } @@ -374,7 +374,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); if (i == 3) { @@ -384,11 +384,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss.str (""); ss << " level: " << (level) << " must be created, pool depth must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_depth + delta - 1) == pool_depth, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN ((old_pool_depth + delta - 1) == pool_depth, ss.str().c_str()); ss.str (""); ss << " level: " << (level) << " must be created, pool sum must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ss.str().c_str()); alloc.free (ptr); } @@ -405,11 +405,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss.str (""); ss << " next level: " << next_level << " must be created, pool depth must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_depth + delta) == pool_depth, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN ((old_pool_depth + delta) == pool_depth, ss.str().c_str()); ss.str (""); ss << " next level: " << next_level << " must be created, pool sum must increased by " << delta << std::endl; - ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ss.str().c_str()); const size_t next_nbytes = chunk_size << next_level; old_pool_depth = alloc.pool_depth (); @@ -422,7 +422,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss << " level: " << next_level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << j << std::endl; - ACE_ASSERT_RETURN (ptr != nullptr, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN (ptr != nullptr, ss.str().c_str()); ptrs.push_back (ptr); } @@ -434,11 +434,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss.str (""); ss << " next level: " << next_level << " pool depth must unchanged" << std::endl; - ACE_ASSERT_RETURN ((old_pool_depth) == pool_depth, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN ((old_pool_depth) == pool_depth, ss.str().c_str()); ss.str (""); ss << " next level: " << next_level << " pool sum must unchanged" << std::endl; - ACE_ASSERT_RETURN ((old_pool_sum) == pool_sum, ACE_TEXT_CHAR_TO_TCHAR(ss.str().c_str())); + ACE_ASSERT_RETURN ((old_pool_sum) == pool_sum, ss.str().c_str()); } return 0; From b0ad5dd10fa5edb630bf68e356506565a68e4f7c Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 26 Sep 2023 20:33:41 +0800 Subject: [PATCH 45/72] fix bug for coredump when free chunks --- ACE/ace/Malloc_T.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index d7f798011edc1..d757bf5250c84 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -513,7 +513,7 @@ void ACE_Cascaded_Multi_Size_Based_Allocator::free (void* ptr) const size_t h = *static_cast (hdr_ptr); if (h < this->hierarchy_.size () && this->hierarchy_[h] != nullptr) - this->hierarchy_[h]->free (ptr); + this->hierarchy_[h]->free (hdr_ptr); } } From 7202657a8f0e68b4c155c42709a5577bebe59715 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 26 Sep 2023 20:48:03 +0800 Subject: [PATCH 46/72] =?UTF-8?q?1=E3=80=81use=20chunk=20header=20type=20t?= =?UTF-8?q?ypedef=202=E3=80=81reduce=20cyclomatic=20complexity=20of=20free?= =?UTF-8?q?=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.cpp | 30 +++++++++++++++--------------- ACE/ace/Malloc_T.h | 3 +++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index d757bf5250c84..2d49eebbd8bf1 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -386,9 +386,9 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based comb_alloc_ptr tmp; // If ACE_NEW fails, the hierarchy_ will be reconstructed when malloc API is called. - // Notice: need one octet to record hierarchy pos + // Notice: need one octet to record hierarchy pos. ACE_NEW (tmp, comb_alloc_type (this->initial_n_chunks_, - this->initial_chunk_size_ + sizeof(ACE_UINT8)) + this->initial_chunk_size_ + sizeof(comb_chunk_header_type)) ); this->hierarchy_.push_back (tmp); @@ -455,8 +455,8 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) if (ptr == nullptr) return nullptr; - *static_cast (ptr) = static_cast (m); - return static_cast (ptr) + sizeof (ACE_UINT8); + *static_cast (ptr) = static_cast (m); + return static_cast (ptr) + sizeof (comb_chunk_header_type); } // The found pos maybe nullptr or beyond the current hierarchy_ size. @@ -467,10 +467,10 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; comb_alloc_ptr newly_alloc; - // Notice: need one octet to record hierarchy pos + // Notice: need one octet to record hierarchy pos. ACE_NEW_RETURN (newly_alloc, comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, - chunk_size + sizeof(ACE_UINT8)), + chunk_size + sizeof(comb_chunk_header_type)), nullptr ); @@ -479,8 +479,8 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) if (ptr == nullptr) return nullptr; - *static_cast (ptr) = static_cast (m); - return static_cast (ptr) + sizeof (ACE_UINT8); + *static_cast (ptr) = static_cast (m); + return static_cast (ptr) + sizeof (comb_chunk_header_type); } template @@ -503,18 +503,18 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::calloc (size_t, size_t, template void ACE_Cascaded_Multi_Size_Based_Allocator::free (void* ptr) { + if (ptr == nullptr) + return; + ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); ACE_ASSERT (this->hierarchy_.size () > 0); - if (ptr != nullptr) - { - void* const hdr_ptr = static_cast (ptr) - sizeof (ACE_UINT8); - const size_t h = *static_cast (hdr_ptr); + void* const hdr_ptr = static_cast (ptr) - sizeof (comb_chunk_header_type); + const size_t h = *static_cast (hdr_ptr); - if (h < this->hierarchy_.size () && this->hierarchy_[h] != nullptr) - this->hierarchy_[h]->free (hdr_ptr); - } + if (h < this->hierarchy_.size () && this->hierarchy_[h] != nullptr) + this->hierarchy_[h]->free (hdr_ptr); } template diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index fc5d24ea503f2..5ffb647ab2d0e 100755 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -404,6 +404,9 @@ class ACE_Cascaded_Multi_Size_Based_Allocator : public ACE_Allocator ACE_LOCK& mutex (); private: + /// Chunk control header type + typedef ACE_UINT8 comb_chunk_header_type; + /// Synchronization variable for API. ACE_LOCK mutex_; From fb136e924ac39cd0a3b69beb202fa08d6a9fe499 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 26 Sep 2023 20:58:36 +0800 Subject: [PATCH 47/72] add calloc test using memcmp,not only check return pointer --- ACE/tests/Allocator_Cascaded_Test.cpp | 37 ++++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index fb8acba09750e..ac5df25dde611 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -269,21 +269,6 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () " pool must return valid ptr when requesting initial chunk_size\n"); alloc.free (ptr); - ACE_DEBUG ((LM_INFO, "%C Only test the basic calloc API ...\n", __func__)); - char initial_value = '\0'; - const size_t CMP_ARRAY_LEN = initial_chunk_size + 1024; - char cmpvalues[CMP_ARRAY_LEN]; - for (nbytes = initial_chunk_size; nbytes < CMP_ARRAY_LEN; ++nbytes, ++initial_value) - { - ACE_OS::memset (cmpvalues, initial_value, CMP_ARRAY_LEN); - ptr = alloc.calloc (nbytes, initial_value); - ACE_ASSERT_RETURN (ptr != nullptr, - " pool must return valid ptr when callinging calloc API with various valid chunk_size\n"); - ACE_ASSERT_RETURN (ACE_OS::memcmp (ptr, cmpvalues, nbytes) == 0, - " pool return memory must be the same as cmpvalues when callinging calloc API with various valid chunk_size\n"); - alloc.free (ptr); - } - ACE_DEBUG ((LM_INFO, "%C Will trigger the creation of nested allocator on next level ...\n", __func__)); level = 1; old_pool_sum = alloc.pool_sum (); @@ -345,6 +330,22 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () ACE_ASSERT_RETURN (pool_depth == (old_pool_depth + delta), ss.str().c_str()); } + ACE_DEBUG ((LM_INFO, "%C Only test the basic calloc API ...\n", __func__)); + char initial_value = '\0'; + const size_t CMP_ARRAY_LEN = initial_chunk_size + 1024; + char cmpvalues[CMP_ARRAY_LEN]; + for (nbytes = initial_chunk_size; nbytes < CMP_ARRAY_LEN; ++nbytes, ++initial_value) + { + ACE_OS::memset (cmpvalues, initial_value, nbytes); + ptr = alloc.calloc (nbytes, initial_value); + ACE_ASSERT_RETURN (ptr != nullptr, + " pool must return valid ptr when calling calloc API with various valid chunk_size\n"); + ACE_ASSERT_RETURN ( + ACE_OS::memcmp (ptr, cmpvalues, nbytes) == 0, + " pool return memory must be the same as cmpvalues when calling calloc API with various valid chunk_size\n"); + alloc.free (ptr); + } + return 0; } @@ -529,10 +530,10 @@ run_main (int, ACE_TCHAR *[]) retval += run_cascaded_allocator_test(); ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Multi_Size_Based_Allocator ...\n", __func__)); - retval += run_cascaded_multi_size_based_allocator_basic_test(); + //retval += run_cascaded_multi_size_based_allocator_basic_test(); retval += run_cascaded_multi_size_based_allocator_hierarchy_test (); - retval += run_cascaded_multi_size_based_allocator_hierarchy_free_test (); - retval += run_cascaded_multi_size_based_allocator_hierarchy_differential_test (); + //retval += run_cascaded_multi_size_based_allocator_hierarchy_free_test (); + //retval += run_cascaded_multi_size_based_allocator_hierarchy_differential_test (); ACE_END_TEST; From f066bb49db1ab6becab2a36ac5c9113a7ac40488 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 26 Sep 2023 21:04:44 +0800 Subject: [PATCH 48/72] fix comment issue of coding style --- ACE/ace/Malloc_T.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 5ffb647ab2d0e..cec76b7a2f653 100755 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -404,7 +404,7 @@ class ACE_Cascaded_Multi_Size_Based_Allocator : public ACE_Allocator ACE_LOCK& mutex (); private: - /// Chunk control header type + /// Chunk control header type. typedef ACE_UINT8 comb_chunk_header_type; /// Synchronization variable for API. From d996a548c83b3aa80a363185294fbb9182081328 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 26 Sep 2023 21:08:30 +0800 Subject: [PATCH 49/72] fix some linux compiler warnings --- ACE/tests/Allocator_Cascaded_Test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index ac5df25dde611..5026fb24810e3 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -130,7 +130,7 @@ run_cascaded_allocator_test () char cmpvalues[chunk_size]; char initial_cmp_value = initial_value; std::vector ptrs; - for (long i = 0; i < totalAllocSum; ++i, ++initial_cmp_value) + for (size_t i = 0; i < totalAllocSum; ++i, ++initial_cmp_value) { ACE_OS::memset (cmpvalues, initial_cmp_value, chunk_size); ptr = alloc.calloc (nbytes, initial_cmp_value); @@ -154,7 +154,7 @@ run_cascaded_allocator_test () ACE_TEST_EXCEPTION_RETURN (pool_depth != pool_sum, " pool depth must equal to pool sum after all chunks has been freed for deeply test\n"); - for (long i = 0; i < totalAllocSum; ++i, ++initial_cmp_value) + for (size_t i = 0; i < totalAllocSum; ++i, ++initial_cmp_value) { ACE_OS::memset (cmpvalues, initial_cmp_value, chunk_size); ptr = alloc.calloc (nbytes, initial_cmp_value); @@ -530,10 +530,10 @@ run_main (int, ACE_TCHAR *[]) retval += run_cascaded_allocator_test(); ACE_DEBUG ((LM_INFO, "%C Run the tests for Cascaded_Multi_Size_Based_Allocator ...\n", __func__)); - //retval += run_cascaded_multi_size_based_allocator_basic_test(); + retval += run_cascaded_multi_size_based_allocator_basic_test(); retval += run_cascaded_multi_size_based_allocator_hierarchy_test (); - //retval += run_cascaded_multi_size_based_allocator_hierarchy_free_test (); - //retval += run_cascaded_multi_size_based_allocator_hierarchy_differential_test (); + retval += run_cascaded_multi_size_based_allocator_hierarchy_free_test (); + retval += run_cascaded_multi_size_based_allocator_hierarchy_differential_test (); ACE_END_TEST; From 378a913c98bc3ee44d055ec1d3651cc6e1b5d9ac Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Wed, 27 Sep 2023 19:13:09 +0800 Subject: [PATCH 50/72] delete ACE/MPC dir for misoperation --- ACE/MPC | 1 - 1 file changed, 1 deletion(-) delete mode 160000 ACE/MPC diff --git a/ACE/MPC b/ACE/MPC deleted file mode 160000 index 307f1fc30267d..0000000000000 --- a/ACE/MPC +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 307f1fc30267dde08c28d5665716530ce7ff0abd From d4e7d1423a51d9d23922072b8854402e241812e8 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 19:08:38 +0800 Subject: [PATCH 51/72] modify growth strategy to exponential growth when hierarchy grows for faster rate --- ACE/ace/Malloc_T.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 1d34e4863782c..6c5c43e0c7c72 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -227,7 +227,9 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) if(ptr == nullptr) { comb_alloc_ptr tmp; - ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * 2 * this->hierarchy_.size(), this->chunk_size_), nullptr); + ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * (1 << this->hierarchy_.size()), + this->chunk_size_), + nullptr); // Increase the chunk sum if succeed. this->chunk_sum_ += tmp->pool_depth(); From 874d9cb7ae6a193536074f54a80ef4cd4123dc13 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 19:11:05 +0800 Subject: [PATCH 52/72] keep the consistent to cascaded malloc branch --- ACE/tests/Allocator_Cascaded_Test.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 624dd4f6d6e5a..314073149f9ec 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -22,7 +22,7 @@ do \ { \ if (expression) \ { \ - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1);\ + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT_CHAR_TO_TCHAR (message)), 1);\ } \ } \ while (0) @@ -84,7 +84,8 @@ run_cascaded_allocator_test () ptr1 = alloc.malloc (nbytes); ACE_TEST_EXCEPTION_RETURN (ptr1 == nullptr, " pool must return valid ptr, cascaded pool must support to alloc more times secondly\n"); - ACE_TEST_EXCEPTION_RETURN (alloc.pool_depth () != 1, + pool_depth = alloc.pool_depth (); + ACE_TEST_EXCEPTION_RETURN (pool_depth != 1, " cascaded pool depth must support to alloc twice\n"); old_pool_depth = alloc.pool_depth(); From 92066a1e40c8a67bca16a60154048e6817eb540f Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 19:27:03 +0800 Subject: [PATCH 53/72] add exception safety for the growth of chunk sum --- ACE/ace/Malloc_T.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 6c5c43e0c7c72..cb87f7c87ed60 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -190,9 +190,10 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All // 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_)); - // Increase the chunk sum if succeed. - this->chunk_sum_ += tmp->pool_depth(); this->hierarchy_.push_back(tmp); + + // Increase the chunk sum if all points having potential risk of exception is passed. + this->chunk_sum_ += tmp->pool_depth (); } template @@ -231,9 +232,10 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) this->chunk_size_), nullptr); - // Increase the chunk sum if succeed. - this->chunk_sum_ += tmp->pool_depth(); this->hierarchy_.push_back(tmp); + + // Increase the chunk sum if all points having potential risk of exception is passed. + this->chunk_sum_ += tmp->pool_depth (); ptr = tmp->malloc(nbytes); } From 4a9c114ec6aeaee6a39f33cc130a7790409009cf Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 19:56:40 +0800 Subject: [PATCH 54/72] fix the exception safety for calling vector push_back API --- ACE/ace/Malloc_T.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index cb87f7c87ed60..879e496422644 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -190,7 +190,13 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All // 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_)); + // Consider the exception of vector push_back call this->hierarchy_.push_back(tmp); + if (0 == this->hierarchy_.size()) + { + delete tmp; + return; + } // Increase the chunk sum if all points having potential risk of exception is passed. this->chunk_sum_ += tmp->pool_depth (); @@ -232,7 +238,14 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) this->chunk_size_), nullptr); + // Consider the exception of vector push_back call + const auto old_size = this->hierarchy_.size(); this->hierarchy_.push_back(tmp); + if (old_size == this->hierarchy_.size()) + { + delete tmp; + return nullptr; + } // Increase the chunk sum if all points having potential risk of exception is passed. this->chunk_sum_ += tmp->pool_depth (); From f1f4bba33460e9f0743b48053c224f92224587bd Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 20:28:27 +0800 Subject: [PATCH 55/72] add exception safety for malloc API when calling vector resize --- ACE/ace/Malloc_T.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index e4486154aef24..478dd8c83ed2e 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -466,7 +466,8 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); - if (m < this->hierarchy_.size() && this->hierarchy_[m] != nullptr) + const auto size = this->hierarchy_.size(); + if (m < size && this->hierarchy_[m] != nullptr) { void* ptr = this->hierarchy_[m]->malloc(nbytes); if (ptr == nullptr) @@ -477,9 +478,14 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) } // The found pos maybe nullptr or beyond the current hierarchy_ size. - if (m >= this->hierarchy_.size()) + if (m >= size) { this->hierarchy_.resize (m + 1, nullptr); + // Consider the exception of vector resize call + if (size == this->hierarchy_.size()) + { + return nullptr; + } } const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; From 84501564f93b1b051f69965a5375f02207783490 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 21:40:55 +0800 Subject: [PATCH 56/72] Using std::unique_ptr will be more exception-safe literally --- ACE/ace/Malloc_T.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 879e496422644..7a42d224fd03e 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -14,6 +14,7 @@ #include "ace/ACE.h" #include "ace/OS_NS_string.h" #include +#include ACE_BEGIN_VERSIONED_NAMESPACE_DECL @@ -191,15 +192,18 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All ACE_NEW (tmp, comb_alloc_type(this->initial_n_chunks_, this->chunk_size_)); // Consider the exception of vector push_back call - this->hierarchy_.push_back(tmp); + std::unique_ptr 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()) { - delete tmp; return; } // Increase the chunk sum if all points having potential risk of exception is passed. - this->chunk_sum_ += tmp->pool_depth (); + this->chunk_sum_ += smart_ptr->pool_depth(); + + smart_ptr.release(); } template @@ -239,17 +243,20 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) nullptr); // Consider the exception of vector push_back call + std::unique_ptr smart_ptr(tmp); const auto old_size = this->hierarchy_.size(); - this->hierarchy_.push_back(tmp); + // Has strong exception safety guarantee for call of push_back. + this->hierarchy_.push_back(smart_ptr.get()); if (old_size == this->hierarchy_.size()) { - delete tmp; return nullptr; } // Increase the chunk sum if all points having potential risk of exception is passed. - this->chunk_sum_ += tmp->pool_depth (); - ptr = tmp->malloc(nbytes); + this->chunk_sum_ += smart_ptr->pool_depth(); + ptr = smart_ptr->malloc(nbytes); + + smart_ptr.release(); } return ptr; From 0c8067c1833ac05ee2d6696cbcfbf441e051fbe1 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 22:20:40 +0800 Subject: [PATCH 57/72] delete defence codes --- ACE/ace/Malloc_T.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 7a42d224fd03e..81fe7e532dbd8 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -195,10 +195,6 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All std::unique_ptr 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(); @@ -247,10 +243,6 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) 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(); From cc4053c66b22904a7d01e4fedb5b77054875901c Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 22:24:48 +0800 Subject: [PATCH 58/72] delete unused temp stack var --- ACE/ace/Malloc_T.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 81fe7e532dbd8..e4932f38e9c60 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -240,7 +240,6 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) // Consider the exception of vector push_back call std::unique_ptr 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()); From 606f80d507f8f92ed009782b99542299052706b6 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 22:39:34 +0800 Subject: [PATCH 59/72] =?UTF-8?q?1=E3=80=81add=20exception=20safety=20for?= =?UTF-8?q?=20constructor=20using=20unique=5Fptr=202=E3=80=81delete=20defe?= =?UTF-8?q?nsive=20codes=20for=20resize=20call=20when=20exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index d74a3246c0e21..e4a44b4fe6690 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -406,7 +406,11 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based this->initial_chunk_size_ + sizeof(comb_chunk_header_type)) ); - this->hierarchy_.push_back (tmp); + // Consider the exception of vector push_back call + std::unique_ptr smart_ptr(tmp); + this->hierarchy_.push_back(smart_ptr.get()); + + smart_ptr.release(); } template @@ -478,12 +482,8 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) // The found pos maybe nullptr or beyond the current hierarchy_ size. if (m >= size) { - this->hierarchy_.resize (m + 1, nullptr); - // Consider the exception of vector resize call - if (size == this->hierarchy_.size()) - { - return nullptr; - } + // Has strong exception safety guarantee for call of resize, maybe throw. + this->hierarchy_.resize(m + 1, nullptr); } const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; From 534fa2e3a8dc8c2f8a06d65cc89e6dad51a6b86c Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 29 Sep 2023 23:12:37 +0800 Subject: [PATCH 60/72] access this->hierarchy_.size() will more quickly than stack temp var --- ACE/ace/Malloc_T.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index e4a44b4fe6690..533740304dc55 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -468,8 +468,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); - const auto size = this->hierarchy_.size(); - if (m < size && this->hierarchy_[m] != nullptr) + if (m < this->hierarchy_.size() && this->hierarchy_[m] != nullptr) { void* ptr = this->hierarchy_[m]->malloc(nbytes); if (ptr == nullptr) @@ -480,7 +479,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) } // The found pos maybe nullptr or beyond the current hierarchy_ size. - if (m >= size) + if (m >= this->hierarchy_.size()) { // Has strong exception safety guarantee for call of resize, maybe throw. this->hierarchy_.resize(m + 1, nullptr); From ac5f611bc29973e99a0f5905395e86b3d5c92b58 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sat, 30 Sep 2023 07:55:20 +0800 Subject: [PATCH 61/72] fix invalid document of class --- ACE/ace/Malloc_T.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.h b/ACE/ace/Malloc_T.h index 64efc7ea4849c..44fca744c3604 100644 --- a/ACE/ace/Malloc_T.h +++ b/ACE/ace/Malloc_T.h @@ -220,7 +220,7 @@ class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator * * Notice that when the latest allocator is empty, the allocator will create a fresh * @a ACE_Dynamic_Cached_Allocator allocator again with - * init_n_chunks* the sum of current allocators as it's constructor parameter, + * init_n_chunks * ( 1 << the sum of current allocators ) as it's constructor parameter, * so all the allocators will form a cascaded hierarchy. * This class can be configured flexibly with different types of From fec93be2bd729424277e74f4f042c547cbadd21a Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sun, 8 Oct 2023 09:51:41 +0800 Subject: [PATCH 62/72] fix coding style of comment --- ACE/ace/Malloc_T.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index e4932f38e9c60..09c79d23c8d56 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -191,7 +191,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All // 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_)); - // Consider the exception of vector push_back call + // Consider the exception of vector push_back call. std::unique_ptr smart_ptr(tmp); // Has strong exception safety guarantee for call of push_back. this->hierarchy_.push_back (smart_ptr.get()); @@ -238,7 +238,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) this->chunk_size_), nullptr); - // Consider the exception of vector push_back call + // Consider the exception of vector push_back call. std::unique_ptr smart_ptr(tmp); // Has strong exception safety guarantee for call of push_back. this->hierarchy_.push_back(smart_ptr.get()); From 9dc4df792a48500206a604c5c2324f7a80e923cf Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sun, 8 Oct 2023 10:13:07 +0800 Subject: [PATCH 63/72] fix right const coding style --- ACE/ace/Malloc_T.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 1c4a163c62fba..af9a6d1a9799f 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -406,7 +406,7 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based this->initial_chunk_size_ + sizeof(comb_chunk_header_type)) ); - // Consider the exception of vector push_back call + // Consider the exception of vector push_back call. std::unique_ptr smart_ptr(tmp); this->hierarchy_.push_back(smart_ptr.get()); @@ -485,14 +485,13 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) this->hierarchy_.resize(m + 1, nullptr); } - const size_t reinitial_n_chunks = this->initial_n_chunks_ >> m; + size_t const reinitial_n_chunks = this->initial_n_chunks_ >> m; comb_alloc_ptr newly_alloc; // Notice: need one octet to record hierarchy pos. ACE_NEW_RETURN (newly_alloc, comb_alloc_type (reinitial_n_chunks > this->min_initial_n_chunks_ ? reinitial_n_chunks : this->min_initial_n_chunks_, chunk_size + sizeof(comb_chunk_header_type)), - nullptr - ); + nullptr); this->hierarchy_[m] = newly_alloc; void* ptr = newly_alloc->malloc (nbytes); @@ -531,7 +530,7 @@ void ACE_Cascaded_Multi_Size_Based_Allocator::free (void* ptr) ACE_ASSERT (this->hierarchy_.size () > 0); void* const hdr_ptr = static_cast (ptr) - sizeof (comb_chunk_header_type); - const size_t h = *static_cast (hdr_ptr); + size_t const h = *static_cast (hdr_ptr); if (h < this->hierarchy_.size () && this->hierarchy_[h] != nullptr) this->hierarchy_[h]->free (hdr_ptr); From 42b409ef86e182f00db7875a3c46cabd06c55188 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sun, 8 Oct 2023 10:29:44 +0800 Subject: [PATCH 64/72] fix for right const coding style --- ACE/tests/Allocator_Cascaded_Test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 314073149f9ec..27bc739f161e3 100644 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -32,8 +32,8 @@ run_cascaded_allocator_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - const size_t initial_n_chunks = 1; - const size_t chunk_size = sizeof(void*) + 7; + size_t const initial_n_chunks = 1; + size_t const chunk_size = sizeof(void*) + 7; void *ptr, *ptr1, *ptr2; size_t nbytes = chunk_size; @@ -112,7 +112,7 @@ run_cascaded_allocator_test () ACE_DEBUG ((LM_INFO, "%C will test cascaded allocator deeply ...\n", __func__)); old_pool_sum = alloc.pool_sum (); - const size_t totalAllocSum = 8 * 1024; + size_t const totalAllocSum = 8 * 1024; char cmpvalues[chunk_size]; char initial_cmp_value = initial_value; std::vector ptrs; From a837b5d44bfb80719516997a76c99ea838198ae7 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sun, 8 Oct 2023 11:10:49 +0800 Subject: [PATCH 65/72] format using clang-format --- ACE/ace/Malloc_T.cpp | 50 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 09c79d23c8d56..75b8b6927ef74 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -189,28 +189,28 @@ ACE_Cascaded_Dynamic_Cached_Allocator::ACE_Cascaded_Dynamic_Cached_All 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_)); + ACE_NEW (tmp, comb_alloc_type (this->initial_n_chunks_, this->chunk_size_)); // Consider the exception of vector push_back call. - std::unique_ptr smart_ptr(tmp); + std::unique_ptr smart_ptr (tmp); // Has strong exception safety guarantee for call of push_back. - this->hierarchy_.push_back (smart_ptr.get()); + this->hierarchy_.push_back (smart_ptr.get ()); // Increase the chunk sum if all points having potential risk of exception is passed. - this->chunk_sum_ += smart_ptr->pool_depth(); + this->chunk_sum_ += smart_ptr->pool_depth (); - smart_ptr.release(); + smart_ptr.release (); } template ACE_Cascaded_Dynamic_Cached_Allocator::~ACE_Cascaded_Dynamic_Cached_Allocator () { - for (size_t h = 0; h < this->hierarchy_.size(); h++) + for (size_t h = 0; h < this->hierarchy_.size (); h++) { delete this->hierarchy_[h]; } - this->hierarchy_.clear(); + this->hierarchy_.clear (); } template void * @@ -222,16 +222,16 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); - void * ptr = nullptr; + void *ptr = nullptr; - for (size_t h = 0; h < this->hierarchy_.size(); h++) + for (size_t h = 0; h < this->hierarchy_.size (); h++) { - ptr = this->hierarchy_[h]->malloc(nbytes); - if(ptr != nullptr) + ptr = this->hierarchy_[h]->malloc (nbytes); + if (ptr != nullptr) break; } - if(ptr == nullptr) + if (ptr == nullptr) { comb_alloc_ptr tmp; ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * (1 << this->hierarchy_.size()), @@ -239,15 +239,15 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) nullptr); // Consider the exception of vector push_back call. - std::unique_ptr smart_ptr(tmp); + std::unique_ptr smart_ptr (tmp); // Has strong exception safety guarantee for call of push_back. - this->hierarchy_.push_back(smart_ptr.get()); + this->hierarchy_.push_back (smart_ptr.get ()); // 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); + this->chunk_sum_ += smart_ptr->pool_depth (); + ptr = smart_ptr->malloc (nbytes); - smart_ptr.release(); + smart_ptr.release (); } return ptr; @@ -280,11 +280,11 @@ ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) { ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_)); - ACE_ASSERT (this->hierarchy_.size() > 0); + 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); + if (ptr != nullptr && this->hierarchy_.size () > 0) + this->hierarchy_[0]->free (ptr); } template int @@ -360,9 +360,9 @@ ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () size_t pool_depth = 0; - for (size_t h = 0; h < this->hierarchy_.size(); h++) + for (size_t h = 0; h < this->hierarchy_.size (); h++) { - pool_depth += this->hierarchy_[h]->pool_depth(); + pool_depth += this->hierarchy_[h]->pool_depth (); } return pool_depth; @@ -378,11 +378,11 @@ ACE_Cascaded_Dynamic_Cached_Allocator::dump () const 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())); + ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("hierarchy_ size = %u\n"), this->hierarchy_.size ())); - for (size_t h = 0; h < this->hierarchy_.size(); h++) + for (size_t h = 0; h < this->hierarchy_.size (); h++) { - this->hierarchy_[h]->dump(); + this->hierarchy_[h]->dump (); ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); } From ce594f4e3d72228c37acb7c7292110832ffadeb4 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sun, 8 Oct 2023 11:26:15 +0800 Subject: [PATCH 66/72] =?UTF-8?q?fix=20coding=20style=201=E3=80=81right=20?= =?UTF-8?q?const=202=E3=80=81pointer=20var=20declaration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/Malloc_T.cpp | 6 ++--- ACE/tests/Allocator_Cascaded_Test.cpp | 36 +++++++++++++-------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 54242f25ae1f7..fc29978faff8f 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -470,7 +470,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) if (m < this->hierarchy_.size() && this->hierarchy_[m] != nullptr) { - void* ptr = this->hierarchy_[m]->malloc(nbytes); + void *ptr = this->hierarchy_[m]->malloc(nbytes); if (ptr == nullptr) return nullptr; @@ -494,7 +494,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) nullptr); this->hierarchy_[m] = newly_alloc; - void* ptr = newly_alloc->malloc (nbytes); + void *ptr = newly_alloc->malloc (nbytes); if (ptr == nullptr) return nullptr; @@ -506,7 +506,7 @@ template void* ACE_Cascaded_Multi_Size_Based_Allocator::calloc (size_t nbytes, char initial_value) { // No need any lock. - void* ptr = malloc (nbytes); + void *ptr = malloc (nbytes); if (ptr != nullptr) ACE_OS::memset (ptr, initial_value, nbytes); diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 48c76f8b466e4..6f0b40ac80ff1 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -174,16 +174,16 @@ run_cascaded_multi_size_based_allocator_basic_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - const size_t initial_n_chunks = 11; - const size_t min_initial_n_chunks = 2; - const size_t initial_chunk_size = sizeof (void*) + 5; - const size_t nbytes = initial_chunk_size; + size_t const initial_n_chunks = 11; + size_t const min_initial_n_chunks = 2; + size_t const initial_chunk_size = sizeof (void*) + 5; + size_t const nbytes = initial_chunk_size; std::vector ptrs; void *ptr; size_t pool_sum, pool_depth; - const char initial_value = '\0'; + char const initial_value = '\0'; ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, initial_chunk_size, min_initial_n_chunks); pool_sum = alloc.pool_sum (); @@ -254,9 +254,9 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - const size_t initial_n_chunks = 11; - const size_t min_initial_n_chunks = 2; - const size_t initial_chunk_size = sizeof (void*) + 5; + size_t const initial_n_chunks = 11; + size_t const min_initial_n_chunks = 2; + size_t const initial_chunk_size = sizeof (void*) + 5; void *ptr; size_t pool_sum, old_pool_sum, pool_depth, old_pool_depth; @@ -333,7 +333,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_test () ACE_DEBUG ((LM_INFO, "%C Only test the basic calloc API ...\n", __func__)); char initial_value = '\0'; - const size_t CMP_ARRAY_LEN = initial_chunk_size + 1024; + size_t const CMP_ARRAY_LEN = initial_chunk_size + 1024; char cmpvalues[CMP_ARRAY_LEN]; for (nbytes = initial_chunk_size; nbytes < CMP_ARRAY_LEN; ++nbytes, ++initial_value) { @@ -355,11 +355,11 @@ run_cascaded_multi_size_based_allocator_hierarchy_free_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - const size_t initial_n_chunks = 11; - const size_t min_initial_n_chunks = 2; - const size_t chunk_size = sizeof (void*) + 5; + size_t const initial_n_chunks = 11; + size_t const min_initial_n_chunks = 2; + size_t const chunk_size = sizeof (void*) + 5; - void* ptr; + void *ptr; size_t level = 3; size_t nbytes = chunk_size << level; std::stringstream ss; @@ -428,9 +428,9 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () { ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - const size_t initial_n_chunks = 11; - const size_t min_initial_n_chunks = 2; - const size_t chunk_size = sizeof (void*) + 5; + size_t const initial_n_chunks = 11; + size_t const min_initial_n_chunks = 2; + size_t const chunk_size = sizeof (void*) + 5; ACE_Cascaded_Multi_Size_Based_Allocator alloc (initial_n_chunks, chunk_size, min_initial_n_chunks); ACE_DEBUG ((LM_INFO, "%C Only test the hierarchy differential ...\n", __func__)); @@ -472,7 +472,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () old_pool_depth = alloc.pool_depth (); old_pool_sum = alloc.pool_sum (); - const size_t next_level = level + 1; + size_t const next_level = level + 1; delta = DELTA (next_level, initial_n_chunks, min_initial_n_chunks); ptr = alloc.malloc (nbytes + 1); alloc.free (ptr); @@ -487,7 +487,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ss << " next level: " << next_level << " must be created, pool sum must increased by " << delta << std::endl; ACE_ASSERT_RETURN ((old_pool_sum + delta) == pool_sum, ss.str().c_str()); - const size_t next_nbytes = chunk_size << next_level; + size_t const next_nbytes = chunk_size << next_level; old_pool_depth = alloc.pool_depth (); old_pool_sum = alloc.pool_sum (); std::vector ptrs; From 3a70c46d14eae8289c77cde719f2a0d172403f2e Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sun, 8 Oct 2023 11:31:11 +0800 Subject: [PATCH 67/72] fix for coding style of no parameter function call --- ACE/ace/Malloc_T.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index fc29978faff8f..a10f11b82f3c8 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -407,10 +407,10 @@ ACE_Cascaded_Multi_Size_Based_Allocator::ACE_Cascaded_Multi_Size_Based ); // Consider the exception of vector push_back call. - std::unique_ptr smart_ptr(tmp); - this->hierarchy_.push_back(smart_ptr.get()); + std::unique_ptr smart_ptr (tmp); + this->hierarchy_.push_back (smart_ptr.get ()); - smart_ptr.release(); + smart_ptr.release (); } template @@ -434,7 +434,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) // Use Binary Search to find minimal pos that value is bigger than nbytes. size_t m = 0; size_t l = 0; - size_t h = this->hierarchy_.size(); + size_t h = this->hierarchy_.size (); while (l <= h) { @@ -468,7 +468,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); - if (m < this->hierarchy_.size() && this->hierarchy_[m] != nullptr) + if (m < this->hierarchy_.size () && this->hierarchy_[m] != nullptr) { void *ptr = this->hierarchy_[m]->malloc(nbytes); if (ptr == nullptr) @@ -479,10 +479,10 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) } // The found pos maybe nullptr or beyond the current hierarchy_ size. - if (m >= this->hierarchy_.size()) + if (m >= this->hierarchy_.size ()) { // Has strong exception safety guarantee for call of resize, maybe throw. - this->hierarchy_.resize(m + 1, nullptr); + this->hierarchy_.resize (m + 1, nullptr); } size_t const reinitial_n_chunks = this->initial_n_chunks_ >> m; From c9ead44c43b171d92c0bc1e764d67eb2f75e35ee Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sun, 8 Oct 2023 11:33:22 +0800 Subject: [PATCH 68/72] fix for coding style of function call --- ACE/ace/Malloc_T.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 75b8b6927ef74..327ea58f9253c 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -234,7 +234,7 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) if (ptr == nullptr) { comb_alloc_ptr tmp; - ACE_NEW_RETURN (tmp, comb_alloc_type(this->initial_n_chunks_ * (1 << this->hierarchy_.size()), + ACE_NEW_RETURN (tmp, comb_alloc_type (this->initial_n_chunks_ * (1 << this->hierarchy_.size()), this->chunk_size_), nullptr); From ffcea606e9e3d86b0d11d196228b530d28b23127 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sun, 8 Oct 2023 20:45:06 +0800 Subject: [PATCH 69/72] fix for codding style of var declaration --- ACE/tests/Allocator_Cascaded_Test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACE/tests/Allocator_Cascaded_Test.cpp b/ACE/tests/Allocator_Cascaded_Test.cpp index 6f0b40ac80ff1..38243af50c8d1 100755 --- a/ACE/tests/Allocator_Cascaded_Test.cpp +++ b/ACE/tests/Allocator_Cascaded_Test.cpp @@ -445,7 +445,7 @@ run_cascaded_multi_size_based_allocator_hierarchy_differential_test () ACE_DEBUG ((LM_INFO, "%C test level: %u size-based cascaded allocator ...\n", __func__, level)); size_t nbytes = chunk_size << level; - void* ptr = alloc.malloc (nbytes); + void *ptr = alloc.malloc (nbytes); ss.str (""); ss << " level: " << level << " size-based cascaded allocator must return valid ptr when requesting normal chunk_size: " << nbytes From 3e0bd04b3d73547db2d4c50e5faec000842ce915 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Fri, 24 Nov 2023 22:03:29 +0800 Subject: [PATCH 70/72] fix pointer arith issue --- ACE/ace/Malloc_T.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index d772c2380339c..4c312b502c85b 100755 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -475,7 +475,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) return nullptr; *static_cast (ptr) = static_cast (m); - return static_cast (ptr) + sizeof (comb_chunk_header_type); + return static_cast (ptr) + 1; } // The found pos maybe nullptr or beyond the current hierarchy_ size. @@ -499,7 +499,7 @@ void* ACE_Cascaded_Multi_Size_Based_Allocator::malloc (size_t nbytes) return nullptr; *static_cast (ptr) = static_cast (m); - return static_cast (ptr) + sizeof (comb_chunk_header_type); + return static_cast (ptr) + 1; } template @@ -529,7 +529,7 @@ void ACE_Cascaded_Multi_Size_Based_Allocator::free (void* ptr) ACE_ASSERT (this->hierarchy_.size () > 0); - void* const hdr_ptr = static_cast (ptr) - sizeof (comb_chunk_header_type); + void* const hdr_ptr = static_cast (ptr) - 1; size_t const h = *static_cast (hdr_ptr); if (h < this->hierarchy_.size () && this->hierarchy_[h] != nullptr) From 021ef62a0f9970ace70fa7882e077bf10f69f01d Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 13 Jan 2024 16:19:04 +0800 Subject: [PATCH 71/72] optimization for cascaded allocator considering the character that only the first and last allocator has free chunks --- ACE/ace/Malloc_T.cpp | 62 +++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 327ea58f9253c..3fe81a0c84c22 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -222,33 +222,37 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, nullptr)); - void *ptr = nullptr; + size_t const size = this->hierarchy_.size (); + if (size == 0) + return nullptr; - for (size_t h = 0; h < this->hierarchy_.size (); h++) + // Only the first and last child allocator maybe has free chunks, the others are empty. + void* ptr = this->hierarchy_[0]->malloc (nbytes); + if (ptr != nullptr) + return ptr; + + if (size > 1) { - ptr = this->hierarchy_[h]->malloc (nbytes); + ptr = this->hierarchy_[size - 1]->malloc (nbytes); if (ptr != nullptr) - break; + return ptr; } - 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 smart_ptr (tmp); - // Has strong exception safety guarantee for call of push_back. - this->hierarchy_.push_back (smart_ptr.get ()); + // Need alloc a new child allocator. + comb_alloc_ptr tmp; + ACE_NEW_RETURN (tmp, comb_alloc_type (this->initial_n_chunks_ * (1 << this->hierarchy_.size()), + this->chunk_size_), + 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); + // Consider the exception of vector push_back call. + std::unique_ptr smart_ptr (tmp); + // Has strong exception safety guarantee for call of push_back. + this->hierarchy_.push_back (smart_ptr.get ()); - smart_ptr.release (); - } + // 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; } @@ -278,12 +282,15 @@ ACE_Cascaded_Dynamic_Cached_Allocator::calloc (size_t, size_t, char) template void ACE_Cascaded_Dynamic_Cached_Allocator::free (void * ptr) { + if (ptr == nullptr) + return; + 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) + if (this->hierarchy_.size () != 0) this->hierarchy_[0]->free (ptr); } @@ -358,12 +365,15 @@ ACE_Cascaded_Dynamic_Cached_Allocator::pool_depth () { ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, 0)); - size_t pool_depth = 0; + size_t const size = this->hierarchy_.size (); + if (size == 0) + return 0; - for (size_t h = 0; h < this->hierarchy_.size (); h++) - { - pool_depth += this->hierarchy_[h]->pool_depth (); - } + // Only the first and last child allocator maybe has non-zero value, the others have zero value. + size_t pool_depth = this->hierarchy_[0]->pool_depth (); + + if (size > 1) + pool_depth += this->hierarchy_[size - 1]->pool_depth (); return pool_depth; } From b84e0c4b5122658dd66ea0c99727f5612983b8b2 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Thu, 4 Apr 2024 09:22:34 +0800 Subject: [PATCH 72/72] modify the branch judgement of the last allocator using const var --- ACE/ace/Malloc_T.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ACE/ace/Malloc_T.cpp b/ACE/ace/Malloc_T.cpp index 3fe81a0c84c22..31ccae938ae2a 100644 --- a/ACE/ace/Malloc_T.cpp +++ b/ACE/ace/Malloc_T.cpp @@ -231,16 +231,18 @@ ACE_Cascaded_Dynamic_Cached_Allocator::malloc (size_t nbytes) if (ptr != nullptr) return ptr; - if (size > 1) + // The code will possibly be optimized by compiler when using const var. + size_t const lastAllocatorPos = size - 1; + if (lastAllocatorPos > 0) { - ptr = this->hierarchy_[size - 1]->malloc (nbytes); + ptr = this->hierarchy_[lastAllocatorPos]->malloc (nbytes); if (ptr != nullptr) return ptr; } // Need alloc a new child allocator. comb_alloc_ptr tmp; - ACE_NEW_RETURN (tmp, comb_alloc_type (this->initial_n_chunks_ * (1 << this->hierarchy_.size()), + ACE_NEW_RETURN (tmp, comb_alloc_type (this->initial_n_chunks_ * (1 << size), this->chunk_size_), nullptr);