~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to examples/APG/Shared_Memory/PI_Malloc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// PI_Malloc.cpp,v 1.3 2004/01/07 22:40:16 shuston Exp
2
 
 
3
 
#include "ace/OS_NS_stdio.h"
4
 
#include "ace/OS_NS_string.h"
5
 
 
6
 
// Listing 1 code/ch17
7
 
#include "ace/Malloc_T.h"
8
 
#include "ace/Null_Mutex.h"
9
 
#include "ace/PI_Malloc.h"
10
 
 
11
 
typedef ACE_Malloc_T <ACE_MMAP_MEMORY_POOL,
12
 
                      ACE_Null_Mutex,
13
 
                      ACE_PI_Control_Block>
14
 
  ALLOCATOR;
15
 
typedef ACE_Malloc_LIFO_Iterator_T<ACE_MMAP_MEMORY_POOL,
16
 
                                   ACE_Null_Mutex,
17
 
                                   ACE_PI_Control_Block>
18
 
  MALLOC_LIFO_ITERATOR;
19
 
 
20
 
ALLOCATOR  *g_allocator;
21
 
// Listing 1
22
 
// Listing 2 code/ch17
23
 
class Record
24
 
{
25
 
public:
26
 
  Record (int id1, int id2, char *name)
27
 
    : id1_(id1), id2_(id2)
28
 
  {
29
 
    size_t len = ACE_OS::strlen (name) + 1;
30
 
    char *buf =
31
 
      ACE_reinterpret_cast (char *,
32
 
                            g_allocator->malloc (len));
33
 
    ACE_OS::strcpy (buf, name);
34
 
    name_ = buf;
35
 
  }
36
 
 
37
 
  ~Record() { g_allocator->free (name_.addr ()); }
38
 
 
39
 
  char *name (void) { return name_; }
40
 
  int id1 (void) { return id1_; }
41
 
  int id2 (void) { return id2_; }
42
 
 
43
 
private:
44
 
  int id1_;
45
 
  int id2_;
46
 
  ACE_Based_Pointer_Basic<char> name_;
47
 
};
48
 
// Listing 2
49
 
 
50
 
void showRecords (void)
51
 
{
52
 
  ACE_DEBUG ((LM_DEBUG,
53
 
              ACE_TEXT ("The following records were found:\n")));
54
 
 
55
 
  {
56
 
    MALLOC_LIFO_ITERATOR iter (*g_allocator);
57
 
 
58
 
    for (void *temp = 0; iter.next (temp) != 0; iter.advance ())
59
 
      {
60
 
        Record *record =
61
 
          ACE_reinterpret_cast (Record *, temp);
62
 
        ACE_DEBUG ((LM_DEBUG,
63
 
                    ACE_TEXT ("Record name: %C|id1:%d|id2:%d\n"),
64
 
                    record->name(), record->id1(), record->id2()));
65
 
      }
66
 
  }
67
 
}
68
 
 
69
 
int addRecords (void)
70
 
{
71
 
  char buf[32];
72
 
 
73
 
  for (int i = 0; i < 10; i++)
74
 
    {
75
 
      ACE_OS::sprintf (buf, "%s:%d", "Record", i);
76
 
 
77
 
      void *memory = g_allocator->malloc (sizeof (Record));
78
 
      if (memory == NULL)
79
 
        ACE_ERROR_RETURN ((LM_ERROR,
80
 
                           ACE_TEXT ("%p\n"),
81
 
                           ACE_TEXT ("Unable to malloc")),
82
 
                          -1);
83
 
 
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,
88
 
                           ACE_TEXT ("%p\n"),
89
 
                           ACE_TEXT ("bind failed")),
90
 
                          -1);
91
 
    }
92
 
 
93
 
  return 0;
94
 
}
95
 
 
96
 
// Listing 3 code/ch17
97
 
// Backing file where the data is kept.
98
 
#define BACKING_STORE "backing2.store"
99
 
 
100
 
int ACE_TMAIN (int argc, ACE_TCHAR *[])
101
 
{
102
 
  if (argc > 1)
103
 
    {
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,
109
 
                                 BACKING_STORE,
110
 
                                 &options),
111
 
                      -1);
112
 
      ACE_DEBUG ((LM_DEBUG,
113
 
                  ACE_TEXT ("Mapped to base address %@\n"),
114
 
                  g_allocator->base_addr ()));
115
 
 
116
 
      showRecords ();
117
 
    }
118
 
  else
119
 
    {
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,
124
 
                                 BACKING_STORE,
125
 
                                 &options),
126
 
                      -1);
127
 
 
128
 
      ACE_DEBUG ((LM_DEBUG,
129
 
                  ACE_TEXT ("Mapped to base address %@\n"),
130
 
                  g_allocator->base_addr ()));
131
 
 
132
 
      addRecords();
133
 
    }
134
 
 
135
 
  g_allocator->sync ();
136
 
  delete g_allocator;
137
 
  return 0;
138
 
}
139
 
// Listing 3
140
 
 
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 */
150