summaryrefslogtreecommitdiff
path: root/examples/Shared_Malloc/test_persistence.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/Shared_Malloc/test_persistence.cpp')
-rw-r--r--examples/Shared_Malloc/test_persistence.cpp270
1 files changed, 132 insertions, 138 deletions
diff --git a/examples/Shared_Malloc/test_persistence.cpp b/examples/Shared_Malloc/test_persistence.cpp
index 7757ae35ef0..19117721f89 100644
--- a/examples/Shared_Malloc/test_persistence.cpp
+++ b/examples/Shared_Malloc/test_persistence.cpp
@@ -1,7 +1,6 @@
// $Id$
-// Test the persistence capabilities of <ACE_Malloc> when configured
-// for mmap-based shared memory management.
+// Test the persistence capabilities of the ACE shared memory manager.
#include "ace/Malloc.h"
#include "ace/streams.h"
@@ -23,33 +22,31 @@ public:
Employee (void): name_ (0), id_ (0) {}
Employee (char* name, u_long id) : id_ (id)
- {
- this->name_ = (char*) shmem_manager->malloc (ACE_OS::strlen (name) + 1);
- ACE_OS::strcpy (this->name_, name );
- }
+ {
+ this->name_ = (char*) shmem_manager->malloc (ACE_OS::strlen (name) + 1);
+ ACE_OS::strcpy (this->name_, name );
+ }
~Employee (void) { shmem_manager->free (this->name_); }
char *name (void) const { return this->name_; }
void name (char* name)
- {
- if (this->name_)
- shmem_manager->free (this->name_);
-
- this->name_ = (char *) shmem_manager->malloc (ACE_OS::strlen (name) + 1);
-
- ACE_OS::strcpy (this->name_, name);
- }
+ {
+ if (this->name_)
+ shmem_manager->free (this->name_);
+ this->name_ = (char*) shmem_manager->malloc (ACE_OS::strlen (name) + 1);
+ ACE_OS::strcpy (this->name_, name);
+ }
u_long id (void) const { return id_; }
void id (u_long id) { id_ = id; }
void *operator new (size_t)
- {
- return shmem_manager->malloc (sizeof (Employee));
- }
+ {
+ return shmem_manager->malloc (sizeof (Employee));
+ }
void operator delete (void *pointer) { shmem_manager->free (pointer); }
@@ -64,151 +61,149 @@ private:
class GUI_Handler
{
public:
- GUI_Handler (void) { menu (); }
+ GUI_Handler (void) { menu(); }
~GUI_Handler (void)
- {
- MALLOC::MEMORY_POOL &pool = shmem_manager->memory_pool();
- pool.sync ();
- }
+ {
+ MALLOC::MEMORY_POOL &pool = shmem_manager->memory_pool();
+ pool.sync ();
+ }
int service(void)
- {
- char option[BUFSIZ];
- char buf1[BUFSIZ];
- char buf2[BUFSIZ];
-
- if (::scanf ("%s", option) <= 0)
- {
- ACE_ERROR ((LM_ERROR, "try again\n"));
- return 0;
- }
-
- int result = 0;
- switch (option[0])
- {
- case 'I' :
- case 'i' :
- if (::scanf ("%s %s", buf1, buf2) <= 0)
+ {
+ char option[BUFSIZ];
+ char buf1[BUFSIZ];
+ char buf2[BUFSIZ];
+
+ if (::scanf ("%s", option) <= 0)
+ {
+ ACE_ERROR ((LM_ERROR, "try again\n"));
+ return 0;
+ }
+
+ int result = 0;
+ switch (option[0])
+ {
+ case 'I' :
+ case 'i' :
+ if (::scanf ("%s %s", buf1, buf2) <= 0)
+ break;
+ result = insert_employee (buf1, ACE_OS::atoi (buf2));
break;
- result = insert_employee (buf1, ACE_OS::atoi (buf2));
- break;
- case 'F' :
- case 'f' :
- if (::scanf ("%s", buf1) <= 0)
+ case 'F' :
+ case 'f' :
+ if (::scanf ("%s", buf1) <= 0)
+ break;
+ result = find_employee (buf1);
break;
- result = find_employee (buf1);
- break;
- case 'D' :
- case 'd' :
- if (::scanf ("%s", buf1) <= 0)
+ case 'D' :
+ case 'd' :
+ if (::scanf ("%s", buf1) <= 0)
+ break;
+ result = delete_employee (buf1);
break;
- result = delete_employee (buf1);
- break;
- case 'L' :
- case 'l' :
- result = list_employees ();
- break;
- case 'Q' :
- case 'q' :
- return -1;
- ACE_NOTREACHED(break);
- default :
- cout << "unrecognized command" << endl;
- }
- if (result == 0)
- cout << "Last operation was successful!!" << endl;
- else
- cout << "Last operation failed!! " << endl;
-
- menu ();
-
- return 0;
- }
+ case 'L' :
+ case 'l' :
+ result = list_employees ();
+ break;
+ case 'Q' :
+ case 'q' :
+ return -1;
+ ACE_NOTREACHED(break);
+ default :
+ cout << "unrecognized command" << endl;
+ }
+ if (result == 0)
+ cout << "Last operation was successful!!" << endl;
+ else
+ cout << "Last operation failed!! " << endl;
+
+ menu ();
+
+ return 0;
+ }
void menu(void)
- {
- cout << endl;
- cout << "\t************************** " << endl;
- cout << "\tThe employee database menu " << endl;
- cout << endl;
- cout << "\t<I> Insert <name> <id> " << endl;
- cout << "\t<D> Delete <name> " << endl;
- cout << "\t<F> Find <name> " << endl;
- cout << endl;
- cout << "\t<L> List all employees " << endl;
- cout << endl;
- cout << "\t<Q> Quit " << endl;
- cout << "\t************************** " << endl;
- }
+ {
+ cout << endl;
+ cout << "\t************************** " << endl;
+ cout << "\tThe employee database menu " << endl;
+ cout << endl;
+ cout << "\t<I> Insert <name> <id> " << endl;
+ cout << "\t<D> Delete <name> " << endl;
+ cout << "\t<F> Find <name> " << endl;
+ cout << endl;
+ cout << "\t<L> List all employees " << endl;
+ cout << endl;
+ cout << "\t<Q> Quit " << endl;
+ cout << "\t************************** " << endl;
+ }
private:
int insert_employee (char* name, u_long id)
- {
- if (find_employee (name) == 0)
- ACE_ERROR_RETURN ((LM_ERROR, "Employee already exists\n"), -1);
+ {
+ if (find_employee (name) == 0)
+ ACE_ERROR_RETURN ((LM_ERROR, "Employee already exists\n"), -1);
- Employee* new_employee = new Employee (name, id);
- shmem_manager->bind (name, new_employee);
- return 0;
- }
+ Employee* new_employee = new Employee (name, id);
+ shmem_manager->bind (name, new_employee);
+ return 0;
+ }
int find_employee (char* name)
- {
- void *temp;
- if (shmem_manager->find (name, temp) == 0)
- {
- Employee *employee = (Employee *) temp;
+ {
+ void *temp;
+ if (shmem_manager->find (name, temp) == 0)
+ {
+ Employee *employee = (Employee *) temp;
- ACE_DEBUG ((LM_DEBUG, "The following employee was found.......\n\n"));
- ACE_DEBUG ((LM_DEBUG, "Employee name: %s\nEmployee id: %d\n",
- employee->name (), employee->id ()));
- return 0;
- }
+ ACE_DEBUG ((LM_DEBUG, "The following employee was found.......\n\n"));
+ ACE_DEBUG ((LM_DEBUG, "Employee name: %s\nEmployee id: %d\n",
+ employee->name (), employee->id ()));
+ return 0;
+ }
- return -1;
- }
+ return -1;
+ }
int list_employees (void)
- {
- MALLOC_ITERATOR iterator (*shmem_manager);
-
- ACE_DEBUG ((LM_DEBUG, "The following employees were found.......\n\n"));
-
- for (void* temp = 0;
- iterator.next (temp) != 0;
- iterator.advance ())
- {
- Employee *employee = (Employee *) temp;
- ACE_DEBUG ((LM_DEBUG, "Employee name: %s\nEmployee id: %d\n",
- employee->name (), employee->id ()));
- }
- return 0;
- }
+ {
+ MALLOC_ITERATOR iterator (*shmem_manager);
+
+ ACE_DEBUG ((LM_DEBUG, "The following employees were found.......\n\n"));
+
+ for (void* temp = 0;
+ iterator.next (temp) != 0;
+ iterator.advance ())
+ {
+ Employee *employee = (Employee *) temp;
+ ACE_DEBUG ((LM_DEBUG, "Employee name: %s\nEmployee id: %d\n",
+ employee->name (), employee->id ()));
+ }
+ return 0;
+ }
int delete_employee (char* name)
- {
- void *temp;
+ {
+ void *temp;
- if (shmem_manager->unbind (name, temp) == 0)
- {
- Employee *employee = (Employee *) temp;
+ if (shmem_manager->unbind (name, temp) == 0)
+ {
+ Employee *employee = (Employee *) temp;
- ACE_DEBUG ((LM_DEBUG,
- "The following employee was found and deleted.......\n\n"));
+ ACE_DEBUG ((LM_DEBUG,
+ "The following employee was found and deleted.......\n\n"));
- ACE_DEBUG ((LM_DEBUG, "Employee name: %s\nEmployee id: %d\n",
- employee->name (), employee->id ()));
+ ACE_DEBUG ((LM_DEBUG, "Employee name: %s\nEmployee id: %d\n",
+ employee->name (), employee->id ()));
- delete employee;
- return 0;
- }
+ delete employee;
+ return 0;
+ }
- ACE_DEBUG ((LM_DEBUG,
- "There is no employee with name %s",
- name));
- return -1;
- }
+ ACE_DEBUG ((LM_DEBUG, "There is no employee with name %s", name));
+ return -1;
+ }
};
void
@@ -230,8 +225,7 @@ main (int argc, char *argv[])
for(;;)
if (handler.service() == -1)
{
- ACE_DEBUG ((LM_DEBUG,
- "closing down ....\n"));
+ ACE_DEBUG ((LM_DEBUG, "closing down ....\n"));
break;
}