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, and ACE_Obstack_T< CHAR >::new_chunk().

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

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

Definition at line 140 of file Obstack_T.cpp.

References ACE_TRACE, ACE_Allocator::free(), and ACE_Obchunk::next_.

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


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 156 of file Obstack_T.cpp.

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

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

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_LIB_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_LIB_TEXT ("size_ = %d\n"), this->size_));
00030   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("head_ = %x\n"), this->head_));
00031   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("curr_ = %x\n"), this->curr_));
00032   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00033 #endif /* ACE_HAS_DUMP */
00034 }

template<class CHAR>
ACE_INLINE 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 27 of file Obstack_T.inl.

References ACE_Obchunk::block_, and ACE_Obchunk::cur_.

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

00028 {
00029   CHAR *retv = reinterpret_cast<CHAR *> (this->curr_->block_);
00030   * (reinterpret_cast<CHAR *> (this->curr_->cur_)) = 0;
00031 
00032   this->curr_->cur_ += sizeof (CHAR);
00033   this->curr_->block_ = this->curr_->cur_;
00034   return retv;
00035 }

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 >::request().

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>
ACE_INLINE 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 20 of file Obstack_T.inl.

References ACE_Obchunk::cur_.

00021 {
00022   * (reinterpret_cast<CHAR *> (this->curr_->cur_)) = c;
00023   this->curr_->cur_ += sizeof (CHAR);
00024 }

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.

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;
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_, and ACE_Obchunk::cur_.

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_Obchunk::end_, ACE_OS::memcpy(), ACE_Obstack_T< CHAR >::new_chunk(), and ACE_Obchunk::next_.

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

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.

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 171 of file Obstack_T.cpp.

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

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

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 180 of file Obstack_T.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_Obchunk::block_, ACE_Obchunk::contents_, ACE_Obchunk::cur_, ACE_Obchunk::end_, LM_ERROR, and ACE_Obchunk::next_.

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

00181 {
00182   ACE_Obchunk* curr;
00183 
00184   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_LIB_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.

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.

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.

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.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:26:05 2006 for ACE by doxygen 1.3.6