spot 2.3.3

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
mspool.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2011, 2013, 2015, 2016 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 <spot/misc/hash.hh>
24 #include <new>
25 #include <cstddef>
26 #include <cstdlib>
27
28 namespace spot
29 {
30
32 class multiple_size_pool
33 {
34 static const size_t alignment_ = 2 * sizeof(size_t) - 1;
35 public:
37 multiple_size_pool()
38 : free_start_(nullptr), free_end_(nullptr), chunklist_(nullptr)
39 {
40 }
41
43 ~multiple_size_pool()
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 SPOT_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 std::unordered_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 }
spot
Definition: graph.hh:33
spot::multiple_size_pool::multiple_size_pool
multiple_size_pool()
Create a pool.
Definition: mspool.hh:37
spot::multiple_size_pool::deallocate
void deallocate(const void *ptr, size_t size)
Recycle size bytes of memory.
Definition: mspool.hh:108
spot::multiple_size_pool::~multiple_size_pool
~multiple_size_pool()
Free any memory allocated by this pool.
Definition: mspool.hh:43
spot::multiple_size_pool
A multiple-size memory pool implementation.
Definition: mspool.hh:32
spot::multiple_size_pool::allocate
void * allocate(size_t size)
Allocate size bytes of memory.
Definition: mspool.hh:63

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.