Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes

ACE_Obstack_T< ACE_CHAR_T > Class Template Reference

Define a simple "mark and release" memory allocation utility. More...

#include <Obstack_T.h>

Collaboration diagram for ACE_Obstack_T< ACE_CHAR_T >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ACE_Obstack_T (size_t size=(4096 *sizeof(ACE_CHAR_T))-sizeof(ACE_Obchunk), ACE_Allocator *allocator_strategy=0)
 ~ACE_Obstack_T (void)
int request (size_t len)
ACE_CHAR_T * grow (ACE_CHAR_T c)
void grow_fast (ACE_CHAR_T c)
ACE_CHAR_T * freeze (void)
ACE_CHAR_T * copy (const ACE_CHAR_T *data, size_t len)
size_t length (void) const
size_t size (void) const
void unwind (void *obj)
void release (void)
void dump (void) const
 Dump the state of an object.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Protected Member Functions

class ACE_Obchunknew_chunk (void)
void unwind_i (void *obj)

Protected Attributes

ACE_Allocatorallocator_strategy_
 Pointer to the allocator used by this Obstack.
size_t size_
 Current size of the Obstack;.
class ACE_Obchunkhead_
 Head of the Obchunk chain.
class ACE_Obchunkcurr_
 Pointer to the current Obchunk.

Detailed Description

template<class ACE_CHAR_T>
class ACE_Obstack_T< ACE_CHAR_T >

Define a simple "mark and release" memory allocation utility.

The implementation is similar to the GNU obstack utility, which is used extensively in the GCC compiler.

Definition at line 38 of file Obstack_T.h.


Constructor & Destructor Documentation

template<class ACE_CHAR_T >
ACE_Obstack_T< ACE_CHAR_T >::ACE_Obstack_T ( size_t  size = (4096 * sizeof (ACE_CHAR_T)) - sizeof (ACE_Obchunk),
ACE_Allocator allocator_strategy = 0 
)

Definition at line 124 of file Obstack_T.cpp.

  : allocator_strategy_ (allocator_strategy),
    size_ (size),
    head_ (0),
    curr_ (0)
{
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::ACE_Obstack");

  if (this->allocator_strategy_ == 0)
    ACE_ALLOCATOR (this->allocator_strategy_,
                   ACE_Allocator::instance ());

  this->head_ = this->new_chunk ();
  this->curr_ = this->head_;
}

template<class ACE_CHAR_T >
ACE_Obstack_T< ACE_CHAR_T >::~ACE_Obstack_T ( void   ) 

Definition at line 142 of file Obstack_T.cpp.

{
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::~ACE_Obstack_T");

  ACE_Obchunk *temp = this->head_;

  while (temp != 0)
    {
      ACE_Obchunk *next = temp->next_;
      temp->next_  = 0;
      this->allocator_strategy_->free (temp);
      temp = next;
    }
}


Member Function Documentation

template<class ACE_CHAR_T>
ACE_CHAR_T * ACE_Obstack_T< ACE_CHAR_T >::copy ( const ACE_CHAR_T *  data,
size_t  len 
)

Copy the data into the current Obchunk and freeze the current block. Return the starting address of the current building block, 0 if error occurs. len specify the string length, not the actually data size.

Definition at line 158 of file Obstack_T.cpp.

{
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::copy");

  if (this->request (len) != 0)
    return 0;

  size_t tsize = len * sizeof (ACE_CHAR_T);
  ACE_OS::memcpy (this->curr_->cur_, s, tsize);
  this->curr_->cur_ += tsize ;
  return this->freeze ();
}

template<class ACE_CHAR_T >
void ACE_Obstack_T< ACE_CHAR_T >::dump ( void   )  const

Dump the state of an object.

Definition at line 23 of file Obstack_T.cpp.

{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("size_ = %d\n"), this->size_));
  ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("head_ = %x\n"), this->head_));
  ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("curr_ = %x\n"), this->curr_));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

template<class ACE_CHAR_T >
ACE_CHAR_T * ACE_Obstack_T< ACE_CHAR_T >::freeze ( void   ) 

