My Project
Loading...
Searching...
No Matches
countedref.cc File Reference
#include "kernel/mod2.h"
#include "ipid.h"
#include "countedref.h"
#include "blackbox.h"
#include "newstruct.h"
#include "ipshell.h"

Go to the source code of this file.

Data Structures

class  CountedRefData
 This class stores a reference counter as well as a Singular interpreter object. More...
 
class  CountedRef
 
class  CountedRefShared
 

Functions

void CountedRefPtr_kill (ring r)
 Overloading ring destruction.
 
void CountedRefPtr_kill (CountedRefData *data)
 Supporting smart pointer CountedRefPtr.
 
void * countedref_Init (blackbox *)
 blackbox support - initialization
 
BOOLEAN countedref_CheckAssign (blackbox *, leftv, leftv)
 We use the function pointer as a marker of reference types for CountedRef::is_ref(leftv), see the latter for details.
 
void countedref_Print (blackbox *, void *ptr)
 blackbox support - convert to string representation
 
char * countedref_String (blackbox *, void *ptr)
 blackbox support - convert to string representation
 
void * countedref_Copy (blackbox *, void *ptr)
 blackbox support - copy element
 
BOOLEAN countedref_Assign (leftv result, leftv arg)
 blackbox support - assign element
 
BOOLEAN countedref_CheckInit (leftv res, leftv arg)
 
BOOLEAN countedref_Op1 (int op, leftv res, leftv head)
 blackbox support - unary operations
 
static BOOLEAN countedref_Op2_ (int op, leftv res, leftv head, leftv arg)
 blackbox support - binary operations (resolve seocnd argument)
 
BOOLEAN countedref_Op2 (int op, leftv res, leftv head, leftv arg)
 
static BOOLEAN countedref_Op3__ (int op, leftv res, leftv head, leftv arg1, leftv arg2)
 
static BOOLEAN countedref_Op3_ (int op, leftv res, leftv head, leftv arg1, leftv arg2)
 
BOOLEAN countedref_Op3 (int op, leftv res, leftv head, leftv arg1, leftv arg2)
 blackbox support - ternary operations
 
void countedref_destroy (blackbox *, void *ptr)
 blackbox support - destruction
 
void * countedref_InitShared (blackbox *)
 Blackbox support - generate initialized, but all-zero - shared data.
 
BOOLEAN countedref_Op1Shared (int op, leftv res, leftv head)
 Blackbox support - unary operation for shared data.
 
BOOLEAN countedref_Op2Shared (int op, leftv res, leftv head, leftv arg)
 blackbox support - binary operations
 
BOOLEAN countedref_OpM (int op, leftv res, leftv args)
 blackbox support - n-ary operations
 
BOOLEAN countedref_AssignShared (leftv result, leftv arg)
 blackbox support - assign element
 
void countedref_destroyShared (blackbox *, void *ptr)
 blackbox support - destruction
 
BOOLEAN countedref_serialize (blackbox *, void *d, si_link f)
 
BOOLEAN countedref_deserialize (blackbox **, void **d, si_link f)
 
void countedref_reference_load ()
 Initialize blackbox types 'reference' and 'shared', or both.
 
void countedref_shared_load ()
 

Detailed Description

Author
Alexander Dreyer
Date
2012-08-15

This file defines reference countes interpreter objects and adds the blackbox operations for high-level types 'reference' and 'shared'.

Note
This works was supported by the "Industrial Algebra" project.
Copyright:
(c) 2012 by The Singular Team, see LICENSE file

Definition in file countedref.cc.

Function Documentation

◆ countedref_Assign()

BOOLEAN countedref_Assign ( leftv result,
leftv arg )

blackbox support - assign element

Definition at line 377 of file countedref.cc.

