00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Obstack_T.h 00006 * 00007 * $Id: Obstack_T.h 80826 2008-03-04 14:51:23Z wotte $ 00008 * 00009 * @author Doug Schmidt <schmidt@cs.wustl.edu> 00010 * @author Nanbor Wang <nanbor@cs.wustl.edu> 00011 */ 00012 //============================================================================= 00013 00014 #ifndef ACE_OBSTACK_T_H 00015 #define ACE_OBSTACK_T_H 00016 00017 #include /**/ "ace/pre.h" 00018 00019 #include "ace/Obchunk.h" 00020 00021 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00022 # pragma once 00023 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00024 00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00026 00027 class ACE_Allocator; 00028 00029 /** 00030 * @class ACE_Obstack_T 00031 * 00032 * @brief Define a simple "mark and release" memory allocation utility. 00033 * 00034 * The implementation is similar to the GNU obstack utility, 00035 * which is used extensively in the GCC compiler. 00036 */ 00037 template <class CHAR> 00038 class ACE_Obstack_T 00039 { 00040 public: 00041 // = Initialization and termination methods. 00042 ACE_Obstack_T (size_t size = (4096 * sizeof (CHAR)) - sizeof (ACE_Obchunk), 00043 ACE_Allocator *allocator_strategy = 0); 00044 ~ACE_Obstack_T (void); 00045 00046 /// Request Obstack to prepare a block at least @a len long for building 00047 /// a new string. Return -1 if fail, 0 if success. 00048 int request (size_t len); 00049 00050 /// Inserting a new CHAR \a c into the current building 00051 /// block without freezing (null terminating) the block. 00052 /// This function will create new chunk by checking the 00053 /// boundary of current Obchunk. Return 00054 /// the location \a c gets inserted to, or 0 if error. 00055 CHAR *grow (CHAR c); 00056 00057 /// Inserting a new CHAR \a c into the current building 00058 /// block without freezing (null terminating) the block and without 00059 /// checking for out-of-bound error. 00060 void grow_fast (CHAR c); 00061 00062 /// Freeze the current building block by null terminating it. 00063 /// Return the starting address of the current building block, 0 00064 /// if error occurs. 00065 CHAR *freeze (void); 00066 00067 /// Copy the data into the current Obchunk and freeze the current 00068 /// block. Return the starting address of the current building 00069 /// block, 0 if error occurs. @a len specify the string length, 00070 /// not the actually data size. 00071 CHAR *copy (const CHAR *data, 00072 size_t len); 00073 00074 /// Return the maximum @a length or @a size of a string that can be put 00075 /// into this Obstack. @a size = @a length * sizeof (CHAR). 00076 /// 00077 /// @deprecated No need to use this function as you can put objects of 00078 /// arbitrary lengths into the obstack now. 00079 size_t length (void) const; 00080 size_t size (void) const; 00081 00082 /// "Unwind" the stack. If @a obj is a null pointer, everything allocated 00083 /// in the stack is released. Otherwise, @a obj must be an address of an 00084 /// object allocated in the stack. In this case, @a obj is released along 00085 /// with everthing allocated in the Obstack since @a obj. 00086 void unwind (void* obj); 00087 00088 /// "Release" the entire stack of Obchunks, putting it back on the free 00089 /// list. 00090 void release (void); 00091 00092 /// Dump the state of an object. 00093 void dump (void) const; 00094 00095 /// Declare the dynamic allocation hooks. 00096 ACE_ALLOC_HOOK_DECLARE; 00097 00098 protected: 00099 class ACE_Obchunk *new_chunk (void); 00100 00101 /// Search through the list of Obchunks and release them. Helper funtion 00102 /// used by unwind. 00103 void unwind_i (void* obj); 00104 00105 /// Pointer to the allocator used by this Obstack. 00106 ACE_Allocator *allocator_strategy_; 00107 00108 /// Current size of the Obstack; 00109 size_t size_; 00110 00111 // Don't change the order of the following two fields. 00112 /// Head of the Obchunk chain. 00113 class ACE_Obchunk *head_; 00114 00115 /// Pointer to the current Obchunk. 00116 class ACE_Obchunk *curr_; 00117 }; 00118 00119 ACE_END_VERSIONED_NAMESPACE_DECL 00120 00121 #if defined (__ACE_INLINE__) 00122 #include "ace/Obstack_T.inl" 00123 #endif /* __ACE_INLINE__ */ 00124 00125 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00126 #include "ace/Obstack_T.cpp" 00127 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00128 00129 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00130 #pragma implementation ("Obstack_T.cpp") 00131 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00132 00133 #include /**/ "ace/post.h" 00134 #endif /* ACE_OBSTACK_T_H */