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

📄 tjmemorymanager1.java.txt

📁 智能卡多应用一直是业界的重要课题
💻 TXT
字号:
/**
 *        Title:  Memory Management Routines
 *
 *      Project:  Universal Application Specification v1.0 for PBOCv2
 *
 *    Copyright:  Copyright (c) CUP 2005
 *      Company:  China Unionpay Co., Ltd
 *
 *  Platform   :  Java virtual machine
 *  Language   :  1.3.0-C
 *  Devl tool  :  Borland (c) JBuilder 4.0 *
 *  author     :  Meng Hongwen<hwmeng@chinaunionpay.com>
 *  version 1.0
 */

package cupUAPP;

import javacard.framework.*;

public class tjMemoryManager {
    private boolean  bReady;

    private Object[]  mArray;
    protected byte[]  vector;

    public short    remain;    // public for debug
    //------------------------------------------------
    public tjMemoryManager() {
        bReady = false;
        remain = (short)0;
    }
    //------------------------------------------------
    public boolean reset()
    {
       return bReady;
    }
    //------------------------------------------------
    public void initlize()
    {
        short i;
        TFreeCell  pfc;

        // alloc memory for storage
        vector = new byte[constdef.ct_memory_size];
        if ( vector == null ) return;

        mArray = new Object[constdef.ct_freespacecells];
        if (mArray==null) return;
        remain = constdef.ct_memory_size;

        pfc = new TFreeCell();
        if (pfc==null)  return;

        pfc.used = true;
        pfc.address.offset = 0;
        pfc.address.length = remain;
        pfc.tailer = remain;

        mArray[0] = pfc;

        for(i=1; i<constdef.ct_freespacecells;i++) {
           pfc = new TFreeCell();
           if (pfc==null)  return;
           pfc.used = false;
           mArray[i] = pfc;
        }

        bReady = true;
    }
    //------------------------------------------------
    private boolean  mergeblock(short fca,short fcb)
    {
       TFreeCell pfca, pfcb;

       pfca = (TFreeCell)mArray[fca];
       pfcb = (TFreeCell)mArray[fcb];

       if (pfca.tailer == pfcb.address.offset) {
          pfca.address.length += pfcb.address.length;
          pfca.tailer += pfcb.address.length;
          pfcb.used = false;
          return true;
       } else if (pfcb.tailer == pfca.address.offset) {
          pfca.address.offset = pfcb.address.offset;
          pfca.tailer = (short)(pfcb.tailer + pfca.address.length);
          pfca.address.length += pfcb.address.length;
          pfcb.used = false;
          return true;
       }
       return false;
    }
    //------------------------------------------------
    public void  JCompress()
    {
       boolean   bMerge,bQuit = false;
       short     uscurrent,uscursor;

       for(uscurrent=0; !bQuit&& (uscurrent<constdef.ct_freespacecells); uscurrent++ )
          for(uscursor=(short)(uscurrent+1); uscursor<constdef.ct_freespacecells; uscursor++ ) {
             if ( ((TFreeCell)mArray[uscurrent]).used && ((TFreeCell)mArray[uscursor]).used ) {
                bMerge = mergeblock(uscurrent,uscursor);
                if (bMerge) { // reset
                   uscurrent=0;
                   break;
                }
             } else bQuit = true;
          }
    }
    //------------------------------------------------
    public TAddress JMalloc(short size)
    {
       short  i;
       TAddress   pSpace;

       JCompress();

       // search free space
       for(i=1; i<constdef.ct_freespacecells; i++ )
          if (((TFreeCell)mArray[i]).used && (((TFreeCell)mArray[i]).address.length >= size) ) break;

       if (i>=constdef.ct_freespacecells) {
          if ( ((TFreeCell)mArray[0]).used && (((TFreeCell)mArray[0]).address.length >= size) )i=0;
          else return null;
       }

       pSpace = new TAddress();

       pSpace.offset = ((TFreeCell)mArray[i]).address.offset;
       pSpace.length = size;

       ((TFreeCell)mArray[i]).address.length -= size;
       if ( ((TFreeCell)mArray[i]).address.length == 0) {
           ((TFreeCell)mArray[i]).used = false;
       } else {
           ((TFreeCell)mArray[i]).address.offset += size;
       }

       remain -= size;
       return pSpace;
    }
    //------------------------------------------------
    public boolean  JFree( TAddress pAdd )
    {
       boolean    rc = false;
       short      uscurrent,uscursor,usfreeTailer, usEmptyLocation;
       TFreeCell  fcCurrent;

       usEmptyLocation = constdef.ct_freespacecells;
       usfreeTailer = (short)(pAdd.offset + pAdd.length);
       for(uscurrent=0; uscurrent<constdef.ct_freespacecells; uscurrent++ ) {

           fcCurrent = (TFreeCell)mArray[uscurrent];

          // nearby ?
          if (fcCurrent.used) {
             if (!rc) {
                if ( fcCurrent.address.offset == usfreeTailer ) { // above ?
                   fcCurrent.address.offset = pAdd.offset;
                   fcCurrent.tailer = (short)(usfreeTailer + fcCurrent.address.length);
                   fcCurrent.address.length += pAdd.length;
                   remain += pAdd.length;
                   rc = true;
                } else if ( fcCurrent.tailer == pAdd.offset ) { // below ?
                   fcCurrent.address.length += pAdd.length;
                   fcCurrent.tailer += pAdd.length;
                   remain += pAdd.length;
                   rc = true;
                }

                // mini merge case
                if (rc) {
                   // backward
                   uscursor = (short)(uscurrent-1);
                   while( uscursor>0 && !((TFreeCell)mArray[uscursor]).used) uscursor--;
                   if (uscursor>=0 && ((TFreeCell)mArray[uscursor]).used )
                       mergeblock(uscursor,uscurrent);

                   // forward
                   uscursor = (short)(uscurrent+1);
                   while( uscursor < constdef.ct_freespacecells && !((TFreeCell)mArray[uscursor]).used) uscursor++;
                   if (uscursor < constdef.ct_freespacecells && ((TFreeCell)mArray[uscursor]).used )
                      mergeblock(uscurrent,uscursor);
                } // end rc

             } // end !rc
          } else if ( uscurrent < usEmptyLocation ) usEmptyLocation = uscurrent;

          if (rc) break;

       } // end for

       if ( !rc )
          if ( usEmptyLocation <constdef.ct_freespacecells) { //add new cell
             fcCurrent = (TFreeCell)mArray[usEmptyLocation];
             fcCurrent.address.offset = pAdd.offset;
             fcCurrent.address.length = pAdd.length;
             fcCurrent.used = true;
             fcCurrent.tailer = usfreeTailer;
             remain += pAdd.length;
             rc = true;
          }

       return rc;
    }
    //------------------------------------------------
}

⌨️ 快捷键说明

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