From a79ca920206f42f8d166b8ceca3086fb200fae4e Mon Sep 17 00:00:00 2001 From: Francisco Date: Fri, 2 Apr 2021 11:58:40 +0100 Subject: [PATCH] refactor type storage and pool tests --- lib/include/srsran/adt/detail/type_storage.h | 34 +++++++---- lib/include/srsran/adt/pool/memblock_cache.h | 1 - lib/test/adt/mem_pool_test.cc | 19 +++--- srsenb/src/stack/mac/mac.cc | 62 ++++++++++---------- srsenb/src/stack/mac/ue.cc | 2 +- 5 files changed, 65 insertions(+), 53 deletions(-) diff --git a/lib/include/srsran/adt/detail/type_storage.h b/lib/include/srsran/adt/detail/type_storage.h index ca9c6b8f0..651a25300 100644 --- a/lib/include/srsran/adt/detail/type_storage.h +++ b/lib/include/srsran/adt/detail/type_storage.h @@ -13,6 +13,7 @@ #ifndef SRSRAN_TYPE_STORAGE_H #define SRSRAN_TYPE_STORAGE_H +#include #include #include #include @@ -32,7 +33,7 @@ union max_alignment_t { uint32_t* ptr; }; -template +template struct type_storage { using value_type = T; @@ -42,19 +43,29 @@ struct type_storage { new (&buffer) T(std::forward(args)...); } void destroy() { get().~T(); } - void copy_ctor(const type_storage& other) { emplace(other.get()); } - void move_ctor(type_storage&& other) { emplace(std::move(other.get())); } - void copy_assign(const type_storage& other) { get() = other.get(); } - void move_assign(type_storage&& other) { get() = std::move(other.get()); } + void copy_ctor(const type_storage& other) { emplace(other.get()); } + void move_ctor(type_storage&& other) { emplace(std::move(other.get())); } + void copy_assign(const type_storage& other) { get() = other.get(); } + void move_assign(type_storage&& other) { get() = std::move(other.get()); } T& get() { return reinterpret_cast(buffer); } const T& get() const { return reinterpret_cast(buffer); } - typename std::aligned_storage::type buffer; + void* addr() { return static_cast(&buffer); } + const void* addr() const { return static_cast(&buffer); } + explicit operator void*() { return addr(); } + + const static size_t obj_size = sizeof(T) > MinSize ? sizeof(T) : MinSize; + const static size_t align_size = alignof(T) > AlignSize ? alignof(T) : AlignSize; + + typename std::aligned_storage::type buffer; }; -template -void copy_if_present_helper(type_storage& lhs, const type_storage& rhs, bool lhs_present, bool rhs_present) +template +void copy_if_present_helper(type_storage& lhs, + const type_storage& rhs, + bool lhs_present, + bool rhs_present) { if (lhs_present and rhs_present) { lhs.get() = rhs.get(); @@ -67,8 +78,11 @@ void copy_if_present_helper(type_storage& lhs, const type_storage& rhs, bo } } -template -void move_if_present_helper(type_storage& lhs, type_storage& rhs, bool lhs_present, bool rhs_present) +template +void move_if_present_helper(type_storage& lhs, + type_storage& rhs, + bool lhs_present, + bool rhs_present) { if (lhs_present and rhs_present) { lhs.move_assign(std::move(rhs)); diff --git a/lib/include/srsran/adt/pool/memblock_cache.h b/lib/include/srsran/adt/pool/memblock_cache.h index aee7d7dd1..a0be95bdc 100644 --- a/lib/include/srsran/adt/pool/memblock_cache.h +++ b/lib/include/srsran/adt/pool/memblock_cache.h @@ -45,7 +45,6 @@ public: void push(void* block) noexcept { - // printf("head: %ld\n", (long)head); node* next = ::new (block) node(head); head = next; count++; diff --git a/lib/test/adt/mem_pool_test.cc b/lib/test/adt/mem_pool_test.cc index a98a5c59e..a7f487268 100644 --- a/lib/test/adt/mem_pool_test.cc +++ b/lib/test/adt/mem_pool_test.cc @@ -21,13 +21,14 @@ public: ~C() { dtor_counter++; } void* operator new(size_t sz); + void* operator new(size_t sz, void*& ptr) { return ptr; } void operator delete(void* ptr)noexcept; - static int default_ctor_counter; - static int dtor_counter; + static std::atomic default_ctor_counter; + static std::atomic dtor_counter; }; -int C::default_ctor_counter = 0; -int C::dtor_counter = 0; +std::atomic C::default_ctor_counter(0); +std::atomic C::dtor_counter(0); srsran::big_obj_pool pool; @@ -41,7 +42,7 @@ void C::operator delete(void* ptr)noexcept pool.deallocate_node(ptr); } -int test_nontrivial_obj_pool() +void test_nontrivial_obj_pool() { // No object creation on reservation { @@ -72,8 +73,6 @@ int test_nontrivial_obj_pool() } TESTASSERT(C::default_ctor_counter == 1); TESTASSERT(C::dtor_counter == 1); - - return SRSRAN_SUCCESS; } struct BigObj { @@ -144,9 +143,9 @@ int main(int argc, char** argv) { srsran::test_init(argc, argv); - TESTASSERT(test_nontrivial_obj_pool() == SRSRAN_SUCCESS); + test_nontrivial_obj_pool(); test_fixedsize_pool(); - srsran::console("Success\n"); + printf("Success\n"); return 0; -} \ No newline at end of file +} diff --git a/srsenb/src/stack/mac/mac.cc b/srsenb/src/stack/mac/mac.cc index 95727f4cd..52726707d 100644 --- a/srsenb/src/stack/mac/mac.cc +++ b/srsenb/src/stack/mac/mac.cc @@ -47,42 +47,42 @@ bool mac::init(const mac_args_t& args_, { started = false; - if (phy && rlc) { - phy_h = phy; - rlc_h = rlc; - rrc_h = rrc; + if (not phy or not rlc) { + return false; + } + phy_h = phy; + rlc_h = rlc; + rrc_h = rrc; - args = args_; - cells = cells_; + args = args_; + cells = cells_; - stack_task_queue = task_sched.make_task_queue(); + stack_task_queue = task_sched.make_task_queue(); - scheduler.init(rrc, args.sched); + scheduler.init(rrc, args.sched); - // Init softbuffer for SI messages - common_buffers.resize(cells.size()); - for (auto& cc : common_buffers) { - for (int i = 0; i < NOF_BCCH_DLSCH_MSG; i++) { - srsran_softbuffer_tx_init(&cc.bcch_softbuffer_tx[i], args.nof_prb); - } - // Init softbuffer for PCCH - srsran_softbuffer_tx_init(&cc.pcch_softbuffer_tx, args.nof_prb); - - // Init softbuffer for RAR - srsran_softbuffer_tx_init(&cc.rar_softbuffer_tx, args.nof_prb); + // Init softbuffer for SI messages + common_buffers.resize(cells.size()); + for (auto& cc : common_buffers) { + for (int i = 0; i < NOF_BCCH_DLSCH_MSG; i++) { + srsran_softbuffer_tx_init(&cc.bcch_softbuffer_tx[i], args.nof_prb); } + // Init softbuffer for PCCH + srsran_softbuffer_tx_init(&cc.pcch_softbuffer_tx, args.nof_prb); - reset(); - - // Pre-alloc UE objects for first attaching users - prealloc_ue(10); - - detected_rachs.resize(cells.size()); - - started = true; + // Init softbuffer for RAR + srsran_softbuffer_tx_init(&cc.rar_softbuffer_tx, args.nof_prb); } - return started; + reset(); + + // Pre-alloc UE objects for first attaching users + prealloc_ue(10); + + detected_rachs.resize(cells.size()); + + started = true; + return true; } void mac::stop() @@ -832,9 +832,9 @@ int mac::get_mch_sched(uint32_t tti, bool is_mcch, dl_sched_list_t& dl_sched_res int requested_bytes = (mcs_data.tbs / 8 > (int)mch.mtch_sched[mtch_index].lcid_buffer_size) ? (mch.mtch_sched[mtch_index].lcid_buffer_size) : ((mcs_data.tbs / 8) - 2); - int bytes_received = ue_db[SRSRAN_MRNTI]->read_pdu(current_lcid, mtch_payload_buffer, requested_bytes); - mch.pdu[0].lcid = current_lcid; - mch.pdu[0].nbytes = bytes_received; + int bytes_received = ue_db[SRSRAN_MRNTI]->read_pdu(current_lcid, mtch_payload_buffer, requested_bytes); + mch.pdu[0].lcid = current_lcid; + mch.pdu[0].nbytes = bytes_received; mch.mtch_sched[0].mtch_payload = mtch_payload_buffer; dl_sched_res->pdsch[0].dci.rnti = SRSRAN_MRNTI; if (bytes_received) { diff --git a/srsenb/src/stack/mac/ue.cc b/srsenb/src/stack/mac/ue.cc index 909f9301a..e0fc9f6c2 100644 --- a/srsenb/src/stack/mac/ue.cc +++ b/srsenb/src/stack/mac/ue.cc @@ -138,7 +138,7 @@ cc_buffer_handler::~cc_buffer_handler() */ void cc_buffer_handler::allocate_cc(uint32_t nof_prb_, uint32_t nof_rx_harq_proc_, uint32_t nof_tx_harq_proc_) { - assert(empty()); + srsran_assert(empty(), "Cannot allocate softbuffers in CC that is already initialized"); nof_prb = nof_prb_; nof_rx_harq_proc = nof_rx_harq_proc_; nof_tx_harq_proc = nof_tx_harq_proc_;