summaryrefslogtreecommitdiff
path: root/examples/Shared_Malloc/test_position_independent_malloc.cpp
blob: 843310da32e04e9d047cec23824df2ef433761a0 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// $Id$

// Test the capability of <ACE_Malloc> to handle a single malloc that
// can be rooted at different base addresses each time it's used.

#include "ace/Malloc.h"
#include "ace/Based_Pointer_T.h"
#include "ace/Get_Opt.h"
#include "ace/Synch.h"
#include "test_position_independent_malloc.h"

ACE_RCSID(Shared_Malloc, test_multiple_mallocs, "$Id$")

#if 0
typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, ACE_Process_Mutex> MALLOC;

// Default address for memory-mapped files.
static void *base_addr = ACE_DEFAULT_BASE_ADDR;

#if 0
// Some dummy data
struct Dummy_Data
{
  int i1_;
  int i2_;
  int i3_;
  // ACE_Based_Pointer<Dummy_Data> next_;
};

struct Long_Test
{
  ACE_Based_Pointer<long> bpl_;
  long array_[10];
};
#endif /* 0 */

static void
print (Dummy_Data *data)
{
#if 0
  ACE_DEBUG ((LM_DEBUG,
              "<<<<\ni1_ = %d, i2_ = %d, i3_ = %d\n",
              data->i1_,
              data->i2_,
              data->i3_));

  ACE_DEBUG ((LM_DEBUG,
              "i1_ = %d, i2_ = %d, i3_ = %d\n>>>>\n",
              data->next_->i1_,
              data->next_->i2_,
              data->next_->i3_));
#else
  ACE_UNUSED_ARG (data);
#endif /* 0 */
}

static void *
initialize (MALLOC *allocator)
{
  void *ptr;
  ACE_ALLOCATOR_RETURN (ptr,
                        allocator->malloc (sizeof (Dummy_Data)),
                        0);
  Dummy_Data *data1 = new (ptr) Dummy_Data;

  data1->i1_ = 111;
  data1->i2_ = 222;
  data1->i3_ = 333;

  void *gap = 0;
  ACE_ALLOCATOR_RETURN (gap,
                        allocator->malloc (sizeof (256)),
                        0);

  allocator->free (gap);

  ACE_ALLOCATOR_RETURN (ptr,
                        allocator->malloc (sizeof (Dummy_Data)),
                        0);
#if 0
  Dummy_Data *data2 = new (ptr) Dummy_Data;

  data1->next_ = data2;
  data1->next_->i1_ = 111;
  data1->next_->i2_ = 222;
  data1->next_->i3_ = 333;
  data2->next_ = data1;
  data2->next_->i1_ = -111;
  data2->next_->i2_ = -222;
  data2->next_->i3_ = -333;

  // Test in shared memory using long (array/pointer)
  ACE_ALLOCATOR_RETURN (ptr,
                        allocator->malloc (sizeof (Long_Test)),
                        0);
  Long_Test *lt = new (ptr) Long_Test;

  lt->array_[0] = 1000;
  lt->array_[1] = 1001;
  lt->array_[2] = 1002;
  lt->array_[3] = 1003;
  lt->array_[4] = 1004;
  lt->bpl_ = lt->array_;

  long longCont1 = *lt->bpl_;
  long longCont3 = lt->bpl_[3];

  // Test in local memory using long (array/pointer)
  ACE_NEW_RETURN (ptr,
                  long[5],
                  0);
  longTest *lt_lcl = new (ptr) Long_Test;

  lt_lcl->array_[0] = 2000;
  lt_lcl->array_[1] = 2001;
  lt_lcl->array_[2] = 2002;
  lt_lcl->array_[3] = 2003;
  lt_lcl->array_[4] = 2004;
  lt_lcl->bpl_ = lt_lcl->array_;

  long longCont_lcl1 = *lt_lcl->bpl_;
  long longCont_lcl4 = lt_lcl->bpl_[4];
#endif /* 0 */

  return data1;
}

static void
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, "a:T");

  for (int c;
       (c = get_opt ()) != -1;
       )
    {
      switch (c)
        {
        case 'a':
          // Override the default base address.
          base_addr = (void *) ACE_OS::atoi (get_opt.optarg);
          break;
        case 'T':
          ACE_Trace::start_tracing ();
          break;
        }
    }
}

int
main (int argc, char *argv[])
{
  parse_args (argc, argv);

  ACE_MMAP_Memory_Pool_Options options (base_addr);

  // Create an allocator.
  MALLOC *allocator;
  ACE_NEW_RETURN (allocator,
                  MALLOC ("dummy_file",
                          "dummy_lock",
                          &options),
                  1);
  void *data = 0;

  // This is the first time in, so we allocate the memory and bind it
  // to the name "foo".
  if (allocator->find ("foo",
                       data) == -1)
    {
      // data = initialize (allocator);

      data = allocator->malloc (sizeof (long));
      *(long *) data = -36;

      if (allocator->bind ("foo",
                           data) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%p\n",
                           "bind"),
                          1);

      ACE_DEBUG ((LM_DEBUG,
                  "Run again to see results and release resources.\n"));
    }
  // If we find "foo" then we're running the "second" time, so we must
  // release the resources.
  else
    {
      // @@ Add a new print statement...
      // print ((Dummy_Data *) data);
      ACE_DEBUG ((LM_DEBUG,
                  "data = %d\n",
                  *(long *) data));
      allocator->free (data);
      allocator->remove ();
      ACE_DEBUG ((LM_DEBUG,
                  "all resources released\n"));
    }

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Based_Pointer<Dummy_Data>;
template class ACE_Based_Pointer_Basic<Dummy_Data>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Based_Pointer<Dummy_Data>
#pragma instantiate ACE_Based_Pointer_Basic<Dummy_Data>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
#else
int
main (int, char *[])
{
  ACE_ERROR_RETURN ((LM_ERROR,
                     "sorry, example not finished yet\n"),
                    1);
}
#endif /* 0 */