spot  2.4.3
fixpool.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2011, 2015, 2016 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 #include <new>
24 #include <cstddef>
25 #include <cstdlib>
26 
27 namespace spot
28 {
29 
32  {
33  public:
35  fixed_size_pool(size_t size)
36  : freelist_(nullptr), free_start_(nullptr),
37  free_end_(nullptr), chunklist_(nullptr)
38  {
39  const size_t alignement = 2 * sizeof(size_t);
40  size_ = ((size >= sizeof(block_) ? size : sizeof(block_))
41  + alignement - 1) & ~(alignement - 1);
42  }
43 
46  {
47  while (chunklist_)
48  {
49  chunk_* prev = chunklist_->prev;
50  free(chunklist_);
51  chunklist_ = prev;
52  }
53  }
54 
56  void*
58  {
59  block_* f = freelist_;
60  // If we have free blocks available, return the first one.
61  if (f)
62  {
63  freelist_ = f->next;
64  return f;
65  }
66 
67  // Else, create a block out of the last chunk of allocated
68  // memory.
69 
70  // If all the last chunk has been used, allocate one more.
71  if (free_start_ + size_ > free_end_)
72  {
73  const size_t requested = (size_ > 128 ? size_ : 128) * 8192 - 64;
74  chunk_* c = reinterpret_cast<chunk_*>(malloc(requested));
75  if (!c)
76  throw std::bad_alloc();
77  c->prev = chunklist_;
78  chunklist_ = c;
79 
80  free_start_ = c->data_ + size_;
81  free_end_ = c->data_ + requested;
82  }
83 
84  void* res = free_start_;
85  free_start_ += size_;
86  return res;
87  }
88 
95  void
96  deallocate (const void* ptr)
97  {
98  SPOT_ASSERT(ptr);
99  block_* b = reinterpret_cast<block_*>(const_cast<void*>(ptr));
100  b->next = freelist_;
101  freelist_ = b;
102  }
103 
104  private:
105  size_t size_;
106  struct block_ { block_* next; }* freelist_;
107  char* free_start_;
108  char* free_end_;
109  // chunk = several agglomerated blocks
110  union chunk_ { chunk_* prev; char data_[1]; }* chunklist_;
111  };
112 }
Definition: automata.hh:26
void deallocate(const void *ptr)
Recycle size bytes of memory.
Definition: fixpool.hh:96
fixed_size_pool(size_t size)
Create a pool allocating objects of size bytes.
Definition: fixpool.hh:35
void * allocate()
Allocate size bytes of memory.
Definition: fixpool.hh:57
~fixed_size_pool()
Free any memory allocated by this pool.
Definition: fixpool.hh:45
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 Tue Dec 19 2017 09:22:58 for spot by doxygen 1.8.13