home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / gpplib22.zoo / libsrc / xallocri.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  3.7 KB  |  207 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989 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. #ifdef __GNUG__
  20. #pragma implementation
  21. #endif
  22. #include <std.h>
  23. #include <xallocri.h>
  24. #include <new.h>
  25.  
  26. #ifdef atarist
  27. static inline size_t good_size(size_t s)
  28. {
  29.   size_t req = s + 4;
  30.   size_t good = 8;
  31.   while (good < req) good <<= 1;
  32.   return good - 4;
  33. }
  34.  
  35. void* AllocRing::alloc(size_t s)
  36. {
  37.   size_t size = good_size(s);
  38.  
  39.   void* p;
  40.   if (nodes[current].ptr != 0 && 
  41.       nodes[current].sz >= int(size) && 
  42.       nodes[current].sz < int(4 * size))
  43.     p = nodes[current].ptr;
  44.   else
  45.   {
  46.     if (nodes[current].ptr != 0) delete nodes[current].ptr;
  47.     p = new char[size];
  48.     nodes[current].ptr = p;
  49.     nodes[current].sz = size;
  50.   }
  51.   ++current;
  52.   if (current >= n) current = 0;
  53.   return p;
  54. }
  55.  
  56. AllocRing::AllocRing(size_t max)
  57.   :n(max), current(0), nodes(new AllocQNode[max])
  58. {
  59.   for (size_t i = 0; i < n; ++i)
  60.   {
  61.     nodes[i].ptr = 0;
  62.     nodes[i].sz = 0;
  63.   }
  64. }
  65.  
  66.  
  67. long AllocRing::find(void* p)
  68. {
  69.   if (p == 0) return -1;
  70.  
  71.   for (size_t i = 0; i < n; ++i)
  72.     if (nodes[i].ptr == p)
  73.       return (long)i;
  74.  
  75.   return -1;
  76. }
  77.  
  78.  
  79. void AllocRing::clear()
  80. {
  81.   for (size_t i = 0; i < n; ++i)
  82.   {
  83.     if (nodes[i].ptr != 0)
  84.     {
  85.       delete(nodes[i].ptr);
  86.       nodes[i].ptr = 0;
  87.     }
  88.     nodes[i].sz = 0;
  89.   }
  90.   current = 0;
  91. }
  92.  
  93.  
  94. void AllocRing::free(void* p)
  95. {
  96.   long idx = find(p);
  97.   if (idx >= 0)
  98.   {
  99.     delete nodes[idx].ptr;
  100.     nodes[idx].ptr = 0;
  101.   }
  102. }
  103.  
  104. AllocRing::~AllocRing()
  105. {
  106.   clear();
  107. }
  108.  
  109. int AllocRing::contains(void* p)
  110. {
  111.   return find(p) >= 0;
  112. }
  113.  
  114. #else
  115.  
  116. void* AllocRing::alloc(int s)
  117. {
  118.   unsigned int size = good_size(s);
  119.  
  120.   void* p;
  121.   if (nodes[current].ptr != 0 && 
  122.       nodes[current].sz >= int(size) && 
  123.       nodes[current].sz < int(4 * size))
  124.     p = nodes[current].ptr;
  125.   else
  126.   {
  127.     if (nodes[current].ptr != 0) delete nodes[current].ptr;
  128.     p = new char[size];
  129.     nodes[current].ptr = p;
  130.     nodes[current].sz = size;
  131.   }
  132.   ++current;
  133.   if (current >= n) current = 0;
  134.   return p;
  135. }
  136.  
  137. AllocRing::AllocRing(int max)
  138.   :n(max), current(0), nodes(new AllocQNode[max])
  139. {
  140.   for (int i = 0; i < n; ++i)
  141.   {
  142.     nodes[i].ptr = 0;
  143.     nodes[i].sz = 0;
  144.   }
  145. }
  146.  
  147. int AllocRing::find(void* p)
  148. {
  149.   if (p == 0) return -1;
  150.  
  151.   for (int i = 0; i < n; ++i)
  152.     if (nodes[i].ptr == p)
  153.       return i;
  154.  
  155.   return -1;
  156. }
  157.  
  158.  
  159. void AllocRing::clear()
  160. {
  161.   for (int i = 0; i < n; ++i)
  162.   {
  163.     if (nodes[i].ptr != 0)
  164.     {
  165.       delete(nodes[i].ptr);
  166.       nodes[i].ptr = 0;
  167.     }
  168.     nodes[i].sz = 0;
  169.   }
  170.   current = 0;
  171. }
  172.  
  173.  
  174. void AllocRing::free(void* p)
  175. {
  176.   int idx = find(p);
  177.   if (idx >= 0)
  178.   {
  179.     delete nodes[idx].ptr;
  180.     nodes[idx].ptr = 0;
  181.   }
  182. }
  183.  
  184. AllocRing::~AllocRing()
  185. {
  186.   clear();
  187. }
  188.  
  189. int AllocRing::contains(void* p)
  190. {
  191.   return find(p) >= 0;
  192. }
  193.  
  194. static inline unsigned int good_size(unsigned int s)
  195. {
  196.   unsigned int req = s + 4;
  197.   unsigned int good = 8;
  198.   while (good < req) good <<= 1;
  199.   return good - 4;
  200. }
  201. #endif
  202.  
  203.     
  204. #ifdef VMS
  205. #include "libgxx-fmtq.cc"    // see Obstack.cc for reason for this
  206. #endif
  207.