"Fossies" - the Fresh Open Source Software Archive

Member "ncc-2.8/mem_pool.h" (13 Dec 2004, 2283 Bytes) of package /linux/privat/old/ncc-2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 //--------------------------------------------------------------------------
    2 //
    3 //  arrays of automatically increasing size.
    4 //
    5 //  earray<X>   for few elements, reallocation + memcpy every 64
    6 //  mem_pool<X> for up to a LOT of elements (64*128*256)
    7 //
    8 //-------------------------------------------------------------------------
    9 
   10 #ifndef mem_pool_INCLUDED
   11 #define mem_pool_INCLUDED
   12 #include <string.h>
   13 
   14 #define TXX template<class X>
   15 
   16 TXX class mem_pool
   17 {
   18 #define PSEG 256
   19 #define NSEG 128
   20 #define TSEG 64
   21     X ***p;
   22     int sp;
   23    public:
   24     mem_pool ();
   25     int alloc ();
   26 inline  X   &operator [] (int);
   27     void    pop ();
   28     int nr ()   { return sp; }
   29     void    copy (X**);
   30     void    destroy ();
   31 };
   32 
   33 #define I1A(x) (x / (PSEG*NSEG))
   34 #define I2A(x) ((x % (PSEG*NSEG)) / PSEG)
   35 #define I3A(x) ((x % (PSEG*NSEG)) % PSEG)
   36 
   37 TXX mem_pool<X>::mem_pool ()
   38 {
   39     sp = 0;
   40     p = new X** [TSEG];
   41 }
   42 
   43 TXX int mem_pool<X>::alloc ()
   44 {
   45     if (sp % (PSEG*NSEG) == 0)
   46         p [I1A (sp)] = new X* [NSEG];
   47     if (sp % PSEG == 0) {
   48         p [I1A (sp)] [I2A (sp)] = new X [PSEG];
   49         memset (p [I1A (sp)] [I2A (sp)], 0, PSEG * sizeof (X));
   50     }
   51     return sp++;
   52 }
   53 
   54 TXX X &mem_pool<X>::operator [] (int i)
   55 {
   56     return p [I1A (i)] [I2A (i)] [I3A (i)];
   57 }
   58 
   59 TXX void mem_pool<X>::pop ()
   60 {
   61     if (sp) sp--;
   62 }
   63 
   64 TXX void mem_pool<X>::copy (X **di)
   65 {
   66     int i;
   67     X *d;
   68     if (sp == 0) return;
   69     if (*di == NULL) *di = new X [sp];
   70     d = *di;
   71     for (i = 0; i + PSEG < sp; i += PSEG)
   72         memcpy (&d [i], p [I1A(i)][I2A(i)], PSEG * sizeof (X));
   73     memcpy (&d [i], p [I1A(i)][I2A(i)], (sp - i) * sizeof (X));
   74 }
   75 
   76 TXX void mem_pool<X>::destroy ()
   77 {
   78     int i;
   79     for (i = 0; i < sp; i += PSEG)
   80         delete [] p [I1A (i)] [I2A (i)];
   81     for (i = 0; i < sp; i += PSEG*NSEG)
   82         delete [] p [I1A (i)];
   83     delete [] p;
   84 }
   85 
   86 TXX class earray
   87 {
   88 #define SSEG 64
   89     int ms;
   90     X *Realloc (int);
   91    public:
   92     int nr;
   93     X *x;
   94     earray ();
   95 inline  int alloc ();
   96     void freeze ();
   97     void erase ();
   98 };
   99 
  100 TXX earray<X>::earray ()
  101 {
  102     ms = nr = 0;
  103     x = NULL;
  104 }
  105 
  106 TXX X *earray<X>::Realloc (int s)
  107 {
  108     X *r = new X [s];
  109     if (x) {
  110         memcpy (r, x, nr * sizeof (X));
  111         delete [] x;
  112     }
  113     return x = r;
  114 }
  115 
  116 TXX int earray<X>::alloc ()
  117 {
  118     if (ms == nr)
  119         x = Realloc (ms += SSEG);
  120     return nr++;
  121 }
  122 
  123 TXX void earray<X>::freeze ()
  124 {
  125     if (nr) x = Realloc (ms = nr);
  126 }
  127 
  128 TXX void earray<X>::erase ()
  129 {
  130     if (x) delete [] x;
  131     x = NULL;
  132     ms = nr = 0;
  133 }
  134 #endif