home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / obstack.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  3.1 KB  |  114 lines

  1. /* 
  2. Copyright (C) 1988 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22.  
  23. #include <values.h>
  24. #include <builtin.h>
  25. #include <Obstack.h>
  26.  
  27. Obstack::Obstack(int size = 4092, int alignment = 4)
  28. {
  29.   alignmentmask = alignment - 1;
  30.   chunk = (_obstack_chunk*) (new char[chunksize = size]);
  31.   nextfree = objectbase = chunk->contents;
  32.   chunklimit = chunk->limit = (char*)(chunk) + chunksize;
  33.   chunk->prev = 0;
  34. }
  35.  
  36. void Obstack::_free(void* obj)
  37. {
  38.   _obstack_chunk*  lp;
  39.   _obstack_chunk*  plp;
  40.  
  41.   lp = chunk;
  42.   while (lp != 0 && ((void*)lp > obj || (void*)(lp)->limit < obj))
  43.   {
  44.     plp = lp -> prev;
  45.     delete(lp);
  46.     lp = plp;
  47.   }
  48.   if (lp)
  49.   {
  50.     objectbase = nextfree = (char *)(obj);
  51.     chunklimit = lp->limit;
  52.     chunk = lp;
  53.   }
  54.   else if (obj != 0)
  55.     (*lib_error_handler)("Obstack", "deletion of nonexistent obj");
  56. }
  57.  
  58. void Obstack::newchunk(int size)
  59. {
  60.   _obstack_chunk*    old_chunk = chunk;
  61.   _obstack_chunk*    new_chunk;
  62.   long    new_size;
  63.   int obj_size = nextfree - objectbase;
  64.  
  65.   new_size = (obj_size + size) << 1;
  66.   if (new_size < chunksize)
  67.     new_size = chunksize;
  68.  
  69.   new_chunk = chunk = (_obstack_chunk*)(new char[new_size]);
  70.   new_chunk->prev = old_chunk;
  71.   new_chunk->limit = chunklimit = (char *) new_chunk + new_size;
  72.  
  73.   bcopy((void*)objectbase, (void*)new_chunk->contents, obj_size);
  74.   objectbase = new_chunk->contents;
  75.   nextfree = objectbase + obj_size;
  76. }
  77.  
  78. void* Obstack::finish()
  79. {
  80.   void* value = (void*) objectbase;
  81.   nextfree = (char*)((int)(nextfree + alignmentmask) & ~(alignmentmask));
  82.   if (nextfree - (char*)chunk > chunklimit - (char*)chunk)
  83.     nextfree = chunklimit;
  84.   objectbase = nextfree;
  85.   return value;
  86. }
  87.  
  88. int Obstack::contains(void* obj) // true if obj somewhere in Obstack
  89. {
  90.   for (_obstack_chunk* ch = chunk; 
  91.        ch != 0 && (obj < (void*)ch || obj >= (void*)(ch->limit)); 
  92.        ch = ch->prev)
  93.   return ch != 0;
  94. }
  95.          
  96. int Obstack::OK()
  97. {
  98.   int v = chunksize > 0;        // valid size
  99.   v &= alignmentmask != 0;      // and alignment
  100.   v &= chunk != 0;
  101.   v &= objectbase >= chunk->contents;
  102.   v &= nextfree >= objectbase;
  103.   v &= nextfree <= chunklimit;
  104.   v &= chunklimit == chunk->limit;
  105.   _obstack_chunk* p = chunk;
  106.   // allow lots of chances to find bottom!
  107.   long x = MAXLONG;
  108.   while (p != 0 && x != 0) { --x; p = p->prev; }
  109.   v &= x > 0;
  110.   if (!v) 
  111.     (*lib_error_handler)("Obstack", "invariant failure");
  112.   return v;
  113. }
  114.