378{
379 // Case: replace assignment behind reference
380 if (result->Data() != NULL) {
382 return CountedRef::resolve(arg) || ref.assign(result, arg);
383 }
384
385 // Case: copy reference
386 if (result->Typ() == arg->Typ())
387 return CountedRef::cast(arg).outcast(result);
388
389 // Case: new reference
390 if ((arg->rtyp == IDHDL) || CountedRef::is_ref(arg))
391 return CountedRef(arg).outcast(result);
392
393 WerrorS("Can only take reference from identifier");
394 return TRUE;
395}
#define TRUE
Definition auxiliary.h:101
static BOOLEAN resolve(leftv arg)
If necessary dereference.
BOOLEAN outcast(leftv res, int typ)
Construct reference data object marked by given identifier number.
BOOLEAN assign(leftv result, leftv arg)
static BOOLEAN is_ref(leftv arg)
Check whether argument is already a reference type.
static self cast(void *data)
Recover the actual object from raw Singular data.
int Typ()
Definition subexpr.cc:1048
int rtyp
Definition subexpr.h:91
return result
void WerrorS(const char *s)
Definition feFopen.cc:24
#define NULL
Definition omList.c:12
#define IDHDL
Definition tok.h:31

◆ countedref_AssignShared()

BOOLEAN countedref_AssignShared ( leftv result,
leftv arg )

blackbox support - assign element

Case: replace assignment behind reference

Case: new reference to already shared data

Case: new shared data

Definition at line 648 of file countedref.cc.

649{
650 /// Case: replace assignment behind reference
651 if ((result->Data() != NULL) && !CountedRefShared::cast(result).unassigned()) {
653 return CountedRef::resolve(arg) || ref.assign(result, arg);
654 }
655
656 /// Case: new reference to already shared data
657 if (result->Typ() == arg->Typ())
658 {
659 if (result->Data() != NULL)
662 }
663 if(CountedRefShared::cast(result).unassigned())
664 {
666 }
667
668 /// Case: new shared data
669 return CountedRefShared(arg).outcast(result);
670}
static self cast(leftv arg)
Recovering outcasted CountedRefShared object from interpreter object.
void destruct()
Kills a link to the referenced object.

◆ countedref_CheckAssign()

BOOLEAN countedref_CheckAssign ( blackbox * ,
leftv ,
leftv  )

We use the function pointer as a marker of reference types for CountedRef::is_ref(leftv), see the latter for details.

Definition at line 193 of file countedref.cc.

194{
195 return FALSE;
196}
#define FALSE
Definition auxiliary.h:97

◆ countedref_CheckInit()

BOOLEAN countedref_CheckInit ( leftv res,
leftv arg )

Definition at line 397 of file countedref.cc.

398{
399 if (arg->Data() != NULL) return FALSE;
400 res->rtyp = NONE;
401 WerrorS("Noninitialized access");
402 return TRUE;
403}
void * Data()
Definition subexpr.cc:1192
CanonicalForm res
Definition facAbsFact.cc:60
#define NONE
Definition tok.h:223

◆ countedref_Copy()

void * countedref_Copy ( blackbox * ,
void * ptr )

blackbox support - copy element

Definition at line 370 of file countedref.cc.

371{
372 if (ptr) return CountedRef::cast(ptr).outcast();
373 return NULL;
374}

◆ countedref_deserialize()

BOOLEAN countedref_deserialize ( blackbox ** ,
void ** d,
si_link f )

Definition at line 691 of file countedref.cc.

692{
693 // rtyp must be set correctly (to the blackbox id) by routine calling
694 leftv data=f->m->Read(f);
695 CountedRefShared sh(data);
696 *d = sh.outcast();
697 return FALSE;
698}
FILE * f
Definition checklibs.c:9
sleftv * leftv
Definition structs.h:53

◆ countedref_destroy()

void countedref_destroy ( blackbox * ,
void * ptr )

blackbox support - destruction

Definition at line 484 of file countedref.cc.

485{
486 if (ptr) CountedRef::cast(ptr).destruct();
487}

◆ countedref_destroyShared()

void countedref_destroyShared ( blackbox * ,
void * ptr )

blackbox support - destruction

Definition at line 673 of file countedref.cc.

674{
675 if (ptr) CountedRefShared::cast(ptr).destruct();
676}

◆ countedref_Init()

void * countedref_Init ( blackbox * )

blackbox support - initialization

Definition at line 186 of file countedref.cc.

187{
188 return NULL;
189}

◆ countedref_InitShared()

