Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/optmem.cpp
3206 views
1
/**************************************************************************/
2
/* File: optmem.cc */
3
/* Author: Joachim Schoeberl */
4
/* Date: 04. Apr. 97 */
5
/**************************************************************************/
6
7
/*
8
Abstract data type ARRAY
9
*/
10
11
12
#include <mystdlib.h>
13
#include <myadt.hpp>
14
15
namespace netgen
16
{
17
//using namespace netgen;
18
19
BlockAllocator :: BlockAllocator (unsigned asize, unsigned ablocks)
20
: bablocks (0)
21
{
22
if (asize < sizeof(void*))
23
asize = sizeof(void*);
24
size = asize;
25
blocks = ablocks;
26
freelist = NULL;
27
}
28
29
BlockAllocator :: ~BlockAllocator ()
30
{
31
//for (unsigned i = 0; i < bablocks.Size(); i++)
32
for (int i = 0; i < bablocks.Size(); i++)
33
delete [] bablocks[i];
34
}
35
36
void * BlockAllocator :: Alloc ()
37
{
38
// return new char[size];
39
if (!freelist)
40
{
41
// cout << "freelist = " << freelist << endl;
42
// cout << "BlockAlloc: " << size*blocks << endl;
43
char * hcp = new char [size * blocks];
44
bablocks.Append (hcp);
45
bablocks.Last() = hcp;
46
for (unsigned i = 0; i < blocks-1; i++)
47
*(void**)&(hcp[i * size]) = &(hcp[ (i+1) * size]);
48
*(void**)&(hcp[(blocks-1)*size]) = NULL;
49
freelist = hcp;
50
}
51
52
void * p = freelist;
53
freelist = *(void**)freelist;
54
return p;
55
}
56
57
/*
58
void BlockAllocator :: Free (void * p)
59
{
60
*(void**)p = freelist;
61
freelist = p;
62
}
63
*/
64
}
65
66