1
// PI_Malloc.cpp,v 1.3 2004/01/07 22:40:16 shuston Exp
3
#include "ace/OS_NS_stdio.h"
4
#include "ace/OS_NS_string.h"
7
#include "ace/Malloc_T.h"
8
#include "ace/Null_Mutex.h"
9
#include "ace/PI_Malloc.h"
11
typedef ACE_Malloc_T <ACE_MMAP_MEMORY_POOL,
15
typedef ACE_Malloc_LIFO_Iterator_T<ACE_MMAP_MEMORY_POOL,
20
ALLOCATOR *g_allocator;
22
// Listing 2 code/ch17
26
Record (int id1, int id2, char *name)
27
: id1_(id1), id2_(id2)
29
size_t len = ACE_OS::strlen (name) + 1;
31
ACE_reinterpret_cast (char *,
32
g_allocator->malloc (len));
33
ACE_OS::strcpy (buf, name);
37
~Record() { g_allocator->free (name_.addr ()); }
39
char *name (void) { return name_; }
40
int id1 (void) { return id1_; }
41
int id2 (void) { return id2_; }
46
ACE_Based_Pointer_Basic<char> name_;
50
void showRecords (void)
53
ACE_TEXT ("The following records were found:\n")));
56
MALLOC_LIFO_ITERATOR iter (*g_allocator);
58
for (void *temp = 0; iter.next (temp) != 0; iter.advance ())
61
ACE_reinterpret_cast (Record *, temp);
63
ACE_TEXT ("Record name: %C|id1:%d|id2:%d\n"),
64
record->name(), record->id1(), record->id2()));
73
for (int i = 0; i < 10; i++)
75
ACE_OS::sprintf (buf, "%s:%d", "Record", i);
77
void *memory = g_allocator->malloc (sizeof (Record));
79
ACE_ERROR_RETURN ((LM_ERROR,
81
ACE_TEXT ("Unable to malloc")),
84
// Allocate and place record
85
Record* newRecord = new (memory) Record (i, i+1, buf);
86
if (g_allocator->bind (buf, newRecord) == -1)
87
ACE_ERROR_RETURN ((LM_ERROR,
89
ACE_TEXT ("bind failed")),
96
// Listing 3 code/ch17
97
// Backing file where the data is kept.
98
#define BACKING_STORE "backing2.store"
100
int ACE_TMAIN (int argc, ACE_TCHAR *[])
104
ACE_MMAP_Memory_Pool_Options options
105
(ACE_DEFAULT_BASE_ADDR,
106
ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED);
107
ACE_NEW_RETURN (g_allocator,
108
ALLOCATOR (BACKING_STORE,
112
ACE_DEBUG ((LM_DEBUG,
113
ACE_TEXT ("Mapped to base address %@\n"),
114
g_allocator->base_addr ()));
120
ACE_MMAP_Memory_Pool_Options options
121
(0, ACE_MMAP_Memory_Pool_Options::NEVER_FIXED);
122
ACE_NEW_RETURN (g_allocator,
123
ALLOCATOR (BACKING_STORE,
128
ACE_DEBUG ((LM_DEBUG,
129
ACE_TEXT ("Mapped to base address %@\n"),
130
g_allocator->base_addr ()));
135
g_allocator->sync ();
141
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
142
template class ACE_Malloc_T <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex, ACE_PI_Control_Block>;
143
template class ACE_Malloc_FIFO_Iterator_T <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex, ACE_PI_Control_Block>;
144
template class ACE_Malloc_LIFO_Iterator_T <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex, ACE_PI_Control_Block>;
145
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
146
#pragma instantiate ACE_Malloc_T <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex, ACE_PI_Control_Block>
147
#pragma instantiate ACE_Malloc_FIFO_Iterator_T <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex, ACE_PI_Control_Block>
148
#pragma instantiate ACE_Malloc_LIFO_Iterator_T <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex, ACE_PI_Control_Block>
149
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */