From 5f0af25f510aff38b563be82feffaa6fe754a434 Mon Sep 17 00:00:00 2001 From: nanbor Date: Wed, 25 Apr 2001 08:38:19 +0000 Subject: ChangeLogTag:Wed Apr 25 03:15:34 2001 Nanbor Wang --- ChangeLog | 40 ++++++++++++++++++++++++++++++ ChangeLogs/ChangeLog-02a | 40 ++++++++++++++++++++++++++++++ ChangeLogs/ChangeLog-03a | 40 ++++++++++++++++++++++++++++++ ace/MEM_IO.cpp | 20 ++------------- ace/MEM_IO.h | 12 +-------- ace/MEM_SAP.cpp | 18 +++++++++++--- ace/MEM_SAP.h | 8 +++--- ace/MEM_Stream.cpp | 3 +-- ace/MEM_Stream.h | 6 ----- ace/MEM_Stream.i | 8 ------ ace/Malloc.h | 17 +++++++------ ace/Malloc_T.cpp | 11 +++++++++ ace/Malloc_T.h | 6 +++++ ace/Malloc_T.i | 20 +++++++++++++++ ace/PI_Malloc.h | 62 +++-------------------------------------------- tests/MEM_Stream_Test.cpp | 1 - tests/Malloc_Test.cpp | 1 + 17 files changed, 194 insertions(+), 119 deletions(-) diff --git a/ChangeLog b/ChangeLog index aec976fde6b..6b659f3127e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,43 @@ +Wed Apr 25 03:15:34 2001 Nanbor Wang + + * ace/Malloc.h: + * ace/PI_Malloc.h: Added new variables to reference counting the + Malloc. + + * ace/Malloc_T.cpp: + * ace/Malloc_T.h: + * ace/Malloc_T.i: Added 2 new methods to query the reference + counter and to release the reference counter. The reference + counter is created with 1 when the malloc first use a new + backing store. Later malloc that attaches to the same backing + store will increase the reference counter. It's the user's + responsibility to invoke release when the malloc is no longer + needed. The reference counter provides a way to indication it's + safe to clean up the backing store without upsetting other + malloc that might still need it. Note, however, the malloc will + not cleanup the backing store automatically when the reference + counter drops to 0 because we may want to attach another malloc + to it in some later point. + + * ace/MEM_IO.cpp: + * ace/MEM_IO.h: + * ace/MEM_SAP.cpp: + * ace/MEM_SAP.h: Use the aforementioned reference counter to + cleanup the backing store automagically. + + * ace/MEM_Stream.h: + * ace/MEM_Stream.i: Removed cleanup method as it is no longer + needed with the previous changes. Removed unnecesary + argument from fini () method. + + * ace/MEM_Stream.cpp: Simply calling fini () without worrying + about cleaning up the backing store. + + * tests/MEM_Stream_Test.cpp: No longer need to hint for cleanup. + + * tests/Malloc_Test.cpp: Added ACE_ASSERT to check for proper + operation of reference counter. + Tue Apr 24 17:54:00 2001 Carlos O'Ryan * ace/Makefile: diff --git a/ChangeLogs/ChangeLog-02a b/ChangeLogs/ChangeLog-02a index aec976fde6b..6b659f3127e 100644 --- a/ChangeLogs/ChangeLog-02a +++ b/ChangeLogs/ChangeLog-02a @@ -1,3 +1,43 @@ +Wed Apr 25 03:15:34 2001 Nanbor Wang + + * ace/Malloc.h: + * ace/PI_Malloc.h: Added new variables to reference counting the + Malloc. + + * ace/Malloc_T.cpp: + * ace/Malloc_T.h: + * ace/Malloc_T.i: Added 2 new methods to query the reference + counter and to release the reference counter. The reference + counter is created with 1 when the malloc first use a new + backing store. Later malloc that attaches to the same backing + store will increase the reference counter. It's the user's + responsibility to invoke release when the malloc is no longer + needed. The reference counter provides a way to indication it's + safe to clean up the backing store without upsetting other + malloc that might still need it. Note, however, the malloc will + not cleanup the backing store automatically when the reference + counter drops to 0 because we may want to attach another malloc + to it in some later point. + + * ace/MEM_IO.cpp: + * ace/MEM_IO.h: + * ace/MEM_SAP.cpp: + * ace/MEM_SAP.h: Use the aforementioned reference counter to + cleanup the backing store automagically. + + * ace/MEM_Stream.h: + * ace/MEM_Stream.i: Removed cleanup method as it is no longer + needed with the previous changes. Removed unnecesary + argument from fini () method. + + * ace/MEM_Stream.cpp: Simply calling fini () without worrying + about cleaning up the backing store. + + * tests/MEM_Stream_Test.cpp: No longer need to hint for cleanup. + + * tests/Malloc_Test.cpp: Added ACE_ASSERT to check for proper + operation of reference counter. + Tue Apr 24 17:54:00 2001 Carlos O'Ryan * ace/Makefile: diff --git a/ChangeLogs/ChangeLog-03a b/ChangeLogs/ChangeLog-03a index aec976fde6b..6b659f3127e 100644 --- a/ChangeLogs/ChangeLog-03a +++ b/ChangeLogs/ChangeLog-03a @@ -1,3 +1,43 @@ +Wed Apr 25 03:15:34 2001 Nanbor Wang + + * ace/Malloc.h: + * ace/PI_Malloc.h: Added new variables to reference counting the + Malloc. + + * ace/Malloc_T.cpp: + * ace/Malloc_T.h: + * ace/Malloc_T.i: Added 2 new methods to query the reference + counter and to release the reference counter. The reference + counter is created with 1 when the malloc first use a new + backing store. Later malloc that attaches to the same backing + store will increase the reference counter. It's the user's + responsibility to invoke release when the malloc is no longer + needed. The reference counter provides a way to indication it's + safe to clean up the backing store without upsetting other + malloc that might still need it. Note, however, the malloc will + not cleanup the backing store automatically when the reference + counter drops to 0 because we may want to attach another malloc + to it in some later point. + + * ace/MEM_IO.cpp: + * ace/MEM_IO.h: + * ace/MEM_SAP.cpp: + * ace/MEM_SAP.h: Use the aforementioned reference counter to + cleanup the backing store automagically. + + * ace/MEM_Stream.h: + * ace/MEM_Stream.i: Removed cleanup method as it is no longer + needed with the previous changes. Removed unnecesary + argument from fini () method. + + * ace/MEM_Stream.cpp: Simply calling fini () without worrying + about cleaning up the backing store. + + * tests/MEM_Stream_Test.cpp: No longer need to hint for cleanup. + + * tests/Malloc_Test.cpp: Added ACE_ASSERT to check for proper + operation of reference counter. + Tue Apr 24 17:54:00 2001 Carlos O'Ryan * ace/Makefile: diff --git a/ace/MEM_IO.cpp b/ace/MEM_IO.cpp index 2e088b53f45..637decbda34 100644 --- a/ace/MEM_IO.cpp +++ b/ace/MEM_IO.cpp @@ -29,14 +29,6 @@ ACE_Reactive_MEM_IO::init (ACE_HANDLE handle, options); } -int -ACE_Reactive_MEM_IO::fini (int remove) -{ - ACE_TRACE ("ACE_Reactive_MEM_IO::fini"); - - return this->close_shm_malloc (remove); -} - int ACE_Reactive_MEM_IO::recv_buf (ACE_MEM_SAP_Node *&buf, int flags, @@ -250,14 +242,6 @@ ACE_MT_MEM_IO::init (ACE_HANDLE handle, return 0; } -int -ACE_MT_MEM_IO::fini (int remove) -{ - ACE_TRACE ("ACE_MT_MEM_IO::fini"); - - return this->close_shm_malloc (remove); -} - int ACE_MT_MEM_IO::recv_buf (ACE_MEM_SAP_Node *&buf, int flags, @@ -357,10 +341,10 @@ ACE_MEM_IO::init (const ACE_TCHAR *name, } int -ACE_MEM_IO::fini (int remove) +ACE_MEM_IO::fini () { if (this->deliver_strategy_ != 0) - return this->deliver_strategy_->fini (remove); + return this->deliver_strategy_->fini (); else return -1; } diff --git a/ace/MEM_IO.h b/ace/MEM_IO.h index 2ee2d9661d6..1f66d88a60b 100644 --- a/ace/MEM_IO.h +++ b/ace/MEM_IO.h @@ -42,11 +42,6 @@ public: const ACE_TCHAR *name, MALLOC_OPTIONS *options); - /** - * Finalizing the MEM_SAP object. This method doesn't invoke - * the method. - */ - virtual int fini (int remove); /** * Fetch location of next available data into . * As this operation read the address of the data off the socket @@ -115,11 +110,6 @@ public: const ACE_TCHAR *name, MALLOC_OPTIONS *options); - /** - * Finalizing the MEM_SAP object. This method doesn't invoke - * the method. - */ - virtual int fini (int remove); /** * Fetch location of next available data into . * As this operation read the address of the data off the socket @@ -195,7 +185,7 @@ public: * Finalizing the MEM_IO object. This method doesn't invoke * the method. */ - int fini (int remove = 0); + int fini (); /// Send an byte buffer to the other process using shm_malloc_ /// connected thru the socket. diff --git a/ace/MEM_SAP.cpp b/ace/MEM_SAP.cpp index ded84c604e6..3031578e046 100644 --- a/ace/MEM_SAP.cpp +++ b/ace/MEM_SAP.cpp @@ -32,6 +32,14 @@ ACE_MEM_SAP::ACE_MEM_SAP (void) // ACE_TRACE ("ACE_MEM_SAP::ACE_MEM_SAP"); } +int +ACE_MEM_SAP::fini () +{ + ACE_TRACE ("ACE_MEM_SAP::fini"); + + return this->close_shm_malloc (); +} + int ACE_MEM_SAP::create_shm_malloc (const ACE_TCHAR *name, MALLOC_OPTIONS *options) @@ -51,14 +59,16 @@ ACE_MEM_SAP::create_shm_malloc (const ACE_TCHAR *name, } int -ACE_MEM_SAP::close_shm_malloc (const int remove) +ACE_MEM_SAP::close_shm_malloc (void) { ACE_TRACE ("ACE_MEM_SAP::close_shm_malloc"); - if (this->shm_malloc_ != 0 && remove != 0) - return this->shm_malloc_->remove (); + int retv = -1; + + if (this->shm_malloc_ != 0 && this->shm_malloc_->release () == 0) + retv = this->shm_malloc_->remove (); - return -1; + return retv; } #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION) diff --git a/ace/MEM_SAP.h b/ace/MEM_SAP.h index 8f62dbb9580..f3e7f4e91f5 100644 --- a/ace/MEM_SAP.h +++ b/ace/MEM_SAP.h @@ -93,7 +93,7 @@ public: * Finalizing the MEM_SAP object. This method doesn't invoke * the method. */ - virtual int fini (int remove) = 0; + virtual int fini (); /** * Fetch location of next available data into . @@ -138,9 +138,9 @@ protected: int create_shm_malloc (const ACE_TCHAR *name, MALLOC_OPTIONS *options); - /// Close down the share memory pool. If != 0, then the - /// mmap file will also get removed. - int close_shm_malloc (const int remove = 0); + /// Close down the share memory pool. Clean up the + /// mmap file if we are the last one using it. + int close_shm_malloc (void); ACE_HANDLE handle_; diff --git a/ace/MEM_Stream.cpp b/ace/MEM_Stream.cpp index 4afbfacea03..79b37e21838 100644 --- a/ace/MEM_Stream.cpp +++ b/ace/MEM_Stream.cpp @@ -23,8 +23,7 @@ ACE_MEM_Stream::close (void) { this->send ((char *)0, 0); - if (this->cleanup_ != 0) - this->fini (1); + this->fini (); #if defined (ACE_WIN32) // We need the following call to make things work correctly on diff --git a/ace/MEM_Stream.h b/ace/MEM_Stream.h index 7455cd77285..24cfbce2733 100644 --- a/ace/MEM_Stream.h +++ b/ace/MEM_Stream.h @@ -53,9 +53,6 @@ public: /// Destructor. ~ACE_MEM_Stream (void); - /// Set whether we shuold cleanup after ourselves. - void cleanup (int set); - //= The following two methods use write and read system calls. /// Send n bytes, keep trying until n are sent. /// Recv n bytes, keep trying until n are received. @@ -133,9 +130,6 @@ public: /// Declare the dynamic allocation hooks. ACE_ALLOC_HOOK_DECLARE; -private: - /// Should we clean up after ourselves. - int cleanup_; }; #if !defined (ACE_LACKS_INLINE_FUNCTIONS) diff --git a/ace/MEM_Stream.i b/ace/MEM_Stream.i index f47f6a527c4..07f68fafdd0 100644 --- a/ace/MEM_Stream.i +++ b/ace/MEM_Stream.i @@ -7,14 +7,12 @@ ASYS_INLINE ACE_MEM_Stream::ACE_MEM_Stream (void) - : cleanup_ (0) { // ACE_TRACE ("ACE_MEM_Stream::ACE_MEM_Stream"); } ASYS_INLINE ACE_MEM_Stream::ACE_MEM_Stream (ACE_HANDLE h) - : cleanup_ (0) { // ACE_TRACE ("ACE_MEM_Stream::ACE_MEM_Stream"); this->set_handle (h); @@ -26,12 +24,6 @@ ACE_MEM_Stream::~ACE_MEM_Stream (void) // ACE_TRACE ("ACE_MEM_Stream::~ACE_MEM_Stream"); } -ASYS_INLINE void -ACE_MEM_Stream::cleanup (int set) -{ - this->cleanup_ = set; -} - ASYS_INLINE int ACE_MEM_Stream::close_reader (void) { diff --git a/ace/Malloc.h b/ace/Malloc.h index f36c60661e0..3c4b6cc745e 100644 --- a/ace/Malloc.h +++ b/ace/Malloc.h @@ -350,20 +350,23 @@ public: void dump (void) const; }; + /// Print out a bunch of size info for debugging. static void print_alignment_info (void); - // Print out a bunch of size info for debugging. + /// Reference counter. + int ref_counter_; + + /// Head of the linked list of Name Nodes. ACE_Name_Node *name_head_; - // Head of the linked list of Name Nodes. + /// Current head of the freelist. ACE_Malloc_Header *freep_; - // Current head of the freelist. + /// Name of lock thats ensures mutual exclusion. char lock_name_[MAXNAMELEN]; - // Name of lock thats ensures mutual exclusion. #if defined (ACE_HAS_MALLOC_STATS) - // Keep statistics about ACE_Malloc state and performance. + /// Keep statistics about ACE_Malloc state and performance. ACE_Malloc_Stats malloc_stats_; #define ACE_CONTROL_BLOCK_SIZE ((int)(sizeof (ACE_Name_Node*) \ + sizeof (ACE_Malloc_Header*) \ @@ -390,11 +393,11 @@ public: // Force alignment. #endif /* ACE_CONTROL_BLOCK_ALIGN_LONGS && ACE_CONTROL_BLOCK_ALIGN_LONGS == 0 */ + /// Dummy node used to anchor the freelist. This needs to come last... ACE_Malloc_Header base_; - // Dummy node used to anchor the freelist. This needs to come last... + /// Dump the state of the object. void dump (void) const; - // Dump the state of the object. }; #if defined (__ACE_INLINE__) diff --git a/ace/Malloc_T.cpp b/ace/Malloc_T.cpp index a8ed0dc2679..084331b08b9 100644 --- a/ace/Malloc_T.cpp +++ b/ace/Malloc_T.cpp @@ -194,6 +194,7 @@ ACE_Malloc_T::open (void) this->cb_ptr_); this->cb_ptr_->freep_->size_ = 0; + this->cb_ptr_->ref_counter_ = 1; if (rounded_bytes > (sizeof *this->cb_ptr_ + sizeof (MALLOC_HEADER))) { @@ -228,6 +229,8 @@ ACE_Malloc_T::open (void) this->shared_free (p + 1); } } + else + ++this->cb_ptr_->ref_counter_; return 0; } @@ -302,6 +305,14 @@ ACE_Malloc_T::remove (void) // Give the memory pool a chance to release its resources. result = this->memory_pool_.release (); + // Reset this->cb_ptr_ as it is no longer valid. + // There's also no need to keep the reference counter as the + // underlying memory pool has been destroyed. + // Also notice that we are leaving the decision of removing + // the pool to users so they can map to the same mmap file + // again. + this->cb_ptr_ = 0; + return result; } diff --git a/ace/Malloc_T.h b/ace/Malloc_T.h index f1d398e7b2c..bc8c42cd66f 100644 --- a/ace/Malloc_T.h +++ b/ace/Malloc_T.h @@ -364,6 +364,12 @@ public: /// Destructor ~ACE_Malloc_T (void); + /// Get Reference counter. + int ref_counter (void); + + /// Release ref counter. + int release (void); + /// Releases resources allocated by . int remove (void); diff --git a/ace/Malloc_T.i b/ace/Malloc_T.i index dbe502de44b..31c5b403cdd 100644 --- a/ace/Malloc_T.i +++ b/ace/Malloc_T.i @@ -170,6 +170,26 @@ ACE_Allocator_Adapter::sync (void *addr, size_t len, int flags) return this->allocator_.sync (addr, len, flags); } +template ACE_INLINE int +ACE_Malloc_T::ref_counter (void) +{ + ACE_GUARD_RETURN (ACE_LOCK, ace_mon, (ACE_LOCK &) this->lock_, -1); + if (this->cb_ptr_ != 0) + return this->cb_ptr_->ref_counter_; + + return -1; +} + +template ACE_INLINE int +ACE_Malloc_T::release (void) +{ + ACE_GUARD_RETURN (ACE_LOCK, ace_mon, (ACE_LOCK &) this->lock_, -1); + if (this->cb_ptr_ != 0) + return --this->cb_ptr_->ref_counter_; + else + return -1; +} + template ACE_INLINE ACE_MEM_POOL & ACE_Malloc_T::memory_pool (void) { diff --git a/ace/PI_Malloc.h b/ace/PI_Malloc.h index 60981cd84e3..ef7d39f160f 100644 --- a/ace/PI_Malloc.h +++ b/ace/PI_Malloc.h @@ -22,63 +22,6 @@ #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) #include "ace/Based_Pointer_T.h" -#endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */ - -#if defined (ACE_HAS_MALLOC_STATS) -#include "ace/Synch_T.h" -#if defined (ACE_HAS_THREADS) -#define ACE_PROCESS_MUTEX ACE_Process_Mutex -#else -#include "ace/SV_Semaphore_Simple.h" -#define ACE_PROCESS_MUTEX ACE_SV_Semaphore_Simple -#endif /* ACE_HAS_THREADS */ - -typedef ACE_Atomic_Op ACE_INT; - -/// This keeps stats on the usage of the memory manager. -struct ACE_Export ACE_Malloc_Stats -{ - ACE_Malloc_Stats (void); - void dump (void) const; - - /// Coarse-grained unit of allocation. - ACE_INT nchunks_; - - /// Fine-grained unit of allocation. - ACE_INT nblocks_; - - /// Number of blocks in use - ACE_INT ninuse_; -}; -#define ACE_MALLOC_STATS(X) X -#else -#define ACE_MALLOC_STATS(X) -#endif /* ACE_HAS_MALLOC_STATS */ - -#if !defined (ACE_MALLOC_PADDING) -// ACE_MALLOC_PADDING allows you to insure that allocated regions are -// at least bytes long. It is especially useful -// when you want areas to be at least a page long, or 32K long, or -// something like that. It doesn't guarantee alignment to an address -// multiple, like 8-byte data alignment, etc. The allocated area's -// padding to your selected size is done with an added array of long[] -// and your compiler will decide how to align things in memory. - -#define ACE_MALLOC_PADDING 1 -#endif /* ACE_MALLOC_PADDING */ - -#if !defined (ACE_MALLOC_ALIGN) -// Align the malloc header size to a multiple of a double. -#define ACE_MALLOC_ALIGN (sizeof (double)) -#endif /* ACE_MALLOC_ALIGN */ - -// ACE_MALLOC_HEADER_SIZE is the normalized malloc header size. -#define ACE_MALLOC_HEADER_SIZE (ACE_MALLOC_PADDING % ACE_MALLOC_ALIGN == 0 \ - ? ACE_MALLOC_PADDING \ - : (((ACE_MALLOC_PADDING / ACE_MALLOC_ALIGN) + 1) \ - * ACE_MALLOC_ALIGN)) - -#if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) // prepare for position independent malloc /** @@ -205,6 +148,9 @@ public: /// Print out a bunch of size info for debugging. static void print_alignment_info (void); + /// Reference counter. + int ref_counter_; + /// Head of the linked list of Name Nodes. NAME_NODE_PTR name_head_; @@ -215,7 +161,7 @@ public: char lock_name_[MAXNAMELEN]; #if defined (ACE_HAS_MALLOC_STATS) - // Keep statistics about ACE_Malloc state and performance. + /// Keep statistics about ACE_Malloc state and performance. ACE_Malloc_Stats malloc_stats_; #define ACE_PI_CONTROL_BLOCK_SIZE ((int)(sizeof (NAME_NODE_PTR) \ + sizeof (MALLOC_HEADER_PTR) \ diff --git a/tests/MEM_Stream_Test.cpp b/tests/MEM_Stream_Test.cpp index f21d35e02ff..9ba0d146b91 100644 --- a/tests/MEM_Stream_Test.cpp +++ b/tests/MEM_Stream_Test.cpp @@ -141,7 +141,6 @@ Echo_Handler::handle_close (ACE_HANDLE, this->connection_)); // Shutdown - this->peer ().cleanup (1); this->destroy (); return 0; } diff --git a/tests/Malloc_Test.cpp b/tests/Malloc_Test.cpp index 143c8e28cdc..2d1282c6573 100644 --- a/tests/Malloc_Test.cpp +++ b/tests/Malloc_Test.cpp @@ -358,6 +358,7 @@ main (int argc, ACE_TCHAR *[]) // Synchronize on the exit of the child. result = p.wait (); ACE_ASSERT (result != -1); + ACE_ASSERT (myalloc->ref_counter () == 1); myalloc->remove (); ACE_END_TEST; return 0; -- cgit v1.2.1