NEST  2.6.0,not_revisioned_source_dir@0
allocator.h
Go to the documentation of this file.
1 /*
2  * allocator.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef ALLOCATOR_H
24 #define ALLOCATOR_H
25 #include <cassert>
26 #include <cstdlib>
27 #include <string>
28 
29 namespace sli {
30 
50  class pool
51  {
52  struct link { link *next; };
53 
54  class chunk
55  {
56  const size_t csize;
57  chunk(const chunk&);
58  chunk& operator=(const chunk&);
59 
60  public:
62  char *mem;
63 
64  chunk(size_t s)
65  :csize(s),
66  next(0),
67  mem(new char[csize])
68  {}
69 
71  {
72  delete [] mem;
73  mem=NULL;
74  }
75 
76  size_t size(void)
77  { return csize;}
78 
79  };
80 
82  size_t growth_factor;
83 
84  size_t block_size;
85  size_t el_size;
86  size_t instantiations;
87  size_t total;
88  size_t capacity;
91 
92  bool initialized_;
93 
94  void grow(size_t);
95  void grow();
96 
97 
98  public:
104  pool();
105  pool(const pool &);
106  pool& operator=(const pool&);
107 
108  pool(size_t n, size_t initial=100, size_t growth=1);
109  void init(size_t n, size_t initial=100, size_t growth=1);
110 
111  ~pool();
112 
121  void reserve_additional(size_t n);
122 
123  size_t available(void) const
124  { return total-instantiations;}
125 
126  inline void *alloc(void);
127  inline void free(void* p);
128  size_t size_of(void) const
129  { return el_size;}
130 
131  inline size_t get_el_size() const;
132  inline size_t get_instantiations() const;
133  inline size_t get_total() const;
134  };
135 
136  inline
137  void * pool::alloc(void)
138  {
139 
140  if(head==0)
141  {
142  grow(block_size);
144  }
145 
146  link *p=head;
147 
148  head = head->next;
149  ++instantiations;
150 
151  return p;
152  }
153 
154  inline
155  void pool::free(void *elp)
156  {
157  link *p= static_cast<link *>(elp);
158  p->next= head;
159  head = p;
160  --instantiations;
161  }
162 
163  inline
164  size_t pool::get_el_size() const
165  {
166  return el_size;
167  }
168 
169  inline
171  {
172  return instantiations;
173  }
174 
175  inline
176  size_t pool::get_total() const
177  {
178  return total;
179  }
180 
181 }
182 
183 #ifdef USE_PMA
184 
185 const int MAX_THREAD = 128;
186 
202 {
203  private:
204 
209  struct chunk
210  {
211  chunk(char* mem, chunk* next) : mem_(mem), next_(next) {};
212  char* mem_;
214  };
215 
216  public:
222  void init(size_t chunk_size=1048576);
223  void destruct();
224  void* alloc(size_t obj_size);
225 
226  private:
227 
232  void new_chunk();
233 
240  size_t chunk_size_;
241 
249  char* head_;
250 
255 
259  size_t capacity_;
260 };
261 
262 #ifdef IS_K
263 
271 {
272  // Only works for sizeof(PoorMansAllocator) < 64
273  char padding[64 - sizeof(PoorMansAllocator)];
274 };
275 #endif /* #ifdef IS_K */
276 
277 #endif /* #ifdef USE_PMA */
278 
279 #endif /* #ifndef ALLOCATOR_H */
size_t size_of(void) const
Definition: allocator.h:128
const int MAX_THREAD
Definition: allocator.h:185
pool()
Create pool for objects of size n.
Definition: allocator.cpp:25
size_t size(void)
Definition: allocator.h:76
void init(size_t n, size_t initial=100, size_t growth=1)
Definition: allocator.cpp:65
size_t capacity
number of free elements
Definition: allocator.h:88
bool initialized_
True if the pool is initialized.
Definition: allocator.h:92
char * mem
Definition: allocator.h:62
char * head_
Points to the next free location in the current chunk.
Definition: allocator.h:249
size_t get_instantiations() const
Definition: allocator.h:170
The Fujitsu compiler on K cannot handle OpenMP thread-private properly.
Definition: allocator.h:270
chunk * next_
Definition: allocator.h:213
size_t growth_factor
Definition: allocator.h:82
chunk * chunks_
First element of the linked list of chunks.
Definition: allocator.h:254
void destruct()
Definition: allocator.cpp:167
chunk(size_t s)
Definition: allocator.h:64
A chunk of memory, one element in the linked list of the memory pool.
Definition: allocator.h:209
char padding[64-sizeof(PoorMansAllocator)]
Definition: allocator.h:273
chunk * chunks
linked list of memory chunks
Definition: allocator.h:89
void new_chunk()
Append a new chunk of memory to the list forming the memory pool.
Definition: allocator.cpp:157
chunk(char *mem, chunk *next)
Definition: allocator.h:211
void init(size_t chunk_size=1048576)
No constructors, as this would be counted as a 'use' of the pool before declaring it thread-private b...
Definition: allocator.cpp:149
size_t chunk_size_
The size of each chunk to be allocated.
Definition: allocator.h:240
~chunk()
Definition: allocator.h:70
link * head
head of free list
Definition: allocator.h:90
void grow()
make pool larger
Definition: allocator.cpp:128
size_t available(void) const
Definition: allocator.h:123
size_t instantiations
number of instantiated elements
Definition: allocator.h:86
void * alloc(size_t obj_size)
Definition: allocator.cpp:174
void * alloc(void)
allocate one element
Definition: allocator.h:137
Definition: allocator.h:54
size_t initial_block_size
Definition: allocator.h:81
void free(void *p)
put element back into the pool
Definition: allocator.h:155
const size_t csize
Definition: allocator.h:56
~pool()
deallocate ALL memory
Definition: allocator.cpp:82
size_t total
total number of allocated elements
Definition: allocator.h:87
The poor man's allocator is a simple pool-based allocator, used to allocate storage for connections i...
Definition: allocator.h:201
void reserve_additional(size_t n)
Increase the pools capacity (free slots) by n.
Definition: allocator.cpp:134
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
size_t get_total() const
Definition: allocator.h:176
chunk & operator=(const chunk &)
not implemented
size_t block_size
number of elements per chunk
Definition: allocator.h:84
pool is a specialized allocator class for many identical small objects.
Definition: allocator.h:50
char * mem_
Definition: allocator.h:211
size_t get_el_size() const
Definition: allocator.h:164
const Name p("p")
current release probability (Tsodyks2_connection)
Definition: nest_names.h:218
chunk * next
Definition: allocator.h:61
pool & operator=(const pool &)
Definition: allocator.cpp:93
size_t el_size
sizeof an element
Definition: allocator.h:85
size_t capacity_
Remaining capacity of the current chunk.
Definition: allocator.h:259
chunk(const chunk &)
not implemented