⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storage.cpp

📁 eC++编译器源码
💻 CPP
字号:
#pragma Storage
//#define CONCURRENT
#include <SYSTEM.H>
#include <BitMap.H>
#if defined(CONCURRENT)
  #include <Atomic.H>
#endif
#include <Errors.H>
#include <InOut.h>

const int MAXBYTES=32767;
const unsigned int ROUND =4;

typedef unsigned int CARDINAL;
typedef struct{
     unsigned int p[MAXBYTES/(ROUND*16)];
     long where,topBit;
     unsigned int start;
     }Map;
typedef Map *pMap;


pMap map[128];
unsigned int i,top,current;
Map p[50];
#if defined(CONCURRENT)
Semaphore s;
#endif

void allocate(long &addr,long words)
//CODE 247C; 4C;
code 0xa704;
};


void alloc(ADDRESS &a,unsigned int size,unsigned int index) /* in TSIZE units */
{
     int i;
     pMap pm;
     pm = map[index];  /*assumes indices are scanned&allocated 0 to top*/
     if (pm->where==0L)
     {
          allocate(pm->where,LONG(HIGH(p[0].p)+1)*16L*LONG(ROUND));
          if (pm->where==0L)
               SoftwareError("runtime Storage error");
          if (pm->where<0L)
          {
               pm->topBit = 0x80000000L; 
               DEC(pm->where,0x80000000L);
          }
          else
               pm->topBit = 0L;
     
          i = index-1;
          while(true)
          {
               if (pm->topBit == map[i]->topBit) {
                    if (pm->where >= map[i]->where)
                         break;
               } else {
                    if (pm->topBit > map[i]->topBit)
                         break;
               };
               map[i+1] = map[i];
               DEC(i);
               if (i < 0)
                    break;
          }
          map[i+1] = pm;  /*insert to preserve ascending order*/
          INC(top);
     }

     i = GetNBit(pm->p,0,size);                       /*RPC08-89*/
     if (i >= 0)
          pm->start = CARDINAL(i)+size;                   /*RPC08-89*/
     else
     {
          a = NULL;
          return;
     }
     a=ADDRESS(pm->where+pm->topBit+LONG(i)*LONG(ROUND*2));
};


void ALLOCATE(ADDRESS &a,unsigned int size) /* IN TSIZE UNITS */
{
     int i;
     unsigned int j;
     unsigned int k;
     pMap m;
          
     m=map[current];
     j=(size+(ROUND-1))/ROUND;
     if (j == 1)
     {
#if defined(CONCURRENT)
          P(s);
#endif
          i = GetBit(m->p,m->start);                       /*RPC08-89*/
          if (i >= 0)
              m->start =i+1;              /*RPC08-89*/
#if defined(CONCURRENT)
          V(s);
#endif
     }
     else
     {
#if defined(CONCURRENT)
          P(s);
#endif
          i = GetNBit(m->p, m->start, j);                  /*RPC08-89*/
          if (i >= 0)
               m->start =CARDINAL(i)+j;                /*RPC08-89*/
#if defined(CONCURRENT)
          V(s);
#endif
     }

     if (i < 0)
     {
#if defined(CONCURRENT)
          P(s);
#endif
          i = GetNBit(m->p,0,j);                      /*RPC08-89*/
          if (i >= 0)
               m->start =CARDINAL(i)+j;             /*RPC08-89*/
          else
          {
               for(k=0;k<=HIGH(p);k++)
               {
                    if (k != current) {
                        alloc(a, j, k);
                        if (a != NULL) {
                          current = k;
#if defined(CONCURRENT)
                          V(s);
#endif
                          return;
                        };
                    }   //if
               }/*for*/
               SoftwareError("Storage.ALLOCATE:out of space"); 
          }
#if defined(CONCURRENT)
          V(s);
#endif
     }
     a = ADDRESS(m->where+m->topBit+LONG(i)*LONG(ROUND*2));
};
                                                                              


void DEALLOCATE(ADDRESS &a,unsigned int size)
{
     int i;
     unsigned int j;
     pMap p;
     long topBit;

     i = top-1;
     if (long(a) < 0L)
     {
          topBit = 0x80000000L;
          a = ADDRESS(long(a)-0x80000000L);
     }
     else
          topBit = 0L;

     while(true)
     {
          if ((i < 0) || (topBit > map[i]->topBit) || (long(a) >= map[i]->where))
               break;
          DEC(i);
     }

     if (i < 0)
     {
          WriteLongHex(long(a),12);
          WriteLn();
          SoftwareError("no match addr for Storage.DEALLOCATE");
     }  
     p = map[i];
     i = TRUNC((long(a)+topBit - p->topBit - p->where)/LONG(ROUND*2));
     if (i < 0)
     {
          WriteLongHex(long(a),12);
          WriteLn();
          SoftwareError("bad addr for Storage.DEALLOCATE");
     }
     a = NULL;
     j = (size+(ROUND-1))/ROUND;
#if defined(CONCURRENT)
     P(s);
#endif
     if (j == 1)
          FreeBit(p->p, i);
     else
          FreeNBit(p->p, i, j);
#if defined(CONCURRENT)
     V(s);
#endif
};

void Storage()
{
#if defined(CONCURRENT)
     InitSemaphore(s, 0);
#endif
     top = 1;
     current = 0;
     for (i=0;i<=HIGH(p);i++)
     {
         map[i] = ADR(p[i]);
         p[i].where = 0L;
         p[i].start = 0;
         FreeMap(p[i].p);
     }
     allocate(p[0].where,LONG(HIGH(p[0].p)+1)*16L*LONG(ROUND));
     if (p[0].where == 0L)
          SoftwareError("runtime Storage error");

     if (p[0].where < 0L)
     {
          p[0].topBit = 0x80000000L; 
          DEC(p[0].where,0x80000000L);
     }
     else
          p[0].topBit = 0L;
#if defined(CONCURRENT)
     V(s);
#endif
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -