Changeset d2a236 in git for Singular/countedref.cc


Ignore:
Timestamp:
Aug 21, 2012, 12:52:56 PM (12 years ago)
Author:
Alexander Dreyer <alexander.dreyer@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
180efc06f46eb5c71b54411dc6be57f125ad2ca0
Parents:
fc70ace7d1e2b805c813bfe25f3dc25f2670531a
git-author:
Alexander Dreyer <alexander.dreyer@itwm.fraunhofer.de>2012-08-21 12:52:56+02:00
git-committer:
Oleksandr Motsak <motsak@mathematik.uni-kl.de>2012-09-05 15:52:15+02:00
Message:
fix: handling unassigned references, new class for handling local identifiers
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/countedref.cc

    rfc70ac rd2a236  
    3636  typedef CountedRefEnv self;
    3737
    38 
    39 
    40 };
    41 idhdl* getmyroot() {
    42   static idhdl myroot = NULL;
    43   if (myroot == NULL) {
    44     myroot = enterid(" _shared_data_ ", 0, PACKAGE_CMD, &IDROOT, TRUE);
    45   }
    46   return &myroot;
    47 }
     38public:
     39
     40  static idhdl* locals() {
     41    static idhdl myroot = NULL;
     42    if (myroot == NULL) {
     43      myroot = enterid(" _shared_data_ ", 0, PACKAGE_CMD, &IDROOT, TRUE);
     44    }
     45    return &myroot;
     46  }
     47
     48  static idhdl newid(int typ, void* data) {
     49    char* name = (char*)omAlloc0(512);
     50    static unsigned int counter = 0;
     51    sprintf(name, " :%u:%p:_shared_: ", ++counter, data);
     52
     53    idhdl* root = locals();
     54    assume((*root)->get(name, 0) == NULL);
     55    short ref = (*root)->ref;
     56    *root = (*root)->set(name, 0, typ, FALSE);
     57    (*root)->ref = ++ref;;
     58
     59    IDDATA(*root) = (char*) data;
     60
     61    return *root;
     62  }
     63
     64  static void erase(idhdl handle) {
     65
     66    idhdl* root = locals();
     67    short ref = (*root)->ref;
     68    killhdl2(handle, root, currRing);
     69    (*root)->ref = --ref;
     70    if(ref > 0) { return;}
     71
     72    killhdl2(*root, &IDROOT, currRing);
     73    (*root) = NULL;
     74 }
     75};
     76
    4877
    4978class RefCounter {
     
    205234  /// @note Sergio Leone memorial function
    206235  BOOLEAN broken() {
    207     if( (m_context == getmyroot()) || (m_context == &currRing->idroot))
     236    if( (m_context == CountedRefEnv::locals()) || (m_context == &currRing->idroot))
    208237      return FALSE;                  // the good,
    209238
     
    347376void countedref_Print(blackbox *b, void* ptr)
    348377{
    349   if (ptr == NULL)  return;
    350   (*CountedRef::cast(ptr))->Print();
     378  if (ptr) (*CountedRef::cast(ptr))->Print();
     379  else PrintS("<unassigned reference or shared memory>");
    351380}
    352381
     
    354383char* countedref_String(blackbox *b, void* ptr)
    355384{
    356   if (ptr == NULL) return NULL;
     385  if (ptr == NULL) return omStrDup(sNoName);
    357386  return (*CountedRef::cast(ptr))->String();
    358387}
     
    388417BOOLEAN countedref_Op1(int op, leftv res, leftv head)
    389418{
     419  if (head->Data() == NULL) return FALSE;
    390420  if(op == TYPEOF_CMD)
    391421    return blackboxDefaultOp1(op, res, head);
     
    398428BOOLEAN countedref_Op2(int op, leftv res, leftv head, leftv arg)
    399429{
    400 
     430  if (head->Data() == NULL) return FALSE;
    401431  return CountedRef::cast(head).dereference(head) || CountedRef::resolve(arg) ||
    402432    iiExprArith2(res, head, op, arg);
     
    406436BOOLEAN countedref_Op3(int op, leftv res, leftv head, leftv arg1, leftv arg2)
    407437{
     438  if (head->Data() == NULL) return FALSE;
    408439  return  CountedRef::cast(head).dereference(head) ||
    409440    CountedRef::resolve(arg1) || CountedRef::resolve(arg2) ||
     
    415446BOOLEAN countedref_OpM(int op, leftv res, leftv args)
    416447{
     448  if (args->Data() == NULL) return FALSE;
    417449  return CountedRef::cast(args).dereference(args) || iiExprArithM(res, args, op);
    418450}
     
    431463public:
    432464  /// Construct new reference from Singular data 
    433   CountedRefShared(leftv arg):  base(new data_type(wrap(arg), getmyroot())) { }
     465  CountedRefShared(leftv arg):  base(new data_type(wrap(arg), CountedRefEnv::locals())) { }
    434466
    435467private:
     
    451483  /// Replace data that reference is pointing to
    452484  self& operator=(leftv rhs) {
    453     m_data->set(wrap(rhs), getmyroot());
     485    m_data->set(wrap(rhs), CountedRefEnv::locals());
    454486    return *this;
    455487  }
     
    464496
    465497  static leftv wrap(leftv arg) {
    466       char* name = (char*)omAlloc0(512);
    467       static unsigned int counter = 0;
    468       idhdl* myroot=getmyroot();
    469       sprintf(name, " :%u:%p:_shared_: ", ++counter, arg->Data());
    470       assume((*myroot)->get(name, 0) == NULL);
    471       idhdl handle = (*myroot)->set(name, 0, arg->Typ(), FALSE);
    472       ++(*myroot)->ref;
    473 
    474       IDDATA(handle) = (char*) arg->CopyD();
    475       arg->CleanUp();
    476       arg->data = handle;
    477       arg->rtyp = IDHDL;
    478       arg->name = name;
    479 
     498    idhdl handle = CountedRefEnv::newid(arg->Typ(), arg->CopyD());
     499    arg->CleanUp();
     500    arg->data = handle;
     501    arg->rtyp = IDHDL;
    480502    return arg;
    481503  }
     
    483505 void kill() {
    484506   if (m_data->count() > 1) return;
    485 
     507   
    486508   LeftvShallow data = base::operator*();
    487    idhdl* myroot = getmyroot();
    488    killhdl2((idhdl)(data->data), myroot, currRing);
     509   CountedRefEnv::erase((idhdl)data->data);
    489510   data->data = NULL;
    490511   data->rtyp = NONE;
    491    
    492    if(--((*myroot)->ref)) {
    493      killhdl2(*myroot, &IDROOT, currRing);
    494      (*myroot) = NULL;
    495    }
    496512 }
    497513};
Note: See TracChangeset for help on using the changeset viewer.