void * countedref_InitShared ( blackbox * )

Blackbox support - generate initialized, but all-zero - shared data.

Definition at line 540 of file countedref.cc.

541{
542 return CountedRefShared().outcast();
543}

◆ countedref_Op1()

BOOLEAN countedref_Op1 ( int op,
leftv res,
leftv head )

blackbox support - unary operations

Definition at line 406 of file countedref.cc.

407{
408 if(op == TYPEOF_CMD)
409 return blackboxDefaultOp1(op, res, head);
410
411 if (countedref_CheckInit(res, head)) return TRUE;
412
413 if ((op == DEF_CMD) || (op == head->Typ()))
414 {
415 res->rtyp = head->Typ();
416 return iiAssign(res, head);
417 }
418
420 return ref.dereference(head) ||
421 iiExprArith1(res, head, op == LINK_CMD? head->Typ(): op);
422}
BOOLEAN blackboxDefaultOp1(int op, leftv l, leftv r)
default procedure blackboxDefaultOp1, to be called as "default:" branch
Definition blackbox.cc:78
CanonicalForm head(const CanonicalForm &f)
BOOLEAN dereference(leftv arg)
Replaces argument by a shallow copy of the references data.
BOOLEAN countedref_CheckInit(leftv res, leftv arg)
BOOLEAN iiExprArith1(leftv res, leftv a, int op)
Definition iparith.cc:9330
BOOLEAN iiAssign(leftv l, leftv r, BOOLEAN toplevel)
Definition ipassign.cc:2097
@ DEF_CMD
Definition tok.h:58
@ LINK_CMD
Definition tok.h:117
@ TYPEOF_CMD
Definition tok.h:196

◆ countedref_Op1Shared()

BOOLEAN countedref_Op1Shared ( int op,
leftv res,
leftv head )

Blackbox support - unary operation for shared data.

Definition at line 546 of file countedref.cc.

547{
548 if(op == TYPEOF_CMD)
549 return blackboxDefaultOp1(op, res, head);
550
551 if (countedref_CheckInit(res, head)) return TRUE;
552
553 if ((op == DEF_CMD) || (op == head->Typ()))
554 {
555 res->rtyp = head->Typ();
556 return iiAssign(res, head);
557 }
558
560
561 if (op == LINK_CMD)
562 {
563 if (ref.dereference(head)) return TRUE;
564 res->Copy(head);
565 return (res->Typ() == NONE);
566 }
567
568 CountedRefShared wrap = ref.wrapid();
569 int typ = head->Typ();
570 return wrap.dereference(head) || iiExprArith1(res, head, op) ||
571 wrap.retrieve(res, typ);
572}
BOOLEAN retrieve(leftv res, int typ)
Recover more information (e.g. subexpression data) from computed result.
self wrapid()
Temporarily wrap with identifier for '[' and '.' operation.

◆ countedref_Op2()

BOOLEAN countedref_Op2 ( int op,
leftv res,
leftv head,
leftv arg )

Definition at line 437 of file countedref.cc.

438{
439 if (countedref_CheckInit(res, head)) return TRUE;
441 {
443 return ref.dereference(head) || countedref_Op2_(op, res, head, arg);
444 }
445 return countedref_Op2_(op, res, head, arg);
446}
static BOOLEAN countedref_Op2_(int op, leftv res, leftv head, leftv arg)
blackbox support - binary operations (resolve seocnd argument)

◆ countedref_Op2_()

static BOOLEAN countedref_Op2_ ( int op,
leftv res,
leftv head,
leftv arg )
static

blackbox support - binary operations (resolve seocnd argument)

Definition at line 427 of file countedref.cc.

428{
429 if (CountedRef::is_ref(arg))
430 {
431 CountedRef ref = CountedRef::cast(arg);
432 return ref.dereference(arg) || iiExprArith2(res, head, op, arg);
433 }
434 return iiExprArith2(res, head, op, arg);
435}
BOOLEAN iiExprArith2(leftv res, leftv a, int op, leftv b, BOOLEAN proccall)
Definition iparith.cc:9141

◆ countedref_Op2Shared()

