BSD 3 development
authorDavid Wasley <dlw@ucbvax.Berkeley.EDU>
Wed, 5 Dec 1979 06:31:24 +0000 (22:31 -0800)
committerDavid Wasley <dlw@ucbvax.Berkeley.EDU>
Wed, 5 Dec 1979 06:31:24 +0000 (22:31 -0800)
Work on file usr/src/new/libI77uc/dballoc.c
Work on file usr/src/new/libI77uc/douio.c
Work on file usr/src/new/libI77uc/fmtlib.c
Work on file usr/src/new/libI77uc/lwrite.c

Synthesized-from: 3bsd

usr/src/new/libI77uc/dballoc.c [new file with mode: 0644]
usr/src/new/libI77uc/douio.c [new file with mode: 0644]
usr/src/new/libI77uc/fmtlib.c [new file with mode: 0644]
usr/src/new/libI77uc/lwrite.c [new file with mode: 0644]

diff --git a/usr/src/new/libI77uc/dballoc.c b/usr/src/new/libI77uc/dballoc.c
new file mode 100644 (file)
index 0000000..5ffc553
--- /dev/null
@@ -0,0 +1,157 @@
+/*     C storage allocator
+ *     circular first-fit strategy
+ *     works with noncontiguous, but monotonically linked, arena
+ *     each block is preceded by a ptr to the (pointer of) 
+ *     the next following block
+ *     blocks are exact number of words long; BUSY
+ *     bit in ptr is 1 for busy, 0 for idle
+ *     gaps in arena are merely noted as busy blocks
+ *     last block of arena (pointed to by alloct) is empty and
+ *     has a pointer to first
+ *     idle blocks are coalesced during space search
+*/
+
+#include       <stdio.h>
+
+/*     all these defines must be powers of 2 */
+#define WORD sizeof(struct store)
+#define BLOCK 1024
+#define BUSY 1
+#define NULL 0
+#define testbusy(p) ((int)(p)&BUSY)
+#define setbusy(p) (struct store *)((int)(p)+BUSY)
+#define clearbusy(p) (struct store *)((int)(p)&~BUSY)
+
+/*
+#define debug YES
+*/
+
+#ifndef debug
+#define ASSERT(p)
+#endif
+
+#ifdef debug
+#define ASSERT(p) if(!(p))botch("p");else
+
+botch(s) char *s;
+{
+       fatal(119,s);
+}
+#endif
+
+struct store { struct store *ptr; };
+
+struct store allocs[] = {      /*initial arena*/
+       setbusy(&allocs[1].ptr),
+       setbusy(&allocs[0].ptr)
+};
+struct store *allocp = &allocs[1];     /*search ptr*/
+struct store *alloct = &allocs[1];     /*arena top*/
+struct store *allocx = 0;              /*for benefit of realloc*/
+struct store *sbrk();
+
+struct store *
+malloc(nbytes)
+unsigned nbytes;
+{
+       struct store *p, *q;
+       register nw;
+       static temp;    /*coroutines assume no auto*/
+
+#ifdef verbose
+       printf("malloc(%d) ",nbytes);
+#endif
+       nw = (nbytes+2*WORD-1)/WORD;
+       ASSERT(allocp>allocs && allocp<=alloct);
+       for(p=allocp; ; ) {
+               for(temp=0; ; ) {
+                       if(!testbusy(p->ptr)) {
+                               while(!testbusy((q=p->ptr)->ptr)) {
+                                       ASSERT(q>p&&q<alloct);
+                                       p->ptr = q->ptr;
+                               }
+                               if(q>=p+nw && p+nw>=p)
+                                       goto found;
+                       }
+                       q = p;
+                       p = clearbusy(p->ptr);
+                       if(p>q)
+                               ASSERT(p<=alloct);
+                       else if(q!=alloct || p!=allocs) {
+                               fatal(119,"dballoc");
+                       } else if(++temp>1)
+                               break;
+               }
+               temp = (nw+BLOCK/WORD)&~(BLOCK/WORD-1);
+               q = sbrk(temp*WORD); /*SYSDEP*/
+               if((int)q == -1)
+                       return(NULL);
+               ASSERT(q>alloct);
+               alloct->ptr = q;
+               if(q!=alloct+1)
+                       alloct->ptr = setbusy(alloct->ptr);
+               alloct = q->ptr = q+temp-1;
+               alloct->ptr = setbusy(allocs);
+       }
+found:
+       allocp = p + nw;
+       ASSERT(allocp<=alloct);
+       if(q>allocp) {
+               allocx = allocp->ptr;
+               allocp->ptr = p->ptr;
+       }
+       p->ptr = setbusy(allocp);
+#ifdef verbose
+       printf("= %o\n",p+1);
+#endif
+       return(p+1);
+}
+
+/*
+ *     freeing strategy tuned for LIFO allocation
+ */
+free(p)
+struct store *p;
+{
+       struct store *savep=p;
+#ifdef verbose
+       printf("free(%o)\n",p);
+#endif
+       ASSERT(p>clearbusy(allocs[1].ptr)&&p<=alloct);
+       allocp = --p;
+       ASSERT(testbusy(p->ptr));
+       p->ptr = clearbusy(p->ptr);
+       ASSERT(p->ptr > allocp && p->ptr <= alloct);
+}
+
+char *calloc(nbytes,count)
+{      char *c;
+       c=(char *)malloc(nbytes*count);
+       return(c);
+}
+
+struct store *
+realloc(p, nbytes)
+register struct store *p;
+unsigned nbytes;
+{
+       register struct store *q;
+       struct store *s, *t;
+       register unsigned nw;
+       unsigned onw;
+
+       onw = p[-1].ptr - p;
+       q = malloc(nbytes);
+       if(q==NULL || q==p)
+               return(q);
+       s = p;
+       t = q;
+       nw = (nbytes+WORD-1)/WORD;
+       if(nw<onw)
+               onw = nw;
+       while(onw--!=0)
+               (t++)->ptr = (s++)->ptr;
+       if(q<p && q+nw>=p)
+               (q+(q+nw-p))->ptr = allocx;
+       return(q);
+}
diff --git a/usr/src/new/libI77uc/douio.c b/usr/src/new/libI77uc/douio.c
new file mode 100644 (file)
index 0000000..435b0ed
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * unformatted external i/o
+ */
+
+#include "fio.h"
+
+char *eor = "eor/uio";
+char *uio = "uio";
+
+do_us(number,ptr,len) ftnint *number; ftnlen len; char *ptr;  /* sequential */
+{
+       if(reading)
+       {
+               recpos += *number * len;
+               if(recpos>reclen)
+               {
+                       err(errflag,110,eor);
+               }
+               if (fread(ptr,(int)len,(int)(*number),cf) != *number)
+               {       if(feof(cf)) err(endflag,EOF,uio)
+                       else
+                       {       clearerr(cf);
+                               err(errflag,errno,uio)
+                       }
+               }
+       }
+       else
+       {
+               reclen += *number * len;
+               fwrite(ptr,(int)len,(int)(*number),cf);
+       }
+       return(OK);
+}
+
+do_uio(number,ptr,len) ftnint *number; ftnlen len; char *ptr;
+{
+       if(sequential)
+               return(do_us(number,ptr,len));
+       else    return(do_ud(number,ptr,len));
+}
+
+do_ud(number,ptr,len) ftnint *number; ftnlen len; char *ptr;  /* direct */
+{
+       recpos += *number * len;
+       if(recpos > curunit->url && curunit->url!=1)
+               err(errflag,110,eor);
+       if(reading)
+       {
+               if(fread(ptr,(int)len,(int)(*number),cf) != *number)
+               {       if(feof(cf)) err(endflag,EOF,uio)
+                       else
+                       {       clearerr(cf);
+                               err(errflag,errno,uio)
+                       }
+               }
+       }
+       else fwrite(ptr,(int)len,(int)(*number),cf);
+       return(OK);
+}
diff --git a/usr/src/new/libI77uc/fmtlib.c b/usr/src/new/libI77uc/fmtlib.c
new file mode 100644 (file)
index 0000000..1b204d6
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * integer to ascii conversion
+ */
+
+#include "fio.h"
+
+#define digit(c)       ( (c > 9) ? (c - 10 + 'a') : c + '0')
+
+char *icvt(value,ndigit,sign) long value; int *ndigit,*sign;
+{
+       static char buf[MAXINTLENGTH+1];
+       register int i;
+       long kludge, rem, mask = 0x7fffffff;
+       int one = 1;
+       char c;
+
+       if (value == 0)
+       {       *sign=0;
+               *ndigit=one;
+               buf[MAXINTLENGTH]='0';
+               return(&buf[MAXINTLENGTH]);
+       }
+       else if (signit)        /* signed ? */
+       {
+               if (value > 0) *sign = 0;
+               else
+               {       value = -value;
+                       *sign = 1;
+               }
+               c = (int)(value % radix);
+               value /= radix;
+       }
+       else                    /* unsigned */
+       {       *sign = 0;
+               if (value < 0)
+               {       /* ALL THIS IS TO SIMULATE UNSIGNED MOD & DIV */
+                       kludge = mask - (radix - one);
+                       value &= mask;
+                       rem = (kludge % radix) + (value % radix);
+                       value = (kludge / radix) + (value / radix)
+                                + (rem / radix) + one;
+                       c = (int)(rem % radix);
+               }
+               else
+               {
+                       c = (int)(value % radix);
+                       value /= radix;
+               }
+       }
+       *(buf+MAXINTLENGTH) = digit(c);
+       for(i=MAXINTLENGTH-one; value!=0; i--)
+       {
+               c = (int)(value % radix);
+               *(buf+i) = digit(c);
+               value /= radix;
+       }
+       *ndigit = MAXINTLENGTH - i;
+       return(&buf[i+one]);
+}
diff --git a/usr/src/new/libI77uc/lwrite.c b/usr/src/new/libI77uc/lwrite.c
new file mode 100644 (file)
index 0000000..b31badb
--- /dev/null
@@ -0,0 +1,188 @@
+/*
+ * list directed write
+ */
+
+#include "fio.h"
+#include "lio.h"
+
+int l_write(), t_putc();
+
+s_wsle(a) cilist *a;
+{
+       int n;
+       reading = NO;
+       if(n=c_le(a,WRITE)) return(n);
+       putn = t_putc;
+       lioproc = l_write;
+       line_len = LINE;
+       curunit->uend = NO;
+       leof = NO;
+       if(!curunit->uwrt) nowwriting(curunit);
+       return(OK);
+}
+
+t_putc(c) char c;
+{
+       if(c=='\n') recpos=0;
+       else recpos++;
+       putc(c,cf);
+       return(OK);
+}
+
+e_wsle()
+{      int n;
+       PUT('\n')
+       return(OK);
+}
+
+l_write(number,ptr,len,type) ftnint *number,type; flex *ptr; ftnlen len;
+{
+       int i,n;
+       ftnint x;
+       float y,z;
+       double yd,zd;
+       float *xx;
+       double *yy;
+       for(i=0;i< *number; i++)
+       {
+               switch((int)type)
+               {
+               case TYSHORT:
+                       x=ptr->flshort;
+                       goto xint;
+               case TYLONG:
+                       x=ptr->flint;
+       xint:           ERR(lwrt_I(x));
+                       break;
+               case TYREAL:
+                       ERR(lwrt_F(ptr->flreal));
+                       break;
+               case TYDREAL:
+                       ERR(lwrt_D(ptr->fldouble));
+                       break;
+               case TYCOMPLEX:
+                       xx= &(ptr->flreal);
+                       y = *xx++;
+                       z = *xx;
+                       ERR(lwrt_C(y,z));
+                       break;
+               case TYDCOMPLEX:
+                       yy = &(ptr->fldouble);
+                       yd= *yy++;
+                       zd = *yy;
+                       ERR(lwrt_DC(yd,zd));
+                       break;
+               case TYLOGICAL:
+                       ERR(lwrt_L(ptr->flint));
+                       break;
+               case TYCHAR:
+                       ERR(lwrt_A((char *)ptr,len));
+                       break;
+               default:
+                       fatal(119,"unknown type in lwrite");
+               }
+               ptr = (char *)ptr + len;
+       }
+       return(OK);
+}
+
+lwrt_I(in) ftnint in;
+{      int n;
+       char buf[16],*p;
+       sprintf(buf,"  %ld",(long)in);
+       if(n=chk_len(LINTW)) return(n);
+       for(p=buf;*p;) PUT(*p++)
+       return(OK);
+}
+
+lwrt_L(ln) ftnint ln;
+{      int n;
+       if(n=chk_len(LLOGW)) return(n);
+       return(wrt_L(&ln,LLOGW));
+}
+
+lwrt_A(p,len) char *p; ftnlen len;
+{      int i,n;
+       if(n=chk_len(LSTRW)) return(n);
+       PUT(' ')
+       PUT(' ')
+       for(i=0;i<len;i++) PUT(*p++)
+       return(OK);
+}
+
+lwrt_F(fn) float fn;
+{      int d,n; float x; ufloat f;
+       if(fn==0.0) return(lwrt_0());
+       f.pf = fn;
+       d = width(fn);
+       if(n=chk_len(d)) return(n);
+       if(d==LFW)
+       {
+               scale = 0;
+               for(d=LFD,x=abs(fn);x>=1.0;x/=10.0,d--);
+               return(wrt_F(&f,LFW,d,(ftnlen)sizeof(float)));
+       }
+       else
+       {
+               scale = 1;
+               return(wrt_E(&f,LEW,LED-scale,LEE,(ftnlen)sizeof(float)));
+       }
+}
+
+lwrt_D(dn) double dn;
+{      int d,n; double x; ufloat f;
+       if(dn==0.0) return(lwrt_0());
+       f.pd = dn;
+       d = dwidth(dn);
+       if(n=chk_len(d)) return(n);
+       if(d==LDFW)
+       {
+               scale = 0;
+               for(d=LDFD,x=abs(dn);x>=1.0;x/=10.0,d--);
+               return(wrt_F(&f,LDFW,d,(ftnlen)sizeof(double)));
+       }
+       else
+       {
+               scale = 1;
+               return(wrt_E(&f,LDEW,LDED-scale,LDEE,(ftnlen)sizeof(double)));
+       }
+}
+
+lwrt_C(a,b) float a,b;
+{      int n;
+       if(n=chk_len(LCW)) return(n);
+       PUT(' ')
+       PUT(' ')
+       PUT('(')
+       if(n=lwrt_F(a)) return(n);
+       PUT(',')
+       if(n=lwrt_F(b)) return(n);
+       PUT(')')
+       return(OK);
+}
+
+lwrt_DC(a,b) double a,b;
+{      int n;
+       if(n=chk_len(LDCW)) return(n);
+       PUT(' ')
+       PUT(' ')
+       PUT('(')
+       if(n=lwrt_D(a)) return(n);
+       PUT(',')
+       if(n=lwrt_D(b)) return(n);
+       PUT(')')
+       return(OK);
+}
+
+lwrt_0()
+{      int n; char *z = "  0.";
+       if(n=chk_len(4)) return(n);
+       while(*z) PUT(*z++)
+       return(OK);
+}
+
+chk_len(w)
+{      int n;
+       if(recpos+w > line_len) PUT('\n')
+       return(OK);
+}