spot  2.9.3
fixpool.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2011, 2015-2018, 2020 Laboratoire de Recherche et
3 // Développement de l'Epita (LRDE)
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #pragma once
21 
22 #include <spot/misc/common.hh>
23 
24 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
25 #include <valgrind/memcheck.h>
26 #endif
27 
28 namespace spot
29 {
31  class SPOT_API fixed_size_pool
32  {
33  public:
35  fixed_size_pool(size_t size);
36 
39  {
40  while (chunklist_)
41  {
42  chunk_* prev = chunklist_->prev;
43  ::operator delete(chunklist_);
44  chunklist_ = prev;
45  }
46  }
47 
49  void*
51  {
52  block_* f = freelist_;
53  // If we have free blocks available, return the first one.
54  if (f)
55  {
56 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
57  VALGRIND_MALLOCLIKE_BLOCK(f, size_, 0, false);
58  // field f->next is initialized: prevents valgrind from complaining
59  // about jumps depending on uninitialized memory
60  VALGRIND_MAKE_MEM_DEFINED(f, sizeof(block_*));
61 #endif
62  freelist_ = f->next;
63  return f;
64  }
65 
66  // Else, create a block out of the last chunk of allocated
67  // memory.
68 
69  // If all the last chunk has been used, allocate one more.
70  if (free_start_ + size_ > free_end_)
71  new_chunk_();
72 
73  void* res = free_start_;
74  free_start_ += size_;
75 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
76  VALGRIND_MALLOCLIKE_BLOCK(res, size_, 0, false);
77 #endif
78  return res;
79  }
80 
87  void
88  deallocate(void* ptr)
89  {
90  SPOT_ASSERT(ptr);
91  block_* b = reinterpret_cast<block_*>(ptr);
92  b->next = freelist_;
93  freelist_ = b;
94 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
95  VALGRIND_FREELIKE_BLOCK(ptr, 0);
96 #endif
97  }
98 
99  private:
100  void new_chunk_();
101 
102  const size_t size_;
103  struct block_ { block_* next; }* freelist_;
104  char* free_start_;
105  char* free_end_;
106  // chunk = several agglomerated blocks
107  union chunk_ { chunk_* prev; char data_[1]; }* chunklist_;
108  };
109 
110 }
Definition: automata.hh:26
void * allocate()
Allocate size bytes of memory.
Definition: fixpool.hh:50
~fixed_size_pool()
Free any memory allocated by this pool.
Definition: fixpool.hh:38
void deallocate(void *ptr)
Recycle size bytes of memory.
Definition: fixpool.hh:88
A fixed-size memory pool implementation.
Definition: fixpool.hh:31

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.8.13