BOOLEAN countedref_Op2Shared ( int op,
leftv res,
leftv head,
leftv arg )

blackbox support - binary operations

Definition at line 576 of file countedref.cc.

577{
578 if (countedref_CheckInit(res, head)) return TRUE;
579
581 {
583 int typ = head->Typ();
584 return wrap.dereference(head) || countedref_Op2_(op, res, head, arg) ||
585 wrap.retrieve(res, typ);
586 }
587
588 return countedref_Op2_(op, res, head, arg);
589}

◆ countedref_Op3()

BOOLEAN countedref_Op3 ( int op,
leftv res,
leftv head,
leftv arg1,
leftv arg2 )

blackbox support - ternary operations

Definition at line 471 of file countedref.cc.

472{
473 if (countedref_CheckInit(res, head)) return TRUE;
475 {
477 return ref.dereference(head) || countedref_Op3_(op, res, head, arg1, arg2);
478 }
479 return countedref_Op3_(op, res, head, arg1, arg2);
480}
static BOOLEAN countedref_Op3_(int op, leftv res, leftv head, leftv arg1, leftv arg2)

◆ countedref_Op3_()

static BOOLEAN countedref_Op3_ ( int op,
leftv res,
leftv head,
leftv arg1,
leftv arg2 )
static

Definition at line 459 of file countedref.cc.

460{
461 if (CountedRef::is_ref(arg1))
462 {
463 CountedRef ref = CountedRef::cast(arg1);
464 return ref.dereference(arg1) || countedref_Op3__(op, res, head, arg1, arg2);
465 }
466 return countedref_Op3__(op, res, head, arg1, arg2);
467}
static BOOLEAN countedref_Op3__(int op, leftv res, leftv head, leftv arg1, leftv arg2)

◆ countedref_Op3__()

static BOOLEAN countedref_Op3__ ( int op,
leftv res,
leftv head,
leftv arg1,
leftv arg2 )
static

Definition at line 448 of file countedref.cc.

449{
450
451 if (CountedRef::is_ref(arg2))
452 {
453 CountedRef ref = CountedRef::cast(arg2);
454 return ref.dereference(arg2) || iiExprArith3(res, op, head, arg1, arg2);
455 }
456 return iiExprArith3(res, op, head, arg1, arg2);
457}
BOOLEAN iiExprArith3(leftv res, int op, leftv a, leftv b, leftv c)
Definition iparith.cc:9540

◆ countedref_OpM()

BOOLEAN countedref_OpM ( int op,
leftv res,
leftv args )

blackbox support - n-ary operations

Definition at line 592 of file countedref.cc.

