OSDN Git Service

7b04069bac1d74cbde659cb2fa76146abb102364
[qt-creator-jp/qt-creator-jp.git] / src / libs / glsl / glslmemorypool.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "glslmemorypool.h"
35 #include <cstring>
36 #include <cassert>
37
38 using namespace GLSL;
39
40 MemoryPool::MemoryPool()
41     : _blocks(0),
42       _allocatedBlocks(0),
43       _blockCount(-1),
44       _ptr(0),
45       _end(0)
46 { }
47
48 MemoryPool::~MemoryPool()
49 {
50     if (_blocks) {
51         for (int i = 0; i < _allocatedBlocks; ++i) {
52             if (char *b = _blocks[i])
53                 std::free(b);
54         }
55
56         std::free(_blocks);
57     }
58 }
59
60 void MemoryPool::reset()
61 {
62     _blockCount = -1;
63     _ptr = _end = 0;
64 }
65
66 void *MemoryPool::allocate_helper(size_t size)
67 {
68     assert(size < BLOCK_SIZE);
69
70     if (++_blockCount == _allocatedBlocks) {
71         if (! _allocatedBlocks)
72             _allocatedBlocks = DEFAULT_BLOCK_COUNT;
73         else
74             _allocatedBlocks *= 2;
75
76         _blocks = (char **) realloc(_blocks, sizeof(char *) * _allocatedBlocks);
77
78         for (int index = _blockCount; index < _allocatedBlocks; ++index)
79             _blocks[index] = 0;
80     }
81
82     char *&block = _blocks[_blockCount];
83
84     if (! block)
85         block = (char *) std::malloc(BLOCK_SIZE);
86
87     _ptr = block;
88     _end = _ptr + BLOCK_SIZE;
89
90     void *addr = _ptr;
91     _ptr += size;
92     return addr;
93 }
94
95 RecursiveMemoryPool::RecursiveMemoryPool(MemoryPool *pool)
96     : _pool(pool),
97       _blockCount(pool->_blockCount),
98       _ptr(pool->_ptr),
99       _end(pool->_end)
100 {
101 }
102
103 RecursiveMemoryPool::~RecursiveMemoryPool()
104 {
105     _pool->_blockCount = _blockCount;
106     _pool->_ptr = _ptr;
107     _pool->_end = _end;
108 }
109
110 Managed::Managed()
111 { }
112
113 Managed::~Managed()
114 { }
115
116 void *Managed::operator new(size_t size, MemoryPool *pool)
117 { return pool->allocate(size); }
118
119 void Managed::operator delete(void *)
120 { }
121
122 void Managed::operator delete(void *, MemoryPool *)
123 { }
124