home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / OGC / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-06  |  4.0 KB  |  187 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1989, Tera Computer Company
  5.  **/
  6.  
  7. #define DEBUG       /* Some run-time consistency checks */
  8. #undef DEBUG
  9. #define VERBOSE
  10. #undef VERBOSE
  11.  
  12. #include <stdio.h>
  13. #include <signal.h>
  14. #include "runtime.h"
  15.  
  16. int dont_gc = 0;
  17. extern long mem_found;
  18.  
  19. /* allocate lb bytes of atomic data */
  20. struct obj * gc_malloc_atomic(lb)
  21. int lb;
  22. {
  23. register struct obj *op;
  24. register struct obj **opp;
  25. register int lw = BYTES_TO_WORDS(lb + (sizeof (word)) -1);
  26.  
  27. #   ifdef VERBOSE
  28.     printf("Here we are in gc_malloc_atomic(%d)\n",lw);
  29. #   endif
  30.     if( lw <= MAXAOBJSZ ) {
  31.     opp = &(aobjfreelist[lw]);
  32.         if( (op = *opp) == ((struct obj *)0) ) {
  33.             op = allocaobj(lw);
  34.         }
  35. #       ifdef DEBUG
  36.         if ((op -> obj_link != ((struct obj *) 0)
  37.         && (((unsigned)(op -> obj_link)) > ((unsigned) HEAPLIM)
  38.            || ((unsigned)(op -> obj_link)) < ((unsigned) HEAPSTART)))) {
  39.         fprintf(stderr, "Bad free list in gc_malloc_atomic\n");
  40.         abort(op);
  41.             }
  42. #       endif
  43.         *opp = op->obj_link;
  44.         op->obj_link = (struct obj *)0;
  45.     } else {
  46.     register struct hblk * h;
  47.     if (!sufficient_hb(-lw) && !dont_gc) {
  48.             gcollect();
  49.     }
  50. #       ifdef VERBOSE
  51.         printf("gc_malloc_atomic calling allochblk(%x)\n",lw);
  52. #    endif
  53.     h = allochblk(-lw);
  54.     add_hblklist(h);
  55.     op = (struct obj *) (h -> hb_body);
  56.     }
  57.     return(op);
  58. }
  59.  
  60. /* allocate lw bytes of possibly composite data */
  61. struct obj * gc_malloc(lb)
  62. int lb;
  63. {
  64. register struct obj *op;
  65. register struct obj **opp;
  66. register int lw = BYTES_TO_WORDS(lb + (sizeof (word)) -1);
  67.  
  68.     if( lw <= MAXOBJSZ ) {
  69.     void gc_init() ;
  70.     if(heaplim == 0) gc_init() ;
  71.     if(lw == 0) lw = 1 ; /* bugfix: cdc */
  72.     opp = &(objfreelist[lw]);
  73.         if( (op = *opp) == ((struct obj *)0) ) {
  74.         op = allocobj(lw);
  75.         }
  76. #       ifdef DEBUG
  77.         if ((op -> obj_link != ((struct obj *) 0)
  78.         && (((unsigned)(op -> obj_link)) > ((unsigned) HEAPLIM)
  79.            || ((unsigned)(op -> obj_link)) < ((unsigned) HEAPSTART)))) {
  80.         fprintf(stderr, "Bad free list in gc_malloc\n");
  81.         abort(op);
  82.             }
  83. #       endif
  84.         *opp = op->obj_link;
  85.         op->obj_link = (struct obj *)0;
  86.     } else {
  87.     register struct hblk * h;
  88.  
  89.     if (!sufficient_hb(lw) && !dont_gc) {
  90.             gcollect();
  91.     }
  92. #       ifdef VERBOSE
  93.         printf("ralloc_comp calling allochblk(%x)\n",lw);
  94. #    endif
  95.     h = allochblk(lw);
  96.     add_hblklist(h);
  97.     op = (struct obj *) (h -> hb_body);
  98.     }
  99.     return(op);
  100. }
  101.  
  102. /* Explicitly deallocate an object p */
  103. gc_free(p)
  104. struct obj *p;
  105. {
  106.     register struct hblk *h;
  107.     register int sz;
  108.     register int i;
  109.  
  110.     h = HBLKPTR(p);
  111.     sz = h -> hb_sz;
  112.     if (sz < 0) {
  113.         sz = -sz;
  114.         if (sz > MAXAOBJSZ) {
  115.         h -> hb_uninit = 1;
  116.         del_hblklist(h);
  117.         freehblk(h);
  118.     } else {
  119.         p -> obj_link = aobjfreelist[sz];
  120.         aobjfreelist[sz] = p;
  121.     }
  122.     } else {
  123.     /* Clear the object, other than link field */
  124.         for (i = 1; i < sz; i++) {
  125.         p -> obj_component[i] = 0;
  126.         }
  127.     if (sz > MAXOBJSZ) {
  128.         p -> obj_link = 0;
  129.         h -> hb_uninit = 0;
  130.         del_hblklist(h);
  131.         freehblk(h);
  132.     } else {
  133.         p -> obj_link = objfreelist[sz];
  134.         objfreelist[sz] = p;
  135.     }
  136.     }
  137.     /* Add it to mem_found to prevent anomalous heap expansion */
  138.     /* in the event of repeated explicit frees of objects of   */
  139.     /* varying sizes.                                          */
  140.         mem_found += sz;
  141. }
  142.  
  143.  
  144. /*
  145.  * Disable non-urgent signals
  146.  */
  147. int holdsigs()
  148. {
  149.     unsigned mask = 0xffffffff;
  150.  
  151.     mask &= ~(1<<(SIGSEGV-1));
  152.     mask &= ~(1<<(SIGILL-1));
  153.     mask &= ~(1<<(SIGBUS-1));
  154.     mask &= ~(1<<(SIGIOT-1));
  155.     mask &= ~(1<<(SIGEMT-1));
  156.     mask &= ~(1<<(SIGTRAP-1));
  157.     mask &= ~(1<<(SIGQUIT-1));
  158.     return(sigsetmask(mask));
  159. }
  160.  
  161. void gc_init()
  162. {
  163.     heaplim = &end;
  164.     hincr = HINCR;
  165.     expand_hp(hincr);
  166.     init_hblklist();
  167. }
  168.  
  169. char * gc_realloc(p,i)
  170.      struct obj * p ;
  171.      int i ;
  172. {
  173.     register struct hblk *h;
  174.     register int sz;
  175.     register char * temp = (char*) gc_malloc(i) ;
  176.  
  177.     h = HBLKPTR(p);
  178.     sz = h -> hb_sz;
  179.     if(sz < 0) sz = - sz ;
  180.     if(sz < i) sz = i ;
  181.     memcpy(temp,p,sz) ;
  182.     gc_free(p) ;
  183.     return temp ;
  184. }
  185.  
  186.     
  187.