diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 1999-07-07 08:27:07 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 1999-07-07 08:27:07 +0000 |
commit | 77f8caed5f5f23c74d55c908da467c2ba53f4327 (patch) | |
tree | 8db313ec4441d28785ffc4a66b6070b99f362dc5 /examples | |
parent | 7d05afaa28181c4e092f5460e499efabcd895ffb (diff) | |
download | ATCD-77f8caed5f5f23c74d55c908da467c2ba53f4327.tar.gz |
ChangeLogTag:Wed Jul 07 01:28:40 1999 Nanbor Wang <nanbor@cs.wustl.edu>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Shared_Malloc/Makefile | 7 | ||||
-rw-r--r-- | examples/Shared_Malloc/test_multiple_mallocs.cpp | 94 | ||||
-rw-r--r-- | examples/Shared_Malloc/test_persistence.cpp | 270 | ||||
-rw-r--r-- | examples/Shared_Malloc/test_position_independent_malloc.cpp | 172 |
4 files changed, 382 insertions, 161 deletions
diff --git a/examples/Shared_Malloc/Makefile b/examples/Shared_Malloc/Makefile index 06372dde7c4..7fb08eec0d1 100644 --- a/examples/Shared_Malloc/Makefile +++ b/examples/Shared_Malloc/Makefile @@ -10,6 +10,7 @@ BIN = test_malloc \ test_persistence \ + test_position_independent_malloc \ test_multiple_mallocs FILES = Malloc \ @@ -57,8 +58,7 @@ include $(ACE_ROOT)/include/makeinclude/rules.local.GNU $(ACE_ROOT)/ace/ACE.i \ $(ACE_ROOT)/ace/Log_Priority.h \ $(ACE_ROOT)/ace/Log_Record.i \ - Malloc.h \ - $(ACE_ROOT)/ace/Malloc.h \ + Malloc.h $(ACE_ROOT)/ace/Malloc.h \ $(ACE_ROOT)/ace/Malloc_Base.h \ $(ACE_ROOT)/ace/Malloc.i \ $(ACE_ROOT)/ace/Malloc_T.h \ @@ -112,7 +112,6 @@ include $(ACE_ROOT)/include/makeinclude/rules.local.GNU $(ACE_ROOT)/ace/ACE.i \ $(ACE_ROOT)/ace/Log_Priority.h \ $(ACE_ROOT)/ace/Log_Record.i \ - $(ACE_ROOT)/ace/Get_Opt.i \ - Options.h + $(ACE_ROOT)/ace/Get_Opt.i Options.h # IF YOU PUT ANYTHING HERE IT WILL GO AWAY diff --git a/examples/Shared_Malloc/test_multiple_mallocs.cpp b/examples/Shared_Malloc/test_multiple_mallocs.cpp index 4a1c8e7d3b4..568d72f39f7 100644 --- a/examples/Shared_Malloc/test_multiple_mallocs.cpp +++ b/examples/Shared_Malloc/test_multiple_mallocs.cpp @@ -1,8 +1,7 @@ // $Id$ -// Test the capabilities of the ACE shared memory manager in terms of -// its ability to handle multiple mallocs rooted at different base -// addresses. +// Test the capability of <ACE_Malloc> to handle multiple mallocs +// rooted at different base addresses. #include "ace/Malloc.h" #include "ace/Synch.h" @@ -13,56 +12,101 @@ 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"; +static void *request_base_addr = ((void *) (64 * 1024 * 1024)); +static 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"; +static void *response_base_addr = ((void *) (128 * 1024 * 1024)); +static const char *response_string = "hello from response repository"; int main (int, char *[]) { - ACE_MMAP_Memory_Pool_Options request_options (REQUEST_BASE_ADDR); + 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_Allocator_Adapter<MALLOC> *shmem_request; - ACE_MMAP_Memory_Pool_Options response_options (RESPONSE_BASE_ADDR); + ACE_NEW_RETURN (shmem_request, + ACE_Allocator_Adapter<MALLOC> ("request_file", + "RequestLock", + &request_options), + 1); - // Create a non-adapter version of an allocator. - MALLOC *shmem_response = - new MALLOC ("response_file","ResponseLock", &response_options); + ACE_MMAP_Memory_Pool_Options response_options (response_base_addr); + // Create a non-adapter version of an allocator. + MALLOC *shmem_response; + ACE_NEW_RETURN (shmem_response, + MALLOC ("response_file", + "ResponseLock", + &response_options), + 1); void *data = 0; - if (shmem_request->find ("foo", data) == 0) + // If we find "foo" then we're running the "second" time, so we must + // release the resources. + if (shmem_request->find ("foo", + data) == 0) { - ACE_OS::printf ("%s\n", data); + ACE_DEBUG ((LM_DEBUG, + "%s\n", + data)); shmem_request->remove (); } + + // This is the first time in, so we allocate the memory and bind it + // to the name "foo". else { - data = shmem_request->malloc (ACE_OS::strlen (REQUEST_STRING) + 1); - ACE_OS::strcpy ((char *) data, REQUEST_STRING); - shmem_request->bind ("foo", data); + ACE_ALLOCATOR_RETURN (data, + shmem_request->malloc (ACE_OS::strlen (request_string) + 1), + 1); + ACE_OS::strcpy ((char *) data, + request_string); + + if (shmem_request->bind ("foo", + data) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "bind"), + 1); } data = 0; - if (shmem_response->find ("foo", data) == 0) + // If we find "foo" then we're running the "second" time, so we must + // release the resources. + if (shmem_response->find ("foo", + data) == 0) { - ACE_OS::printf ("%s\n", data); + ACE_DEBUG ((LM_DEBUG, + "%s\n", + data)); shmem_response->remove (); + ACE_DEBUG ((LM_DEBUG, + "all resources have been released\n")); } + + // This is the first time in, so we allocate the memory and bind it + // to the name "foo". else { - data = shmem_response->malloc (ACE_OS::strlen (RESPONSE_STRING) + 1); - ACE_OS::strcpy ((char *) data, RESPONSE_STRING); - shmem_response->bind ("foo", data); + ACE_ALLOCATOR_RETURN (data, + shmem_response->malloc (ACE_OS::strlen (response_string) + 1), + 1); + ACE_OS::strcpy ((char *) data, + response_string); + + if (shmem_response->bind ("foo", + data) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "bind"), + 1); - ACE_OS::printf ("Run again to see results and release resources.\n"); + ACE_DEBUG ((LM_DEBUG, + "Run again to see results and release resources.\n")); } return 0; diff --git a/examples/Shared_Malloc/test_persistence.cpp b/examples/Shared_Malloc/test_persistence.cpp index 19117721f89..7757ae35ef0 100644 --- a/examples/Shared_Malloc/test_persistence.cpp +++ b/examples/Shared_Malloc/test_persistence.cpp @@ -1,6 +1,7 @@ // $Id$ -// Test the persistence capabilities of the ACE shared memory manager. +// Test the persistence capabilities of <ACE_Malloc> when configured +// for mmap-based shared memory management. #include "ace/Malloc.h" #include "ace/streams.h" @@ -22,31 +23,33 @@ 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); } @@ -61,149 +64,151 @@ 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) - break; - result = insert_employee (buf1, ACE_OS::atoi (buf2)); - break; - case 'F' : - case 'f' : - if (::scanf ("%s", buf1) <= 0) - break; - result = find_employee (buf1); + { + 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; - case 'D' : - case 'd' : - if (::scanf ("%s", buf1) <= 0) - break; - result = delete_employee (buf1); + result = insert_employee (buf1, ACE_OS::atoi (buf2)); + break; + case 'F' : + case 'f' : + if (::scanf ("%s", buf1) <= 0) break; - case 'L' : - case 'l' : - result = list_employees (); + result = find_employee (buf1); + break; + case 'D' : + case 'd' : + if (::scanf ("%s", buf1) <= 0) 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; - } + 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; + } 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 @@ -225,7 +230,8 @@ 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; } diff --git a/examples/Shared_Malloc/test_position_independent_malloc.cpp b/examples/Shared_Malloc/test_position_independent_malloc.cpp new file mode 100644 index 00000000000..1b62b6d184b --- /dev/null +++ b/examples/Shared_Malloc/test_position_independent_malloc.cpp @@ -0,0 +1,172 @@ +// $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/Synch.h" + +ACE_RCSID(Shared_Malloc, test_multiple_mallocs, "$Id$") + +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; + +// Some dummy data +struct Dummy_Data +{ + int i1_; + int i2_; + int i3_; + ACE_Based_Pointer<Dummy_Data> next_; +}; + +#if 0 +struct Long_Test +{ + ACE_Based_Pointer<long> bpl_; + long array_[10]; +}; +#endif /* 0 */ + +static void +print (Dummy_Data *data) +{ + 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_)); +} + +static void * +initialize (MALLOC *allocator) +{ + void *ptr; + ACE_ALLOCATOR_RETURN (ptr, + allocator->malloc (sizeof (Dummy_Data)), + 0); + Dummy_Data *data1 = new (ptr) Dummy_Data; + + void *gap = 0; + ACE_ALLOCATOR_RETURN (gap, + allocator->malloc (sizeof (256)), + 0); + + ACE_ALLOCATOR_RETURN (ptr, + allocator->malloc (sizeof (Dummy_Data)), + 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; + +#if 0 + // 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 */ + + allocator->free (gap); + + return data1; +} + +int +main (int argc, char *argv[]) +{ + if (argc > 1) + // Override the default base address. + base_addr = (void *) ACE_OS::atoi (argv[1]); + + 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); + + 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); + + 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>; +#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA) +#pragma instantiate ACE_Based_Pointer<Dummy_Data> +#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */ |