diff options
Diffstat (limited to 'ace/Malloc_T.cpp')
-rw-r--r-- | ace/Malloc_T.cpp | 184 |
1 files changed, 87 insertions, 97 deletions
diff --git a/ace/Malloc_T.cpp b/ace/Malloc_T.cpp index 8d3ba28d10f..946e026b256 100644 --- a/ace/Malloc_T.cpp +++ b/ace/Malloc_T.cpp @@ -101,18 +101,17 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::print_stats (void) const return; this->cb_ptr_->malloc_stats_.dump (); ACE_DEBUG ((LM_DEBUG, - ASYS_TEXT ("(%P|%t) contents of freelist:\n"))); + ASYS_TEXT (" (%P|%t) contents of freelist:\n"))); - for (ACE_Malloc_Header *currp = ACE_POINTER_CAST (this->cb_ptr_->freep_->s_.next_block_); + for (ACE_Malloc_Header::HEADER_PTR currp = this->cb_ptr_->freep_->s_.next_block_; ; - currp = ACE_POINTER_CAST (currp->s_.next_block_)) + currp = currp->s_.next_block_) { ACE_DEBUG ((LM_DEBUG, - ASYS_TEXT ("(%P|%t) ptr = %u, ACE_Malloc_Header units = %d, byte units = %d\n"), - currp, - currp->s_.size_, + ASYS_TEXT (" (%P|%t) ptr = %u, ACE_Malloc_Header units = %d, byte units = %d\n"), + currp, currp->s_.size_, currp->s_.size_ * sizeof (ACE_Malloc_Header))); - if (currp == ACE_POINTER_CAST (this->cb_ptr_->freep_)) + if (currp == this->cb_ptr_->freep_) break; } } @@ -151,12 +150,12 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::open (void) first_time); if (this->cb_ptr_ == 0) ACE_ERROR_RETURN ((LM_ERROR, - ASYS_TEXT ("(%P|%t) %p\n"), + ASYS_TEXT (" (%P|%t) %p\n"), ASYS_TEXT ("init_acquire failed")), -1); else if (first_time) { - // ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("(%P|%t) first time in, control block = %u\n"), this->cb_ptr_)); + // ACE_DEBUG ((LM_DEBUG, ASYS_TEXT (" (%P|%t) first time in, control block = %u\n"), this->cb_ptr_)); #if defined (ACE_HAS_MALLOC_STATS) // Call the constructor on the ACE_LOCK, using the placement @@ -164,30 +163,23 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::open (void) new ((void *) &this->cb_ptr_->malloc_stats_) ACE_Malloc_Stats; #endif /* ACE_HAS_MALLOC_STATS */ -#if defined (ACE_HAS_POSITION_INDEPENDENT_MALLOC) // Initialize the freelist pointer to point to the dummy - // <ACE_Malloc_Header>. - new ((void *) &this->cb_ptr_->freep_) ACE_Malloc_Header::HEADER_PTR (&this->cb_ptr_->base_); - new ((void *) &this->cb_ptr_->name_head_) ACE_Based_Pointer<ACE_Name_Node>; -#else - // Initialize the freelist pointer to point to the dummy - // <ACE_Malloc_Header>. + // ACE_Malloc_Header. this->cb_ptr_->freep_ = &this->cb_ptr_->base_; - // initialize the name list to 0 -#endif /* ACE_HAS_POSITION_INDEPENDENT_MALLOC */ - this->cb_ptr_->name_head_ = (ACE_Name_Node *) 0; - - // Initialize the dummy <ACE_Malloc_Header> to point to itself. + // Initialize the dummy ACE_Malloc_Header to point to itself. this->cb_ptr_->freep_->s_.size_ = 0; - this->cb_ptr_->freep_->s_.next_block_ = ACE_POINTER_CAST (this->cb_ptr_->freep_); + this->cb_ptr_->freep_->s_.next_block_ = this->cb_ptr_->freep_; + + // initialize the name list to 0 + this->cb_ptr_->name_head_ = 0; if (rounded_bytes > (sizeof *this->cb_ptr_ + sizeof (ACE_Malloc_Header))) { // If we've got any extra space at the end of the control // block, then skip past the dummy ACE_Malloc_Header to // point at the first free block. - ACE_Malloc_Header *p = ACE_POINTER_CAST (this->cb_ptr_->freep_) + 1; + ACE_Malloc_Header::HEADER_PTR p (this->cb_ptr_->freep_ + 1); // Why aC++ in 64-bit mode can't grok this, I have no // idea... but it ends up with an extra bit set which makes @@ -205,10 +197,12 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::open (void) AMS (++this->cb_ptr_->malloc_stats_.nblocks_); AMS (++this->cb_ptr_->malloc_stats_.ninuse_); + // Skip over the ACE_Malloc_Header when returning pointer. + ACE_Malloc_Header::HEADER_PTR tmp (p + 1); // Insert the newly allocated chunk of memory into the free - // list. Skip over the ACE_Malloc_Header when returning - // pointer. - this->shared_free (p + 1); + // list. Note that this triggers operator void *() if we're + // using the ACE_POSITION_INDEPENDENT_MALLOC configuration. + this->shared_free ((void *) tmp); } } return 0; @@ -223,7 +217,7 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::ACE_Malloc (LPCTSTR pool_name) ACE_TRACE ("ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::ACE_Malloc"); if (this->open () == -1) ACE_ERROR ((LM_ERROR, - ASYS_TEXT ("%p\n"), + ASYS_TEXT ("%p"), ASYS_TEXT ("ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::ACE_Malloc"))); } @@ -271,7 +265,7 @@ template <ACE_MEM_POOL_1, class ACE_LOCK> int ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::remove (void) { ACE_TRACE ("ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::remove"); - // ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("(%P|%t) destroying ACE_Malloc\n"))); + // ACE_DEBUG ((LM_DEBUG, ASYS_TEXT (" (%P|%t) destroying ACE_Malloc\n"))); int result = 0; #if defined (ACE_HAS_MALLOC_STATS) @@ -304,20 +298,20 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_malloc (size_t nbytes) // Begin the search starting at the place in the freelist where the // last block was found. - ACE_Malloc_Header *prevp = ACE_POINTER_CAST (this->cb_ptr_->freep_); - ACE_Malloc_Header *currp = ACE_POINTER_CAST (prevp->s_.next_block_); + ACE_Malloc_Header::HEADER_PTR prevp = this->cb_ptr_->freep_; + ACE_Malloc_Header::HEADER_PTR currp = prevp->s_.next_block_; // Search the freelist to locate a block of the appropriate size. for (int i = 0; - ; i++, prevp = currp, currp = ACE_POINTER_CAST (currp->s_.next_block_)) + ; i++, prevp = currp, currp = currp->s_.next_block_) { if (currp->s_.size_ >= nunits) // Big enough { AMS (++this->cb_ptr_->malloc_stats_.ninuse_); if (currp->s_.size_ == nunits) // Exact size, just update the pointers. - prevp->s_.next_block_ = ACE_POINTER_CAST (currp->s_.next_block_); + prevp->s_.next_block_ = currp->s_.next_block_; else { // Remaining chunk is larger than requested block, so @@ -330,9 +324,13 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_malloc (size_t nbytes) this->cb_ptr_->freep_ = prevp; // Skip over the ACE_Malloc_Header when returning pointer. - return currp + 1; + ACE_Malloc_Header::HEADER_PTR tmp (currp + 1); + // Insert the newly allocated chunk of memory into the free + // list. Note that this triggers operator void *() if we're + // using the ACE_POSITION_INDEPENDENT_MALLOC configuration. + return (void *) tmp; } - else if (currp == ACE_POINTER_CAST (this->cb_ptr_->freep_)) + else if (currp == this->cb_ptr_->freep_) { // We've wrapped around freelist without finding a block. // Therefore, we need to ask the memory pool for a new chunk @@ -340,11 +338,9 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_malloc (size_t nbytes) size_t chunk_bytes = 0; - currp = (ACE_Malloc_Header *) - this->memory_pool_.acquire (nunits * sizeof (ACE_Malloc_Header), - chunk_bytes); - - if (currp != 0) + if ((currp = (ACE_Malloc_Header *) + this->memory_pool_.acquire (nunits * sizeof (ACE_Malloc_Header), + chunk_bytes)) != 0) { AMS (++this->cb_ptr_->malloc_stats_.nblocks_); AMS (++this->cb_ptr_->malloc_stats_.nchunks_); @@ -353,15 +349,17 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_malloc (size_t nbytes) // Compute the chunk size in ACE_Malloc_Header units. currp->s_.size_ = chunk_bytes / sizeof (ACE_Malloc_Header); - // Insert the newly allocated chunk of memory into the - // free list. Skip over the ACE_Malloc_Header when - // returning pointer. - this->shared_free (currp + 1); - currp = ACE_POINTER_CAST (this->cb_ptr_->freep_); + // Skip over the ACE_Malloc_Header when returning pointer. + ACE_Malloc_Header::HEADER_PTR tmp (currp + 1); + // Insert the newly allocated chunk of memory into the free + // list. Note that this triggers operator void *() if we're + // using the ACE_POSITION_INDEPENDENT_MALLOC configuration. + this->shared_free ((void *) tmp); + currp = this->cb_ptr_->freep_; } else ACE_ERROR_RETURN ((LM_ERROR, - ASYS_TEXT ("(%P|%t) %p\n"), + ASYS_TEXT (" (%P|%t) %p\n"), ASYS_TEXT ("malloc")), 0); } @@ -405,38 +403,37 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_free (void *ap) return; // Adjust AP to point to the block ACE_Malloc_Header - ACE_Malloc_Header *blockp = (ACE_Malloc_Header *) ap - 1; - ACE_Malloc_Header *currp = ACE_POINTER_CAST (this->cb_ptr_->freep_); + ACE_Malloc_Header::HEADER_PTR blockp ((ACE_Malloc_Header *) ap - 1); + ACE_Malloc_Header::HEADER_PTR currp = this->cb_ptr_->freep_; // Search until we find the location where the blocks belongs. Note // that addresses are kept in sorted order. for (; - blockp <= currp || blockp >= ACE_POINTER_CAST (currp->s_.next_block_); - currp = ACE_POINTER_CAST (currp->s_.next_block_)) + blockp <= currp || blockp >= currp->s_.next_block_; + currp = currp->s_.next_block_) { - if (currp >= ACE_POINTER_CAST (currp->s_.next_block_) - && (blockp > currp || blockp < ACE_POINTER_CAST (currp->s_.next_block_))) - // Freed block at the start or the end of the memory pool. + if (currp >= currp->s_.next_block_ + && (blockp > currp || blockp < currp->s_.next_block_)) + // Freed block at the start or the end of the memory pool break; } - // Join to upper neighbor. - if (blockp + blockp->s_.size_ == ACE_POINTER_CAST (currp->s_.next_block_)) + // Join to upper neighbor + if (blockp + blockp->s_.size_ == currp->s_.next_block_) { AMS (--this->cb_ptr_->malloc_stats_.nblocks_); blockp->s_.size_ += currp->s_.next_block_->s_.size_; - blockp->s_.next_block_ = ACE_POINTER_CAST (currp->s_.next_block_->s_.next_block_); + blockp->s_.next_block_ = currp->s_.next_block_->s_.next_block_; } else - blockp->s_.next_block_ = ACE_POINTER_CAST (currp->s_.next_block_); + blockp->s_.next_block_ = currp->s_.next_block_; - // Join to lower neighbor. - if (currp + currp->s_.size_ == blockp) + if (currp + currp->s_.size_ == blockp) // Join to lower neighbor { AMS (--this->cb_ptr_->malloc_stats_.nblocks_); currp->s_.size_ += blockp->s_.size_; - currp->s_.next_block_ = ACE_POINTER_CAST (blockp->s_.next_block_); + currp->s_.next_block_ = blockp->s_.next_block_; } else currp->s_.next_block_ = blockp; @@ -455,10 +452,10 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_find (const char *name) if (this->cb_ptr_ == 0) return 0; - for (ACE_Name_Node *node = ACE_POINTER_CAST (this->cb_ptr_->name_head_); + for (ACE_Name_Node *node = this->cb_ptr_->name_head_; node != 0; - node = ACE_POINTER_CAST (node->next_)) - if (ACE_OS::strcmp (node->name (), + node = node->next_) + if (ACE_OS::strcmp (node->name_, name) == 0) return node; @@ -473,31 +470,28 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_bind (const char *name, return -1; // Combine the two allocations into one to avoid overhead... - ACE_Name_Node *new_node = 0; + ACE_Name_Node *new_node; ACE_ALLOCATOR_RETURN (new_node, (ACE_Name_Node *) this->shared_malloc (sizeof (ACE_Name_Node) + ACE_OS::strlen (name) + 1), -1); - // This is a clever trick that skips over the <ACE_Name_Node> chunk - // and points to where the string name goes. + // This is a clever trick ;-) new_node->name_ = (char *) (new_node + 1); // Insert new node at the head of the list. Note that (new_node) is // *not* a cast, it's operator placement new. ACE_NEW_RETURN (this->cb_ptr_->name_head_, - (new_node) ACE_Name_Node (name, - ACE_reinterpret_cast (char *, - pointer), - ACE_POINTER_CAST (this->cb_ptr_->name_head_)), + (new_node) ACE_Name_Node (name, pointer, + this->cb_ptr_->name_head_), -1); return 0; } template <ACE_MEM_POOL_1, class ACE_LOCK> int ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::trybind (const char *name, - void *&pointer) + void *&pointer) { ACE_TRACE ("ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::trybind"); ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1); @@ -509,16 +503,15 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::trybind (const char *name, else { // Found it, so return a copy of the current entry. - pointer = ACE_reinterpret_cast (void *, - ACE_POINTER_CAST (node->pointer_)); + pointer = node->pointer_; return 1; } } template <ACE_MEM_POOL_1, class ACE_LOCK> int ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::bind (const char *name, - void *pointer, - int duplicates) + void *pointer, + int duplicates) { ACE_TRACE ("ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::bind"); ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1); @@ -535,8 +528,7 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::bind (const char *name, } template <ACE_MEM_POOL_1, class ACE_LOCK> int -ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::find (const char *name, - void *&pointer) +ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::find (const char *name, void *&pointer) { ACE_TRACE ("ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::find"); @@ -548,8 +540,7 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::find (const char *name, return -1; else { - pointer = ACE_reinterpret_cast (void *, - ACE_POINTER_CAST (node->pointer_)); + pointer = node->pointer_; return 0; } } @@ -576,10 +567,10 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::avail_chunks (size_t size) const // Avoid dividing by 0... size = size == 0 ? 1 : size; - for (ACE_Malloc_Header *currp = ACE_POINTER_CAST (this->cb_ptr_->freep_->s_.next_block_); - currp != ACE_POINTER_CAST (this->cb_ptr_->freep_); - currp = ACE_POINTER_CAST (currp->s_.next_block_)) - // Calculate how many will fit in this block. + for (ACE_Malloc_Header::HEADER_PTR currp = this->cb_ptr_->freep_->s_.next_block_; + currp != this->cb_ptr_->freep_; + currp = currp->s_.next_block_) + // calculate how many will fit in this block. if (currp->s_.size_ * sizeof (ACE_Malloc_Header) >= size) count += currp->s_.size_ * sizeof (ACE_Malloc_Header) / size; @@ -606,18 +597,18 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::unbind (const char *name, void *&pointer) ACE_Name_Node *prev = 0; - for (ACE_Name_Node *curr = ACE_POINTER_CAST (this->cb_ptr_->name_head_); + for (ACE_Name_Node *curr = this->cb_ptr_->name_head_; curr != 0; - curr = ACE_POINTER_CAST (curr->next_)) + curr = curr->next_) { - if (ACE_OS::strcmp (curr->name (), name) == 0) + if (ACE_OS::strcmp (curr->name_, name) == 0) { - pointer = ACE_POINTER_CAST (curr->pointer_); + pointer = curr->pointer_; if (prev == 0) - this->cb_ptr_->name_head_ = ACE_POINTER_CAST (curr->next_); + this->cb_ptr_->name_head_ = curr->next_; else - prev->next_ = ACE_POINTER_CAST (curr->next_); + prev->next_ = curr->next_; // This will free up both the node and the name due to our // clever trick in <bind>! @@ -663,10 +654,9 @@ ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::ACE_Malloc_Iterator (ACE_Malloc<A { ACE_TRACE ("ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::ACE_Malloc_Iterator"); // Cheap trick to make code simple. - // @@ Doug, this looks like trouble... ACE_Name_Node temp; this->curr_ = &temp; - this->curr_->next_ = ACE_POINTER_CAST (malloc_.cb_ptr_->name_head_); + this->curr_->next_ = malloc_.cb_ptr_->name_head_; this->advance (); } @@ -679,14 +669,14 @@ ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::~ACE_Malloc_Iterator (void) template <ACE_MEM_POOL_1, class ACE_LOCK> int ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::next (void *&next_entry, - const char *&name) + char *&name) { ACE_TRACE ("ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::next"); if (this->curr_ != 0) { - next_entry = ACE_POINTER_CAST (this->curr_->pointer_); - name = this->curr_->name (); + next_entry = this->curr_->pointer_; + name = this->curr_->name_; return 1; } else @@ -700,7 +690,7 @@ ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::next (void *&next_entry) if (this->curr_ != 0) { - next_entry = ACE_POINTER_CAST (this->curr_->pointer_); + next_entry = this->curr_->pointer_; return 1; } else @@ -720,14 +710,14 @@ ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::advance (void) { ACE_TRACE ("ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::advance"); - this->curr_ = ACE_POINTER_CAST (this->curr_->next_); + this->curr_ = this->curr_->next_; if (this->name_ == 0) return this->curr_ != 0; while (this->curr_ != 0 - && ACE_OS::strcmp (this->name_, this->curr_->name ()) != 0) - this->curr_ = ACE_POINTER_CAST (this->curr_->next_); + && ACE_OS::strcmp (this->name_, this->curr_->name_) != 0) + this->curr_ = this->curr_->next_; return this->curr_ != 0; } |