home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / segment.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-24  |  3.9 KB  |  158 lines

  1. // $Id: segment.cpp,v 1.8 2001/01/10 16:49:45 mdejong Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1999, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10.  
  11. #include "segment.h"
  12.  
  13. #ifdef    HAVE_JIKES_NAMESPACE
  14. namespace Jikes {    // Open namespace Jikes block
  15. #endif
  16.  
  17. u2 &PairSegment::Image(u2 target)
  18. {
  19.     if (array == NULL)
  20.     {
  21.         for (int i = 0; i < top; i++)
  22.         {
  23.             if (list[i].target == target)
  24.                 return list[i].value;
  25.         }
  26.  
  27.         if (top < (int)LIST_LIMIT)
  28.         {
  29.             int j = top++;
  30.             list[j].target = target;
  31.             list[j].value = 0;
  32.             return list[j].value;
  33.         }
  34.  
  35.         unsigned offset = ((unsigned) target) & MASK;
  36.         array = (u2 *) memset(new u2[BLKSIZE], 0, BLKSIZE * sizeof(u2));
  37.         array -= offset;
  38.  
  39.         for (int j = 0; j < top; j++)
  40.             array[list[j].target] = list[j].value;
  41.     }
  42.  
  43.     return array[target];
  44. }
  45.  
  46.  
  47. u2 &Pair::operator[](const u2 target)
  48. {
  49.     int k = ((unsigned) target) >> PairSegment::LOG_BLKSIZE;
  50.  
  51.     if (k >= base_size)
  52.     {
  53.         int old_base_size = base_size;
  54.         PairSegment **old_base = base;
  55.  
  56.         //
  57.         // For the first allocation, assume that there won't be that many more.
  58.         // If there are others add a much bigger margin.
  59.         //
  60.         base_size = k + (old_base_size == 0 ? 2 : 16);
  61.         base = new PairSegment*[base_size];
  62.  
  63.         if (old_base != NULL)
  64.         {
  65.             memmove(base, old_base, old_base_size * sizeof(PairSegment *));
  66.             delete [] old_base;
  67.         }
  68.  
  69.         memset(&base[old_base_size], 0, (base_size - old_base_size) * sizeof(PairSegment *));
  70.     }
  71.  
  72.     if (! base[k])
  73.         base[k] = segment_pool.AllocatePairSegment();
  74.  
  75.     return base[k] -> Image(target);
  76. }
  77.  
  78.  
  79. Pair &TripletSegment::Image(u2 target)
  80. {
  81.     if (array == NULL)
  82.     {
  83.         for (int i = 0; i < top; i++)
  84.         {
  85.             if (list[i].target == target)
  86.                 return *list[i].value;
  87.         }
  88.  
  89.         if (top < (int)LIST_LIMIT)
  90.         {
  91.             int j = top++;
  92.             list[j].target = target;
  93.             return *(list[j].value = segment_pool.AllocatePair());
  94.         }
  95.  
  96.         unsigned offset = ((unsigned) target) & MASK;
  97.         array = (Pair **) memset(new Pair*[BLKSIZE], 0, BLKSIZE * sizeof(Pair *));
  98.         array -= offset;
  99.  
  100.         for (int j = 0; j < top; j++)
  101.             array[list[j].target] = list[j].value;
  102.     }
  103.  
  104.     return *(array[target] ? array[target] : array[target] = segment_pool.AllocatePair());
  105. }
  106.  
  107.  
  108. u2 &Triplet::Image(const u2 target, const u2 target2)
  109. {
  110.     int k = ((unsigned) target) >> TripletSegment::LOG_BLKSIZE;
  111.  
  112.     if (k >= base_size)
  113.     {
  114.         int old_base_size = base_size;
  115.         TripletSegment **old_base = base;
  116.  
  117.         base_size = k + 4;
  118.         base = new TripletSegment*[base_size];
  119.  
  120.         if (old_base != NULL)
  121.         {
  122.             memmove(base, old_base, old_base_size * sizeof(TripletSegment *));
  123.             delete [] old_base;
  124.         }
  125.  
  126.         memset(&base[old_base_size], 0, (base_size - old_base_size) * sizeof(TripletSegment *));
  127.     }
  128.  
  129.     if (! base[k])
  130.         base[k] = segment_pool.AllocateTripletSegment();
  131.  
  132.     return base[k] -> Image(target)[target2];
  133. }
  134.  
  135.  
  136. SegmentPool::SegmentPool() : triplet_segment_pool(1024),
  137.                              pair_segment_pool(4096),
  138.                              pair_pool(4096)
  139. {}
  140.  
  141.  
  142. SegmentPool::~SegmentPool()
  143. {
  144.     for (int i = 0; i < triplet_segment_pool.Length(); i++)
  145.         delete triplet_segment_pool[i];
  146.  
  147.     for (int j = 0; j < pair_segment_pool.Length(); j++)
  148.         delete pair_segment_pool[j];
  149.  
  150.     for (int k = 0; k < pair_pool.Length(); k++)
  151.         delete pair_pool[k];
  152. }
  153.  
  154. #ifdef    HAVE_JIKES_NAMESPACE
  155. }            // Close namespace Jikes block
  156. #endif
  157.  
  158.