spot  1.2.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mspool.hh
1 // Copyright (C) 2011 Laboratoire de Recherche et Developpement de
2 // l'Epita (LRDE)
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 #ifndef SPOT_MISC_MSPOOL_HH
20 # define SPOT_MISC_MSPOOL_HH
21 
22 #include <new>
23 #include <cstddef>
24 #include <cstdlib>
25 #include <cassert>
26 #include "misc/hash.hh"
27 
28 namespace spot
29 {
30 
33  {
34  static const size_t alignment_ = 2 * sizeof(size_t) - 1;
35  public:
38  : free_start_(0), free_end_(0), chunklist_(0)
39  {
40  }
41 
44  {
45  while (chunklist_)
46  {
47  chunk_* prev = chunklist_->prev;
48  free(chunklist_);
49  chunklist_ = prev;
50  }
51  }
52 
53  size_t fixsize(size_t size) const
54  {
55  if (size < sizeof(block_))
56  size = sizeof(block_);
57 
58  return (size + alignment_ - 1) & ~(alignment_ - 1);
59  }
60 
62  void*
63  allocate(size_t size)
64  {
65  size = fixsize(size);
66 
67  block_*& f = freelist_[size];
68  // If we have free blocks available, return the first one.
69  if (f)
70  {
71  block_* first = f;
72  f = f->next;
73  return first;
74  }
75 
76  // Else, create a block out of the last chunk of allocated
77  // memory.
78 
79  // If all the last chunk has been used, allocate one more.
80  if (free_start_ + size > free_end_)
81  {
82  const size_t requested = (size > 128 ? size : 128) * 8192 - 64;
83  chunk_* c = reinterpret_cast<chunk_*>(malloc(requested));
84  if (!c)
85  throw std::bad_alloc();
86  c->prev = chunklist_;
87  chunklist_ = c;
88 
89  free_start_ = c->data_ + size;
90  free_end_ = c->data_ + requested;
91  }
92 
93  void* res = free_start_;
94  free_start_ += size;
95  return res;
96  }
97 
107  void
108  deallocate (const void* ptr, size_t size)
109  {
110  assert(ptr);
111  size = fixsize(size);
112  block_* b = reinterpret_cast<block_*>(const_cast<void*>(ptr));
113  block_*& f = freelist_[size];
114  b->next = f;
115  f = b;
116  }
117 
118  private:
119  struct block_ { block_* next; };
120  Sgi::hash_map<size_t, block_*> freelist_;
121  char* free_start_;
122  char* free_end_;
123  // chunk = several agglomerated blocks
124  union chunk_ { chunk_* prev; char data_[1]; }* chunklist_;
125  };
126 
127 }
128 
129 #endif // SPOT_MISC_INTVPOOL_HH

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Sat Dec 6 2014 12:28:44 for spot by doxygen 1.8.4