Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add simple example #5

Open
tigercosmos opened this issue Apr 3, 2018 · 1 comment
Open

add simple example #5

tigercosmos opened this issue Apr 3, 2018 · 1 comment

Comments

@tigercosmos
Copy link

show how to use this lib

@m110h
Copy link

m110h commented Jun 13, 2020

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants