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

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