ACE_Obstack_T< CHAR > Class Template Reference

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

#include <Obstack_T.h>

Collaboration diagram for ACE_Obstack_T< CHAR >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Obstack_T (size_t size=(4096 *sizeof(CHAR))-sizeof(ACE_Obchunk), ACE_Allocator *allocator_strategy=0)
 ~ACE_Obstack_T (void)
int request (size_t len)
CHAR * grow (CHAR c)
void grow_fast (CHAR c)
CHAR * freeze (void)
CHAR * copy (const CHAR *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

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;.
ACE_Obchunkhead_
 Head of the Obchunk chain.
ACE_Obchunkcurr_
 Pointer to the current Obchunk.

Detailed Description

template<class CHAR>
class ACE_Obstack_T< CHAR >

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 CHAR>
ACE_Obstack_T< CHAR >::ACE_Obstack_T ( size_t  size = (4096 *sizeof(CHAR))-sizeof(ACE_Obchunk),
ACE_Allocator allocator_strategy = 0 
)

Definition at line 124 of file Obstack_T.cpp.

References ACE_ALLOCATOR, ACE_TRACE, ACE_Obstack_T< CHAR >::curr_, ACE_Obstack_T< CHAR >::head_, ACE_Allocator::instance(), and ACE_Obstack_T< CHAR >::new_chunk().

00126   : allocator_strategy_ (allocator_strategy),
00127     size_ (size),
00128     head_ (0),
00129     curr_ (0)
00130 {
00131   ACE_TRACE ("ACE_Obstack_T<CHAR>::ACE_Obstack");
00132 
00133   if (this->allocator_strategy_ == 0)
00134     ACE_ALLOCATOR (this->allocator_strategy_,
00135                    ACE_Allocator::instance ());
00136 
00137   this->head_ = this->new_chunk ();
00138   this->curr_ = this->head_;
00139 }

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

Definition at line 142 of file Obstack_T.cpp.

References ACE_TRACE, ACE_Obstack_T< CHAR >::allocator_strategy_, ACE_Allocator::free(), ACE_Obstack_T< CHAR >::head_, and ACE_Obchunk::next_.

00143 {
00144   ACE_TRACE ("ACE_Obstack_T<CHAR>::~ACE_Obstack_T");
00145 
00146   ACE_Obchunk *temp = this->head_;
00147 
00148   while (temp != 0)
00149     {
00150       ACE_Obchunk *next = temp->next_;
00151       temp->next_  = 0;
00152       this->allocator_strategy_->free (temp);
00153       temp = next;
00154     }
00155 }


Member Function Documentation

template<class CHAR>
CHAR * ACE_Obstack_T< CHAR >::copy ( const CHAR *  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.

References ACE_TRACE, ACE_Obchunk::cur_, ACE_Obstack_T< CHAR >::curr_, ACE_Obstack_T< CHAR >::freeze(), and ACE_OS::memcpy().

00160 {
00161   ACE_TRACE ("ACE_Obstack_T<CHAR>::copy");
00162 
00163   if (this->request (len) != 0)
00164     return 0;
00165 
00166   size_t tsize = len * sizeof (CHAR);
00167   ACE_OS::memcpy (this->curr_->cur_, s, tsize);
00168   this->curr_->cur_ += tsize ;
00169   return this->freeze ();
00170 }

template<class CHAR>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL void ACE_Obstack_T< CHAR >::dump ( void   )  const

Dump the state of an object.

Definition at line 23 of file Obstack_T.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_TEXT, ACE_TRACE, and LM_DEBUG.

00024 {
00025 #if defined (ACE_HAS_DUMP)
00026   ACE_TRACE ("ACE_Obstack_T<CHAR>::dump");
00027 
00028   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00029   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("size_ = %d\n"), this->size_));
00030   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("head_ = %x\n"), this->head_));
00031   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("curr_ = %x\n"), this->curr_));
00032   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00033 #endif /* ACE_HAS_DUMP */
00034 }

template<class CHAR>
CHAR * ACE_Obstack_T< CHAR >::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.

References ACE_Obchunk::block_, ACE_Obchunk::cur_, and ACE_Obstack_T< CHAR >::curr_.

Referenced by ACE_Obstack_T< CHAR >::copy().