Freeze the current building block by null terminating it. Return the starting address of the current building block, 0 if error occurs.

Definition at line 214 of file Obstack_T.cpp.

{
  ACE_CHAR_T *retv = reinterpret_cast<ACE_CHAR_T *> (this->curr_->block_);
  * (reinterpret_cast<ACE_CHAR_T *> (this->curr_->cur_)) = 0;

  this->curr_->cur_ += sizeof (ACE_CHAR_T);
  this->curr_->block_ = this->curr_->cur_;
  return retv;
}

template<class ACE_CHAR_T>
ACE_CHAR_T * ACE_Obstack_T< ACE_CHAR_T >::grow ( ACE_CHAR_T  c  ) 

Inserting a new ACE_CHAR_T c into the current building block without freezing (null terminating) the block. This function will create new chunk by checking the boundary of current Obchunk. Return the location c gets inserted to, or 0 if error.

Definition at line 93 of file Obstack_T.cpp.

{
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::grow");

  if (this->request (1) == 0)
    {
      ACE_CHAR_T *retv = reinterpret_cast<ACE_CHAR_T *> (this->curr_->cur_);
      this->curr_->cur_ += sizeof (ACE_CHAR_T);
      *retv = c;
      return retv;
    }
  else
    return 0;
}

template<class ACE_CHAR_T>
void ACE_Obstack_T< ACE_CHAR_T >::grow_fast ( ACE_CHAR_T  c  ) 

Inserting a new ACE_CHAR_T c into the current building block without freezing (null terminating) the block and without checking for out-of-bound error.

Definition at line 207 of file Obstack_T.cpp.

{
  * (reinterpret_cast<ACE_CHAR_T *> (this->curr_->cur_)) = c;
  this->curr_->cur_ += sizeof (ACE_CHAR_T);
}

template<class ACE_CHAR_T >
size_t ACE_Obstack_T< ACE_CHAR_T >::length ( void   )  const [inline]

Return the maximum length or size of a string that can be put into this Obstack. size = length * sizeof (ACE_CHAR_T).

Deprecated:
No need to use this function as you can put objects of arbitrary lengths into the obstack now.

Definition at line 8 of file Obstack_T.inl.

{
  return this->size_ / sizeof (ACE_CHAR_T);
}

template<class ACE_CHAR_T >
ACE_Obchunk * ACE_Obstack_T< ACE_CHAR_T >::new_chunk ( void   )  [protected]

Definition at line 109 of file Obstack_T.cpp.

{
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::new_chunk");

  ACE_Obchunk *temp = 0;

  ACE_NEW_MALLOC_RETURN (temp,
                         static_cast<ACE_Obchunk *> (this->allocator_strategy_->malloc
                             (sizeof (class ACE_Obchunk) + this->size_)),
                         ACE_Obchunk (this->size_),
                         0);
  return temp;
}

template<class ACE_CHAR_T >
void ACE_Obstack_T< ACE_CHAR_T >::release ( void   ) 

"Release" the entire stack of Obchunks, putting it back on the free list.

Definition at line 198 of file Obstack_T.cpp.

{
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::release");

  this->curr_ = this->head_;
  this->curr_->block_ = this->curr_->cur_ = this->curr_->contents_;
}

template<class ACE_CHAR_T >
int ACE_Obstack_T< ACE_CHAR_T >::request ( size_t  len  ) 

Request Obstack to prepare a block at least len long for building a new string. Return -1 if fail, 0 if success.

Definition at line 37 of file Obstack_T.cpp.

