medfall

A super great game engine
Log | Files | Refs

slab_allocator.hpp (4102B)


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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*  Relacy Race Detector
 *  Copyright (c) 2008-2010, Dmitry S. Vyukov
 *  All rights reserved.
 *  This software is provided AS-IS with no warranty, either express or implied.
 *  This software is distributed under a license and may not be copied,
 *  modified or distributed except as expressly authorized under the
 *  terms of the license contained in the file LICENSE.TXT in this distribution.
 */

#ifndef RL_SLAB_ALLOCATOR_HPP
#define RL_SLAB_ALLOCATOR_HPP
#ifdef _MSC_VER
#   pragma once
#endif

#include "base.hpp"


namespace rl
{


template<typename type>
class slab_allocator : nocopy<>
{
public:
    slab_allocator()
        : freelist_()
        , blocks_()
        , alloc_count_()
    {
    }

    ~slab_allocator()
    {
        char* pos = blocks_;
        while (pos)
        {
            char* const next = *reinterpret_cast<char**>(pos);
            ::free(pos);
            pos = next;
        }
    }

    type* alloc(void* ctx = 0)
    {
        if (freelist_)
        {
            type* p = freelist_;
            freelist_ = *reinterpret_cast<type**>(p);
            alloc_count_ += 1;
            *(void**)p = ctx;
            type* pp = reinterpret_cast<type*>((reinterpret_cast<void**>(p) + 1));
            return pp;
        }
        else
        {
            return alloc_batch();
        }
    }

    void free(type* p)
    {
        type** pos = reinterpret_cast<type**>((reinterpret_cast<void**>(p) - 1));
        pos[0] = freelist_;
        freelist_ = reinterpret_cast<type*>(pos);
        alloc_count_ -= 1;
    }

    bool iteration_end()
    {
#ifndef RL_GC
        return alloc_count_ == 0;
#else
        freelist_ = 0;
        size_t elem_size = sizeof(void*) + sizeof(type);
        elem_size = (elem_size + 15) & ~15;
        char* pos = blocks_;
        while (pos)
        {
            char* p = pos;
            p += elem_size;
            for (size_t i = 0; i != batch_size; ++i)
            {
                *reinterpret_cast<type**>(p) = freelist_;
                freelist_ = reinterpret_cast<type*>(p);
                p += elem_size;
            }
            pos = *reinterpret_cast<char**>(pos);
        }
        return true;
#endif
    }

    void output_allocs(std::ostream& stream)
    {
        size_t elem_size = sizeof(void*) + sizeof(type);
        elem_size = (elem_size + 15) & ~15;
        set<void*>::type allocs;
        char* pos = blocks_;
        while (pos)
        {
            char* p = pos;
            p += elem_size;
            for (size_t i = 0; i != batch_size; ++i)
            {
                allocs.insert(p);
                p += elem_size;
            }
            pos = *reinterpret_cast<char**>(pos);
        }
        set<void*>::type avail;
        type* pos2 = freelist_;
        while (pos2)
        {
            avail.insert(pos2);
            pos2 = *reinterpret_cast<type**>(pos2);
        }
        vector<void*>::type diff;
        std::set_difference(allocs.begin(), allocs.end(), avail.begin(), avail.end(), std::back_inserter(diff));
        for (size_t i = 0; i != diff.size(); ++i)
        {
            stream << *(void**)diff[i] << std::endl;
        }
    }

private:
    static size_t const batch_size = 128;
    type* freelist_;
    char* blocks_;
    size_t alloc_count_;

    RL_NOINLINE type* alloc_batch()
    {
        size_t elem_size = sizeof(void*) + sizeof(type);
        elem_size = (elem_size + 15) & ~15;
        char* const batch = (char*)(::malloc)(elem_size * (batch_size + 1));
        if (0 == batch)
            throw std::bad_alloc();
        *reinterpret_cast<char**>(batch) = blocks_;
        blocks_ = batch;
        char* p = batch;
        p += elem_size;
        for (size_t i = 0; i != batch_size; ++i)
        {
            *reinterpret_cast<type**>(p) = freelist_;
            freelist_ = reinterpret_cast<type*>(p);
            p += elem_size;
        }
        return alloc();
    }
};


}

#endif