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"
9 typedef ACE_Malloc
<ACE_MMAP_MEMORY_POOL
, ACE_Null_Mutex
>
11 typedef ACE_Malloc_LIFO_Iterator
<ACE_MMAP_MEMORY_POOL
,
15 ALLOCATOR
*g_allocator
;
18 // Listing 2 code/ch17
22 Record (int id1
, int id2
, char *name
)
23 : id1_(id1
), id2_(id2
), name_(0)
25 size_t len
= ACE_OS::strlen (name
) + 1;
27 reinterpret_cast<char *> (g_allocator
->malloc (len
));
28 ACE_OS::strcpy (this->name_
, name
);
31 ~Record () { g_allocator
->free (name_
); }
32 char* name() { return name_
; }
33 int id1 () { return id1_
; }
34 int id2 () { return id2_
; }
42 // Listing 5 code/ch17
46 ACE_TEXT ("The following records were found:\n")));
48 MALLOC_LIFO_ITERATOR
iter (*g_allocator
);
50 for (void *temp
= 0; iter
.next (temp
) != 0; iter
.advance ())
53 reinterpret_cast<Record
*> (temp
);
55 ACE_TEXT ("Record name: %C|id1:%d|id2:%d\n"),
63 // Listing 3 code/ch17
68 for (int i
= 0; i
< 10; i
++)
70 ACE_OS::sprintf (buf
, "%s:%d", "Record", i
);
71 void *memory
= g_allocator
->malloc (sizeof (Record
));
73 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
74 ACE_TEXT ("Unable to malloc")),
77 // Allocate and place record
78 Record
* newRecord
= new (memory
) Record (i
, i
+1, buf
);
79 if (g_allocator
->bind (buf
, newRecord
) == -1)
80 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
81 ACE_TEXT ("bind failed")),
88 // Listing 4 code/ch17
89 // Backing file where the data is kept.
90 #define BACKING_STORE ACE_TEXT("backing.store")
92 int ACE_TMAIN (int argc
, ACE_TCHAR
*[])
94 ACE_NEW_RETURN (g_allocator
,
95 ALLOCATOR (BACKING_STORE
),
106 g_allocator
->sync ();