diff options
Diffstat (limited to 'examples/Shared_Malloc')
-rw-r--r-- | examples/Shared_Malloc/Makefile | 90 | ||||
-rw-r--r-- | examples/Shared_Malloc/Malloc.cpp | 60 | ||||
-rw-r--r-- | examples/Shared_Malloc/Malloc.h | 25 | ||||
-rw-r--r-- | examples/Shared_Malloc/Options.cpp | 186 | ||||
-rw-r--r-- | examples/Shared_Malloc/Options.h | 81 | ||||
-rw-r--r-- | examples/Shared_Malloc/test_malloc.cpp | 196 | ||||
-rw-r--r-- | examples/Shared_Malloc/test_multiple_mallocs.cpp | 59 | ||||
-rw-r--r-- | examples/Shared_Malloc/test_persistence.cpp | 246 |
8 files changed, 0 insertions, 943 deletions
diff --git a/examples/Shared_Malloc/Makefile b/examples/Shared_Malloc/Makefile deleted file mode 100644 index d14e9eb6489..00000000000 --- a/examples/Shared_Malloc/Makefile +++ /dev/null @@ -1,90 +0,0 @@ -#---------------------------------------------------------------------------- -# @(#)Makefile 1.1 10/18/96 -# -# Makefile for tests of the Shared_Malloc wrappers -#---------------------------------------------------------------------------- - -#---------------------------------------------------------------------------- -# Local macros -#---------------------------------------------------------------------------- - -BIN = test_malloc \ - test_persistence \ - test_multiple_mallocs - -FILES = Malloc \ - Options - -LSRC = $(addsuffix .cpp,$(FILES)) -LOBJ = $(addsuffix .o,$(FILES)) -SHOBJ = $(addsuffix .so,$(FILES)) - -LDLIBS = $(addprefix .shobj/,$(SHOBJ)) - -VLDLIBS = $(LDLIBS:%=%$(VAR)) - -BUILD = $(VBIN) - -#---------------------------------------------------------------------------- -# Include macros and targets -#---------------------------------------------------------------------------- - -include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU -include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU -include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU -include $(WRAPPER_ROOT)/include/makeinclude/rules.nonested.GNU -include $(WRAPPER_ROOT)/include/makeinclude/rules.lib.GNU -include $(WRAPPER_ROOT)/include/makeinclude/rules.bin.GNU -include $(WRAPPER_ROOT)/include/makeinclude/rules.local.GNU - -#---------------------------------------------------------------------------- -# Local targets -#---------------------------------------------------------------------------- - -#---------------------------------------------------------------------------- -# Dependencies -#---------------------------------------------------------------------------- - -# DO NOT DELETE THIS LINE -- g++dep uses it. -# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY. - -.obj/Malloc.o .shobj/Malloc.so: Malloc.cpp Options.h \ - $(WRAPPER_ROOT)/ace/OS.h \ - $(WRAPPER_ROOT)/ace/Time_Value.h \ - $(WRAPPER_ROOT)/ace/config.h \ - $(WRAPPER_ROOT)/ace/stdcpp.h \ - $(WRAPPER_ROOT)/ace/Trace.h \ - $(WRAPPER_ROOT)/ace/Log_Msg.h \ - $(WRAPPER_ROOT)/ace/Log_Record.h \ - $(WRAPPER_ROOT)/ace/Log_Priority.h \ - $(WRAPPER_ROOT)/ace/ACE.h \ - $(WRAPPER_ROOT)/ace/ACE.i \ - $(WRAPPER_ROOT)/ace/Log_Record.i \ - Malloc.h $(WRAPPER_ROOT)/ace/Malloc.h \ - $(WRAPPER_ROOT)/ace/Malloc_T.h \ - $(WRAPPER_ROOT)/ace/Synch.h \ - $(WRAPPER_ROOT)/ace/SV_Semaphore_Complex.h \ - $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.h \ - $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.i \ - $(WRAPPER_ROOT)/ace/SV_Semaphore_Complex.i \ - $(WRAPPER_ROOT)/ace/Synch_T.h \ - $(WRAPPER_ROOT)/ace/Event_Handler.h \ - $(WRAPPER_ROOT)/ace/Memory_Pool.h \ - $(WRAPPER_ROOT)/ace/Signal.h \ - $(WRAPPER_ROOT)/ace/Set.h \ - $(WRAPPER_ROOT)/ace/Mem_Map.h -.obj/Options.o .shobj/Options.so: Options.cpp \ - $(WRAPPER_ROOT)/ace/Get_Opt.h \ - $(WRAPPER_ROOT)/ace/ACE.h \ - $(WRAPPER_ROOT)/ace/OS.h \ - $(WRAPPER_ROOT)/ace/Time_Value.h \ - $(WRAPPER_ROOT)/ace/config.h \ - $(WRAPPER_ROOT)/ace/stdcpp.h \ - $(WRAPPER_ROOT)/ace/Trace.h \ - $(WRAPPER_ROOT)/ace/Log_Msg.h \ - $(WRAPPER_ROOT)/ace/Log_Record.h \ - $(WRAPPER_ROOT)/ace/Log_Priority.h \ - $(WRAPPER_ROOT)/ace/Log_Record.i \ - $(WRAPPER_ROOT)/ace/ACE.i Options.h - -# IF YOU PUT ANYTHING HERE IT WILL GO AWAY diff --git a/examples/Shared_Malloc/Malloc.cpp b/examples/Shared_Malloc/Malloc.cpp deleted file mode 100644 index 78759527377..00000000000 --- a/examples/Shared_Malloc/Malloc.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "Options.h" -// $Id$ - -#include "Malloc.h" - -// Determine the type of dynamic memory manager. -#if defined (ACE_HAS_THREADS) -typedef ACE_Thread_Mutex THREAD_MUTEX; -#else -typedef ACE_Null_Mutex THREAD_MUTEX; -#endif /* ACE_HAS_THREADS */ - -// Strategic typedefs for memory allocation. - -typedef ACE_Malloc <ACE_LOCAL_MEMORY_POOL, THREAD_MUTEX> L_ALLOCATOR; -typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, ACE_Process_Mutex> M_ALLOCATOR; - -#if defined (ACE_LACKS_SYSV_SHMEM) -typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, THREAD_MUTEX> SP_ALLOCATOR; -typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, THREAD_MUTEX> ST_ALLOCATOR; -#else -typedef ACE_Malloc <ACE_SHARED_MEMORY_POOL, ACE_Process_Mutex> SP_ALLOCATOR; -typedef ACE_Malloc <ACE_SHARED_MEMORY_POOL, THREAD_MUTEX> ST_ALLOCATOR; -#endif /* ACE_LACKS_SYSV_SHMEM */ - -#if defined (ACE_LACKS_SBRK) -typedef ACE_Malloc <ACE_LOCAL_MEMORY_POOL, THREAD_MUTEX> SB_ALLOCATOR; -#else -typedef ACE_Malloc <ACE_SBRK_MEMORY_POOL, THREAD_MUTEX> SB_ALLOCATOR; -#endif /* ACE_LACKS_SBRK */ - -// Singleton -ACE_Allocator *Malloc::instance_ = 0; - -// This is a factory that decides what type of allocator to create. - -ACE_Allocator * -Malloc::instance (void) -{ - if (Malloc::instance_ == 0) - { - if (Options::instance ()->child ()) - Malloc::instance_ = new ACE_Allocator_Adapter<M_ALLOCATOR>; - else if (Options::instance ()->spawn_threads ()) - { - if (Options::instance ()->use_sbrk ()) - Malloc::instance_ = new ACE_Allocator_Adapter<SB_ALLOCATOR>; - else if (Options::instance ()->use_shmem ()) - Malloc::instance_ = new ACE_Allocator_Adapter<ST_ALLOCATOR>; - else - Malloc::instance_ = new ACE_Allocator_Adapter<L_ALLOCATOR>; - } - else if (Options::instance ()->use_mmap ()) - Malloc::instance_ = new ACE_Allocator_Adapter<M_ALLOCATOR>; - else // Use Shared_Memory_Pool. - Malloc::instance_ = new ACE_Allocator_Adapter<SP_ALLOCATOR>; - } - - return Malloc::instance_; -} diff --git a/examples/Shared_Malloc/Malloc.h b/examples/Shared_Malloc/Malloc.h deleted file mode 100644 index 57a1eecd1f2..00000000000 --- a/examples/Shared_Malloc/Malloc.h +++ /dev/null @@ -1,25 +0,0 @@ -/* -*- C++ -*- */ -// $Id$ - -#if !defined (MY_MALLOC_H) -#define MY_MALLOC_H - -#include "ace/Malloc.h" - -class Malloc - // = TITLE - // Allocator Singleton. -{ -public: - static ACE_Allocator *instance (void); - // Returns static instance. - -private: - Malloc (void); - // Ensure Singleton. - - static ACE_Allocator *instance_; - // Malloc Singleton. -}; - -#endif /* MY_MALLOC_H */ diff --git a/examples/Shared_Malloc/Options.cpp b/examples/Shared_Malloc/Options.cpp deleted file mode 100644 index bded6518699..00000000000 --- a/examples/Shared_Malloc/Options.cpp +++ /dev/null @@ -1,186 +0,0 @@ -#include "ace/Get_Opt.h" -// $Id$ - - -#include "Options.h" - -// Static Singleton instance. -Options *Options::instance_ = 0; - -Options * -Options::instance (void) -{ - if (Options::instance_ == 0) - Options::instance_ = new Options (); - - return Options::instance_; -} - -char * -Options::program_name (void) -{ - return this->program_name_; -} - -const char * -Options::slave_name (void) -{ - return this->slave_name_; -} - -int -Options::debug (void) -{ - return this->debug_; -} - -int -Options::exec_slave (void) -{ - return this->exec_slave_; -} - -size_t -Options::iteration_count (void) -{ - return this->iteration_count_; -} - -int -Options::use_sbrk (void) -{ - return this->use_sbrk_; -} - -size_t -Options::max_msg_size (void) -{ - return this->max_msg_size_; -} - -size_t -Options::spawn_count (void) -{ - return this->spawn_count_; -} - -int -Options::spawn_threads (void) -{ - return this->spawn_threads_; -} - -int -Options::use_mmap (void) -{ - return this->use_mmap_; -} - -int -Options::use_shmem (void) -{ - return this->use_shmem_; -} - -int -Options::child (void) -{ - return this->child_; -} - -// Explain usage and exit. - -void -Options::print_usage_and_die (void) -{ - ACE_ERROR ((LM_ERROR, "usage: %n" - "\n[-d] (run in debugging mode)\n" - "[-e] (use exec(2) in addition to fork(2))\n" - "[-l] (use C++ new operator rather than sbrk(2)\n" - "[-L max_msg_size]\n" - "[-m] (use mmap rather than SysV shared memory)\n" - "[-p] (use processes rather than threads)\n" - "[-s] (use SysV shared memory rather than mmap)\n" - "[-t number of threads or processes to spawn]\n" - "[-n iteration_count]\n%a", -1)); - /* NOTREACHED */ -} - -Options::Options (void) - : slave_name_ ("slave"), - debug_ (0), - exec_slave_ (0), - iteration_count_ (100), - use_sbrk_ (0), - use_shmem_ (0), - max_msg_size_ (127), - spawn_count_ (1), - spawn_threads_ (1), - use_mmap_ (0), - child_ (0) -{ -} - -void -Options::parse_args (int argc, char *argv[]) -{ - ACE_Get_Opt get_opt (argc, argv, "dehlL:mn:pst:"); - - this->program_name_ = argv[0]; - ACE_LOG_MSG->open (this->program_name_); - - // Put in a special-case check for child process. - if (ACE_OS::strcmp (this->program_name_, slave_name_) == 0) - { - this->child_ = 1; - this->use_mmap_ = 1; - } - - int c; - - while ((c = get_opt ()) != -1) - { - switch (c) - { - case 'd': - this->debug_ = 1; - break; - case 'e': - this->exec_slave_ = 1; - break; - case 'h': - this->print_usage_and_die (); - /* NOTREACHED */ - break; - case 'l': - this->use_sbrk_ = 0; - break; - case 'L': - this->max_msg_size_ = ACE_OS::atoi (get_opt.optarg); - break; - case 'm': - this->use_mmap_ = 1; - break; - case 'n': - this->iteration_count_ = ACE_OS::atoi (get_opt.optarg); - break; - case 'p': // Spawn processes rather than threads - this->spawn_threads_ = 0; - break; - case 's': - // Use System V shared memory... - this->use_shmem_ = 1; - break; - case 't': - this->spawn_count_ = ACE_OS::atoi (get_opt.optarg); - break; - default: - this->print_usage_and_die (); - /* NOTREACHED */ - break; - } - } - // Switch to using MMAP when the -p and -e options are enabled. - if (this->exec_slave_ == 1 && this->spawn_threads_ == 0) - this->use_mmap_ = 1; -} diff --git a/examples/Shared_Malloc/Options.h b/examples/Shared_Malloc/Options.h deleted file mode 100644 index 931f7411673..00000000000 --- a/examples/Shared_Malloc/Options.h +++ /dev/null @@ -1,81 +0,0 @@ -/* -*- C++ -*- */ -// $Id$ - -#if !defined (_OPTIONS) -#define _OPTIONS - -#include "ace/OS.h" - -class Options - // = TITLE - // Options Singleton. -{ -public: - static Options *instance (void); - // Return Singleton. - - void parse_args (int argc, char *argv[]); - // Parse the arguments. - - // = Accessor methods. - char *program_name (void); - const char *slave_name (void); - int debug (void); - int exec_slave (void); - size_t iteration_count (void); - int use_sbrk (void); - int use_shmem (void); - size_t max_msg_size (void); - size_t spawn_count (void); - int spawn_threads (void); - int use_mmap (void); - int child (void); - -private: - Options (void); - // Ensure Singleton. - - static Options *instance_; - // Singleton. - - void print_usage_and_die (void); - // Explain usage and exit. - - char *program_name_; - // Name of the program. - - const char *slave_name_; - // Name of slave process. - - int debug_; - // Flag to indicate if we are debugging. - - int exec_slave_; - // Flag to indicate if we should exec after forking. - - size_t iteration_count_; - // Number of iterations to call malloc_recurse(). - - int use_sbrk_; - // Should we use sbrk(2)? - - int use_shmem_; - // Should we use Shared Memory? - - size_t max_msg_size_; - // Maximum number of bytes to malloc. - - size_t spawn_count_; - // Number of threads. - - int spawn_threads_; - // Spawn threads vs. processes. - - int use_mmap_; - // Use mmap() as the backing store. - - int child_; - // We're a child process. -}; - -#endif /* _OPTIONS */ diff --git a/examples/Shared_Malloc/test_malloc.cpp b/examples/Shared_Malloc/test_malloc.cpp deleted file mode 100644 index 694e534251b..00000000000 --- a/examples/Shared_Malloc/test_malloc.cpp +++ /dev/null @@ -1,196 +0,0 @@ -// $Id$ - -// This program tests out all the various ACE_Malloc combinations and -// the ACE_Allocator_Adapter. - -#include "ace/Thread.h" -#include "ace/Thread_Manager.h" -#include "Malloc.h" -#include "Options.h" - -// Global thread manager. -static ACE_Thread_Manager thread_manager; - -static int -gen_size (void) -{ -#if defined (ACE_HAS_THREADS) - ACE_RANDR_TYPE seed = ACE_RANDR_TYPE (&seed); - return (ACE_OS::rand_r (ACE_RANDR_TYPE (seed)) % Options::instance ()->max_msg_size ()) + 1; -#else - return (ACE_OS::rand () % Options::instance ()->max_msg_size ()) + 1; -#endif /* ACE_HAS_THREADS */ -} - -// Recursively allocate and deallocate dynamic memory. - -static int -malloc_recurse (int count) -{ - static char default_char = 0; - - if (count <= 0) - { - if (Options::instance ()->debug ()) - AMS (Malloc::instance ()->print_stats ()); - } - else - { - int alloc_size = gen_size (); - void *ptr = Malloc::instance ()->malloc (alloc_size); - - if (ptr == 0) - ACE_ERROR ((LM_ERROR, "(%P|%t) *** malloc of size %d failed, %p\n%a", - "malloc", alloc_size)); - else - { - ACE_OS::memset (ptr, default_char++, alloc_size); - - if (Options::instance ()->debug ()) - ACE_DEBUG ((LM_INFO, "(%P|%t) %u (alloc), size = %d\n", ptr, alloc_size)); - - // Call ourselves recursively - malloc_recurse (count - 1); - - if (Options::instance ()->debug ()) - ACE_DEBUG ((LM_INFO, "(%P|%t) %u (free), size = %d\n", ptr, alloc_size)); - - Malloc::instance ()->free (ptr); - } - } - return 0; -} - -static void * -worker (void *arg) -{ - // Allocate a thread control object, which automatically removes the - // thread from the thread manager on exit. - ACE_Thread_Control tc (&thread_manager); - - malloc_recurse (int (arg)); - return 0; -} - -// Create the appropriate type of process/thread. - -static void -spawn (void) -{ - if (Options::instance ()->spawn_threads ()) - { -#if defined (ACE_HAS_THREADS) - if (thread_manager.spawn (ACE_THR_FUNC (worker), - (void *) Options::instance ()->iteration_count (), - THR_BOUND) == -1) - ACE_ERROR ((LM_ERROR, "%p\n%a", "thread create failed")); -#else - if (Options::instance ()->spawn_count () > 1) - ACE_ERROR ((LM_ERROR, "only one thread may be run in a process on this platform\n%a", 1)); -#endif /* ACE_HAS_THREADS */ - } -#if !defined (ACE_WIN32) - else - { - if (ACE_OS::fork () == 0) - { - if (Options::instance ()->exec_slave ()) - { - char iterations[20]; - char msg_size[20]; - - ACE_OS::sprintf (iterations, "%d", Options::instance ()->iteration_count ()); - ACE_OS::sprintf (msg_size, "%d", Options::instance ()->max_msg_size ()); - - char *argv[8]; - argv[0] = (char *) Options::instance ()->slave_name (); - argv[1] = "-p"; - argv[2] = "-n"; - argv[3] = iterations; - argv[4] = "-L"; - argv[5] = msg_size; - argv[6] = Options::instance ()->debug () ? "-d" : ""; - argv[7] = (char *) 0; - - if (ACE_OS::execv (Options::instance ()->program_name (), argv) == -1) - ACE_ERROR ((LM_ERROR, "%p\n", "exec failed")); - ACE_OS::_exit (1); - } - else - { - ACE_LOG_MSG->sync (Options::instance ()->program_name ()); - - ACE_DEBUG ((LM_INFO, - "(%P|%t) about to recurse with iteration count = %d\n", - Options::instance ()->iteration_count ())); - - malloc_recurse (Options::instance ()->iteration_count ()); - ACE_OS::exit (0); - } - } - } -#endif /* ACE_WIN32 */ -} - -// Wait for all the child processes/threads to exit. - -static void -wait_for_children (void) -{ - if (Options::instance ()->spawn_threads ()) - { -#if defined (ACE_HAS_THREADS) - // Wait for the threads to terminate. - thread_manager.wait (); -#else - malloc_recurse (Options::instance ()->iteration_count ()); -#endif /* ACE_HAS_THREADS */ - } -#if !defined (ACE_WIN32) - else - { - pid_t pid; - - while ((pid = ACE_OS::wait (0)) != -1) - ACE_DEBUG ((LM_DEBUG, "(%P|%t) reaped pid = %d\n", pid)); - } -#endif /* ACE_WIN32 */ -} - -extern "C" void -handler (int) -{ - Malloc::instance ()->remove (); - ACE_ERROR ((LM_ERROR, "(%P|%t) removed handler\n%a", 0)); -} - -int -main (int argc, char *argv[]) -{ - // Register a signal handler. - ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); - - Options::instance ()->parse_args (argc, argv); - -#if !defined (ACE_WIN32) - if (Options::instance ()->child ()) - { - ACE_DEBUG ((LM_INFO, - "(%P|%t) about to recurse with iteration count = %d, debug = %d\n", - Options::instance ()->iteration_count ())); - - // We've been forked... - malloc_recurse (Options::instance ()->iteration_count ()); - } - else -#endif /* ACE_WIN32 */ - { - for (size_t i = 0; i < Options::instance ()->spawn_count (); i++) - spawn (); - - wait_for_children (); - Malloc::instance ()->remove (); - } - return 0; -} - diff --git a/examples/Shared_Malloc/test_multiple_mallocs.cpp b/examples/Shared_Malloc/test_multiple_mallocs.cpp deleted file mode 100644 index e4d769a93e6..00000000000 --- a/examples/Shared_Malloc/test_multiple_mallocs.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// $Id$ - -// Test the capabilities of the ACE shared memory manager in terms of -// its ability to handle multiple mallocs rooted at different base -// addresses. - -#include "ace/Malloc.h" -#include "ace/Synch.h" - -typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, ACE_Process_Mutex> MALLOC; - -// Default address for shared memory mapped files and SYSV shared -// memory (defaults to 64 M). -void *REQUEST_BASE_ADDR = ((void *) (64 * 1024 * 1024)); -const char *REQUEST_STRING = "hello from request repository"; - -// Default address for shared memory mapped files and SYSV shared -// memory (defaults to 64 M). -void *RESPONSE_BASE_ADDR = ((void *) (128 * 1024 * 1024)); -const char *RESPONSE_STRING = "hello from response repository"; - -int -main (void) -{ - ACE_MMAP_Memory_Pool_Options request_options (REQUEST_BASE_ADDR); - - // Create an adapter version of an allocator. - ACE_Allocator_Adapter<MALLOC> *shmem_request = - new ACE_Allocator_Adapter<MALLOC> ("request_file", "RequestLock", &request_options); - - ACE_MMAP_Memory_Pool_Options response_options (RESPONSE_BASE_ADDR); - - // Create a non-adapter version of an allocator. - MALLOC *shmem_response = - new MALLOC ("response_file","ResponseLock", &response_options); - - void *data = 0; - - if (shmem_request->find ("foo", data) == 0) - cout << (char *) data << endl; - else - { - data = shmem_request->malloc (ACE_OS::strlen (REQUEST_STRING) + 1); - ACE_OS::strcpy ((char *) data, REQUEST_STRING); - shmem_request->bind ("foo", data); - } - data = 0; - - if (shmem_response->find ("foo", data) == 0) - cout << (char *) data << endl; - else - { - data = shmem_response->malloc (ACE_OS::strlen (RESPONSE_STRING) + 1); - ACE_OS::strcpy ((char *) data, RESPONSE_STRING); - shmem_response->bind ("foo", data); - } - - return 0; -} diff --git a/examples/Shared_Malloc/test_persistence.cpp b/examples/Shared_Malloc/test_persistence.cpp deleted file mode 100644 index 1afef0a4a60..00000000000 --- a/examples/Shared_Malloc/test_persistence.cpp +++ /dev/null @@ -1,246 +0,0 @@ -// Test the persistence capabilities of the ACE shared memory manager. -// $Id$ - - - -#include "ace/Malloc.h" - -typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex> MALLOC; -typedef ACE_Malloc_Iterator <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex> MALLOC_ITERATOR; - -// Shared memory manager. -static MALLOC *shmem_manager = 0; - -// Backing store name. -static char *backing_store = ACE_DEFAULT_BACKING_STORE; - -class Employee -{ -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 ); - } - - ~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); - } - - u_long id (void) const { return id_; } - - void id (u_long id) { id_ = id; } - - friend ostream &operator<<(ostream &stream, const Employee &employee) - { - stream << endl; - stream << "Employee name: " << employee.name() << endl; - stream << "Employee id: " << employee.id() << endl; - stream << endl; - - return stream; - } - - void *operator new (size_t) - { - return shmem_manager->malloc (sizeof (Employee)); - } - - void operator delete (void *pointer) { shmem_manager->free (pointer); } - -private: - char *name_; - // Employee name. - - u_long id_; - // Employee ID. -}; - -class GUI_Handler -{ -public: - GUI_Handler (void) { menu(); } - - ~GUI_Handler (void) - { - 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) - break; - result = insert_employee (buf1, ACE_OS::atoi (buf2)); - break; - case 'F' : - case 'f' : - if (::scanf ("%s", buf1) <= 0) - break; - result = find_employee (buf1); - break; - case 'D' : - case 'd' : - if (::scanf ("%s", buf1) <= 0) - break; - result = delete_employee (buf1); - break; - case 'L' : - case 'l' : - result = list_employees (); - break; - case 'Q' : - case 'q' : - return -1; - 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; - } - -private: - int insert_employee (char* name, u_long id) - { - 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; - } - - int find_employee (char* name) - { - 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; - } - - 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; - } - - int delete_employee (char* name) - { - void *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, "Employee name: %s\nEmployee id: %d\n", - employee->name (), employee->id ())); - - delete employee; - return 0; - } - - ACE_DEBUG ((LM_DEBUG, "There is no employee with name %s", name)); - return -1; - } -}; - -void -parse_args (int argc, char *argv[]) -{ - if (argc > 1); - backing_store = argv[1]; -} - -int -main (int argc, char *argv[]) -{ - parse_args (argc, argv); - - shmem_manager = new MALLOC (backing_store); - - GUI_Handler handler; - - for(;;) - if (handler.service() == -1) - { - ACE_DEBUG ((LM_DEBUG, "closing down ....\n")); - break; - } - - return 0; -} - -#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) -template class ACE_Malloc <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex>; -template class ACE_Malloc_Iterator <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex>; -#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ |