Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
BlockAllocator.h
1 /*
2  * BlockAllocator class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2010, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_ALLOC_BLOCKALLOCATOR_H_
22 #define ELM_ALLOC_BLOCKALLOCATOR_H_
23 
24 #include <elm/alloc/StackAllocator.h>
25 #include <elm/compare.h>
26 
27 namespace elm {
28 
29 // RawBlockAllocator class
30 template <class T>
32 
33  typedef struct block_t {
34  struct block_t *next;
35  } block_t;
36 
37 public:
38  static const int default_block_per_chunk = 32;
39 
40  BlockAllocator(int block_per_chunk = default_block_per_chunk)
41  : alloc(min(sizeof(T), sizeof(block_t)) * block_per_chunk), list(0) { }
42 
43  inline T *allocate() {
44  if(list != nullptr) {
45  block_t *res = list;
46  list = list->next;
47  return reinterpret_cast<T *>(res);
48  }
49  else
50  return reinterpret_cast<T *>(alloc.allocate(sizeof(T)));
51  }
52 
53  void free(T *p) {
54  block_t *b = reinterpret_cast<block_t *>(p);
55  b->next = list;
56  list = b;
57  }
58 
59 private:
60  StackAllocator alloc;
61  block_t *list;
62 };
63 
64 } // elm
65 
66 #endif /* ELM_ALLOC_BLOCKALLOCATOR_H_ */
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::StackAllocator
Definition: StackAllocator.h:34
elm::BlockAllocator::default_block_per_chunk
static const int default_block_per_chunk
Definition: BlockAllocator.h:38
elm::min
const T & min(const T &x, const T &y)
Definition: compare.h:104
elm
Definition: adapter.h:26
elm::BlockAllocator::free
void free(T *p)
Definition: BlockAllocator.h:53
elm::BlockAllocator
Definition: BlockAllocator.h:31
elm::BlockAllocator::allocate
T * allocate()
Definition: BlockAllocator.h:43
elm::BlockAllocator::BlockAllocator
BlockAllocator(int block_per_chunk=default_block_per_chunk)
Definition: BlockAllocator.h:40
elm::StackAllocator::allocate
void * allocate(t::size size)
Definition: alloc_StackAllocator.cpp:75