Comments (1)
Catch it
#include <iostream>
#include <string>
#include <array>
#include "freelistallocator.h"
class Base
{
public:
Base()
{
std::cout << "base constructor" << std::endl;
}
virtual ~Base()
{
std::cout << "base destructor" << std::endl;
}
virtual void Does() = 0;
};
class Child: public Base
{
public:
Child(const std::string& _msg)
{
std::cout << "child constructor" << std::endl;
msg = _msg;
}
~Child()
{
std::cout << "child destructor" << std::endl;
}
void Does()
{
std::cout << msg << std::endl;
}
private:
std::string msg {""};
};
class System
{
public:
System(const System& src) = delete;
~System()
{
std::cout << "~System()" << std::endl;
_allocator.Reset();
}
Allocator* GetAllocator()
{
return &_allocator;
}
void SayHello() const
{
std::cout << "Hello from System" << std::endl;
}
static System& getInstance()
{
static System _instance;
return _instance;
}
private:
System(): _allocator(sizeof(Child)*1000, FreeListAllocator::FIND_FIRST)
{
std::cout << "System()" << std::endl;
_allocator.Init();
}
private:
FreeListAllocator _allocator;
};
void TestSingleAllocation()
{
Allocator* _allocator = System::getInstance().GetAllocator();
Base* b_ptr = nullptr;
{
Child* c_ptr = (Child*)_allocator->Allocate(sizeof(Child),8);
if (c_ptr)
{
new(c_ptr) Child("\tchild does smth");
b_ptr = c_ptr;
}
}
if (b_ptr)
{
b_ptr->Does();
b_ptr->~Base();
_allocator->Free(b_ptr);
b_ptr = nullptr;
}
}
void TestMultiAllocation()
{
std::array<Base*,10> container {nullptr};
Allocator* _allocator = System::getInstance().GetAllocator();
for (size_t i=0; i<container.size(); i++)
{
Base* b_ptr = nullptr;
{
Child* c_ptr = (Child*)_allocator->Allocate(sizeof(Child),8);
if (c_ptr)
{
new(c_ptr) Child("\tchild");
b_ptr = c_ptr;
}
}
container[i] = b_ptr;
}
for (size_t i=0; i<container.size(); i++)
{
if (container[i])
container[i]->Does();
}
// std::cout << "total: " << _allocator->GetTotal() << " used: " << _allocator->GetUsed() << std::endl;
for (size_t i=0; i<container.size(); i++)
{
Base* b_ptr = container[i];
if (b_ptr)
{
b_ptr->~Base();
_allocator->Free(b_ptr);
container[i]=nullptr;
}
}
// std::cout << "total: " << _allocator->GetTotal() << " used: " << _allocator->GetUsed() << std::endl;
}
int main()
{
System::getInstance().SayHello();
//TestSingleAllocation();
TestMultiAllocation();
return 0;
}
from memory-allocators.
Related Issues (20)
- Can you share rest ppt of Dynamic memory ? HOT 2
- FreeListAllocator :: FindBest() question HOT 1
- I made mistake, sorry
- Bug in Free List Allocator on 32bit systems HOT 3
- Build errors HOT 3
- How to use allocated memory HOT 3
- why use virtual method HOT 1
- find a small bug here HOT 2
- Make AllocationHeader::padding size_t
- StackAllocator is not performant and wastes memory on padding
- Optimize PoolAllocator initialization by adding an Offset variable?
- Found a bug in StackAllocator
- using malloc on custom memory allocator HOT 1
- Can't compile the code. HOT 4
- Question about FreeListAllocator HOT 2
- Error while compiling in MSVC HOT 3
- consider adding tests HOT 1
- AllocationHeader is not copied from stack to the memory position HOT 2
- Potential infinite loop caused by linked list node pointing to itself HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memory-allocators.