spot  2.12.2
fixpool.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) by the Spot authors, see the AUTHORS file for details.
3 //
4 // This file is part of Spot, a model checking library.
5 //
6 // Spot is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Spot is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #pragma once
20 
21 #include <spot/misc/common.hh>
22 #include <spot/misc/clz.hh>
23 
24 #if SPOT_DEBUG && __has_include(<valgrind/memcheck.h>)
25 #undef USES_MEMCHECK_H
26 #define USES_MEMCHECK_H 1
27 #include <valgrind/memcheck.h>
28 #endif
29 
30 namespace spot
31 {
41  enum class pool_type { Safe , Unsafe };
42 
44  template<pool_type Kind>
45  class SPOT_API fixed_size_pool
46  {
47  public:
49  fixed_size_pool(size_t size)
50  : size_(
51  [](size_t size)
52  {
53  // to properly store chunks and freelist, we need size to be at
54  // least the size of a block_
55  if (size < sizeof(block_))
56  size = sizeof(block_);
57  // powers of 2 are a good alignment
58  if (!(size & (size-1)))
59  return size;
60  // small numbers are best aligned to the next power of 2
61  else if (size < alignof(std::max_align_t))
62  return size_t{1} << (CHAR_BIT*sizeof(size_t) - clz(size));
63  else
64  {
65  size_t mask = alignof(std::max_align_t)-1;
66  return (size + mask) & ~mask;
67  }
68  }(size)),
69  freelist_(nullptr),
70  chunklist_(nullptr)
71  {
72  new_chunk_();
73  }
74 
77  {
78  while (chunklist_)
79  {
80  chunk_* prev = chunklist_->prev;
81  ::operator delete(chunklist_);
82  chunklist_ = prev;
83  }
84  }
85 
87  void*
89  {
90  block_* f = freelist_;
91  // If we have free blocks available, return the first one.
92  if (f)
93  {
94 #ifdef USES_MEMCHECK_H
95  if (Kind == pool_type::Safe)
96  {
97  VALGRIND_MALLOCLIKE_BLOCK(f, size_, 0, false);
98  // field f->next is initialized: prevents valgrind from
99  // complaining about jumps depending on uninitialized memory
100  VALGRIND_MAKE_MEM_DEFINED(f, sizeof(block_*));
101  }
102 #endif
103  freelist_ = f->next;
104  return f;
105  }
106 
107  // Else, create a block out of the last chunk of allocated
108  // memory.
109 
110  // If all the last chunk has been used, allocate one more.
111  if (free_start_ + size_ > free_end_)
112  new_chunk_();
113 
114  void* res = free_start_;
115  free_start_ += size_;
116 #ifdef USES_MEMCHECK_H
117  if (Kind == pool_type::Safe)
118  {
119  VALGRIND_MALLOCLIKE_BLOCK(res, size_, 0, false);
120  }
121 #endif
122  return res;
123  }
124 
131  void
132  deallocate(void* ptr)
133  {
134  SPOT_ASSERT(ptr);
135  block_* b = reinterpret_cast<block_*>(ptr);
136  b->next = freelist_;
137  freelist_ = b;
138 #ifdef USES_MEMCHECK_H
139  if (Kind == pool_type::Safe)
140  {
141  VALGRIND_FREELIKE_BLOCK(ptr, 0);
142  }
143 #endif
144  }
145 
146  private:
147  void new_chunk_()
148  {
149  const size_t requested = (size_ > 128 ? size_ : 128) * 8192 - 64;
150  chunk_* c = reinterpret_cast<chunk_*>(::operator new(requested));
151  c->prev = chunklist_;
152  chunklist_ = c;
153 
154  free_start_ = c->data_ + size_;
155  free_end_ = c->data_ + requested;
156  }
157 
158 
159  const size_t size_;
160  struct block_ { block_* next; }* freelist_;
161  char* free_start_;
162  char* free_end_;
163  // chunk = several agglomerated blocks
164  union chunk_ { chunk_* prev; char data_[1]; }* chunklist_;
165  };
166 }
A fixed-size memory pool implementation.
Definition: fixpool.hh:46
void * allocate()
Allocate size bytes of memory.
Definition: fixpool.hh:88
void deallocate(void *ptr)
Recycle size bytes of memory.
Definition: fixpool.hh:132
~fixed_size_pool()
Free any memory allocated by this pool.
Definition: fixpool.hh:76
fixed_size_pool(size_t size)
Create a pool allocating objects of size bytes.
Definition: fixpool.hh:49
Definition: automata.hh:26
pool_type
Definition: fixpool.hh:41

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.1