{
  ACE_TRACE ("ACE_Obstack_T<ACE_CHAR_T>::request");

  // normalize the length.
  len *= sizeof (ACE_CHAR_T);

  // Check to see if there's room for the requested length, including
  // any part of an existing string, if any.
  size_t resulting_len = (this->curr_->cur_ - this->curr_->block_) + len;

  // Increase the length of the underlying chunks if the request made is
  // for bigger sized chunks.
  if (this->size_ < resulting_len)
    this->size_ = this->size_ << 1;

  // We now know the request will fit; see if it can fit in the current
  // chunk or will need a new one.
  if (this->curr_->cur_ + len >= this->curr_->end_)
    {
      // Need a new chunk. Save the current one so the current string can be
      // copied to the new chunk.
      ACE_Obchunk *temp = this->curr_;
      if (this->curr_->next_ == 0)
        {
          // We must allocate new memory.
          ACE_Obchunk* tmp = this->new_chunk();
          if (!tmp)
            return -1;
          this->curr_->next_ = tmp;
          this->curr_ = this->curr_->next_;
        }
      else
        {
          // We can reuse previously allocated memory.
          this->curr_ = this->curr_->next_;
          this->curr_->block_ = this->curr_->cur_ = this->curr_->contents_;
        }

      // Copy any initial characters to the new chunk.
      if (temp->cur_ != temp->block_)
        {
          size_t datasize = temp->cur_ - temp->block_;
          ACE_OS::memcpy (this->curr_->block_,
                          temp->block_,
                          datasize);
          this->curr_->cur_ = this->curr_->block_ + datasize;
          // Reset the old chunk.
          temp->cur_ = temp->block_;
        }
    }

  return 0;
}

template<class ACE_CHAR_T >
size_t ACE_Obstack_T< ACE_CHAR_T >::size ( void   )  const [inline]

Definition at line 14 of file Obstack_T.inl.

{
  return this->size_;
}

template<class ACE_CHAR_T >
void ACE_Obstack_T< ACE_CHAR_T >::unwind ( void *  obj  ) 

"Unwind" the stack. If obj is a null pointer, everything allocated in the stack is released. Otherwise, obj must be an address of an object allocated in the stack. In this case, obj is released along with everthing allocated in the Obstack since obj.

Definition at line 173 of file Obstack_T.cpp.

{
  if (obj >= this->curr_->contents_ && obj < this->curr_->end_)
    this->curr_->block_ = this->curr_->cur_ = reinterpret_cast<char*> (obj);
  else
    this->unwind_i (obj);
}

template<class ACE_CHAR_T >
void ACE_Obstack_T< ACE_CHAR_T >::unwind_i ( void *  obj  )  [protected]

Search through the list of Obchunks and release them. Helper funtion used by unwind.

Definition at line 182 of file Obstack_T.cpp.

{
  ACE_Obchunk* curr = this->head_;
  while (curr != 0 && (curr->contents_ > obj || curr->end_ < obj))
      curr = curr->next_;
  if (curr)
    {
      this->curr_ = curr;
      this->curr_->block_ = this->curr_->cur_ = reinterpret_cast<char*> (obj);
    }
  else if (obj != 0)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("Deletion of non-existent object.\n%a")));
}


Member Data Documentation

template<class ACE_CHAR_T>
ACE_Obstack_T< ACE_CHAR_T >::ACE_ALLOC_HOOK_DECLARE

Declare the dynamic allocation hooks.

Definition at line 96 of file Obstack_T.h.

template<class ACE_CHAR_T>
ACE_Allocator* ACE_Obstack_T< ACE_CHAR_T >::allocator_strategy_ [protected]

Pointer to the allocator used by this Obstack.

Definition at line 106 of file Obstack_T.h.

template<class ACE_CHAR_T>
class ACE_Obchunk* ACE_Obstack_T< ACE_CHAR_T >::curr_ [protected]

Pointer to the current Obchunk.

Definition at line 116 of file Obstack_T.h.

template<class ACE_CHAR_T>
class ACE_Obchunk* ACE_Obstack_T< ACE_CHAR_T >::head_ [protected]

Head of the Obchunk chain.

Definition at line 113 of file Obstack_T.h.

template<class ACE_CHAR_T>
size_t ACE_Obstack_T< ACE_CHAR_T >::size_ [protected]

Current size of the Obstack;.

Definition at line 109 of file Obstack_T.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines