home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer) / NeXT_Developer-3.3.iso / NextDeveloper / Headers / g++ / Obstack.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  4.3 KB  |  218 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, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _Obstack_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #pragma cplusplus
  24. #endif
  25. #define _Obstack_h 1
  26.  
  27. #include <std.h>
  28.  
  29. class Obstack
  30. {
  31.   struct _obstack_chunk
  32.   {
  33.     char*           limit;
  34.     _obstack_chunk* prev;
  35.     char            contents[4];
  36.   };
  37.  
  38. protected:
  39.   long              chunksize;
  40.   _obstack_chunk* chunk;
  41.   char*              objectbase;
  42.   char*              nextfree;
  43.   char*              chunklimit;
  44.   int             alignmentmask;
  45.  
  46.   void  _free(void* obj);
  47.   void  newchunk(int size);
  48.  
  49. public:
  50.         Obstack(int size = 4080, int alignment = 4); // 4080=4096-mallocslop
  51.  
  52.         ~Obstack();
  53.  
  54.   void* base();
  55.   void* next_free();
  56.   int   alignment_mask();
  57.   int   chunk_size();
  58.   int   size();
  59.   int   room();
  60.   int   contains(void* p);      // does Obstack hold pointer p?
  61.  
  62.   void  grow(const void* data, int size);
  63.   void  grow(const void* data, int size, char terminator);
  64.   void  grow(const char* s);
  65.   void  grow(char c);
  66.   void  grow_fast(char c);
  67.   void  blank(int size);
  68.   void  blank_fast(int size);
  69.  
  70.   void* finish();
  71.   void* finish(char terminator);
  72.  
  73.   void* copy(const void* data, int size);
  74.   void* copy(const void* data, int size, char terminator);
  75.   void* copy(const char* s);
  76.   void* copy(char c);
  77.   void* alloc(int size);
  78.  
  79.   void  free(void* obj);
  80.   void  shrink(int size = 1); // suggested by ken@cs.rochester.edu
  81.  
  82.   int   OK();                   // rep invariant
  83. };
  84.  
  85.  
  86. inline Obstack::~Obstack()
  87. {
  88.   _free(0); 
  89. }
  90.  
  91. inline void* Obstack::base()
  92. {
  93.   return objectbase; 
  94. }
  95.  
  96. inline void* Obstack::next_free()
  97. {
  98.   return nextfree; 
  99. }
  100.  
  101. inline int Obstack::alignment_mask()
  102. {
  103.   return alignmentmask; 
  104. }
  105.  
  106. inline int Obstack::chunk_size()
  107. {
  108.   return chunksize; 
  109. }
  110.  
  111. inline int Obstack::size()
  112. {
  113.   return nextfree - objectbase; 
  114. }
  115.  
  116. inline int Obstack::room()
  117. {
  118.   return chunklimit - nextfree; 
  119. }
  120.  
  121. inline void Obstack:: grow(const void* data, int size)
  122. {
  123.   if (nextfree+size > chunklimit) 
  124.     newchunk(size);
  125.   memcpy(nextfree, data, size);
  126.   nextfree += size; 
  127. }
  128.  
  129. inline void Obstack:: grow(const void* data, int size, char terminator)
  130. {
  131.   if (nextfree+size+1 > chunklimit) 
  132.     newchunk(size+1);
  133.   memcpy(nextfree, data, size);
  134.   nextfree += size; 
  135.   *(nextfree)++ = terminator; 
  136. }
  137.  
  138. inline void Obstack:: grow(const char* s)
  139. {
  140.   grow((const void*)s, strlen(s), 0); 
  141. }
  142.  
  143. inline void Obstack:: grow(char c)
  144. {
  145.   if (nextfree+1 > chunklimit) 
  146.     newchunk(1); 
  147.   *(nextfree)++ = c; 
  148. }
  149.  
  150. inline void Obstack:: blank(int size)
  151. {
  152.   if (nextfree+size > chunklimit) 
  153.     newchunk(size);
  154.   nextfree += size; 
  155. }
  156.  
  157. inline void* Obstack::finish(char terminator)
  158. {
  159.   grow(terminator); 
  160.   return finish(); 
  161. }
  162.  
  163. inline void* Obstack::copy(const void* data, int size)
  164. {
  165.   grow (data, size);
  166.   return finish(); 
  167. }
  168.  
  169. inline void* Obstack::copy(const void* data, int size, char terminator)
  170. {
  171.   grow(data, size, terminator); 
  172.   return finish(); 
  173. }
  174.  
  175. inline void* Obstack::copy(const char* s)
  176. {
  177.   grow((const void*)s, strlen(s), 0); 
  178.   return finish(); 
  179. }
  180.  
  181. inline void* Obstack::copy(char c)
  182. {
  183.   grow(c);
  184.   return finish(); 
  185. }
  186.  
  187. inline void* Obstack::alloc(int size)
  188. {
  189.   blank(size);
  190.   return finish(); 
  191. }
  192.  
  193. inline void Obstack:: free(void* obj)     
  194. {
  195.   if (obj >= (void*)chunk && obj<(void*)chunklimit)
  196.     nextfree = objectbase = (char *) obj;
  197.   else 
  198.     _free(obj); 
  199. }
  200.  
  201. inline void Obstack:: grow_fast(char c)
  202. {
  203.   *(nextfree)++ = c; 
  204. }
  205.  
  206. inline void Obstack:: blank_fast(int size)
  207. {
  208.   nextfree += size; 
  209. }
  210.  
  211. inline void Obstack:: shrink(int size) // from ken@cs.rochester.edu
  212. {
  213.   if (nextfree >= objectbase + size)
  214.     nextfree -= size;
  215. }
  216.  
  217. #endif
  218.