spot
0.9.2
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
misc
mspool.hh
Go to the documentation of this file.
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 2 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 Spot; see the file COPYING. If not, write to the Free
18
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19
// 02111-1307, USA.
20
21
#ifndef SPOT_MISC_MSPOOL_HH
22
# define SPOT_MISC_MSPOOL_HH
23
24
#include <new>
25
#include <cstddef>
26
#include <cstdlib>
27
#include <cassert>
28
#include "
misc/hash.hh
"
29
30
namespace
spot
31
{
32
34
class
multiple_size_pool
35
{
36
static
const
size_t
alignment_
= 2 *
sizeof
(size_t) - 1;
37
public
:
39
multiple_size_pool
()
40
:
free_start_
(0),
free_end_
(0),
chunklist_
(0)
41
{
42
}
43
45
~multiple_size_pool
()
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
f = f->
next
;
75
return
first;
76
}
77
78
// Else, create a block out of the last chunk of allocated
79
// memory.
80
81
// If all the last chunk has been used, allocate one more.
82
if
(
free_start_
+ size >
free_end_
)
83
{
84
const
size_t
requested = (size > 128 ? size : 128) * 8192 - 64;
85
chunk_
* c =
reinterpret_cast<
chunk_
*
>
(malloc(requested));
86
if
(!c)
87
throw
std::bad_alloc();
88
c->
prev
=
chunklist_
;
89
chunklist_
= c;
90
91
free_start_
= c->
data_
+ size;
92
free_end_
= c->
data_
+ requested;
93
}
94
95
void
* res =
free_start_
;
96
free_start_
+= size;
97
return
res;
98
}
99
109
void
110
deallocate
(
const
void
* ptr,
size_t
size)
111
{
112
assert(ptr);
113
size =
fixsize
(size);
114
block_
* b =
reinterpret_cast<
block_
*
>
(
const_cast<
void
*
>
(ptr));
115
block_
*& f =
freelist_
[size];
116
b->
next
= f;
117
f = b;
118
}
119
120
private
:
121
struct
block_
{
block_
*
next
; };
122
Sgi::hash_map<size_t, block_*>
freelist_
;
123
char
*
free_start_
;
124
char
*
free_end_
;
125
// chunk = several agglomerated blocks
126
union
chunk_
{
chunk_
*
prev
;
char
data_
[1]; }*
chunklist_
;
127
};
128
129
}
130
131
#endif // SPOT_MISC_INTVPOOL_HH
Please
comment
this page and
report errors
about it on
the RefDocComments page
.
Generated on Mon Jul 2 2012 17:35:47 for spot by
1.8.1.1