spot  2.7.4
fixpool.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2011, 2015-2018 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  {
72  const size_t requested = (size_ > 128 ? size_ : 128) * 8192 - 64;
73  chunk_* c = reinterpret_cast<chunk_*>(::operator new(requested));
74  c->prev = chunklist_;
75  chunklist_ = c;
76 
77  free_start_ = c->data_ + size_;
78  free_end_ = c->data_ + requested;
79  }
80 
81  void* res = free_start_;
82  free_start_ += size_;
83 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
84  VALGRIND_MALLOCLIKE_BLOCK(res, size_, 0, false);
85 #endif
86  return res;
87  }
88 
95  void
96  deallocate(void* ptr)
97  {
98  SPOT_ASSERT(ptr);
99  block_* b = reinterpret_cast<block_*>(ptr);
100  b->next = freelist_;
101  freelist_ = b;
102 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
103  VALGRIND_FREELIKE_BLOCK(ptr, 0);
104 #endif
105  }
106 
107  private:
108  const size_t size_;
109  struct block_ { block_* next; }* freelist_;
110  char* free_start_;
111  char* free_end_;
112  // chunk = several agglomerated blocks
113  union chunk_ { chunk_* prev; char data_[1]; }* chunklist_;
114  };
115 
116 }
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:96
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