refactor type storage and pool tests

master
Francisco 4 years ago committed by Francisco Paisana
parent 2723993740
commit a79ca92020

@ -13,6 +13,7 @@
#ifndef SRSRAN_TYPE_STORAGE_H #ifndef SRSRAN_TYPE_STORAGE_H
#define SRSRAN_TYPE_STORAGE_H #define SRSRAN_TYPE_STORAGE_H
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -32,7 +33,7 @@ union max_alignment_t {
uint32_t* ptr; uint32_t* ptr;
}; };
template <typename T> template <typename T, size_t MinSize = 0, size_t AlignSize = 0>
struct type_storage { struct type_storage {
using value_type = T; using value_type = T;
@ -42,19 +43,29 @@ struct type_storage {
new (&buffer) T(std::forward<Args>(args)...); new (&buffer) T(std::forward<Args>(args)...);
} }
void destroy() { get().~T(); } void destroy() { get().~T(); }
void copy_ctor(const type_storage<T>& other) { emplace(other.get()); } void copy_ctor(const type_storage& other) { emplace(other.get()); }
void move_ctor(type_storage<T>&& other) { emplace(std::move(other.get())); } void move_ctor(type_storage&& other) { emplace(std::move(other.get())); }
void copy_assign(const type_storage<T>& other) { get() = other.get(); } void copy_assign(const type_storage& other) { get() = other.get(); }
void move_assign(type_storage<T>&& other) { get() = std::move(other.get()); } void move_assign(type_storage&& other) { get() = std::move(other.get()); }
T& get() { return reinterpret_cast<T&>(buffer); } T& get() { return reinterpret_cast<T&>(buffer); }
const T& get() const { return reinterpret_cast<const T&>(buffer); } const T& get() const { return reinterpret_cast<const T&>(buffer); }
typename std::aligned_storage<sizeof(T), alignof(T)>::type buffer; void* addr() { return static_cast<void*>(&buffer); }
const void* addr() const { return static_cast<void*>(&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<obj_size, align_size>::type buffer;
}; };
template <typename T> template <typename T, size_t MinSize, size_t AlignSize>
void copy_if_present_helper(type_storage<T>& lhs, const type_storage<T>& rhs, bool lhs_present, bool rhs_present) void copy_if_present_helper(type_storage<T, MinSize, AlignSize>& lhs,
const type_storage<T, MinSize, AlignSize>& rhs,
bool lhs_present,
bool rhs_present)
{ {
if (lhs_present and rhs_present) { if (lhs_present and rhs_present) {
lhs.get() = rhs.get(); lhs.get() = rhs.get();
@ -67,8 +78,11 @@ void copy_if_present_helper(type_storage<T>& lhs, const type_storage<T>& rhs, bo
} }
} }
template <typename T> template <typename T, size_t MinSize, size_t AlignSize>
void move_if_present_helper(type_storage<T>& lhs, type_storage<T>& rhs, bool lhs_present, bool rhs_present) void move_if_present_helper(type_storage<T, MinSize, AlignSize>& lhs,
type_storage<T, MinSize, AlignSize>& rhs,
bool lhs_present,
bool rhs_present)
{ {
if (lhs_present and rhs_present) { if (lhs_present and rhs_present) {
lhs.move_assign(std::move(rhs)); lhs.move_assign(std::move(rhs));

@ -45,7 +45,6 @@ public:
void push(void* block) noexcept void push(void* block) noexcept
{ {
// printf("head: %ld\n", (long)head);
node* next = ::new (block) node(head); node* next = ::new (block) node(head);
head = next; head = next;
count++; count++;

@ -21,13 +21,14 @@ public:
~C() { dtor_counter++; } ~C() { dtor_counter++; }
void* operator new(size_t sz); void* operator new(size_t sz);
void* operator new(size_t sz, void*& ptr) { return ptr; }
void operator delete(void* ptr)noexcept; void operator delete(void* ptr)noexcept;
static int default_ctor_counter; static std::atomic<int> default_ctor_counter;
static int dtor_counter; static std::atomic<int> dtor_counter;
}; };
int C::default_ctor_counter = 0; std::atomic<int> C::default_ctor_counter(0);
int C::dtor_counter = 0; std::atomic<int> C::dtor_counter(0);
srsran::big_obj_pool<C> pool; srsran::big_obj_pool<C> pool;
@ -41,7 +42,7 @@ void C::operator delete(void* ptr)noexcept
pool.deallocate_node(ptr); pool.deallocate_node(ptr);
} }
int test_nontrivial_obj_pool() void test_nontrivial_obj_pool()
{ {
// No object creation on reservation // No object creation on reservation
{ {
@ -72,8 +73,6 @@ int test_nontrivial_obj_pool()
} }
TESTASSERT(C::default_ctor_counter == 1); TESTASSERT(C::default_ctor_counter == 1);
TESTASSERT(C::dtor_counter == 1); TESTASSERT(C::dtor_counter == 1);
return SRSRAN_SUCCESS;
} }
struct BigObj { struct BigObj {
@ -144,9 +143,9 @@ int main(int argc, char** argv)
{ {
srsran::test_init(argc, argv); srsran::test_init(argc, argv);
TESTASSERT(test_nontrivial_obj_pool() == SRSRAN_SUCCESS); test_nontrivial_obj_pool();
test_fixedsize_pool(); test_fixedsize_pool();
srsran::console("Success\n"); printf("Success\n");
return 0; return 0;
} }

@ -47,42 +47,42 @@ bool mac::init(const mac_args_t& args_,
{ {
started = false; started = false;
if (phy && rlc) { if (not phy or not rlc) {
phy_h = phy; return false;
rlc_h = rlc; }
rrc_h = rrc; phy_h = phy;
rlc_h = rlc;
rrc_h = rrc;
args = args_; args = args_;
cells = cells_; 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 // Init softbuffer for SI messages
common_buffers.resize(cells.size()); common_buffers.resize(cells.size());
for (auto& cc : common_buffers) { for (auto& cc : common_buffers) {
for (int i = 0; i < NOF_BCCH_DLSCH_MSG; i++) { for (int i = 0; i < NOF_BCCH_DLSCH_MSG; i++) {
srsran_softbuffer_tx_init(&cc.bcch_softbuffer_tx[i], args.nof_prb); 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 PCCH
srsran_softbuffer_tx_init(&cc.pcch_softbuffer_tx, args.nof_prb);
reset(); // Init softbuffer for RAR
srsran_softbuffer_tx_init(&cc.rar_softbuffer_tx, args.nof_prb);
}
// Pre-alloc UE objects for first attaching users reset();
prealloc_ue(10);
detected_rachs.resize(cells.size()); // Pre-alloc UE objects for first attaching users
prealloc_ue(10);
started = true; detected_rachs.resize(cells.size());
}
return started; started = true;
return true;
} }
void mac::stop() 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) int requested_bytes = (mcs_data.tbs / 8 > (int)mch.mtch_sched[mtch_index].lcid_buffer_size)
? (mch.mtch_sched[mtch_index].lcid_buffer_size) ? (mch.mtch_sched[mtch_index].lcid_buffer_size)
: ((mcs_data.tbs / 8) - 2); : ((mcs_data.tbs / 8) - 2);
int bytes_received = ue_db[SRSRAN_MRNTI]->read_pdu(current_lcid, mtch_payload_buffer, requested_bytes); 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].lcid = current_lcid;
mch.pdu[0].nbytes = bytes_received; mch.pdu[0].nbytes = bytes_received;
mch.mtch_sched[0].mtch_payload = mtch_payload_buffer; mch.mtch_sched[0].mtch_payload = mtch_payload_buffer;
dl_sched_res->pdsch[0].dci.rnti = SRSRAN_MRNTI; dl_sched_res->pdsch[0].dci.rnti = SRSRAN_MRNTI;
if (bytes_received) { if (bytes_received) {

@ -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_) 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_prb = nof_prb_;
nof_rx_harq_proc = nof_rx_harq_proc_; nof_rx_harq_proc = nof_rx_harq_proc_;
nof_tx_harq_proc = nof_tx_harq_proc_; nof_tx_harq_proc = nof_tx_harq_proc_;

Loading…
Cancel
Save