File: blockallocator.h

package info (click to toggle)
dc-qt 0.2.0.alpha-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,936 kB
  • ctags: 5,361
  • sloc: cpp: 28,936; makefile: 19
file content (102 lines) | stat: -rw-r--r-- 2,713 bytes parent folder | download | duplicates (4)
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
#ifndef __blockallocator_h__
#define __blockallocator_h__

#include <vector>
#include <iostream>

using namespace std;

typedef unsigned char byte;

//! Custom memory allocator for single items
template <class T, size_t blocksPerBatch=100, size_t blockAlignment=4>
class BlockAllocator
{
public:
    void* operator new(size_t)
    {
        //cerr << "blocknew\n";
		return s_Store.AllocateBlock();
    }

    void operator delete(void* pBlock)
    {
		//cerr << "blockdelete" << endl;
        s_Store.ReleaseBlock((T*)pBlock);
    }

    static T*	AllocateBlock()
    {
        return s_Store.AllocateBlock();
    }
    static void	ReleaseBlock(T* pBlock)
    {
        s_Store.ReleaseBlock(pBlock);
    }

private:

    struct BlockStore
    {
        BlockStore() : ppNextBlock(0)
        {}
        ;
        ~BlockStore()
        {
            size_t iNum = batches.size();
            for (size_t i=0; i<iNum; ++i)
            {
                byte* p = (byte*)batches[i];
                delete [] p;
            }
        }

        T* AllocateBlock()
        {
            if (!ppNextBlock || !*ppNextBlock)
            {
                // determine the allligned size of the blocks
                static const size_t blockSize = (sizeof(T)+blockAlignment-1)&(~(blockAlignment-1));

                // make a new batch
                byte *pNewBatch = new byte[blocksPerBatch*blockSize+15];
                batches.push_back(pNewBatch);

                //* Align the block on a 16-byte boundary
                byte* pAlignedPtr =(byte*)((uintptr_t)(pNewBatch+15)&(~15));

                // fill the pointers with the new blocks
                ppNextBlock = (byte**)pAlignedPtr;
                for (int i=0; i<blocksPerBatch-1; ++i)
                {
                    *((uintptr_t*)(pAlignedPtr + i*blockSize)) = (uintptr_t)(pAlignedPtr + (i+1)*blockSize);
                }
                *((uintptr_t*)(pAlignedPtr + (blocksPerBatch-1)*blockSize)) = (uintptr_t)0;
            }

            byte* pBlock = (byte*)ppNextBlock;
            ppNextBlock = (byte**)*ppNextBlock;
            return (T*)pBlock;
        }

        void ReleaseBlock(T* pBlock)
        {
            if(pBlock)
            {
                *((uintptr_t*)pBlock) = (uintptr_t)ppNextBlock;
                ppNextBlock = (byte**)((byte*)pBlock);
            }
        }

        typedef vector<byte*> BatchPtrVector;

        byte**			ppNextBlock;		// Pointer to the next available block pointer
        BatchPtrVector	batches;			// Array of pointers to batches
    };
	

    static BlockStore s_Store;
};

#endif