593{
594 if (args->Data() == NULL) return FALSE;
595
596 if(op == SYSTEM_CMD)
597 {
598 if (args->next)
599 {
600 leftv next = args->next;
601 args->next = NULL;
602
603 char* name = (next->Typ() == STRING_CMD?
604 (char*) next->Data(): (char*)next->Name());
605 next = next->next;
606
607 if (strcmp(name, "help") == 0)
608 {
609 PrintS("system(<ref>, ...): extended functionality for reference/shared data <ref>\n");
610 PrintS(" system(<ref>, count) - number of references pointing to <ref>\n");
611 PrintS(" system(<ref>, enumerate) - unique number for identifying <ref>\n");
612 PrintS(" system(<ref>, undefined) - checks whether <ref> had been assigned\n");
613 PrintS(" system(<ref>, \"help\") - prints this information message\n");
614 PrintS(" system(<ref>, \"typeof\") - actual type referenced by <ref>\n");
615 PrintS(" system(<ref1>, same, <ref2>) - tests for identic reference objects\n");
617 }
618 if (strncmp(name, "undef", 5) == 0)
619 {
620 return CountedRef::construct(res, args->Data()?
621 (CountedRef::cast(args).unassigned()? 1: 2): 0);
622 }
623
624 CountedRef obj = CountedRef::cast(args);
625 if (next)
626 {
627 if (strcmp(name, "same") == 0) return obj.same(res, next);
628 // likewise may be hard to interprete, so we not not document it above
629 if (strncmp(name, "like", 4) == 0) return obj.likewise(res, next);
630 }
631 if (strncmp(name, "count", 5) == 0) return obj.count(res);
632 if (strncmp(name, "enum", 4) == 0) return obj.enumerate(res);
633 if (strcmp(name, "name") == 0) return obj.name(res); // undecumented
634 if (strncmp(name, "typ", 3) == 0) return obj.type(res);
635 }
636 return TRUE;
637 }
638 if (op == LIST_CMD)
639 {
640 res->rtyp = op;
641 return jjLIST_PL(res, args);
642 }
643 CountedRef ref = CountedRef::cast(args);
644 return ref.dereference(args) || iiExprArithM(res, args, op);
645}
BOOLEAN type(leftv res)
Get type of references data.
BOOLEAN likewise(leftv res, leftv arg)
Check for likewise identifiers.
BOOLEAN enumerate(leftv res)
BOOLEAN same(leftv res, leftv arg)
Check for identical reference objects.
static BOOLEAN construct(leftv res, long data)
Construct integer value.
BOOLEAN name(leftv res)
Get (possibly) internal identifier name.
BOOLEAN count(leftv res)
Get number of references pointing here, too.
leftv next
Definition subexpr.h:86
BOOLEAN iiExprArithM(leftv res, leftv a, int op)
Definition iparith.cc:9631
BOOLEAN jjLIST_PL(leftv res, leftv v)
Definition iparith.cc:8216
ListNode * next
Definition janet.h:31
void PrintS(const char *s)
Definition reporter.cc:284
int name
New type name for int.
@ LIST_CMD
Definition tok.h:118
@ STRING_CMD
Definition tok.h:187
@ SYSTEM_CMD
Definition tok.h:189

◆ countedref_Print()

void countedref_Print ( blackbox * ,
void * ptr )

blackbox support - convert to string representation

Definition at line 356 of file countedref.cc.

357{
358 if (ptr) (*CountedRef::cast(ptr))->Print();
359 else PrintS("<unassigned reference or shared memory>");
360}
#define Print
Definition emacs.cc:80

◆ countedref_reference_load()

void countedref_reference_load ( )

Initialize blackbox types 'reference' and 'shared', or both.

Definition at line 700 of file countedref.cc.

701{
702 int tok;
703 if (blackboxIsCmd("reference", tok) == ROOT_DECL)
704 return;
705
706 blackbox *bbx = (blackbox*)omAlloc0(sizeof(blackbox));
707 bbx->blackbox_CheckAssign = countedref_CheckAssign;
708 bbx->blackbox_destroy = countedref_destroy;
709 bbx->blackbox_String = countedref_String;
710 bbx->blackbox_Print = countedref_Print;
711 bbx->blackbox_Init = countedref_Init;
712 bbx->blackbox_Copy = countedref_Copy;
713 bbx->blackbox_Assign = countedref_Assign;
714 bbx->blackbox_Op1 = countedref_Op1;
715 bbx->blackbox_Op2 = countedref_Op2;
716 bbx->blackbox_Op3 = countedref_Op3;
717 bbx->blackbox_OpM = countedref_OpM;
718 bbx->blackbox_serialize = countedref_serialize;
719 bbx->blackbox_deserialize = countedref_deserialize;
720 bbx->data = omAlloc0(newstruct_desc_size());
721 setBlackboxStuff(bbx, "reference");
722}
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition blackbox.cc:143
int blackboxIsCmd(const char *n, int &tok)
used by scanner: returns ROOT_DECL for known types (and the type number in tok)
Definition blackbox.cc:219
char * countedref_String(blackbox *, void *ptr)
blackbox support - convert to string representation
BOOLEAN countedref_serialize(blackbox *, void *d, si_link f)
void countedref_Print(blackbox *, void *ptr)
blackbox support - convert to string representation
BOOLEAN countedref_deserialize(blackbox **, void **d, si_link f)
BOOLEAN countedref_OpM(int op, leftv res, leftv args)
blackbox support - n-ary operations
BOOLEAN countedref_Op3(int op, leftv res, leftv head, leftv arg1, leftv arg2)
blackbox support - ternary operations
BOOLEAN countedref_Assign(leftv result, leftv arg)
blackbox support - assign element
BOOLEAN countedref_Op2(int op, leftv res, leftv head, leftv arg)
void * countedref_Init(blackbox *)
blackbox support - initialization
void countedref_destroy(blackbox *, void *ptr)
blackbox support - destruction
BOOLEAN countedref_Op1(int op, leftv res, leftv head)
blackbox support - unary operations
BOOLEAN countedref_CheckAssign(blackbox *, leftv, leftv)
We use the function pointer as a marker of reference types for CountedRef::is_ref(leftv),...
void * countedref_Copy(blackbox *, void *ptr)
blackbox support - copy element
@ ROOT_DECL
Definition grammar.cc:320
int newstruct_desc_size()
Definition newstruct.cc:42
#define omAlloc0(size)

