spot  2.12.2
mspool.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 <unordered_map>
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 {
32 
35  {
36  static const size_t alignment_ = 2 * sizeof(size_t) - 1;
37  public:
40  : free_start_(nullptr), free_end_(nullptr), chunklist_(nullptr)
41  {
42  }
43 
46  {
47  while (chunklist_)
48  {
49  chunk_* prev = chunklist_->prev;
50  free(chunklist_);
51  chunklist_ = prev;
52  }
53  }
54 
55  size_t fixsize(size_t size) const
56  {
57  if (size < sizeof(block_))
58  size = sizeof(block_);
59 
60  return (size + alignment_ - 1) & ~(alignment_ - 1);
61  }
62 
64  void*
65  allocate(size_t size)
66  {
67  size = fixsize(size);
68 
69  block_*& f = freelist_[size];
70  // If we have free blocks available, return the first one.
71  if (f)
72  {
73  block_* first = f;
74 #ifdef USES_MEMCHECK_H
75  VALGRIND_MALLOCLIKE_BLOCK(f, size, 0, false);
76  // field f->next is initialized: prevents valgrind from complaining
77  // about jumps depending on uninitialized memory
78  VALGRIND_MAKE_MEM_DEFINED(f, sizeof(block_*));
79 #endif
80  f = f->next;
81  return first;
82  }
83 
84  // Else, create a block out of the last chunk of allocated
85  // memory.
86 
87  // If all the last chunk has been used, allocate one more.
88  if (free_start_ + size > free_end_)
89  {
90  const size_t requested = (size > 128 ? size : 128) * 8192 - 64;
91  chunk_* c = reinterpret_cast<chunk_*>(malloc(requested));
92  if (!c)
93  throw std::bad_alloc();
94  c->prev = chunklist_;
95  chunklist_ = c;
96 
97  free_start_ = c->data_ + size;
98  free_end_ = c->data_ + requested;
99  }
100 
101  void* res = free_start_;
102  free_start_ += size;
103 #ifdef USES_MEMCHECK_H
104  VALGRIND_MALLOCLIKE_BLOCK(res, size, 0, false);
105 #endif
106  return res;
107  }
108 
118  void
119  deallocate (const void* ptr, size_t size)
120  {
121  SPOT_ASSERT(ptr);
122  size = fixsize(size);
123  block_* b = reinterpret_cast<block_*>(const_cast<void*>(ptr));
124  block_*& f = freelist_[size];
125  b->next = f;
126  f = b;
127 #ifdef USES_MEMCHECK_H
128  VALGRIND_FREELIKE_BLOCK(ptr, 0);
129 #endif
130  }
131 
132  private:
133  struct block_ { block_* next; };
134  std::unordered_map<size_t, block_*> freelist_;
135  char* free_start_;
136  char* free_end_;
137  // chunk = several agglomerated blocks
138  union chunk_ { chunk_* prev; char data_[1]; }* chunklist_;
139  };
140 }
A multiple-size memory pool implementation.
Definition: mspool.hh:35
void * allocate(size_t size)
Allocate size bytes of memory.
Definition: mspool.hh:65
~multiple_size_pool()
Free any memory allocated by this pool.
Definition: mspool.hh:45
void deallocate(const void *ptr, size_t size)
Recycle size bytes of memory.
Definition: mspool.hh:119
multiple_size_pool()
Create a pool.
Definition: mspool.hh:39
Definition: automata.hh:26

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