home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / Obstack.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  4KB  |  219 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19.  
  20. #ifndef _Obstack_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _Obstack_h 1
  25.  
  26. #include <std.h>
  27.  
  28. #undef OK
  29.  
  30. class Obstack
  31. {
  32.   struct _obstack_chunk
  33.   {
  34.     char*           limit;
  35.     _obstack_chunk* prev;
  36.     char            contents[4];
  37.   };
  38.  
  39. protected:
  40.   long              chunksize;
  41.   _obstack_chunk* chunk;
  42.   char*              objectbase;
  43.   char*              nextfree;
  44.   char*              chunklimit;
  45.   int             alignmentmask;
  46.  
  47.   void  _free(void* obj);
  48.   void  newchunk(int size);
  49.  
  50. public:
  51.         Obstack(int size = 4080, int alignment = 4); // 4080=4096-mallocslop
  52.  
  53.         ~Obstack();
  54.  
  55.   void* base();
  56.   void* next_free();
  57.   int   alignment_mask();
  58.   int   chunk_size();
  59.   int   size();
  60.   int   room();
  61.   int   contains(void* p);      // does Obstack hold pointer p?
  62.  
  63.   void  grow(const void* data, int size);
  64.   void  grow(const void* data, int size, char terminator);
  65.   void  grow(const char* s);
  66.   void  grow(char c);
  67.   void  grow_fast(char c);
  68.   void  blank(int size);
  69.   void  blank_fast(int size);
  70.  
  71.   void* finish();
  72.   void* finish(char terminator);
  73.  
  74.   void* copy(const void* data, int size);
  75.   void* copy(const void* data, int size, char terminator);
  76.   void* copy(const char* s);
  77.   void* copy(char c);
  78.   void* alloc(int size);
  79.  
  80.   void  free(void* obj);
  81.   void  shrink(int size = 1); // suggested by ken@cs.rochester.edu
  82.  
  83.   int   OK();                   // rep invariant
  84. };
  85.  
  86.  
  87. inline Obstack::~Obstack()
  88. {
  89.   _free(0); 
  90. }
  91.  
  92. inline void* Obstack::base()
  93. {
  94.   return objectbase; 
  95. }
  96.  
  97. inline void* Obstack::next_free()
  98. {
  99.   return nextfree; 
  100. }
  101.  
  102. inline int Obstack::alignment_mask()
  103. {
  104.   return alignmentmask; 
  105. }
  106.  
  107. inline int Obstack::chunk_size()
  108. {
  109.   return chunksize; 
  110. }
  111.  
  112. inline int Obstack::size()
  113. {
  114.   return nextfree - objectbase; 
  115. }
  116.  
  117. inline int Obstack::room()
  118. {
  119.   return chunklimit - nextfree; 
  120. }
  121.  
  122. inline void Obstack:: grow(const void* data, int size)
  123. {
  124.   if (nextfree+size > chunklimit) 
  125.     newchunk(size);
  126.   memcpy(nextfree, data, size);
  127.   nextfree += size; 
  128. }
  129.  
  130. inline void Obstack:: grow(const void* data, int size, char terminator)
  131. {
  132.   if (nextfree+size+1 > chunklimit) 
  133.     newchunk(size+1);
  134.   memcpy(nextfree, data, size);
  135.   nextfree += size; 
  136.   *(nextfree)++ = terminator; 
  137. }
  138.  
  139. inline void Obstack:: grow(const char* s)
  140. {
  141.   grow((const void*)s, strlen(s), 0); 
  142. }
  143.  
  144. inline void Obstack:: grow(char c)
  145. {
  146.   if (nextfree+1 > chunklimit) 
  147.     newchunk(1); 
  148.   *(nextfree)++ = c; 
  149. }
  150.  
  151. inline void Obstack:: blank(int size)
  152. {
  153.   if (nextfree+size > chunklimit) 
  154.     newchunk(size);
  155.   nextfree += size; 
  156. }
  157.  
  158. inline void* Obstack::finish(char terminator)
  159. {
  160.   grow(terminator); 
  161.   return finish(); 
  162. }
  163.  
  164. inline void* Obstack::copy(const void* data, int size)
  165. {
  166.   grow (data, size);
  167.   return finish(); 
  168. }
  169.  
  170. inline void* Obstack::copy(const void* data, int size, char terminator)
  171. {
  172.   grow(data, size, terminator); 
  173.   return finish(); 
  174. }
  175.  
  176. inline void* Obstack::copy(const char* s)
  177. {
  178.   grow((const void*)s, strlen(s), 0); 
  179.   return finish(); 
  180. }
  181.  
  182. inline void* Obstack::copy(char c)
  183. {
  184.   grow(c);
  185.   return finish(); 
  186. }
  187.  
  188. inline void* Obstack::alloc(int size)
  189. {
  190.   blank(size);
  191.   return finish(); 
  192. }
  193.  
  194. inline void Obstack:: free(void* obj)     
  195. {
  196.   if (obj >= (void*)chunk && obj<(void*)chunklimit)
  197.     nextfree = objectbase = (char *) obj;
  198.   else 
  199.     _free(obj); 
  200. }
  201.  
  202. inline void Obstack:: grow_fast(char c)
  203. {
  204.   *(nextfree)++ = c; 
  205. }
  206.  
  207. inline void Obstack:: blank_fast(int size)
  208. {
  209.   nextfree += size; 
  210. }
  211.  
  212. inline void Obstack:: shrink(int size) // from ken@cs.rochester.edu
  213. {
  214.   if (nextfree >= objectbase + size)
  215.     nextfree -= size;
  216. }
  217.  
  218. #endif
  219.