Sierra Toolkit
Version of the Day
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
Pool.hpp
1
/*--------------------------------------------------------------------*/
2
/* Copyright 2005 Sandia Corporation. */
3
/* Under the terms of Contract DE-AC04-94AL85000, there is a */
4
/* non-exclusive license for use of this work by or on behalf */
5
/* of the U.S. Government. Export of this program may require */
6
/* a license from the United States Government. */
7
/*--------------------------------------------------------------------*/
8
9
#ifndef _stk_util_util_Pool_hpp_
10
#define _stk_util_util_Pool_hpp_
11
12
#include <cstdlib>
13
14
#ifndef STK_UTIL_POOL_ALLOC_CHUNK_SIZE_K
15
#define STK_UTIL_POOL_ALLOC_CHUNK_SIZE_K 512
16
#endif
17
18
//The macro STK_UTIL_POOL_ALLOC_CHUNK_SIZE_K determines the number
19
//of kilobytes that each internally-allocated chunk of memory will
20
//occupy. The Pool object will then dispense "sub-chunks"
21
//of memory having size determined by the argument to the class
22
//constructor.
23
24
namespace
stk_classic {
25
namespace
util {
26
class
Pool {
27
public
:
28
Pool(
unsigned
int
nbytes);
// nbytes is the size of elements
29
~Pool();
30
31
void
* alloc();
//allocate one element
32
void
free(
void
* b);
//put an element back into the pool
33
struct
Link { Link* next; };
34
35
private
:
36
struct
Chunk {
37
//Stroustrup's comment:
38
//slightly less than specified K so that a chunk will fit in
39
//allocation area first to get stringent alignment
40
enum
{ size = STK_UTIL_POOL_ALLOC_CHUNK_SIZE_K*1024-16 };
41
char
mem[size];
42
Chunk* next;
43
};
44
45
Chunk* chunks;
46
const
unsigned
int
esize;
47
Link* head;
48
49
Pool(
const
Pool&);
//private copy constructor
50
Pool& operator=(
const
Pool&);
//private assignment operator
51
void
grow();
//make pool larger
52
};
53
54
inline
void
* Pool::alloc()
55
{
56
if
(head == NULL) {
57
grow();
58
}
59
Link* p = head;
//return first element
60
head = p->next;
61
return
p;
62
}
63
64
inline
void
Pool::free(
void
* b)
65
{
66
Link* p =
static_cast<
Link*
>
(b);
67
p->next = head;
//put b back as first element
68
head = p;
69
}
70
71
}
//namespace util
72
}
//namespace stk_classic
73
74
#endif
75
stk_util
stk_util
util
Pool.hpp
Generated on Wed Sep 26 2018 14:44:35 for Sierra Toolkit by
1.8.6