Skip to content
Snippets Groups Projects
Commit f46e3123 authored by Pietro Incardona's avatar Pietro Incardona
Browse files

Small fix on BHeapMemory

parent ddece6ce
No related branches found
No related tags found
No related merge requests found
......@@ -166,6 +166,20 @@ public:
HeapMemory::destroy();
buf_sz = 0;
}
/*! \brief swap the two mwmory object
*
* \param mem Memory to swap with
*
*/
void swap(BHeapMemory & mem)
{
HeapMemory::swap(mem);
size_t buf_sz_t = mem.buf_sz;
mem.buf_sz = buf_sz;
buf_sz = buf_sz_t;
}
};
......
......@@ -145,6 +145,38 @@ public:
else
std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
};
/*! \brief Swap the memory
*
* \param mem memory to swap
*
*/
void swap(HeapMemory & mem)
{
size_t alignement_tmp;
size_t sz_tmp;
byte * dm_tmp;
byte * dmOrig_tmp;
long int ref_cnt_tmp;
alignement_tmp = alignement;
sz_tmp = sz;
dm_tmp = dm;
dmOrig_tmp = dmOrig;
ref_cnt_tmp = ref_cnt;
alignement = mem.alignement;
sz = mem.sz;
dm = mem.dm;
dmOrig = mem.dmOrig;
ref_cnt = mem.ref_cnt;
mem.alignement = alignement_tmp;
mem.sz = sz_tmp;
mem.dm = dm_tmp;
mem.dmOrig = dmOrig_tmp;
mem.ref_cnt = ref_cnt_tmp;
}
};
......
......@@ -174,6 +174,45 @@ template<typename T> void Btest()
BOOST_REQUIRE_EQUAL(mem.size(),FIRST_ALLOCATION);
}
template<typename T> void Stest()
{
T mem1;
T mem2;
mem1.allocate(5*sizeof(size_t));
mem2.allocate(6*sizeof(size_t));
BOOST_REQUIRE_EQUAL(mem1.size(),5*sizeof(size_t));
BOOST_REQUIRE_EQUAL(mem2.size(),6*sizeof(size_t));
// get the pointer of the allocated memory and fill
size_t * ptr1 = (size_t *)mem1.getPointer();
size_t * ptr2 = (size_t *)mem2.getPointer();
for (size_t i = 0 ; i < 5 ; i++)
ptr1[i] = i;
for (size_t i = 0 ; i < 6 ; i++)
ptr2[i] = i+100;
mem1.swap(mem2);
bool ret = true;
ptr1 = (size_t *)mem2.getPointer();
ptr2 = (size_t *)mem1.getPointer();
for (size_t i = 0 ; i < 5 ; i++)
ret &= ptr1[i] == i;
for (size_t i = 0 ; i < 6 ; i++)
ret &= ptr2[i] == i+100;
BOOST_REQUIRE_EQUAL(ret,true);
BOOST_REQUIRE_EQUAL(mem1.size(),6*sizeof(size_t));
BOOST_REQUIRE_EQUAL(mem2.size(),5*sizeof(size_t));
}
BOOST_AUTO_TEST_CASE( use_heap_memory )
{
test<HeapMemory>();
......@@ -195,6 +234,11 @@ BOOST_AUTO_TEST_CASE( use_bheap_memory )
Btest<BHeapMemory>();
}
BOOST_AUTO_TEST_CASE( swap_heap_memory )
{
Stest<HeapMemory>();
}
BOOST_AUTO_TEST_SUITE_END()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment