aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/include/G3D/AreaMemoryManager.h
blob: 5ff46505b0fdca8fd8c86bd62564aadab0e82329 (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
/**
 \file AreaMemoryManager.h
  
 \maintainer Morgan McGuire, http://graphics.cs.williams.edu

 \created 2009-01-20
 \edited  2010-10-29

 Copyright 2000-2012, Morgan McGuire.
 All rights reserved.
 */


#ifndef G3D_AreaMemoryManager_h
#define G3D_AreaMemoryManager_h

#include "G3D/platform.h"
#include "G3D/g3dmath.h"
#include "G3D/Array.h"
#include "G3D/MemoryManager.h"

namespace G3D {

/** 
  \brief Allocates memory in large blocks and then frees it as an area.

  Useful for ensuring cache coherence and for reducing the time cost of 
  multiple allocations and deallocations.

  <b>Not threadsafe</b>
 */
class AreaMemoryManager : public MemoryManager {
private:

    class Buffer {
    private:
        uint8*              m_first;
        size_t              m_size;
        size_t              m_used;

    public:

        Buffer(size_t size);

        ~Buffer();

        /** Returns NULL if out of space */
        void* alloc(size_t s);
    };
    
    size_t                  m_sizeHint;

    /** The underlying array is stored in regular MemoryManager heap memory */
    Array<Buffer*>          m_bufferArray;

    AreaMemoryManager(size_t sizeHint);

public:

    typedef shared_ptr<AreaMemoryManager> Ref;

    /** 
        \param sizeHint Total amount of memory expected to be allocated.
        The allocator will allocate memory from the system in increments
        of this size.
    */
    static AreaMemoryManager::Ref create(size_t sizeHint = 10 * 1024 * 1024);

    /** Invokes deallocateAll. */
    ~AreaMemoryManager();

    size_t bytesAllocated() const;

    /** Allocates memory out of the buffer pool. 
        @param s must be no larger than sizeHint */
    virtual void* alloc(size_t s);

    /** Ignored. */
    virtual void free(void* x);

    virtual bool isThreadsafe() const;

    /** Deletes all previously allocated memory. Because delete is not
        invoked on objects in this memory, it is not safe to simply
        free memory containing C++ objects that expect their destructors
        to be called. */
    void deallocateAll();
};

typedef AreaMemoryManager CoherentAllocator;
}

#endif