summaryrefslogtreecommitdiff
path: root/lib/theron_allocator.cpp
blob: e5b3748e3c801601b553c3e5b84b878638639568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Copyright 2012 Josh Blum
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with io_sig program.  If not, see <http://www.gnu.org/licenses/>.

/***********************************************************************
 * There is allocation overhead for sending messages.
 * Want per worker-allocator for message allocation...
 * But until thats possible, install a new global allocator.
 * This allocator uses a fixed pool for small sized buffers,
 * and otherwise the regular malloc/free for larger buffers.
 **********************************************************************/

#include <gras/gras.hpp>
#include <gras_impl/debug.hpp>
#include <Theron/Detail/Threading/SpinLock.h>
#include <Theron/IAllocator.h>
#include <Theron/AllocatorManager.h>
#include <boost/circular_buffer.hpp>

#define MY_ALLOCATOR_CHUNK_SIZE 256
#define MY_ALLOCATOR_POOL_SIZE (MY_ALLOCATOR_CHUNK_SIZE * (1 << 16))

static struct WorkerAllocator : Theron::IAllocator
{
    WorkerAllocator(void)
    {
        const size_t N = MY_ALLOCATOR_POOL_SIZE/MY_ALLOCATOR_CHUNK_SIZE;
        queue.resize(N);
        for (size_t i = 0; i < N; i++)
        {
            const ptrdiff_t pool_ptr = ptrdiff_t(pool) + i*MY_ALLOCATOR_CHUNK_SIZE;
            queue.push_back((void *)pool_ptr);
        }
        pool_end = ptrdiff_t(pool) + MY_ALLOCATOR_POOL_SIZE;
        Theron::AllocatorManager::Instance().SetAllocator(this);
    }

    ~WorkerAllocator(void)
    {
        //NOP
    }

    void *Allocate(const SizeType size)
    {
        if (size <= MY_ALLOCATOR_CHUNK_SIZE)
        {
            mSpinLock.Lock();
            if (queue.empty())
            {
                mSpinLock.Unlock();
                std::cout << "~" << std::flush;
                return std::malloc(size);
            }
            void *memory = queue.front();
            queue.pop_front();
            mSpinLock.Unlock();
            return memory;
        }
        else
        {
            //std::cout << "malloc size " << size << std::endl;
            return new char[size];
        }
    }

    void Free(void *const memory)
    {
        const bool in_pool = ptrdiff_t(memory) >= ptrdiff_t(pool) and ptrdiff_t(memory) < pool_end;
        if (in_pool)
        {
            mSpinLock.Lock();
            queue.push_front(memory);
            mSpinLock.Unlock();
        }
        else
        {
            delete [] ((char *)memory);
        }
    }

    boost::circular_buffer<void *> queue;
    THERON_PREALIGN(GRAS_MAX_ALIGNMENT)
        char pool[MY_ALLOCATOR_POOL_SIZE]
    THERON_POSTALIGN(GRAS_MAX_ALIGNMENT);
    ptrdiff_t pool_end;
    Theron::Detail::SpinLock mSpinLock;

} my_alloc;