00215 {
00216   CHAR *retv = reinterpret_cast<CHAR *> (this->curr_->block_);
00217   * (reinterpret_cast<CHAR *> (this->curr_->cur_)) = 0;
00218 
00219   this->curr_->cur_ += sizeof (CHAR);
00220   this->curr_->block_ = this->curr_->cur_;
00221   return retv;
00222 }

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

Inserting a new CHAR 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.

References ACE_TRACE, ACE_Obchunk::cur_, and ACE_Obstack_T< CHAR >::curr_.

00094 {
00095   ACE_TRACE ("ACE_Obstack_T<CHAR>::grow");
00096 
00097   if (this->request (1) == 0)
00098     {
00099       CHAR *retv = reinterpret_cast<CHAR *> (this->curr_->cur_);
00100       this->curr_->cur_ += sizeof (CHAR);
00101       *retv = c;
00102       return retv;
00103     }
00104   else
00105     return 0;
00106 }

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

Inserting a new CHAR 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.

References ACE_Obchunk::cur_, and ACE_Obstack_T< CHAR >::curr_.

00208 {
00209   * (reinterpret_cast<CHAR *> (this->curr_->cur_)) = c;
00210   this->curr_->cur_ += sizeof (CHAR);
00211 }

template<class CHAR>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE size_t ACE_Obstack_T< CHAR >::length ( void   )  const

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

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.

References ACE_Obstack_T< CHAR >::size_.

00009 {
00010   return this->size_ / sizeof (CHAR);
00011 }

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

Definition at line 109 of file Obstack_T.cpp.

References ACE_NEW_MALLOC_RETURN, and ACE_TRACE.

Referenced by ACE_Obstack_T< CHAR >::ACE_Obstack_T(), and ACE_Obstack_T< CHAR >::request().

00110 {
00111   ACE_TRACE ("ACE_Obstack_T<CHAR>::new_chunk");
00112 
00113   ACE_Obchunk *temp = 0;
00114 
00115   ACE_NEW_MALLOC_RETURN (temp,
00116                          static_cast<ACE_Obchunk *> (this->allocator_strategy_->malloc
00117                              (sizeof (class ACE_Obchunk) + this->size_)),
00118                          ACE_Obchunk (this->size_),
00119                          0);
00120   return temp;
00121 }

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

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

Definition at line 198 of file Obstack_T.cpp.

References ACE_TRACE, ACE_Obchunk::block_, ACE_Obchunk::contents_, ACE_Obchunk::cur_, ACE_Obstack_T< CHAR >::curr_, and ACE_Obstack_T< CHAR >::head_.

00199 {
00200   ACE_TRACE ("ACE_Obstack_T<CHAR>::release");
00201 
00202   this->curr_ = this->head_;
00203   this->curr_->block_ = this->curr_->cur_ = this->curr_->contents_;
00204 }

template<class CHAR>
int ACE_Obstack_T< CHAR >::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.

References ACE_TRACE, ACE_Obchunk::block_, ACE_Obchunk::contents_, ACE_Obchunk::cur_, ACE_Obstack_T< CHAR >::curr_, ACE_OS::memcpy(), ACE_Obstack_T< CHAR >::new_chunk(), ACE_Obchunk::next_, and ACE_Obstack_T< CHAR >::size_.

00038 {
00039   ACE_TRACE ("ACE_Obstack_T<CHAR>::request");
00040 
00041   // normalize the length.
00042   len *= sizeof (CHAR);
00043 
00044   // Check to see if there's room for the requested length, including
00045   // any part of an existing string, if any.
00046   size_t resulting_len = (this->curr_->cur_ - this->curr_->block_) + len;
00047 
00048   // Increase the length of the underlying chunks if the request made is
00049   // for bigger sized chunks.
00050   if (this->size_ < resulting_len)
00051     this->size_ = this->size_ << 1;
00052 
00053   // We now know the request will fit; see if it can fit in the current
00054   // chunk or will need a new one.
00055   if (this->curr_->cur_ + len >= this->curr_->end_)
00056     {
00057       // Need a new chunk. Save the current one so the current string can be
00058       // copied to the new chunk.
00059       ACE_Obchunk *temp = this->curr_;
00060       if (this->curr_->next_ == 0)
00061         {
00062           // We must allocate new memory.
00063           ACE_Obchunk* tmp = this->new_chunk();
00064           if (!tmp)
00065             return -1;
00066           this->curr_->next_ = tmp;
00067           this->curr_ = this->curr_->next_;
00068         }
00069       else
00070         {
00071           // We can reuse previously allocated memory.
00072           this->curr_ = this->curr_->next_;
00073           this->curr_->block_ = this->curr_->cur_ = this->curr_->contents_;
00074         }
00075 
00076       // Copy any initial characters to the new chunk.
00077       if (temp->cur_ != temp->block_)
00078         {
00079           size_t datasize = temp->cur_ - temp->block_;
00080           ACE_OS::memcpy (this->curr_->block_,
00081                           temp->block_,
00082                           datasize);
00083           this->curr_->cur_ = this->curr_->block_ + datasize;
00084           // Reset the old chunk.
00085           temp->cur_ = temp->block_;
00086         }
00087     }
00088 
00089   return 0;
00090 }

