1 #include "ace/OS_NS_stdio.h"
2 #include "ace/OS_NS_string.h"
5 #include "ace/MMAP_Memory_Pool.h"
6 #include "ace/Malloc_T.h"
7 #include "ace/Null_Mutex.h"
8 #include "ace/PI_Malloc.h"
10 typedef ACE_Malloc_T
<ACE_MMAP_MEMORY_POOL
,
14 typedef ACE_Malloc_LIFO_Iterator_T
<ACE_MMAP_MEMORY_POOL
,
19 ALLOCATOR
*g_allocator
;
21 // Listing 2 code/ch17
25 Record (int id1
, int id2
, char *name
)
26 : id1_(id1
), id2_(id2
)
28 size_t len
= ACE_OS::strlen (name
) + 1;
30 reinterpret_cast<char *> (g_allocator
->malloc (len
));
31 ACE_OS::strcpy (buf
, name
);
35 ~Record() { g_allocator
->free (name_
.addr ()); }
37 char *name () { return name_
; }
38 int id1 () { return id1_
; }
39 int id2 () { return id2_
; }
44 ACE_Based_Pointer_Basic
<char> name_
;
51 ACE_TEXT ("The following records were found:\n")));
54 MALLOC_LIFO_ITERATOR
iter (*g_allocator
);
56 for (void *temp
= 0; iter
.next (temp
) != 0; iter
.advance ())
59 reinterpret_cast<Record
*> (temp
);
61 ACE_TEXT ("Record name: %C|id1:%d|id2:%d\n"),
62 record
->name(), record
->id1(), record
->id2()));
71 for (int i
= 0; i
< 10; i
++)
73 ACE_OS::sprintf (buf
, "%s:%d", "Record", i
);
75 void *memory
= g_allocator
->malloc (sizeof (Record
));
77 ACE_ERROR_RETURN ((LM_ERROR
,
79 ACE_TEXT ("Unable to malloc")),
82 // Allocate and place record
83 Record
* newRecord
= new (memory
) Record (i
, i
+1, buf
);
84 if (g_allocator
->bind (buf
, newRecord
) == -1)
85 ACE_ERROR_RETURN ((LM_ERROR
,
87 ACE_TEXT ("bind failed")),
94 // Listing 3 code/ch17
95 // Backing file where the data is kept.
96 #define BACKING_STORE ACE_TEXT("backing2.store")
98 int ACE_TMAIN (int argc
, ACE_TCHAR
*[])
102 ACE_MMAP_Memory_Pool_Options options
103 (ACE_DEFAULT_BASE_ADDR
,
104 ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED
);
105 ACE_NEW_RETURN (g_allocator
,
106 ALLOCATOR (BACKING_STORE
,
110 ACE_DEBUG ((LM_DEBUG
,
111 ACE_TEXT ("Mapped to base address %@\n"),
112 g_allocator
->base_addr ()));
118 ACE_MMAP_Memory_Pool_Options options
119 (0, ACE_MMAP_Memory_Pool_Options::NEVER_FIXED
);
120 ACE_NEW_RETURN (g_allocator
,
121 ALLOCATOR (BACKING_STORE
,
126 ACE_DEBUG ((LM_DEBUG
,
127 ACE_TEXT ("Mapped to base address %@\n"),
128 g_allocator
->base_addr ()));
133 g_allocator
->sync ();