spot
0.9.2
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
misc
fixpool.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_FIXPOOL_HH
22
# define SPOT_MISC_FIXPOOL_HH
23
24
#include <new>
25
#include <cstddef>
26
#include <cstdlib>
27
#include <cassert>
28
29
namespace
spot
30
{
31
33
class
fixed_size_pool
34
{
35
public
:
37
fixed_size_pool
(
size_t
size)
38
:
freelist_
(0),
free_start_
(0),
free_end_
(0),
chunklist_
(0)
39
{
40
const
size_t
alignement = 2 *
sizeof
(size_t);
41
size_
= ((size >=
sizeof
(
block_
) ? size :
sizeof
(
block_
))
42
+ alignement - 1) & ~(alignement - 1);
43
}
44
46
~fixed_size_pool
()
47
{
48
while
(
chunklist_
)
49
{
50
chunk_
* prev =
chunklist_
->
prev
;
51
free(
chunklist_
);
52
chunklist_
= prev;
53
}
54
}
55
57
void
*
58
allocate
()
59
{
60
block_
* f =
freelist_
;
61
// If we have free blocks available, return the first one.
62
if
(f)
63
{
64
freelist_
= f->
next
;
65
return
f;
66
}
67
68
// Else, create a block out of the last chunk of allocated
69
// memory.
70
71
// If all the last chunk has been used, allocate one more.
72
if
(
free_start_
+
size_
>
free_end_
)
73
{
74
const
size_t
requested = (
size_
> 128 ?
size_
: 128) * 8192 - 64;
75
chunk_
* c =
reinterpret_cast<
chunk_
*
>
(malloc(requested));
76
if
(!c)
77
throw
std::bad_alloc();
78
c->
prev
=
chunklist_
;
79
chunklist_
= c;
80
81
free_start_
= c->
data_
+
size_
;
82
free_end_
= c->
data_
+ requested;
83
}
84
85
void
* res =
free_start_
;
86
free_start_
+=
size_
;
87
return
res;
88
}
89
96
void
97
deallocate
(
const
void
* ptr)
98
{
99
assert(ptr);
100
block_
* b =
reinterpret_cast<
block_
*
>
(
const_cast<
void
*
>
(ptr));
101
b->
next
=
freelist_
;
102
freelist_
= b;
103
}
104
105
private
:
106
size_t
size_
;
107
struct
block_
{
block_
*
next
; }*
freelist_
;
108
char
*
free_start_
;
109
char
*
free_end_
;
110
// chunk = several agglomerated blocks
111
union
chunk_
{
chunk_
*
prev
;
char
data_
[1]; }*
chunklist_
;
112
};
113
114
}
115
116
#endif // SPOT_MISC_FIXPOOL_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