Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Shared_Malloc / test_persistence.cpp
blobc57544b1d9b1028084cae5fcbb30b99d98ad29b2
1 // Test the persistence capabilities of <ACE_Malloc> when configured
2 // for mmap-based shared memory management.
4 // This examples uses scanf to read user inputs from stdin into fixed
5 // sized buffers. This may cause buffers to overrun.
7 #include "ace/OS_NS_string.h"
8 #include "ace/Malloc_T.h"
9 #include "ace/MMAP_Memory_Pool.h"
10 #include "ace/Lib_Find.h"
12 // FUZZ: disable check_for_streams_include
13 #include "ace/streams.h"
15 #include "ace/Null_Mutex.h"
17 typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex> TEST_MALLOC;
18 typedef ACE_Malloc_LIFO_Iterator <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex> MALLOC_LIFO_ITERATOR;
19 typedef ACE_Malloc_FIFO_Iterator <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex> MALLOC_FIFO_ITERATOR;
21 // Shared memory manager.
22 static TEST_MALLOC *shmem_allocator = 0;
24 // Backing store name.
25 static ACE_TCHAR backing_store[MAXPATHLEN + 1] = ACE_TEXT ("");
27 class Employee
29 public:
30 Employee () : name_ (0), id_ (0) {}
32 Employee (const char *name, u_long id) : id_ (id)
34 size_t len = ACE_OS::strlen (name) + 1;
35 this->name_ = reinterpret_cast<char *> (shmem_allocator->malloc (len));
36 ACE_OS::strcpy (this->name_, name);
39 ~Employee () { shmem_allocator->free (this->name_); }
41 const char *name () const { return this->name_; }
43 void name (const char *name)
45 if (this->name_)
46 shmem_allocator->free (this->name_);
48 size_t len = ACE_OS::strlen (name) + 1;
49 this->name_ = reinterpret_cast<char *> (shmem_allocator->malloc (len));
50 ACE_OS::strcpy (this->name_, name);
53 u_long id () const { return id_; }
55 void id (u_long id) { id_ = id; }
57 void *operator new (size_t)
59 return shmem_allocator->malloc (sizeof (Employee));
62 void *operator new (size_t, const std::nothrow_t&)
64 return shmem_allocator->malloc (sizeof (Employee));
66 void operator delete (void *p, const std::nothrow_t&) noexcept
68 shmem_allocator->free (p);
71 void operator delete (void *pointer)
73 shmem_allocator->free (pointer);
76 private:
77 char *name_;
78 // Employee name.
80 u_long id_;
81 // Employee ID.
84 class GUI_Handler
86 public:
87 GUI_Handler () { menu (); }
89 ~GUI_Handler ()
91 TEST_MALLOC::MEMORY_POOL &pool =
92 shmem_allocator->memory_pool ();
93 pool.sync ();
96 int service()
98 char option[BUFSIZ];
99 char buf1[BUFSIZ];
100 char buf2[BUFSIZ];
102 if (::scanf ("%s", option) <= 0)
104 ACE_ERROR ((LM_ERROR,
105 "try again\n"));
106 return 0;
109 int result = 0;
110 switch (option[0])
112 case 'I' :
113 case 'i' :
114 if (::scanf ("%s %s", buf1, buf2) <= 0)
115 break;
116 result = insert_employee (buf1,
117 ACE_OS::atoi (buf2));
118 break;
119 case 'F' :
120 case 'f' :
121 if (::scanf ("%s", buf1) <= 0)
122 break;
123 result = find_employee (buf1);
124 break;
125 case 'D' :
126 case 'd' :
127 if (::scanf ("%s", buf1) <= 0)
128 break;
129 result = delete_employee (buf1);
130 break;
131 case 'L' :
132 case 'l' :
133 result = list_employees ();
134 break;
135 case 'Q' :
136 case 'q' :
137 return -1;
138 ACE_NOTREACHED(break);
139 default :
140 cout << "unrecognized command" << endl;
142 if (result == 0)
143 cout << "Last operation was successful!!" << endl;
144 else
145 cout << "Last operation failed!! " << endl;
147 menu ();
149 return 0;
152 void menu()
154 cout << endl;
155 cout << "\t************************** " << endl;
156 cout << "\tThe employee database menu " << endl;
157 cout << endl;
158 cout << "\t<I> Insert <name> <id> " << endl;
159 cout << "\t<D> Delete <name> " << endl;
160 cout << "\t<F> Find <name> " << endl;
161 cout << endl;
162 cout << "\t<L> List all employees " << endl;
163 cout << endl;
164 cout << "\t<Q> Quit " << endl;
165 cout << "\t************************** " << endl;
168 private:
169 int insert_employee (const char *name,
170 u_long id);
171 int find_employee (const char *name);
172 int list_employees ();
173 int delete_employee (const char *name);
177 GUI_Handler::insert_employee (const char *name,
178 u_long id)
180 if (find_employee (name) == 0)
181 ACE_ERROR_RETURN ((LM_ERROR,
182 "Employee already exists\n"),
183 -1);
185 Employee *new_employee = 0;
187 ACE_NEW_RETURN (new_employee,
188 Employee (name, id),
189 -1);
191 if (shmem_allocator->bind (name,
192 new_employee) == -1)
193 ACE_ERROR_RETURN ((LM_ERROR,
194 "bind failed\n"),
195 -1);
196 return 0;
200 GUI_Handler::find_employee (const char *name)
202 void *temp = 0;
204 if (shmem_allocator->find (name,
205 temp) == 0)
207 Employee *employee = reinterpret_cast<Employee *> (temp);
209 ACE_DEBUG ((LM_DEBUG,
210 "The following employee was found.......\n\n"));
211 ACE_DEBUG ((LM_DEBUG,
212 "Employee name: %s\nEmployee id: %d\n",
213 employee->name (),
214 employee->id ()));
215 return 0;
218 return -1;
222 GUI_Handler::list_employees ()
224 ACE_DEBUG ((LM_DEBUG,
225 "The following employees were found.......\n\n"));
228 ACE_DEBUG ((LM_DEBUG,
229 "LIFO order:\n"));
230 MALLOC_LIFO_ITERATOR iterator (*shmem_allocator);
232 for (void *temp = 0;
233 iterator.next (temp) != 0;
234 iterator.advance ())
236 Employee *employee = reinterpret_cast<Employee *> (temp);
237 ACE_DEBUG ((LM_DEBUG,
238 "Employee name: %s\nEmployee id: %d\n",
239 employee->name (),
240 employee->id ()));
245 ACE_DEBUG ((LM_DEBUG,
246 "FIFO order:\n"));
247 MALLOC_FIFO_ITERATOR iterator (*shmem_allocator);
249 for (void *temp = 0;
250 iterator.next (temp) != 0;
251 iterator.advance ())
253 Employee *employee = reinterpret_cast<Employee *> (temp);
254 ACE_DEBUG ((LM_DEBUG,
255 "Employee name: %s\nEmployee id: %d\n",
256 employee->name (),
257 employee->id ()));
260 return 0;
264 GUI_Handler::delete_employee (const char *name)
266 void *temp = 0;
268 if (shmem_allocator->unbind (name,
269 temp) == 0)
271 Employee *employee = reinterpret_cast<Employee *> (temp);
273 ACE_DEBUG ((LM_DEBUG,
274 "The following employee was found and deleted.......\n\n"));
276 ACE_DEBUG ((LM_DEBUG,
277 "Employee name: %s\nEmployee id: %d\n",
278 employee->name (),
279 employee->id ()));
281 delete employee;
282 return 0;
285 ACE_DEBUG ((LM_DEBUG,
286 "There is no employee with name %s",
287 name));
288 return -1;
291 void
292 parse_args (int argc, ACE_TCHAR *argv[])
294 if (argc > 1)
295 ACE_OS::strcpy (backing_store, argv[1]);
299 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
301 parse_args (argc, argv);
303 if (ACE_OS::strcmp (backing_store, ACE_TEXT ("")) == 0)
305 #if defined (ACE_DEFAULT_BACKING_STORE)
306 // Create a temporary file.
307 ACE_OS::strcpy (backing_store,
308 ACE_DEFAULT_BACKING_STORE);
309 #else /* ACE_DEFAULT_BACKING_STORE */
310 if (ACE::get_temp_dir (backing_store,
311 MAXPATHLEN - 17) == -1) // -17 for ace-malloc-XXXXXX
313 ACE_ERROR ((LM_ERROR,
314 ACE_TEXT ("Temporary path too long, ")
315 ACE_TEXT ("defaulting to current directory\n")));
316 backing_store[0] = 0;
319 // Add the filename to the end
320 ACE_OS::strcat (backing_store, ACE_TEXT ("ace-malloc-XXXXXX"));
322 #endif /* ACE_DEFAULT_BACKING_STORE */
325 ACE_NEW_RETURN (shmem_allocator,
326 TEST_MALLOC (backing_store),
327 -1);
329 GUI_Handler handler;
331 for (;;)
332 if (handler.service () == -1)
334 ACE_DEBUG ((LM_DEBUG,
335 ACE_TEXT ("closing down ....\n")));
336 break;
339 return 0;