OPAL (Object Oriented Parallel Accelerator Library)
2.2.0
OPAL
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
ippl
src
Utility
Pool.h
Go to the documentation of this file.
1
// -*- C++ -*-
2
/***************************************************************************
3
*
4
* The IPPL Framework
5
*
6
*
7
* Visit http://people.web.psi.ch/adelmann/ for more details
8
*
9
***************************************************************************/
10
11
#ifndef POOL_H
12
#define POOL_H
13
14
// include files
15
#include <cstddef>
16
#include <vector>
17
#include <memory.h>
18
class
Pool
19
{
20
private
:
21
struct
Link
{
Link
*
next
; };
// Point to the next pointer.
22
static
inline
size_t
page
() {
return
4096-8; }
23
static
inline
int
blocks_in_page
(
size_t
sz)
24
{
25
return
(
page
()>sz)?(
page
()/sz):1;
26
}
27
28
public
:
29
30
static
inline
size_t
log2_align
() {
return
3; }
31
static
inline
size_t
align
() {
return
(1<<
log2_align
()); }
32
static
inline
size_t
align_mask
() {
return
align
()-1; }
33
34
static
inline
size_t
round_to_align
(
size_t
s)
35
{
36
if
(s)
37
s = (s & ~
align_mask
()) + ((s&
align_mask
())?
align
():0);
38
else
39
s =
align
();
40
return
s;
41
}
42
43
44
// Null ctor creates an invalid Pool.
45
Pool
();
46
47
// Make a new Pool for objects of a given size.
48
Pool
(
size_t
);
49
50
// Destroy the Pool.
51
~Pool
();
52
53
// Allocate a block from the pool.
54
inline
void
*
alloc
()
55
{
56
++
outstandingAllocs
;
// Record an alloc.
57
if
(
head
==0 )
grow
();
// If we're out of space, get more.
58
Link
*p =
head
;
// get the top of the stack.
59
60
// Make the next one the new head of the list.
61
// We can't do head = p->next since p will soon be treated
62
// as something other than a Link. By doing this assignment
63
// with memcpy, we ensure that p->next will be read before
64
// it is clobbered.
65
memcpy(&
head
, &p->
next
,
sizeof
(
head
));
66
67
// Return the requested block.
68
return
p;
69
}
70
71
// Release a block to the pool.
72
inline
void
free
(
void
*b)
73
{
74
--
outstandingAllocs
;
// Record a free.
75
Link
*p = (
Link
*)b;
// Cast this to a Link
76
p->
next
=
head
;
// Makew it point to top of stack.
77
head
= p;
// Make it the new top.
78
}
79
80
private
:
81
int
outstandingAllocs
;
// Number of blocks given to user.
82
size_t
bsize
;
// How big is each block.
83
size_t
nblock
;
// How many to allocate at once.
84
Link
*
head
;
// The first one.
85
std::vector<char*>
chunks
;
// Currently allocated chunks
86
void
grow
();
// Allocate another chunk and put
87
// its blocks in the free list
88
};
89
91
92
#endif // POOL_H
93
94
/***************************************************************************
95
* $RCSfile: Pool.h,v $ $Author: adelmann $
96
* $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
97
* IPPL_VERSION_ID: $Id: Pool.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
98
***************************************************************************/
Pool::blocks_in_page
static int blocks_in_page(size_t sz)
Definition:
Pool.h:23
Pool
Definition:
Pool.h:18
Pool::head
Link * head
Definition:
Pool.h:84
Pool::bsize
size_t bsize
Definition:
Pool.h:82
Pool::align_mask
static size_t align_mask()
Definition:
Pool.h:32
Pool::Link
Definition:
Pool.h:21
Pool::grow
void grow()
Definition:
Pool.cpp:63
Pool::chunks
std::vector< char * > chunks
Definition:
Pool.h:85
Pool::outstandingAllocs
int outstandingAllocs
Definition:
Pool.h:81
Pool::Link::next
Link * next
Definition:
Pool.h:21
Pool::round_to_align
static size_t round_to_align(size_t s)
Definition:
Pool.h:34
Pool::align
static size_t align()
Definition:
Pool.h:31
Pool::free
void free(void *b)
Definition:
Pool.h:72
Pool::~Pool
~Pool()
Definition:
Pool.cpp:52
Pool::alloc
void * alloc()
Definition:
Pool.h:54
Pool::Pool
Pool()
Definition:
Pool.cpp:34
Pool::page
static size_t page()
Definition:
Pool.h:22
Pool::log2_align
static size_t log2_align()
Definition:
Pool.h:30
Pool::nblock
size_t nblock
Definition:
Pool.h:83
Generated on Sat Jan 4 2020 14:47:40 for OPAL (Object Oriented Parallel Accelerator Library) by
1.8.5