fragmentedobjectallocator.cpp

// Fragmented object allocator
// 
// Simplified code as example for a fragmented object allocator.
//
// by feyd//godX.de
 
#include <cstdint>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#ifdef _WINDOWS
#include <windows.h>
#else
#include <time.h>
#endif
 
template<class _TYPE>
class CFragmentedObjectAllocator
{
public:
    CFragmentedObjectAllocator(size_t nSize) : 
        m_objects(nSize), m_fragments(nSize), 
        m_nFragmentPosition(0), m_nMaxPosition(0), 
        m_nSize(nSize) {};
    virtual ~CFragmentedObjectAllocator() {};
 
    virtual _TYPE* Allocate()
    {
        if(m_nFragmentPosition>0)
            return &m_objects[m_fragments[--m_nFragmentPosition]];
        if(m_nMaxPosition>=m_nSize)
            return nullptr;
        return &m_objects[m_nMaxPosition++];
    }
 
    virtual void Free(const _TYPE* pObject)
    {
        size_t nIndex = pObject-&m_objects[0];
        m_fragments[m_nFragmentPosition++] = nIndex;
    }
private:
    std::vector<_TYPE> m_objects;
    std::vector<size_t> m_fragments;
    size_t m_nFragmentPosition;
    size_t m_nMaxPosition;
    size_t m_nSize;
};
 
template<class _TYPE>
class CNormalObjectAllocator
{
public:
    CNormalObjectAllocator(size_t nSize) {};
    virtual ~CNormalObjectAllocator() {};
 
    virtual _TYPE* Allocate()
    {
        return new _TYPE;
    }
 
    virtual void Free(const _TYPE* pObject)
    {
        delete pObject;
    }
};
 
struct BinaryTreeNode
{
    BinaryTreeNode* pParent;
    BinaryTreeNode* pSide[2];
    bool bWordEnd;
};
 
template<class _ALLOC>
class CWordIndex : _ALLOC
{
public:
    CWordIndex(size_t nSize) : _ALLOC(nSize)
    {
        m_pRoot = _ALLOC::Allocate();
        m_pRoot->pParent = nullptr;
        m_pRoot->pSide[0] = nullptr;
        m_pRoot->pSide[1] = nullptr;
        m_pRoot->bWordEnd = false;
    }
 
    virtual ~CWordIndex()
    {
        RecursiveDelete(m_pRoot);
        _ALLOC::Free(m_pRoot);
    }
 
    void Add(const char* pWord)
    {
        BinaryTreeNode* pIndex = Find(pWord, true);
        pIndex->bWordEnd = true;
    }
 
    void Remove(const char* pWord)
    {
        BinaryTreeNode* pIndex = Find(pWord, false);
        if(pIndex)
            pIndex->bWordEnd = false;
        while(pIndex && pIndex!=m_pRoot)
        {
            if(pIndex->pSide[0] || pIndex->pSide[1])
                break;
 
            BinaryTreeNode* pParent = pIndex->pParent;
            if(pParent->pSide[0]==pIndex)
                pParent->pSide[0] = nullptr;
            if(pParent->pSide[1]==pIndex)
                pParent->pSide[1] = nullptr;
            _ALLOC::Free(pIndex);
            pIndex = pParent;
        }
    }
 
    BinaryTreeNode* Find(const char* pWord, bool bCreate=false)
    {
        BinaryTreeNode* pIndex = m_pRoot;
        while(*pWord)
        {
            for(int n=7; n>=0; n--)
            {
                int nSide = (*pWord>>n)&1;
                if(!pIndex->pSide[nSide])
                {
                    if(!bCreate)
                        return nullptr;
                    BinaryTreeNode* pNewIndex = _ALLOC::Allocate();
                    pIndex->pSide[nSide] = pNewIndex;
                    pNewIndex->pParent = pIndex;
                    pNewIndex->pSide[0] = nullptr;
                    pNewIndex->pSide[1] = nullptr;
                    pNewIndex->bWordEnd = false;
                    pIndex = pNewIndex;
                } else {
                    pIndex = pIndex->pSide[nSide];
                }
            }
            pWord++;
        }
        return pIndex;
    }
 
private:
    void RecursiveDelete(BinaryTreeNode* pIndex)
    {
        if(!pIndex)
            return;
        RecursiveDelete(pIndex->pSide[0]);
        RecursiveDelete(pIndex->pSide[1]);
    }
    BinaryTreeNode* m_pRoot;
};
 
uint64_t GetTime()
{
#ifdef _WINDOWS
    return GetTickCount();
#else
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC, &t);
    return t.tv_sec*1000+t.tv_nsec/1000000;
#endif
}
 
template<class _ALLOC>
class CTest
{
public:
    CTest(const char* pType, int argc, const char* argv[], int nNumberOfTests)
    {
        CWordIndex<_ALLOC> index(16384);
 
        int nTime = GetTime();
        for(int m=0; m<nNumberOfTests; m++)
        {
            for(int n=1; n<argc-1; n++)
                index.Add(argv[n]);
            for(int n=1; n<argc-1; n++)
                index.Remove(argv[n]);
        }
        nTime = GetTime()-nTime;
        printf("    %s object allocator %dms for %d iterations\r\n", pType, nTime, nNumberOfTests);
 
        for(int n=1; n<argc-1; n++)
            index.Add(argv[n]);
        BinaryTreeNode* pIndex = index.Find(argv[argc-1]);
        if(!pIndex)
        {
            printf("    '%s' not found\r\n", argv[argc-1]);
        } else {
            if(!pIndex->bWordEnd)
            {
                printf("    '%s' not found\r\n", argv[argc-1]);
            } else {
                printf("    '%s' found\r\n", argv[argc-1]);
            }
        }       
    }
};
 
// Application entry point (from libc)
int main(int argc, const char* argv[])
{
    printf("Fragmented Object Allocator test...\r\n\r\n");
 
    if(argc>2)
    {
        int nNumberOfTests = 100000;
 
        CTest<CFragmentedObjectAllocator<BinaryTreeNode> > testFragmented("Fragmented", argc, argv, nNumberOfTests);
        CTest<CNormalObjectAllocator<BinaryTreeNode> > testNormal("Normal", argc, argv, nNumberOfTests);
    } else {
        printf("not enough arguments\r\n");
    }
    return 0;
}
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220