-
Notifications
You must be signed in to change notification settings - Fork 0
/
sli_allocator.cpp
133 lines (115 loc) · 2.53 KB
/
sli_allocator.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* allocator.cpp
*
* This file is part of NEST
*
* Copyright (C) 2004 by
* The NEST Initiative
*
* See the file AUTHORS for details.
*
* Permission is granted to compile and modify
* this file for non-commercial use.
* See the file LICENSE for details.
*
*/
#include "sli_allocator.h"
sli3::pool::pool()
: initial_block_size(1024),
growth_factor(1),
block_size(initial_block_size),
el_size(sizeof(link)),
instantiations(0),
total(0),
capacity(0),
chunks(0),
head(0),
initialized_(false)
{}
sli3::pool::pool(const sli3::pool &p)
: initial_block_size(p.initial_block_size),
growth_factor(p.growth_factor),
block_size(initial_block_size),
el_size(sizeof(link)),
instantiations(0),
total(0),
capacity(0),
chunks(0),
head(0),
initialized_(false)
{}
sli3::pool::pool(size_t n, size_t initial, size_t growth)
: initial_block_size(initial),
growth_factor(growth),
block_size(initial_block_size),
el_size( ( n < sizeof(link))? sizeof(link): n),
instantiations(0),
total(0),
capacity(0),
chunks(0),
head(0),
initialized_(true)
{}
void sli3::pool::init(size_t n, size_t initial, size_t growth)
{
assert(instantiations == 0);
initialized_=true;
initial_block_size=initial;
growth_factor=growth;
block_size=initial_block_size;
el_size = ( n < sizeof(link))? sizeof(link): n;
instantiations=0;
total=0;
capacity=0;
chunks=0;
head=0;
}
sli3::pool::~pool()
{
chunk *n=chunks;
while(n)
{
chunk *p=n;
n=n->next;
delete p;
}
}
sli3::pool & sli3::pool::operator=(const sli3::pool&p)
{
if(&p == this)
return *this;
initial_block_size=p.initial_block_size;
growth_factor=p.growth_factor;
block_size=initial_block_size;
el_size=p.el_size;
instantiations=0;
total=0;
chunks=0;
head=0;
initialized_=false;
return *this;
}
void sli3::pool::grow(size_t nelements)
{
chunk *n = new chunk(nelements*el_size);
total += nelements;
n->next = chunks;
chunks = n;
char *start= n->mem;
char *last= &start[ (nelements-1)*el_size];
for(char *p=start; p<last; p+=el_size)
reinterpret_cast<link*>(p)->next=reinterpret_cast<link*>(p+el_size);
reinterpret_cast<link*>(last)->next = NULL;
head = reinterpret_cast<link*>(start);
}
void sli3::pool::grow(void)
{
grow(block_size);
block_size *= growth_factor;
}
void sli3::pool::reserve(size_t n)
{
const size_t capacity=total-instantiations;
if(capacity < n)
grow(((n-capacity)/block_size+1)*block_size);
}