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
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: memory.cpp,v 1.1.1.1.2.2 2009/12/19 23:35:39 spamatica Exp $
//
// (C) Copyright 2003 Werner Schweer (ws@seh.de)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=========================================================
#include "memory.h"
Pool audioRTmemoryPool;
Pool midiRTmemoryPool;
//---------------------------------------------------------
// Pool
//---------------------------------------------------------
Pool::Pool()
{
for (int idx = 0; idx < dimension; ++idx) {
head[idx] = 0;
chunks[idx] = 0;
grow(idx); // preallocate
}
}
//---------------------------------------------------------
// ~Pool
//---------------------------------------------------------
Pool::~Pool()
{
for (int i = 0; i < dimension; ++i) {
Chunk* n = chunks[i];
while (n) {
Chunk* p = n;
n = n->next;
delete p;
}
}
}
//---------------------------------------------------------
// grow
//---------------------------------------------------------
void Pool::grow(int idx)
{
int esize = (idx+1) * sizeof(unsigned long);
Chunk* n = new Chunk;
n->next = chunks[idx];
chunks[idx] = n;
const int nelem = Chunk::size / esize;
char* start = n->mem;
char* last = &start[(nelem-1) * esize];
for (char* p = start; p < last; p += esize)
reinterpret_cast<Verweis*>(p)->next =
reinterpret_cast<Verweis*>(p + esize);
reinterpret_cast<Verweis*>(last)->next = 0;
head[idx] = reinterpret_cast<Verweis*>(start);
}
#ifdef TEST
//=========================================================
// TEST
//=========================================================
struct mops {
char a, c;
int b;
mops(int x) : b(x) {}
};
typedef std::list<struct mops, RTalloc<struct mops> > List;
typedef List::iterator iList;
//---------------------------------------------------------
// main
// 2.8 s normal 0.7 vector
// 2.5 s RTalloc
// 1.18 all optimisations (0.97)
//---------------------------------------------------------
int main()
{
List l;
for (int i = 0; i < 10000000; ++i)
l.push_back(mops(i));
return 0;
}
#endif
|