diff options
Diffstat (limited to 'ace/Malloc_T.cpp')
-rw-r--r-- | ace/Malloc_T.cpp | 182 |
1 files changed, 96 insertions, 86 deletions
diff --git a/ace/Malloc_T.cpp b/ace/Malloc_T.cpp index 946e026b256..939bbd4d594 100644 --- a/ace/Malloc_T.cpp +++ b/ace/Malloc_T.cpp @@ -101,17 +101,18 @@ 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::HEADER_PTR currp = this->cb_ptr_->freep_->s_.next_block_; + for (ACE_Malloc_Header *currp = ACE_POINTER_CAST (this->cb_ptr_->freep_->s_.next_block_); ; - currp = currp->s_.next_block_) + currp = ACE_POINTER_CAST (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 == this->cb_ptr_->freep_) + if (currp == ACE_POINTER_CAST (this->cb_ptr_->freep_)) break; } } @@ -150,12 +151,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 @@ -163,23 +164,30 @@ 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. + // <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>. this->cb_ptr_->freep_ = &this->cb_ptr_->base_; + // initialize the name list to 0 +#endif /* ACE_HAS_POSITION_INDEPENDENT_MALLOC */ - // Initialize the dummy ACE_Malloc_Header to point to itself. - this->cb_ptr_->freep_->s_.size_ = 0; - this->cb_ptr_->freep_->s_.next_block_ = this->cb_ptr_->freep_; + this->cb_ptr_->name_head_ = (ACE_Name_Node *) 0; - // initialize the name list to 0 - this->cb_ptr_->name_head_ = 0; + // 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_); 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::HEADER_PTR p (this->cb_ptr_->freep_ + 1); + ACE_Malloc_Header *p = ACE_POINTER_CAST (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 @@ -197,12 +205,10 @@ 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. Note that this triggers operator void *() if we're - // using the ACE_POSITION_INDEPENDENT_MALLOC configuration. - this->shared_free ((void *) tmp); + // list. Skip over the ACE_Malloc_Header when returning + // pointer. + this->shared_free (p + 1); } } return 0; @@ -265,7 +271,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) @@ -298,20 +304,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::HEADER_PTR prevp = this->cb_ptr_->freep_; - ACE_Malloc_Header::HEADER_PTR currp = prevp->s_.next_block_; + ACE_Malloc_Header *prevp = ACE_POINTER_CAST (this->cb_ptr_->freep_); + ACE_Malloc_Header *currp = ACE_POINTER_CAST (prevp->s_.next_block_); // Search the freelist to locate a block of the appropriate size. for (int i = 0; - ; i++, prevp = currp, currp = currp->s_.next_block_) + ; i++, prevp = currp, currp = ACE_POINTER_CAST (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_ = currp->s_.next_block_; + prevp->s_.next_block_ = ACE_POINTER_CAST (currp->s_.next_block_); else { // Remaining chunk is larger than requested block, so @@ -324,13 +330,9 @@ 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. - 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; + return currp + 1; } - else if (currp == this->cb_ptr_->freep_) + else if (currp == ACE_POINTER_CAST (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 @@ -338,9 +340,11 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::shared_malloc (size_t nbytes) size_t chunk_bytes = 0; - if ((currp = (ACE_Malloc_Header *) - this->memory_pool_.acquire (nunits * sizeof (ACE_Malloc_Header), - chunk_bytes)) != 0) + currp = (ACE_Malloc_Header *) + this->memory_pool_.acquire (nunits * sizeof (ACE_Malloc_Header), + chunk_bytes); + + if (currp != 0) { AMS (++this->cb_ptr_->malloc_stats_.nblocks_); AMS (++this->cb_ptr_->malloc_stats_.nchunks_); @@ -349,17 +353,15 @@ 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); - // 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_; + // 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_); } else ACE_ERROR_RETURN ((LM_ERROR, - ASYS_TEXT (" (%P|%t) %p\n"), + ASYS_TEXT ("(%P|%t) %p\n"), ASYS_TEXT ("malloc")), 0); } @@ -403,37 +405,38 @@ 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::HEADER_PTR blockp ((ACE_Malloc_Header *) ap - 1); - ACE_Malloc_Header::HEADER_PTR currp = this->cb_ptr_->freep_; + ACE_Malloc_Header *blockp = (ACE_Malloc_Header *) ap - 1; + ACE_Malloc_Header *currp = ACE_POINTER_CAST (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 >= currp->s_.next_block_; - currp = currp->s_.next_block_) + blockp <= currp || blockp >= ACE_POINTER_CAST (currp->s_.next_block_); + currp = ACE_POINTER_CAST (currp->s_.next_block_)) { - 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 + 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. break; } - // Join to upper neighbor - if (blockp + blockp->s_.size_ == currp->s_.next_block_) + // Join to upper neighbor. + if (blockp + blockp->s_.size_ == ACE_POINTER_CAST (currp->s_.next_block_)) { AMS (--this->cb_ptr_->malloc_stats_.nblocks_); blockp->s_.size_ += currp->s_.next_block_->s_.size_; - blockp->s_.next_block_ = currp->s_.next_block_->s_.next_block_; + blockp->s_.next_block_ = ACE_POINTER_CAST (currp->s_.next_block_->s_.next_block_); } else - blockp->s_.next_block_ = currp->s_.next_block_; + blockp->s_.next_block_ = ACE_POINTER_CAST (currp->s_.next_block_); - if (currp + currp->s_.size_ == blockp) // Join to lower neighbor + // Join to lower neighbor. + if (currp + currp->s_.size_ == blockp) { AMS (--this->cb_ptr_->malloc_stats_.nblocks_); currp->s_.size_ += blockp->s_.size_; - currp->s_.next_block_ = blockp->s_.next_block_; + currp->s_.next_block_ = ACE_POINTER_CAST (blockp->s_.next_block_); } else currp->s_.next_block_ = blockp; @@ -452,10 +455,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 = this->cb_ptr_->name_head_; + for (ACE_Name_Node *node = ACE_POINTER_CAST (this->cb_ptr_->name_head_); node != 0; - node = node->next_) - if (ACE_OS::strcmp (node->name_, + node = ACE_POINTER_CAST (node->next_)) + if (ACE_OS::strcmp (node->name (), name) == 0) return node; @@ -470,28 +473,31 @@ 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; + ACE_Name_Node *new_node = 0; 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 ;-) + // This is a clever trick that skips over the <ACE_Name_Node> chunk + // and points to where the string name goes. 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, pointer, - this->cb_ptr_->name_head_), + (new_node) ACE_Name_Node (name, + ACE_reinterpret_cast (char *, + pointer), + ACE_POINTER_CAST (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); @@ -503,15 +509,16 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::trybind (const char *name, else { // Found it, so return a copy of the current entry. - pointer = node->pointer_; + pointer = ACE_reinterpret_cast (void *, + ACE_POINTER_CAST (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); @@ -528,7 +535,8 @@ 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"); @@ -540,7 +548,8 @@ ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK>::find (const char *name, void *&pointer) return -1; else { - pointer = node->pointer_; + pointer = ACE_reinterpret_cast (void *, + ACE_POINTER_CAST (node->pointer_)); return 0; } } @@ -567,10 +576,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::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. + 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. if (currp->s_.size_ * sizeof (ACE_Malloc_Header) >= size) count += currp->s_.size_ * sizeof (ACE_Malloc_Header) / size; @@ -597,18 +606,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 = this->cb_ptr_->name_head_; + for (ACE_Name_Node *curr = ACE_POINTER_CAST (this->cb_ptr_->name_head_); curr != 0; - curr = curr->next_) + curr = ACE_POINTER_CAST (curr->next_)) { - if (ACE_OS::strcmp (curr->name_, name) == 0) + if (ACE_OS::strcmp (curr->name (), name) == 0) { - pointer = curr->pointer_; + pointer = ACE_POINTER_CAST (curr->pointer_); if (prev == 0) - this->cb_ptr_->name_head_ = curr->next_; + this->cb_ptr_->name_head_ = ACE_POINTER_CAST (curr->next_); else - prev->next_ = curr->next_; + prev->next_ = ACE_POINTER_CAST (curr->next_); // This will free up both the node and the name due to our // clever trick in <bind>! @@ -654,9 +663,10 @@ 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_ = malloc_.cb_ptr_->name_head_; + this->curr_->next_ = ACE_POINTER_CAST (malloc_.cb_ptr_->name_head_); this->advance (); } @@ -669,14 +679,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, - char *&name) + const char *&name) { ACE_TRACE ("ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::next"); if (this->curr_ != 0) { - next_entry = this->curr_->pointer_; - name = this->curr_->name_; + next_entry = ACE_POINTER_CAST (this->curr_->pointer_); + name = this->curr_->name (); return 1; } else @@ -690,7 +700,7 @@ ACE_Malloc_Iterator<ACE_MEM_POOL_2, ACE_LOCK>::next (void *&next_entry) if (this->curr_ != 0) { - next_entry = this->curr_->pointer_; + next_entry = ACE_POINTER_CAST (this->curr_->pointer_); return 1; } else @@ -710,14 +720,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_ = this->curr_->next_; + this->curr_ = ACE_POINTER_CAST (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_ = this->curr_->next_; + && ACE_OS::strcmp (this->name_, this->curr_->name ()) != 0) + this->curr_ = ACE_POINTER_CAST (this->curr_->next_); return this->curr_ != 0; } |