source: git/Singular/ipid.cc @ 1059a1

spielwiese
Last change on this file since 1059a1 was 1059a1, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: memory leak git-svn-id: file:///usr/local/Singular/svn/trunk@8746 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 22.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ipid.cc,v 1.72 2005-10-24 11:54:53 Singular Exp $ */
5
6/*
7* ABSTRACT: identfier handling
8*/
9
10#include <string.h>
11
12#include "mod2.h"
13#include "static.h"
14#include "omalloc.h"
15#include "tok.h"
16#include "ipshell.h"
17#include "intvec.h"
18#include "febase.h"
19#include "numbers.h"
20#include "polys.h"
21#include "ring.h"
22#include "ideals.h"
23#include "matpol.h"
24#include "lists.h"
25#include "attrib.h"
26#include "silink.h"
27#include "syz.h"
28#include "ipid.h"
29
30#ifdef HAVE_DYNAMIC_LOADING
31#include "mod_raw.h"
32#endif /* HAVE_DYNAMIC_LOADING */
33
34omBin sip_command_bin = omGetSpecBin(sizeof(sip_command));
35omBin ip_command_bin = omGetSpecBin(sizeof(ip_command));
36omBin sip_package_bin = omGetSpecBin(sizeof(sip_package));
37omBin ip_package_bin = omGetSpecBin(sizeof(ip_package));
38omBin idrec_bin = omGetSpecBin(sizeof(idrec));
39
40proclevel *procstack=NULL;
41#define TEST
42idhdl idroot = NULL;
43
44#ifdef HAVE_NS
45idhdl currPackHdl = NULL;
46idhdl basePackHdl = NULL;
47package currPack =NULL;
48package basePack =NULL;
49#endif /* HAVE_NS */
50idhdl currRingHdl = NULL;
51ring  currRing = NULL;
52ideal currQuotient = NULL;
53char* iiNoName="_";
54
55void paCleanUp(package pack);
56
57/*0 implementation*/
58
59idhdl idrec::get(const char * s, int lev)
60{
61  assume(s!=NULL);
62  assume((lev>=0) && (lev<1000)); //not really, but if it isnt in that bounds..
63  idhdl h = this;
64  idhdl found=NULL;
65  int l;
66  char *id;
67  if (s[1]=='\0')
68  {
69    while (h!=NULL)
70    {
71      omCheckAddr(IDID(h));
72// =============================================================
73#if 0
74// timings: ratchwum: 515 s, wilde13: 373 s, nepomuck: 267 s, lukas 863 s
75    id=IDID(h);
76    l=IDLEV(h);
77    if ((l==0) && (*(short *)s==*(short *)id) && (0 == strcmp(s+1,id+1)))
78    {
79      found=h;
80    }
81    else if ((l==lev) && (*(short *)s==*(short *)id) && (0 == strcmp(s+1,id+1)))
82    {
83      return h;
84    }
85#endif
86// =============================================================
87#if 0
88// timings: ratchwum: 515 s, wilde13: 398 s, nepomuck: 269 s, lukas 834 s
89    id=IDID(h);
90    if (*(short *)s==*(short *)id)
91    {
92      l=IDLEV(h);
93      if ((l==0) && (0 == strcmp(s+1,id+1)))
94      {
95        found=h;
96      }
97      else if ((l==lev) && (0 == strcmp(s+1,id+1)))
98      {
99        return h;
100      }
101    }
102#endif
103// =============================================================
104#if 1
105// timings: ratchwum: 501 s, wilde13: 357 s, nepomuck: 267 s, lukas 816 s
106// timings bug4: ratchwum: s, wilde13: s, nepomuck: 379.74 s, lukas s
107    l=IDLEV(h);
108    if ((l==0)||(l==lev))
109    {
110      id=IDID(h);
111      if (*(short *)s==*(short *)id)
112      {
113        if (0 == strcmp(s+1,id+1))
114        {
115          if (l==lev) return h;
116          found=h;
117        }
118      }
119    }
120#endif
121// =============================================================
122#if 0
123// timings: ratchwum: s, wilde13: s, nepomuck: s, lukas s
124// timings bug4: ratchwum: s, wilde13: s, nepomuck: s, lukas s
125    l=IDLEV(h);
126    if ((l==0)||(l==lev))
127    {
128      id=IDID(h);
129      if (*(short *)s==*(short *)id)
130      {
131        if (l==lev) return h;
132        found=h;
133      }
134    }
135#endif
136// =============================================================
137    h = IDNEXT(h);
138  }
139  }
140  else
141  {
142  while (h!=NULL)
143  {
144    omCheckAddr(IDID(h));
145// =============================================================
146#if 0
147// timings: ratchwum: 515 s, wilde13: 373 s, nepomuck: 267 s, lukas 863 s
148    id=IDID(h);
149    l=IDLEV(h);
150    if ((l==0) && (*(short *)s==*(short *)id) && (0 == strcmp(s+1,id+1)))
151    {
152      found=h;
153    }
154    else if ((l==lev) && (*(short *)s==*(short *)id) && (0 == strcmp(s+1,id+1)))
155    {
156      return h;
157    }
158#endif
159// =============================================================
160#if 0
161// timings: ratchwum: 515 s, wilde13: 398 s, nepomuck: 269 s, lukas 834 s
162    id=IDID(h);
163    if (*(short *)s==*(short *)id)
164    {
165      l=IDLEV(h);
166      if ((l==0) && (0 == strcmp(s+1,id+1)))
167      {
168        found=h;
169      }
170      else if ((l==lev) && (0 == strcmp(s+1,id+1)))
171      {
172        return h;
173      }
174    }
175#endif
176// =============================================================
177#if 0
178// timings: ratchwum: 501 s, wilde13: 357 s, nepomuck: 267 s, lukas 816 s
179// timings bug4: ratchwum: s, wilde13: s, nepomuck: 379.74 s, lukas s
180    l=IDLEV(h);
181    if ((l==0)||(l==lev))
182    {
183      id=IDID(h);
184      if (*(short *)s==*(short *)id)
185      {
186        if (0 == strcmp(s+1,id+1))
187        {
188          if (l==lev) return h;
189          found=h;
190        }
191      }
192    }
193#endif
194// =============================================================
195#if 1
196// timings: ratchwum: s, wilde13: s, nepomuck: s, lukas s
197// timings bug4: ratchwum: s, wilde13: s, nepomuck: s, lukas s
198    l=IDLEV(h);
199    if ((l==0)||(l==lev))
200    {
201      id=IDID(h);
202      if (*(short *)s==*(short *)id)
203      {
204        if (0 == strcmp(s+2,id+2))
205        {
206          if (l==lev) return h;
207          found=h;
208        }
209      }
210    }
211#endif
212// =============================================================
213    h = IDNEXT(h);
214  }
215  }
216  return found;
217}
218
219//idrec::~idrec()
220//{
221//  if (id!=NULL)
222//  {
223//    omFree((ADDRESS)id);
224//    id=NULL;
225//  }
226//  /* much more !! */
227//}
228
229idhdl idrec::set(char * s, int lev, idtyp t, BOOLEAN init)
230{
231  //printf("define %s, %x, lev: %d, typ: %d\n", s,s,lev,t);
232  idhdl h = (idrec *)omAlloc0Bin(idrec_bin);
233  int   len = 0;
234  IDID(h)   = s;
235  IDTYP(h)  = t;
236  IDLEV(h)  = lev;
237  IDNEXT(h) = this;
238  if (init)
239  {
240    switch (t)
241    {
242      //the type with init routines:
243      case INTVEC_CMD:
244      case INTMAT_CMD:
245        IDINTVEC(h) = new intvec();
246        break;
247      case NUMBER_CMD:
248        IDNUMBER(h) = nInit(0);
249        break;
250      case IDEAL_CMD:
251      case MODUL_CMD:
252        IDFLAG(h) = Sy_bit(FLAG_STD);
253      case MATRIX_CMD:
254        IDIDEAL(h) = idInit(1,1);
255        break;
256      case MAP_CMD:
257        IDIDEAL(h) = idInit(1,1);
258        IDMAP(h)->preimage = omStrDup(IDID(currRingHdl));
259        break;
260      case STRING_CMD:
261        IDSTRING(h) = omStrDup("");
262        break;
263      case LIST_CMD:
264        IDLIST(h)=(lists)omAllocBin(slists_bin);
265        IDLIST(h)->Init();
266        break;
267      case LINK_CMD:
268        IDLINK(h)=(si_link) omAlloc0Bin(sip_link_bin);
269        break;
270      case RING_CMD:
271      case QRING_CMD:
272        IDRING(h) = (ring) omAlloc0Bin(sip_sring_bin);
273        break;
274      case PACKAGE_CMD:
275        IDPACKAGE(h) = (package) omAlloc0Bin(sip_package_bin);
276        break;
277      case PROC_CMD:
278        IDPROC(h) = (procinfo*) omAlloc0Bin(procinfo_bin);
279        break;
280        //the types with the standard init: set the struct to zero
281      case RESOLUTION_CMD:
282        len=sizeof(ssyStrategy);
283        break;
284    //other types: without init (int,script,poly,def,package)
285    }
286    if (len!=0)
287    {
288      IDSTRING(h) = (char *)omAlloc0(len);
289    }
290    // additional settings:--------------------------------------
291#if 0
292    // this leads to a memory leak
293    if (t == QRING_CMD)
294    {
295      // IDRING(h)=rCopy(currRing);
296      /* QRING_CMD is ring dep => currRing !=NULL */
297    }
298    else
299#endif
300      if (t == PROC_CMD)
301    {
302      IDPROC(h)->language=LANG_NONE;
303    }
304    else if (t == PACKAGE_CMD)
305    {
306      IDPACKAGE(h)->language=LANG_NONE;
307      IDPACKAGE(h)->loaded = FALSE;
308    }
309  }
310  // --------------------------------------------------------
311  return  h;
312}
313
314char * idrec::String()
315{
316  sleftv tmp;
317  memset(&tmp,0,sizeof(sleftv));
318  tmp.rtyp=IDTYP(this);
319  tmp.data=IDDATA(this);
320  tmp.name=IDID(this);
321  return tmp.String();
322}
323
324//#define KAI
325idhdl enterid(char * s, int lev, idtyp t, idhdl* root, BOOLEAN init)
326{
327  idhdl h;
328  s=omStrDup(s);
329  // is it already defined in root ?
330  if ((h=(*root)->get(s,lev))!=NULL)
331  {
332    if (IDLEV(h)==lev)
333    {
334    if ((IDTYP(h) == t)||(t==DEF_CMD))
335    {
336      if ((IDTYP(h)==PACKAGE_CMD)
337      && (strcmp(s,"Top")==0))
338      {
339        goto errlabel;
340      } 
341      if (BVERBOSE(V_REDEFINE))
342        Warn("redefining %s **",s);
343      if (s==IDID(h)) IDID(h)=NULL;
344      killhdl2(h,root,currRing);
345    }
346    else
347      goto errlabel;
348    }
349  }
350  // is it already defined in idroot ?
351  else if (*root != IDROOT)
352  {
353    if ((h=IDROOT->get(s,lev))!=NULL)
354    {
355      if (IDLEV(h)==lev)
356      {
357      if ((IDTYP(h) == t)||(t==DEF_CMD))
358      {
359        if (BVERBOSE(V_REDEFINE))
360          Warn("redefining %s **",s);
361        if (s==IDID(h)) IDID(h)=NULL;
362        killhdl2(h,&IDROOT,NULL);
363      }
364      else
365        goto errlabel;
366      }
367    }
368  }
369  // is it already defined in currRing->idroot ?
370  else if ((currRing!=NULL)&&((*root) != currRing->idroot))
371  {
372    if ((h=currRing->idroot->get(s,lev))!=NULL)
373    {
374      if (IDLEV(h)==lev)
375      {
376      if ((IDTYP(h) == t)||(t==DEF_CMD))
377      {
378        if (BVERBOSE(V_REDEFINE))
379          Warn("redefining %s **",s);
380        IDID(h)=NULL;
381        killhdl2(h,&currRing->idroot,currRing);
382      }
383      else
384        goto errlabel;
385      }
386    }
387  }
388  *root = (*root)->set(s, lev, t, init);
389#ifdef HAVE_NS
390#ifndef NDEBUG
391  checkall();
392#endif
393#endif
394  return *root;
395
396  errlabel:
397    //Werror("identifier `%s` in use(lev h=%d,typ=%d,t=%d, curr=%d)",s,IDLEV(h),IDTYP(h),t,lev);
398    Werror("identifier `%s` in use",s);
399#ifdef HAVE_NS
400    //listall();
401#endif
402    omFree(s);
403    return NULL;
404}
405
406void killid(char * id, idhdl * ih)
407{
408  if (id!=NULL)
409  {
410    idhdl h = (*ih)->get(id,myynest);
411
412    // id not found in global list, is it defined in current ring ?
413    if (h==NULL)
414    {
415      if ((currRing!=NULL) && (*ih != (currRing->idroot)))
416      {
417        h = currRing->idroot->get(id,myynest);
418        if (h!=NULL)
419        {
420          killhdl2(h,&(currRing->idroot),currRing);
421          return;
422        }
423      }
424      Werror("`%s` is not defined",id);
425      return;
426    }
427    killhdl2(h,ih,currRing);
428  }
429  else
430    Werror("kill what ?");
431}
432
433void killhdl(idhdl h)
434{
435  int t=IDTYP(h);
436  if ((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD))
437    killhdl2(h,&currRing->idroot,currRing);
438  else
439  {
440#ifdef HAVE_NS
441    if(t==PACKAGE_CMD)
442    {
443      killhdl2(h,&(basePack->idroot),NULL);
444    }
445    else
446    {
447      idhdl s=currPack->idroot;
448      while ((s!=h) && (s!=NULL)) s=s->next;
449      if (s!=NULL)
450        killhdl2(h,&(currPack->idroot),NULL);
451      else if (basePack!=currPack)
452      {
453        idhdl s=basePack->idroot;
454        while ((s!=h) && (s!=NULL)) s=s->next;
455        if (s!=NULL)
456          killhdl2(h,&(basePack->idroot),currRing);
457        else
458          killhdl2(h,&(currRing->idroot),currRing);
459       }
460    }
461#else /* HAVE_NS */
462    {
463      idhdl s=IDROOT;
464      while ((s!=h) && (s!=NULL)) s=s->next;
465      if (s==NULL) killhdl2(h,&(currRing->idroot),currRing);
466      else killhdl2(h,&IDROOT,currRing);
467    }
468#endif /* HAVE_NS */
469  }
470}
471
472#ifdef HAVE_NS
473void killhdl(idhdl h, package proot)
474{
475  int t=IDTYP(h);
476  if ((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD))
477    killhdl2(h,&currRing->idroot,currRing);
478  else
479  {
480    if(t==PACKAGE_CMD)
481    {
482      killhdl2(h,&(basePack->idroot),NULL);
483    }
484    else
485    {
486      idhdl s=proot->idroot;
487      while ((s!=h) && (s!=NULL)) s=s->next;
488      if (s!=NULL)
489        killhdl2(h,&(proot->idroot),NULL);
490      else if (basePack!=proot)
491      {
492        idhdl s=basePack->idroot;
493        while ((s!=h) && (s!=NULL)) s=s->next;
494        if (s!=NULL)
495          killhdl2(h,&(basePack->idroot),currRing);
496        else
497          killhdl2(h,&(currRing->idroot),currRing);
498       }
499    }
500  }
501}
502#endif /* HAVE_NS */
503
504void killhdl2(idhdl h, idhdl * ih, ring r)
505{
506  //printf("kill %s, id %x, typ %d lev: %d\n",IDID(h),(int)IDID(h),IDTYP(h),IDLEV(h));
507  idhdl hh;
508
509  if (h->attribute!=NULL)
510  {
511    atKillAll(h);
512    //h->attribute=NULL;
513  }
514  if ((IDTYP(h) == PACKAGE_CMD) && (strcmp(IDID(h),"Top")==0))
515  {
516    WarnS("can not kill `Top`");
517    return;
518  }
519  // ring / qring  --------------------------------------------------------
520  if ((IDTYP(h) == RING_CMD) || (IDTYP(h) == QRING_CMD))
521  {
522    // any objects defined for this ring ?
523    // Hmm ... why only for rings and not for qrings??
524    // if (((IDTYP(h)==RING_CMD) && (IDRING(h)->ref<=0))
525    if ((IDRING(h)->ref<=0)  &&  (IDRING(h)->idroot!=NULL))
526    {
527      idhdl * hd = &IDRING(h)->idroot;
528      idhdl  hdh = IDNEXT(*hd);
529      idhdl  temp;
530      if (IDRING(h)==currRing) //we are not killing the base ring, so switch
531      {
532        // we are killing the basering, so: make sure that
533        // sLastPrinted is killed before this ring is destroyed
534        if (((sLastPrinted.rtyp>BEGIN_RING) && (sLastPrinted.rtyp<END_RING))
535        || ((sLastPrinted.rtyp==LIST_CMD)&&(lRingDependend((lists)sLastPrinted.data))))
536        {
537          sLastPrinted.CleanUp();
538        }
539      }
540      while (hdh!=NULL)
541      {
542        temp = IDNEXT(hdh);
543        killhdl2(hdh,&(IDRING(h)->idroot),IDRING(h));
544        hdh = temp;
545      }
546      killhdl2(*hd,hd,IDRING(h));
547    }
548    rKill(h);
549  }
550#ifdef HAVE_NS
551  // package -------------------------------------------------------------
552  else if (IDTYP(h) == PACKAGE_CMD)
553  {
554    // any objects defined for this package ?
555    if ((IDPACKAGE(h)->ref<=0)  &&  (IDPACKAGE(h)->idroot!=NULL))
556    {
557      if (currPack==IDPACKAGE(h))
558      {
559        currPack=basePack;
560        currPackHdl=NULL;
561      }
562      idhdl * hd = &IDRING(h)->idroot;
563      idhdl  hdh = IDNEXT(*hd);
564      idhdl  temp;
565      while (hdh!=NULL)
566      {
567        temp = IDNEXT(hdh);
568        killhdl2(hdh,&(IDPACKAGE(h)->idroot),NULL);
569        hdh = temp;
570      }
571      killhdl2(*hd,hd,NULL);
572      if (IDPACKAGE(h)->libname!=NULL) omFree((ADDRESS)(IDPACKAGE(h)->libname));
573    }
574    paKill(IDPACKAGE(h));
575    if (currPackHdl==h) currPackHdl=packFindHdl(currPack);
576    iiCheckPack(currPack);
577  }
578#endif /* HAVE_NS */
579  // poly / vector -------------------------------------------------------
580  else if ((IDTYP(h) == POLY_CMD) || (IDTYP(h) == VECTOR_CMD))
581  {
582    assume(r!=NULL);
583    p_Delete(&IDPOLY(h),r);
584  }
585  // ideal / module/ matrix / map ----------------------------------------
586  else if ((IDTYP(h) == IDEAL_CMD)
587           || (IDTYP(h) == MODUL_CMD)
588           || (IDTYP(h) == MATRIX_CMD)
589           || (IDTYP(h) == MAP_CMD))
590  {
591    assume(r!=NULL);
592    ideal iid = IDIDEAL(h);
593    if (IDTYP(h) == MAP_CMD)
594    {
595      map im = IDMAP(h);
596      omFree((ADDRESS)im->preimage);
597    }
598    id_Delete(&iid,r);
599  }
600  // string -------------------------------------------------------------
601  else if (IDTYP(h) == STRING_CMD)
602  {
603    omFree((ADDRESS)IDSTRING(h));
604    //IDSTRING(h)=NULL;
605  }
606  // proc ---------------------------------------------------------------
607  else if (IDTYP(h) == PROC_CMD)
608  {
609    if (piKill(IDPROC(h))) return;
610  }
611  // number -------------------------------------------------------------
612  else if (IDTYP(h) == NUMBER_CMD)
613  {
614    assume(r!=NULL);
615    n_Delete(&IDNUMBER(h),r);
616  }
617  // intvec / intmat  ---------------------------------------------------
618  else if ((IDTYP(h) == INTVEC_CMD)||(IDTYP(h) == INTMAT_CMD))
619  {
620    delete IDINTVEC(h);
621  }
622  // list  -------------------------------------------------------------
623  else if (IDTYP(h)==LIST_CMD)
624  {
625    IDLIST(h)->Clean(r);
626    //omFreeSize((ADDRESS)IDLIST(h)->m, (IDLIST(h)->nr+1)*sizeof(sleftv));
627    //omFreeBin((ADDRESS)IDLIST(h),  slists_bin);
628  }
629  // link  -------------------------------------------------------------
630  else if (IDTYP(h)==LINK_CMD)
631  {
632    slKill(IDLINK(h));
633  }
634  else if(IDTYP(h)==RESOLUTION_CMD)
635  {
636    assume(r!=NULL);
637    if (IDDATA(h)!=NULL)
638      syKillComputation((syStrategy)IDDATA(h),r);
639  }
640#ifdef TEST
641  else if ((IDTYP(h)!= INT_CMD)&&(IDTYP(h)!=DEF_CMD) && (IDTYP(h)!=NONE))
642    Warn("unknown type to kill: %s(%d)",Tok2Cmdname(IDTYP(h)),IDTYP(h));
643#endif
644
645  //  general  -------------------------------------------------------------
646  // now dechain it and delete idrec
647#ifdef KAI
648  if(h->next != NULL)
649    Print("=======>%s(%x) -> %s<====\n", IDID(h), IDID(h), IDID(h->next));
650  else
651    Print("=======>%s(%x)<====\n", IDID(h), IDID(h));
652#endif
653
654  if (IDID(h)) // OB: ?????
655    omFree((ADDRESS)IDID(h));
656  IDID(h)=NULL;
657  IDDATA(h)=NULL;
658  if (h == (*ih))
659  {
660    // h is at the beginning of the list
661    *ih = IDNEXT(h) /* ==*ih */;
662  }
663  else if (ih!=NULL)
664  {
665    // h is somethere in the list:
666    hh = *ih;
667    loop
668    {
669      if (hh==NULL)
670      {
671        PrintS(">>?<< not found for kill\n");
672        return;
673      }
674      idhdl hhh = IDNEXT(hh);
675      if (hhh == h)
676      {
677        IDNEXT(hh) = IDNEXT(hhh);
678        break;
679      }
680      hh = hhh;
681    }
682  }
683  omFreeBin((ADDRESS)h, idrec_bin);
684}
685
686idhdl ggetid(const char *n, BOOLEAN local, idhdl *packhdl)
687{
688  idhdl h = IDROOT->get(n,myynest);
689  idhdl h2=NULL;
690  *packhdl = NULL;
691  if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest)))
692  {
693    h2 = currRing->idroot->get(n,myynest);
694  }
695  if (h2==NULL) return h;
696  return h2;
697}
698
699idhdl ggetid(const char *n, BOOLEAN local)
700{
701  idhdl h = IDROOT->get(n,myynest);
702  if ((h!=NULL)&&(IDLEV(h)==myynest)) return h;
703  idhdl h2=NULL;
704  if (currRing!=NULL)
705  {
706    h2 = currRing->idroot->get(n,myynest);
707  }
708  if (h2!=NULL) return h2;
709  if (h!=NULL) return h;
710#ifdef HAVE_NS 
711  if (basePack!=currPack)
712    return basePack->idroot->get(n,myynest);
713#endif     
714  return NULL; 
715}
716
717void ipListFlag(idhdl h)
718{
719  if (hasFlag(h,FLAG_STD)) PrintS(" (SB)");
720#ifdef HAVE_PLURAL 
721  if (hasFlag(h,FLAG_TWOSTD)) PrintS(" (2SB)");
722#endif 
723}
724
725lists ipNameList(idhdl root)
726{
727  idhdl h=root;
728  /* compute the length */
729  int l=0;
730  while (h!=NULL) { l++; h=IDNEXT(h); }
731  /* allocate list */
732  lists L=(lists)omAllocBin(slists_bin);
733  L->Init(l);
734  /* copy names */
735  h=root;
736  l=0;
737  while (h!=NULL)
738  {
739    /* list is initialized with 0 => no need to clear anything */
740    L->m[l].rtyp=STRING_CMD;
741    L->m[l].data=omStrDup(IDID(h));
742    l++;
743    h=IDNEXT(h);
744  }
745  return L;
746}
747
748/*
749* move 'tomove' from root1 list to root2 list
750*/
751static int ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2)
752{
753  idhdl h;
754  /* search 'tomove' in root2 : if found -> do nothing */
755  h=root2;
756  while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h);
757  if (h!=NULL) return FALSE; /*okay */
758  /* search predecessor of h in root1, remove 'tomove' */
759  h=root1;
760  if (tomove==h)
761  {
762    root1=IDNEXT(h);
763  }
764  else
765  {
766    while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h);
767    if (h==NULL) return TRUE; /* not in the list root1 -> do nothing */
768    IDNEXT(h)=IDNEXT(tomove);
769  }
770  /* add to root2 list */
771  IDNEXT(tomove)=root2;
772  root2=tomove;
773  return FALSE;
774}
775
776void  ipMoveId(idhdl tomove)
777{
778  if ((currRing!=NULL)&&(tomove!=NULL))
779  {
780    if (((QRING_CMD!=IDTYP(tomove)) && RingDependend(IDTYP(tomove)))
781    || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove)))))
782    {
783      /*move 'tomove' to ring id's*/
784#ifdef HAVE_NS
785      if (ipSwapId(tomove,IDROOT,currRing->idroot))
786      ipSwapId(tomove,basePack->idroot,currRing->idroot);
787#else
788      ipSwapId(tomove,IDROOT,currRing->idroot);
789#endif
790    }
791    else
792    {
793      /*move 'tomove' to global id's*/
794      ipSwapId(tomove,currRing->idroot,IDROOT);
795    }
796  }
797}
798
799char * piProcinfo(procinfov pi, char *request)
800{
801  if(pi == NULL) return "empty proc";
802  else if (strcmp(request, "libname")  == 0) return pi->libname;
803  else if (strcmp(request, "procname") == 0) return pi->procname;
804  else if (strcmp(request, "type")     == 0)
805  {
806    switch (pi->language)
807    {
808      case LANG_SINGULAR: return "singular"; break;
809      case LANG_C:        return "object";   break;
810      case LANG_NONE:     return "none";     break;
811      default:            return "unknow language";
812    }
813  }
814  else if (strcmp(request, "ref")      == 0)
815  {
816    char p[8];
817    sprintf(p, "%d", pi->ref);
818    return omStrDup(p);  // MEMORY-LEAK
819  }
820  return "??";
821}
822
823void piCleanUp(procinfov pi)
824{
825  (pi->ref)--;
826  if (pi->ref <= 0)
827  {
828    if (pi->libname != NULL) // OB: ????
829      omFree((ADDRESS)pi->libname);
830    if (pi->procname != NULL) // OB: ????
831      omFree((ADDRESS)pi->procname);
832
833    if( pi->language == LANG_SINGULAR)
834    {
835      if (pi->data.s.body != NULL) // OB: ????
836        omFree((ADDRESS)pi->data.s.body);
837    }
838    if( pi->language == LANG_C)
839    {
840    }
841    memset((void *) pi, 0, sizeof(procinfo));
842    pi->language=LANG_NONE;
843  }
844}
845
846BOOLEAN piKill(procinfov pi)
847{
848  Voice *p=currentVoice;
849  while (p!=NULL)
850  {
851    if (p->pi==pi && pi->ref <= 1)
852    {
853      Warn("`%s` in use, can not be killed",pi->procname);
854      return TRUE;
855    }
856    p=p->next;
857  }
858  piCleanUp(pi);
859  if (pi->ref <= 0)
860    omFreeBin((ADDRESS)pi,  procinfo_bin);
861  return FALSE;
862}
863
864void paCleanUp(package pack)
865{
866  (pack->ref)--;
867  if (pack->ref < 0)
868  {
869#ifndef HAVE_STATIC
870    if( pack->language == LANG_C)
871    {
872      Print("//dlclose(%s)\n",pack->libname);
873#ifdef HAVE_DYNAMIC_LOADING
874      dynl_close (pack->handle);
875#endif /* HAVE_DYNAMIC_LOADING */
876    }
877#endif /* HAVE_STATIC */
878    omfree((ADDRESS)pack->libname);
879    memset((void *) pack, 0, sizeof(sip_package));
880    pack->language=LANG_NONE;
881  }
882}
883
884char *idhdl2id(idhdl pck, idhdl h)
885{
886  char *name = (char *)omAlloc(strlen(pck->id) + strlen(h->id) + 3);
887  sprintf(name, "%s::%s", pck->id, h->id);
888  return(name);
889}
890
891void iiname2hdl(const char *name, idhdl *pck, idhdl *h)
892{
893  const char *q = strchr(name, ':');
894  char *p, *i;
895
896  if(q==NULL)
897  {
898    p = omStrDup("");
899    i = (char *)omAlloc(strlen(name)+1);
900    *i = '\0';
901    sscanf(name, "%s", i);
902  }
903  else {
904    if( *(q+1) != ':') return;
905    i = (char *)omAlloc(strlen(name)+1);
906    *i = '\0';
907    if(name == q)
908    {
909      p = omStrDup("");
910      sscanf(name, "::%s", i);
911    }
912    else
913    {
914      p = (char *)omAlloc(strlen(name)+1);
915      sscanf(name, "%[^:]::%s", p, i);
916    }
917  }
918  //printf("Package: '%s'\n", p);
919  //printf("Id Rec : '%s'\n", i);
920  omFree(p);
921  omFree(i);
922}
923
924#if 0
925char *getnamelev()
926{
927  char buf[256];
928  sprintf(buf, "(%s:%d)", namespaceroot->name,namespaceroot->lev);
929  return(buf);
930}
931// warning: address of local variable `buf' returned
932#endif
933
934void proclevel::push(char *n)
935{
936  //Print("push %s\n",n);
937  proclevel *p=(proclevel*)omAlloc0(sizeof(proclevel));
938  p->cRing=currRing;
939  p->cRingHdl=currRingHdl;
940  p->name=n;
941  #ifdef HAVE_NS
942  p->cPackHdl=currPackHdl;
943  p->cPack=currPack;
944  #endif
945  p->next=this;
946  procstack=p;
947}
948void proclevel::pop()
949{
950  //Print("pop %s\n",name);
951  //if (currRing!=::currRing) PrintS("currRing wrong\n");;
952  //::currRing=this->currRing;
953  //if (r==NULL) Print("set ring to NULL at lev %d(%s)\n",myynest,name);
954  //::currRingHdl=this->currRingHdl;
955  //if((::currRingHdl==NULL)||(IDRING(::currRingHdl)!=(::currRing)))
956  //  ::currRingHdl=rFindHdl(::currRing,NULL,NULL);
957  #ifdef HAVE_NS
958  //Print("restore pack=%s,1.obj=%s\n",IDID(currPackHdl),IDID(currPack->idroot));
959  currPackHdl=this->cPackHdl;
960  currPack=this->cPack;
961  iiCheckPack(currPack);
962  #endif
963  proclevel *p=this;
964  procstack=next;
965  omFreeSize(p,sizeof(proclevel));
966}
967
968#ifdef HAVE_NS
969idhdl packFindHdl(package r)
970{
971  idhdl h=basePack->idroot;
972  while (h!=NULL)
973  {
974    if ((IDTYP(h)==PACKAGE_CMD)
975        && (IDPACKAGE(h)==r))
976      return h;
977    h=IDNEXT(h);
978  }
979  return NULL;
980}
981#endif
Note: See TracBrowser for help on using the repository browser.