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 ("");
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
)
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
);
87 GUI_Handler () { menu (); }
91 TEST_MALLOC::MEMORY_POOL
&pool
=
92 shmem_allocator
->memory_pool ();
102 if (::scanf ("%s", option
) <= 0)
104 ACE_ERROR ((LM_ERROR
,
114 if (::scanf ("%s %s", buf1
, buf2
) <= 0)
116 result
= insert_employee (buf1
,
117 ACE_OS::atoi (buf2
));
121 if (::scanf ("%s", buf1
) <= 0)
123 result
= find_employee (buf1
);
127 if (::scanf ("%s", buf1
) <= 0)
129 result
= delete_employee (buf1
);
133 result
= list_employees ();
138 ACE_NOTREACHED(break);
140 cout
<< "unrecognized command" << endl
;
143 cout
<< "Last operation was successful!!" << endl
;
145 cout
<< "Last operation failed!! " << endl
;
155 cout
<< "\t************************** " << endl
;
156 cout
<< "\tThe employee database menu " << endl
;
158 cout
<< "\t<I> Insert <name> <id> " << endl
;
159 cout
<< "\t<D> Delete <name> " << endl
;
160 cout
<< "\t<F> Find <name> " << endl
;
162 cout
<< "\t<L> List all employees " << endl
;
164 cout
<< "\t<Q> Quit " << endl
;
165 cout
<< "\t************************** " << endl
;
169 int insert_employee (const char *name
,
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
,
180 if (find_employee (name
) == 0)
181 ACE_ERROR_RETURN ((LM_ERROR
,
182 "Employee already exists\n"),
185 Employee
*new_employee
= 0;
187 ACE_NEW_RETURN (new_employee
,
191 if (shmem_allocator
->bind (name
,
193 ACE_ERROR_RETURN ((LM_ERROR
,
200 GUI_Handler::find_employee (const char *name
)
204 if (shmem_allocator
->find (name
,
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",
222 GUI_Handler::list_employees ()
224 ACE_DEBUG ((LM_DEBUG
,
225 "The following employees were found.......\n\n"));
228 ACE_DEBUG ((LM_DEBUG
,
230 MALLOC_LIFO_ITERATOR
iterator (*shmem_allocator
);
233 iterator
.next (temp
) != 0;
236 Employee
*employee
= reinterpret_cast<Employee
*> (temp
);
237 ACE_DEBUG ((LM_DEBUG
,
238 "Employee name: %s\nEmployee id: %d\n",
245 ACE_DEBUG ((LM_DEBUG
,
247 MALLOC_FIFO_ITERATOR
iterator (*shmem_allocator
);
250 iterator
.next (temp
) != 0;
253 Employee
*employee
= reinterpret_cast<Employee
*> (temp
);
254 ACE_DEBUG ((LM_DEBUG
,
255 "Employee name: %s\nEmployee id: %d\n",
264 GUI_Handler::delete_employee (const char *name
)
268 if (shmem_allocator
->unbind (name
,
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",
285 ACE_DEBUG ((LM_DEBUG
,
286 "There is no employee with name %s",
292 parse_args (int argc
, ACE_TCHAR
*argv
[])
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
),
332 if (handler
.service () == -1)
334 ACE_DEBUG ((LM_DEBUG
,
335 ACE_TEXT ("closing down ....\n")));