◆ countedref_serialize()

BOOLEAN countedref_serialize ( blackbox * ,
void * d,
si_link f )

Definition at line 679 of file countedref.cc.

680{
681 sleftv l;
682 memset(&l,0,sizeof(l));
683 l.rtyp = STRING_CMD;
684 l.data = (void*)omStrDup("shared"); // references are converted
685 f->m->Write(f, &l);
687 f->m->Write(f, &l);
688 return FALSE;
689}
int l
Definition cfEzgcd.cc:100
Class used for (list of) interpreter objects.
Definition subexpr.h:83
#define omStrDup(s)

◆ countedref_shared_load()

void countedref_shared_load ( )

Definition at line 724 of file countedref.cc.

725{
726 int tok;
727 if (blackboxIsCmd("shared", tok) == ROOT_DECL)
728 return;
729
730 blackbox *bbxshared = (blackbox*)omAlloc0(sizeof(blackbox));
731 bbxshared->blackbox_String = countedref_String;
732 bbxshared->blackbox_Print = countedref_Print;
733 bbxshared->blackbox_Copy = countedref_Copy;
734 bbxshared->blackbox_Op3 = countedref_Op3;
735 bbxshared->blackbox_OpM = countedref_OpM;
736 bbxshared->blackbox_serialize = countedref_serialize;
737 bbxshared->blackbox_deserialize = countedref_deserialize;
738
739 bbxshared->blackbox_CheckAssign = countedref_CheckAssign;
740 bbxshared->blackbox_Assign = countedref_AssignShared;
741 bbxshared->blackbox_destroy = countedref_destroyShared;
742 bbxshared->blackbox_Op1 = countedref_Op1Shared;
743 bbxshared->blackbox_Op2 = countedref_Op2Shared;
744 bbxshared->blackbox_Init = countedref_InitShared;
745 bbxshared->data = omAlloc0(newstruct_desc_size());
746 setBlackboxStuff(bbxshared, "shared");
747}
BOOLEAN countedref_Op2Shared(int op, leftv res, leftv head, leftv arg)
blackbox support - binary operations
void countedref_destroyShared(blackbox *, void *ptr)
blackbox support - destruction
void * countedref_InitShared(blackbox *)
Blackbox support - generate initialized, but all-zero - shared data.
BOOLEAN countedref_AssignShared(leftv result, leftv arg)
blackbox support - assign element
BOOLEAN countedref_Op1Shared(int op, leftv res, leftv head)
Blackbox support - unary operation for shared data.

◆ countedref_String()

char * countedref_String ( blackbox * ,
void * ptr )

blackbox support - convert to string representation

Definition at line 363 of file countedref.cc.

364{
365 if (ptr == NULL) return omStrDup(sNoName_fe);
366 return (*CountedRef::cast(ptr))->String();
367}
const char sNoName_fe[]
Definition fevoices.cc:57

◆ CountedRefPtr_kill() [1/2]

void CountedRefPtr_kill ( CountedRefData * data)
inline

Supporting smart pointer CountedRefPtr.

Definition at line 182 of file countedref.cc.

182{ delete data; }

◆ CountedRefPtr_kill() [2/2]

void CountedRefPtr_kill ( ring r)
inline

Overloading ring destruction.

Definition at line 34 of file countedref.cc.

34{ rKill(r); }
void rKill(ring r)
Definition ipshell.cc:6174