50 likes | 259 Views
mem.h. #define roundew(x) ( (3 + (WORD)(x)) & (~3) ) #define truncew(x) ( ((WORD)(x)) & (~3) ) #define getstk(n) getmem(n) #define freestk(b,s) freemem(b,s) typedef struct mblock { struct mblock *mnext; word mlen; } MBLOCK; #define end endaddr /* avoid C library conflict */
E N D
mem.h #define roundew(x) ( (3 + (WORD)(x)) & (~3) ) #define truncew(x) ( ((WORD)(x)) & (~3) ) #define getstk(n) getmem(n) #define freestk(b,s) freemem(b,s) typedef struct mblock { struct mblock *mnext; word mlen; } MBLOCK; #define end endaddr /* avoid C library conflict */ extern MBLOCK memlist; /* head of free memory list */ extern char *maxaddr; /* max memory address */ extern char *end; /* address beyond loaded memory */ #define MMAX 65024 /* maximum memory size */ #define MBLK 512 /* block size for global alloc */ #define MMIN 8192 /* minimum Xinu allocation */ #define MDOS 1024 /* save something for MS-DOS */ extern char *getmem(); extern int freemem();
memlist mlen mnext mlen mnext mlen NULL mnext Memory allocation List of free memory blocks char *getmem(word nbytes) { int ps; struct mblock *p, *q, *leftover; disable(ps); if ( nbytes == 0 ) { restore (ps); return ( NULL ); } nbytes = roundew (nbytes); for ( q=&memlist, p = q->mnext; (char *) p != NULL; q = p, p = p->mnext ) if ( p->mlen == nbytes ) { q->mnext = p->mnext; restore (ps); return ( (char *) p ); } else if ( p->mlen > nbytes ) { leftover = (struct mblock *) ( (char *) p + nbytes ); q->mnext = leftover; leftover->mnext = p->mnext; leftover->mlen = p->mlen – nbytes; restore (ps); return ( (char *) p ); } restore (ps); return( NULL ); }
Memory release SYSCALL freemem (char *block, word size) { int ps; struct mblock *p, *q; char *top; size = roundew (size); block = (char *) truncew ( (word) block ); if ( size == 0 || block > maxaddr || maxaddr – block < size || block < end ) return (SYSERR); disable (ps); ( char *) q = NULL; for ( p = memlist.mnext; (char*) p != NULL && (char*) p < block; q = p, p = p->mnext ) ; if ( (char*)q != NULL && (top = (char*)q + q->mlen > block || (char*)p != NULL && (block + size) > (char*)p ) { restore (pd); return (SYSERR); } if ( (char*)q != NULL && top == block ) q->mlen += size; else { ( ( stuct mblock *) block ) -> mlen = size; ( ( stuct mblock *) block ) -> mnext = p; q->mnext = (struct mblock*) block; } if ( ( char*) p != NULL && ( (char*) q + q->mlen ) == (char *)p ) { q->mlen += p->mlen; q->mnext = p->mnext; } restore (ps); return (OK); }
Queue initialization /* newqueue intializes new linked list */ int newqueue ( ) { struct qent *hptr; struct qent *tptr; int hindex, tindex; hptr = &q[hindex = nextqueue++] ; /* nextqueue is global variable */ tptr = &q[tindex = nextqueue ++ ] ; hptr->qnext = tindex; hptr->qprev = EMPTY; hptr->qkey = MININT; tptr->qnext = EMPTY; tptr->qprev = hindex; tptr->qkey = MAXINT; return (hindex); }
proc.h extern struct pentry proctab [ ] ; extern int numproc; /* no of currently active proc*/ extern int nextproc; /* next free slot */ extern int currpid; /* pid of running proc */ /* proc.h – isbadpid */ /* process state constants */ #define PRCURR ‘\01’ /* running */ #define PRFREE ‘\02’ /* process slot is free */ #define PRREADY ‘\03’ #define PRRECV ‘\04’ /* waiting for msg */ #define PRSLEEP ‘\05’ #define PRSUSP ‘\06’ /* suspended */ #define PRWAIT ‘\07’ /* waiting in sem que */ #define isbadpid(x) (x<=0 || x>=NPROC) struct pentry { char pstate; int pprio; int psem; /* sem where proc waits */ int pmsg; /* msg sent to this process */ char *pregs; /* saved regs (SP) */ char *pbase; /* base of the run-time stack */ word plen; /* stack length */ char pname[PNMLEN+1]; /* proc name */ int pargs; /* number of arguments */ int (*paddr) ( ); /* code address */ }