OSDN Git Service

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