Commit 3f05464c authored by BRAMAS Berenger's avatar BRAMAS Berenger

Do not use block allocator because of Gcc bug

parent 5d75ad58
#ifndef FBLOCKALLOCATOR_HPP
#define FBLOCKALLOCATOR_HPP
#include <list>
#include <list>
#include <cstring>
#include "../Utils/FAssert.hpp"
/**
* What a cell allocator should implement
*/
......@@ -43,28 +46,39 @@ public:
*/
template <class ObjectClass, int SizeOfBlock >
class FListBlockAllocator : public FAbstractBlockAllocator<ObjectClass>{
static_assert(SizeOfBlock >= 1, "SizeOfBlock should be 1 minimum");
class CellsBlock {
CellsBlock(const CellsBlock&) = delete;
CellsBlock& operator=(const CellsBlock&) = delete;
int nbObjectInBlock;
ObjectClass*const objectPtr;
unsigned char usedBlocks[SizeOfBlock];
unsigned char objectMemory[SizeOfBlock * sizeof(ObjectClass)];
public:
CellsBlock() : nbObjectInBlock(0){
CellsBlock() : nbObjectInBlock(0), objectPtr(reinterpret_cast<ObjectClass*>(objectMemory)){
memset(usedBlocks, 0, sizeof(unsigned char) * SizeOfBlock);
}
~CellsBlock(){
FAssertLF(nbObjectInBlock == 0x0);
}
bool isInsideBlock(const ObjectClass*const cellPtr) const{
const unsigned char*const inPtr = reinterpret_cast<const unsigned char*>( cellPtr );
return objectMemory <= inPtr && inPtr < (objectMemory+ SizeOfBlock * sizeof(ObjectClass));
return objectPtr <= cellPtr && cellPtr < &objectPtr[SizeOfBlock];
}
int getPositionInBlock(const ObjectClass*const cellPtr) const{
const unsigned char*const inPtr = reinterpret_cast<const unsigned char*>( cellPtr );
return int((inPtr - objectMemory) / sizeof(ObjectClass));
const int position = int((cellPtr - objectPtr));
FAssertLF(usedBlocks[position] != 0x0);
return position;
}
void deleteObject(const int position){
reinterpret_cast<ObjectClass*>(objectMemory)[position].~ObjectClass();
FAssertLF(usedBlocks[position] != 0x0);
objectPtr[position].~ObjectClass();
nbObjectInBlock -= 1;
usedBlocks[position] = 0x0;
}
......@@ -74,8 +88,8 @@ class FListBlockAllocator : public FAbstractBlockAllocator<ObjectClass>{
if(usedBlocks[idx] == 0){
nbObjectInBlock += 1;
usedBlocks[idx] = 0x1;
new (&reinterpret_cast<ObjectClass*>(objectMemory)[idx]) ObjectClass;
return &reinterpret_cast<ObjectClass*>(objectMemory)[idx];
new (&(objectPtr[idx])) ObjectClass;
return &(objectPtr[idx]);
}
}
......
......@@ -52,7 +52,7 @@
* at level 0 the space is not split
* CellAllocator can be FListBlockAllocator<CellClass, 10> or FBasicBlockAllocator<CellClass>
*/
template< class CellClass, class ContainerClass, class LeafClass, class CellAllocatorClass = FListBlockAllocator<CellClass, 15> >
template< class CellClass, class ContainerClass, class LeafClass, class CellAllocatorClass = FBasicBlockAllocator<CellClass> /*FListBlockAllocator<CellClass, 15>*/ >
class FOctree : public FNoCopyable {
typedef FSubOctreeWithLeafs< CellClass , ContainerClass, LeafClass, CellAllocatorClass> SubOctreeWithLeaves;
typedef FSubOctree< CellClass , ContainerClass, LeafClass, CellAllocatorClass> SubOctree;
......
......@@ -27,15 +27,16 @@
/**
* This class is simply used to count alloc dealloc
*/
static const int SizeArray = 50;
class TestObject{
public:
static int counter;
static int dealloced;
int array[50];
int array[SizeArray];
TestObject(){
memset(array, 0, 50 * sizeof(int));
memset(array, 0, SizeArray * sizeof(int));
++counter;
}
TestObject(const TestObject&){
......@@ -62,8 +63,9 @@ class TestBlock : public FUTester<TestBlock> {
void TestBlockFunction(){
FListBlockAllocator<TestObject, 10> alloc;
TestObject* ptr[100];
for(int idx = 0 ; idx < 100 ; ++idx){
const int NbAlloc = 2;
TestObject* ptr[NbAlloc];
for(int idx = 0 ; idx < NbAlloc ; ++idx){
TestObject* dl1 = alloc.newObject();
TestObject* dl2 = alloc.newObject();
alloc.deleteObject(dl1);
......@@ -71,23 +73,23 @@ class TestBlock : public FUTester<TestBlock> {
alloc.deleteObject(dl2);
}
for(int idx = 0 ; idx < 100 ; ++idx){
for(int idxval = 0 ; idxval < 50 ; ++idxval){
for(int idx = 0 ; idx < 2 ; ++idx){
for(int idxval = 0 ; idxval < 17 ; ++idxval){
ptr[idx]->array[idxval] += (idxval * idx);
}
}
for(int idx = 0 ; idx < 100 ; ++idx){
for(int idxval = 0 ; idxval < 50 ; ++idxval){
for(int idx = 0 ; idx < NbAlloc ; ++idx){
for(int idxval = 0 ; idxval < SizeArray ; ++idxval){
uassert(ptr[idx]->array[idxval] == (idxval * idx));
}
}
for(int idx = 0 ; idx < 100 ; ++idx){
for(int idx = 0 ; idx < NbAlloc ; ++idx){
alloc.deleteObject(ptr[idx]);
}
uassert(TestObject::counter == 300);
uassert(TestObject::counter == (3*NbAlloc));
uassert(TestObject::counter == TestObject::dealloced);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment