## modules/up/src/gnug++/AllocRing.cc

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. AllocRing
2. find
3. clear
4. free
5. AllocRing
6. contains
7. good_size
8. alloc

       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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
      17 */
      18
      19 #ifdef __GNUG__
      20 #pragma implementation
      21 #endif
      22 #include <std.h>
      23 #include <AllocRing.h>
      24 #include <new.h>
      25
      26 AllocRing::AllocRing(int max)
         /* [<][>][^][v][top][bottom][index][help] */
      27   :nodes(new AllocQNode[max]), n(max), current(0)
      28 {
      29   for (int i = 0; i < n; ++i)
      30   {
      31     nodes[i].ptr = 0;
      32     nodes[i].sz = 0;
      33   }
      34 }
      35
      36 int AllocRing::find(void* p)
         /* [<][>][^][v][top][bottom][index][help] */
      37 {
      38   if (p == 0) return -1;
      39
      40   for (int i = 0; i < n; ++i)
      41     if (nodes[i].ptr == p)
      42       return i;
      43
      44   return -1;
      45 }
      46
      47
      48 void AllocRing::clear()
         /* [<][>][^][v][top][bottom][index][help] */
      49 {
      50   for (int i = 0; i < n; ++i)
      51   {
      52     if (nodes[i].ptr != 0)
      53     {
      54       delete(nodes[i].ptr);
      55       nodes[i].ptr = 0;
      56     }
      57     nodes[i].sz = 0;
      58   }
      59   current = 0;
      60 }
      61
      62
      63 void AllocRing::free(void* p)
         /* [<][>][^][v][top][bottom][index][help] */
      64 {
      65   int idx = find(p);
      66   if (idx >= 0)
      67   {
      68     delete nodes[idx].ptr;
      69     nodes[idx].ptr = 0;
      70   }
      71 }
      72
      73 AllocRing::~AllocRing()
         /* [<][>][^][v][top][bottom][index][help] */
      74 {
      75   clear();
      76 }
      77
      78 int AllocRing::contains(void* p)
         /* [<][>][^][v][top][bottom][index][help] */
      79 {
      80   return find(p) >= 0;
      81 }
      82
      83 static inline unsigned int good_size(unsigned int s)
         /* [<][>][^][v][top][bottom][index][help] */
      84 {
      85   unsigned int req = s + 4;
      86   unsigned int good = 8;
      87   while (good < req) good <<= 1;
      88   return good - 4;
      89 }
      90
      91 void* AllocRing::alloc(int s)
         /* [<][>][^][v][top][bottom][index][help] */
      92 {
      93   unsigned int size = good_size(s);
      94
      95   void* p;
      96   if (nodes[current].ptr != 0 &&
      97       nodes[current].sz >= int(size) &&
      98       nodes[current].sz < int(4 * size))
      99     p = nodes[current].ptr;
     100   else
     101   {
     102     if (nodes[current].ptr != 0) operator delete (nodes[current].ptr);
     103     p = operator new (size);
     104     nodes[current].ptr = p;
     105     nodes[current].sz = size;
     106   }
     107   ++current;
     108   if (current >= n) current = 0;
     109   return p;
     110 }

/* [<][>][^][v][top][bottom][index][help] */