blob: 4cac1bc9040b613b749877ba0829ccb80cb16442 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="James CE Johnson">
<TITLE>ACE Tutorial 021</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">
<CENTER><B><FONT SIZE=+2>ACE Tutorial 021</FONT></B></CENTER>
<CENTER><B><FONT SIZE=+2>Pooling your memories</FONT></B></CENTER>
<P>
<HR WIDTH="100%">
And here we have the implementation of the Allocator...
<hr>
<PRE>
<font color=red>// $Id$ </font>
<font color=blue>#include</font> "<font color=green>mpool.h</font>"
<font color=blue>#if !defined</font> (<font color=purple>ACE_LACKS_SYSV_SHMEM</font>)
<font color=red>/*
Set the values of all of the constants. This guarantees that client
and server don't get confused.
*/</font>
const int <font color=#008888>Constants::SEM_KEY_1</font> = ACE_DEFAULT_SEM_KEY + 1;
const int <font color=#008888>Constants::SEM_KEY_2</font> = ACE_DEFAULT_SEM_KEY + 2;
const int <font color=#008888>Constants::SHMSZ</font> = 27;
const char * <font color=#008888>Constants::SHMDATA</font> = "<font color=green>abcdefghijklmnopqrstuvwxyz</font>";
const char * <font color=#008888>Constants::PoolName</font> = "<font color=green>SharedMemoryPool</font>";
const char * <font color=#008888>Constants::RegionName</font> = "<font color=green>Alphabet</font>";
<font color=red>/*
We have to create a copy of the _name parameter in case the caller
has dynamically allocated it. The pool_ is set to NULL & will be
allocated by the accessor.
*/</font>
<font color=#008888>Allocator::Allocator</font> (const char *_name)
: name_ (<font color=#008888>ACE_OS::strdup</font> (_name)),
pool_ (0)
{
if (name_ == 0)
ACE_ERROR ((LM_ERROR, "<font color=green>(%P) %p</font>",
"<font color=green><font color=#008888>Allocator::Allocator</font> cannot strdup pool name</font>"));
}
<font color=#008888>Allocator::~Allocator</font> (void)
{
<font color=red>/*
strdup() uses malloc(), so we must use free() to clean up.
*/</font>
if (name_)
<font color=#008888>ACE_OS::free</font> (name_);
<font color=red>// delete doesn't really care if you give it a NULL pointer.</font>
delete pool_;
}
<font color=red>/*
Allocate the pool. Since we return a reference, we'll be in really
bad shape if the new fails. This is a great place to throw an
exception!
The other concern is thread safety. If two threads call here at
about the same time, we may create the pool twice. We can't use a
Singleton because we want to have multiple Allocator instances. The
Singleton techniques can be used though.
*/</font>
<font color=#008888>Allocator::pool_t</font> &
<font color=#008888>Allocator::pool</font> (void)
{
if (pool_ == 0)
pool_ = new pool_t (name_);
return *pool_;
}
<font color=blue>#endif</font> <font color=red>/* ACE_LACKS_SYSV_SHMEM */</font>
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page06.html">Continue This Tutorial</A>]</CENTER>
|