summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ace/Based_Pointer_Repository.h2
-rw-r--r--ace/Based_Pointer_T.cpp10
-rw-r--r--ace/Based_Pointer_T.h2
-rw-r--r--ace/Malloc.h10
-rw-r--r--ace/Malloc_T.h4
-rw-r--r--ace/Memory_Pool.cpp16
-rw-r--r--ace/README3
-rw-r--r--examples/Shared_Malloc/Makefile7
-rw-r--r--examples/Shared_Malloc/test_multiple_mallocs.cpp94
-rw-r--r--examples/Shared_Malloc/test_persistence.cpp270
-rw-r--r--examples/Shared_Malloc/test_position_independent_malloc.cpp172
11 files changed, 408 insertions, 182 deletions
diff --git a/ace/Based_Pointer_Repository.h b/ace/Based_Pointer_Repository.h
index 206d845d0c8..b3180c008b9 100644
--- a/ace/Based_Pointer_Repository.h
+++ b/ace/Based_Pointer_Repository.h
@@ -15,7 +15,7 @@
//
// ============================================================================
-#if !defined (ACE_BASED_POINTER_REPOSITORY_H)
+#ifndef ACE_BASED_POINTER_REPOSITORY_H
#define ACE_BASED_POINTER_REPOSITORY_H
#include "ace/Singleton.h"
diff --git a/ace/Based_Pointer_T.cpp b/ace/Based_Pointer_T.cpp
index f8c6717889e..6a01f656f8c 100644
--- a/ace/Based_Pointer_T.cpp
+++ b/ace/Based_Pointer_T.cpp
@@ -1,21 +1,23 @@
// $Id$
-#if !defined (ACE_BASED_POINTER_T_CPP)
+#ifndef ACE_BASED_POINTER_T_CPP
#define ACE_BASED_POINTER_T_CPP
#include "ace/Based_Pointer_Repository.h"
#include "ace/Based_Pointer_T.h"
template <class CONCRETE>
-ACE_Based_Pointer<CONCRETE>::ACE_BasedPointer (void)
+ACE_Based_Pointer<CONCRETE>::ACE_Based_Pointer (void)
: target_ (0),
base_offset_ (0)
{
+ void *base_addr = 0;
+
// Find the base address associated with our <this> pointer. Note
// that it's ok for <find> to return 0, which simply indicates that
// the address is not in memory-mapped virtual address space.
- void *base_addr =
- ACE_BASED_POINTER_REPOSITORY::instance ()->find (this);
+ ACE_BASED_POINTER_REPOSITORY::instance ()->find (this,
+ base_addr);
this->base_offset_ = (char *) this - (char *) base_addr;
}
diff --git a/ace/Based_Pointer_T.h b/ace/Based_Pointer_T.h
index 1f0c1838582..31f4547776b 100644
--- a/ace/Based_Pointer_T.h
+++ b/ace/Based_Pointer_T.h
@@ -15,7 +15,7 @@
//
// ============================================================================
-#if !defined (ACE_BASED_POINTER_T_H)
+#ifndef ACE_BASED_POINTER_T_H
#define ACE_BASED_POINTER_T_H
#if defined (_MSC_VER)
diff --git a/ace/Malloc.h b/ace/Malloc.h
index c8a896bda2a..bcbc452aecf 100644
--- a/ace/Malloc.h
+++ b/ace/Malloc.h
@@ -34,9 +34,9 @@
#define ACE_PROCESS_MUTEX ACE_SV_Semaphore_Simple
#endif /* ACE_HAS_THREADS */
-#if defined (ACE_HAS_BASED_POINTER_MALLOC)
+#if defined (ACE_HAS_POSITION_INDEPENDENT_MALLOC)
#include "ace/Based_Pointer_T.h"
-#endif /* ACE_HAS_BASED_POINTER_MALLOC */
+#endif /* ACE_HAS_POSITION_INDEPENDENT_MALLOC */
typedef ACE_Atomic_Op<ACE_PROCESS_MUTEX, int> ACE_INT;
@@ -91,13 +91,13 @@ union ACE_Export ACE_Malloc_Header
// to keep track of each chunk of data when it's in the free
// list or in use.
public:
-#if defined (ACE_HAS_BASED_POINTER_MALLOC)
- ACE_Based_Pointer<ACE_Malloc_Header> *next_block_;
+#if defined (ACE_HAS_POSITION_INDEPENDENT_MALLOC)
+ ACE_Based_Pointer<ACE_Malloc_Header> next_block_;
// Points to next block if on free list.
#else
ACE_Malloc_Header *next_block_;
// Points to next block if on free list.
-#endif /* ACE_HAS_BASED_POINTER_MALLOC */
+#endif /* ACE_HAS_POSITION_INDEPENDENT_MALLOC */
size_t size_;
// Size of this block.
diff --git a/ace/Malloc_T.h b/ace/Malloc_T.h
index 4c49896d923..b41afe45986 100644
--- a/ace/Malloc_T.h
+++ b/ace/Malloc_T.h
@@ -163,10 +163,10 @@ public:
virtual int find (const char *name, void *&pointer);
// Locate <name> and pass out parameter via pointer. If found,
- // return 0, Returns -1 if failure occurs.
+ // return 0, Returns -1 if <name> isn't found.
virtual int find (const char *name);
- // returns 0 if the name is in the mapping. -1, otherwise.
+ // Returns 0 if the name is in the mapping and -1 if not.
virtual int unbind (const char *name);
// Unbind (remove) the name from the map. Don't return the pointer
diff --git a/ace/Memory_Pool.cpp b/ace/Memory_Pool.cpp
index 4d0ca6d12ce..1d07027de11 100644
--- a/ace/Memory_Pool.cpp
+++ b/ace/Memory_Pool.cpp
@@ -10,9 +10,9 @@
#include "ace/Auto_Ptr.h"
-#if defined (ACE_HAS_BASED_POINTER_MALLOC)
+#if defined (ACE_HAS_POSITION_INDEPENDENT_MALLOC)
#include "ace/Based_Pointer_T.h"
-#endif /* ACE_HAS_BASED_POINTER_MALLOC */
+#endif /* ACE_HAS_POSITION_INDEPENDENT_MALLOC */
ACE_RCSID(ace, Memory_Pool, "$Id$")
@@ -79,10 +79,10 @@ ACE_MMAP_Memory_Pool::release (void)
{
ACE_TRACE ("ACE_MMAP_Memory_Pool::release");
-#if defined (ACE_HAS_BASED_POINTER_MALLOC)
- ACE_BASED_POINTER_REPOSITORY::instance ()->unbind (this->mmap_.addr (),
+#if defined (ACE_HAS_POSITION_INDEPENDENT_MALLOC)
+ ACE_POSITION_INDEPENDENT_REPOSITORY::instance ()->unbind (this->mmap_.addr (),
this->mmap_.size ());
-#endif /* ACE_HAS_BASED_POINTER_MALLOC */
+#endif /* ACE_HAS_POSITION_INDEPENDENT_MALLOC */
this->mmap_.remove ();
return 0;
@@ -264,10 +264,10 @@ ACE_MMAP_Memory_Pool::map_file (off_t map_size)
}
else
{
-#if defined (ACE_HAS_BASED_POINTER_MALLOC)
- ACE_BASED_POINTER_REPOSITORY::instance ()->bind (this->base_addr_,
+#if defined (ACE_HAS_POSITION_INDEPENDENT_MALLOC)
+ ACE_POSITION_INDEPENDENT_REPOSITORY::instance ()->bind (this->base_addr_,
map_size);
-#endif /* ACE_HAS_BASED_POINTER_MALLOC */
+#endif /* ACE_HAS_POSITION_INDEPENDENT_MALLOC */
return 0;
}
}
diff --git a/ace/README b/ace/README
index d431daf2317..44e5118b055 100644
--- a/ace/README
+++ b/ace/README
@@ -379,6 +379,9 @@ ACE_HAS_OSF_TIMOD_H Platform supports the OSF TLI
ACE_HAS_PENTIUM Platform is an Intel Pentium
microprocessor.
ACE_HAS_POLL Platform contains <poll.h>
+ACE_HAS_POSITION_INDEPENDENT_MALLOC The ACE_Malloc<> class uses
+ "position-independent" features
+ provided by ACE_Based_Pointer<>.
ACE_HAS_POSIX_NONBLOCK Platform supports POSIX
O_NONBLOCK semantics
ACE_HAS_POSIX_SEM Platform supports POSIX
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 */