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