refactor type storage and pool tests

This commit is contained in:
Francisco 2021-04-02 11:58:40 +01:00 committed by Francisco Paisana
parent 2723993740
commit a79ca92020
5 changed files with 65 additions and 53 deletions

View File

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

View File

@ -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++;

View File

@ -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<int> default_ctor_counter;
static std::atomic<int> dtor_counter;
};
int C::default_ctor_counter = 0;
int C::dtor_counter = 0;
std::atomic<int> C::default_ctor_counter(0);
std::atomic<int> C::dtor_counter(0);
srsran::big_obj_pool<C> 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;
}
}

View File

@ -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) {

View File

@ -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_;