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
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: memory.cpp,v 1.1.1.1 2003/10/29 10:04:59 wschweer Exp $
//
// (C) Copyright 2003 Werner Schweer (ws@seh.de)
//=========================================================
#include "memory.h"
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(int);
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 std::vector<struct mops> List;
typedef List::iterator iList;
//---------------------------------------------------------
// main
// 2.8 s normal 0.7 vector
// 2.5 s RTalloc
// 1.18 alle optimierungen (0.97)
//---------------------------------------------------------
int main()
{
List l;
for (int i = 0; i < 10000000; ++i)
l.push_back(mops(i));
return 0;
}
#endif
|