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,7 +47,9 @@ bool mac::init(const mac_args_t& args_,
{ {
started = false; started = false;
if (phy && rlc) { if (not phy or not rlc) {
return false;
}
phy_h = phy; phy_h = phy;
rlc_h = rlc; rlc_h = rlc;
rrc_h = rrc; rrc_h = rrc;
@ -80,9 +82,7 @@ bool mac::init(const mac_args_t& args_,
detected_rachs.resize(cells.size()); detected_rachs.resize(cells.size());
started = true; started = true;
} return true;
return started;
} }
void mac::stop() void mac::stop()

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