template<class CHAR>
ACE_INLINE size_t ACE_Obstack_T< CHAR >::size ( void   )  const

Definition at line 14 of file Obstack_T.inl.

References ACE_Obstack_T< CHAR >::size_.

00015 {
00016   return this->size_;
00017 }

template<class CHAR>
void ACE_Obstack_T< CHAR >::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.

References ACE_Obchunk::block_, ACE_Obchunk::cur_, ACE_Obstack_T< CHAR >::curr_, ACE_Obchunk::end_, and ACE_Obstack_T< CHAR >::unwind_i().

00174 {
00175   if (obj >= this->curr_->contents_ && obj < this->curr_->end_)
00176     this->curr_->block_ = this->curr_->cur_ = reinterpret_cast<char*> (obj);
00177   else
00178     this->unwind_i (obj);
00179 }

template<class CHAR>
void ACE_Obstack_T< CHAR >::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.

References ACE_ERROR, ACE_TEXT, ACE_Obchunk::block_, ACE_Obchunk::contents_, ACE_Obchunk::cur_, ACE_Obstack_T< CHAR >::curr_, ACE_Obchunk::end_, ACE_Obstack_T< CHAR >::head_, LM_ERROR, and ACE_Obchunk::next_.

Referenced by ACE_Obstack_T< CHAR >::unwind().

00183 {
00184   ACE_Obchunk* curr = this->head_;
00185   while (curr != 0 && (curr->contents_ > obj || curr->end_ < obj))
00186       curr = curr->next_;
00187   if (curr)
00188     {
00189       this->curr_ = curr;
00190       this->curr_->block_ = this->curr_->cur_ = reinterpret_cast<char*> (obj);
00191     }
00192   else if (obj != 0)
00193     ACE_ERROR ((LM_ERROR,
00194                 ACE_TEXT ("Deletion of non-existent object.\n%a")));
00195 }


Member Data Documentation

template<class CHAR>
ACE_Obstack_T< CHAR >::ACE_ALLOC_HOOK_DECLARE

Declare the dynamic allocation hooks.

Definition at line 96 of file Obstack_T.h.

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

Pointer to the allocator used by this Obstack.

Definition at line 106 of file Obstack_T.h.

Referenced by ACE_Obstack_T< CHAR >::~ACE_Obstack_T().

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

Pointer to the current Obchunk.

Definition at line 116 of file Obstack_T.h.

Referenced by ACE_Obstack_T< CHAR >::ACE_Obstack_T(), ACE_Obstack_T< CHAR >::copy(), ACE_Obstack_T< CHAR >::freeze(), ACE_Obstack_T< CHAR >::grow(), ACE_Obstack_T< CHAR >::grow_fast(), ACE_Obstack_T< CHAR >::release(), ACE_Obstack_T< CHAR >::request(), ACE_Obstack_T< CHAR >::unwind(), and ACE_Obstack_T< CHAR >::unwind_i().

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

Head of the Obchunk chain.

Definition at line 113 of file Obstack_T.h.

Referenced by ACE_Obstack_T< CHAR >::ACE_Obstack_T(), ACE_Obstack_T< CHAR >::release(), ACE_Obstack_T< CHAR >::unwind_i(), and ACE_Obstack_T< CHAR >::~ACE_Obstack_T().

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

Current size of the Obstack;.

Definition at line 109 of file Obstack_T.h.

Referenced by ACE_Obstack_T< CHAR >::length(), ACE_Obstack_T< CHAR >::request(), and ACE_Obstack_T< CHAR >::size().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:25 2010 for ACE by  doxygen 1.4.7