From b6095304b7ee2b69d2ee4a9a7265999d1a2b9675 Mon Sep 17 00:00:00 2001
From: rsc <rsc>
Date: Fri, 10 Aug 2007 16:37:27 +0000
Subject: [PATCH] Make cp a magic symbol.

---
 console.c |  2 +-
 fs.c      | 11 +++++------
 kalloc.c  | 14 ++++++--------
 pipe.c    |  4 ++--
 proc.c    | 14 ++++----------
 proc.h    | 10 ++++++++++
 syscall.c |  5 +----
 sysfile.c |  7 +------
 sysproc.c |  5 ++---
 trap.c    |  1 -
 10 files changed, 32 insertions(+), 41 deletions(-)

diff --git a/console.c b/console.c
index cd52570..d119a29 100644
--- a/console.c
+++ b/console.c
@@ -397,7 +397,7 @@ console_read(int minor, char *dst, int n)
   acquire(&kbd_lock);
   while(n > 0){
     while(kbd_r == kbd_w){
-      if(curproc[cpu()]->killed){
+      if(cp->killed){
         release(&kbd_lock);
         return -1;
       }
diff --git a/fs.c b/fs.c
index 7c5e0ca..66d8c73 100644
--- a/fs.c
+++ b/fs.c
@@ -557,7 +557,6 @@ namei(char *path, int mode, uint *ret_off,
       char **ret_last, struct inode **ret_ip)
 {
   struct inode *dp;
-  struct proc *cp = curproc[cpu()];
   char *name;
   int namelen;
   uint off, dev, inum;
@@ -648,15 +647,15 @@ wdir(struct inode *dp, char *name, uint ino)
     panic("wdir write");
 }
 
-// Create the path cp and return its locked inode structure.
+// Create the path and return its locked inode structure.
 // If cp already exists, return 0.
 struct inode*
-mknod(char *cp, short type, short major, short minor)
+mknod(char *path, short type, short major, short minor)
 {
   struct inode *ip, *dp;
   char *last;
 
-  if((dp = namei(cp, NAMEI_CREATE, 0, &last, 0)) == 0)
+  if((dp = namei(path, NAMEI_CREATE, 0, &last, 0)) == 0)
     return 0;
 
   ip = mknod1(dp, last, type, major, minor);
@@ -691,13 +690,13 @@ mknod1(struct inode *dp, char *name, short type, short major, short minor)
 
 // Unlink the inode named cp.
 int
-unlink(char *cp)
+unlink(char *path)
 {
   struct inode *ip, *dp;
   struct dirent de;
   uint off, inum, dev;
 
-  dp = namei(cp, NAMEI_DELETE, &off, 0, 0);
+  dp = namei(path, NAMEI_DELETE, &off, 0, 0);
   if(dp == 0)
     return -1;
 
diff --git a/kalloc.c b/kalloc.c
index bfa9207..cc2faac 100644
--- a/kalloc.c
+++ b/kalloc.c
@@ -40,24 +40,22 @@ kinit(void)
   kfree(start, mem * PAGE);
 }
 
-// Free the len bytes of memory pointed at by cp,
+// Free the len bytes of memory pointed at by v,
 // which normally should have been returned by a
-// call to kalloc(cp).  (The exception is when
+// call to kalloc(len).  (The exception is when
 // initializing the allocator; see kinit above.)
 void
-kfree(char *cp, int len)
+kfree(char *v, int len)
 {
   struct run **rr;
-  struct run *p = (struct run*) cp;
-  struct run *pend = (struct run*) (cp + len);
-  int i;
+  struct run *p = (struct run*)v;
+  struct run *pend = (struct run*)(v + len);
 
   if(len % PAGE)
     panic("kfree");
 
   // Fill with junk to catch dangling refs.
-  for(i = 0; i < len; i++)
-    cp[i] = 1;
+  memset(v, 1, len);
 
   acquire(&kalloc_lock);
 
diff --git a/pipe.c b/pipe.c
index 284d4ee..b866a0e 100644
--- a/pipe.c
+++ b/pipe.c
@@ -87,7 +87,7 @@ pipe_write(struct pipe *p, char *addr, int n)
 
   for(i = 0; i < n; i++){
     while(((p->writep + 1) % PIPESIZE) == p->readp){
-      if(p->readopen == 0 || curproc[cpu()]->killed){
+      if(p->readopen == 0 || cp->killed){
         release(&p->lock);
         return -1;
       }
@@ -111,7 +111,7 @@ pipe_read(struct pipe *p, char *addr, int n)
   acquire(&p->lock);
 
   while(p->readp == p->writep){
-    if(p->writeopen == 0 || curproc[cpu()]->killed){
+    if(p->writeopen == 0 || cp->killed){
       release(&p->lock);
       return 0;
     }
diff --git a/proc.c b/proc.c
index 89eb1d4..6df3814 100644
--- a/proc.c
+++ b/proc.c
@@ -59,7 +59,6 @@ setupsegs(struct proc *p)
 int
 growproc(int n)
 {
-  struct proc *cp = curproc[cpu()];
   char *newmem, *oldmem;
 
   newmem = kalloc(cp->sz + n);
@@ -183,14 +182,14 @@ scheduler(void)
       // before jumping back to us.
 
       setupsegs(p);
-      curproc[cpu()] = p;
+      cp = p;
       p->state = RUNNING;
       if(setjmp(&cpus[cpu()].jmpbuf) == 0)
         longjmp(&p->jmpbuf);
 
       // Process is done running for now.
       // It should have changed its p->state before coming back.
-      curproc[cpu()] = 0;
+      cp = 0;
 
       setupsegs(0);
     }
@@ -204,7 +203,6 @@ scheduler(void)
 void
 sched(void)
 {
-  struct proc *cp = curproc[cpu()];
 
   if(cp->state == RUNNING)
     panic("sched running");
@@ -213,7 +211,7 @@ sched(void)
   if(cpus[cpu()].nlock != 1)
     panic("sched locks");
 
-  if(setjmp(&p->jmpbuf) == 0)
+  if(setjmp(&cp->jmpbuf) == 0)
     longjmp(&cpus[cpu()].jmpbuf);
 }
 
@@ -221,7 +219,6 @@ sched(void)
 void
 yield(void)
 {
-  struct proc *cp = curproc[cpu()];
 
   acquire(&proc_table_lock);
   cp->state = RUNNABLE;
@@ -238,7 +235,7 @@ forkret(void)
   release(&proc_table_lock);
 
   // Jump into assembly, never to return.
-  forkret1(curproc[cpu()]->tf);
+  forkret1(cp->tf);
 }
 
 // Atomically release lock and sleep on chan.
@@ -246,7 +243,6 @@ forkret(void)
 void
 sleep(void *chan, struct spinlock *lk)
 {
-  struct proc *cp = curproc[cpu()];
 
   if(cp == 0)
     panic("sleep");
@@ -332,7 +328,6 @@ void
 proc_exit(void)
 {
   struct proc *p;
-  struct proc *cp = curproc[cpu()];
   int fd;
 
   if(cp->pid == 1)
@@ -376,7 +371,6 @@ int
 proc_wait(void)
 {
   struct proc *p;
-  struct proc *cp = curproc[cpu()];
   int i, havekids, pid;
 
   acquire(&proc_table_lock);
diff --git a/proc.h b/proc.h
index 2848ecd..7a04cd5 100644
--- a/proc.h
+++ b/proc.h
@@ -50,7 +50,17 @@ struct proc {
 //   expandable heap
 
 extern struct proc proc[];
+
+// If xv6 was only for uniprocessors, this could be
+//   struct proc *cp;
+// Instead we have an array curproc, one per
+// processor, and #define cp to the right element
+// in the array.  In general such preprocessor 
+// subterfuge is to be avoided, but cp is used 
+// so often that having the shorthand is worth the ugliness.
 extern struct proc *curproc[NCPU];  // Current (running) process per CPU
+#define cp (curproc[cpu()])  // Current process on this CPU
+
 
 #define MPSTACK 512
 
diff --git a/syscall.c b/syscall.c
index adc2b58..d697337 100644
--- a/syscall.c
+++ b/syscall.c
@@ -53,7 +53,6 @@ fetchstr(struct proc *p, uint addr, char **pp)
 int
 argint(int argno, int *ip)
 {
-  struct proc *cp = curproc[cpu()];
 
   return fetchint(cp, cp->tf->esp + 4 + 4*argno, ip);
 }
@@ -65,7 +64,6 @@ int
 argptr(int argno, char **pp, int size)
 {
   int i;
-  struct proc *cp = curproc[cpu()];
   
   if(argint(argno, &i) < 0)
     return -1;
@@ -85,7 +83,7 @@ argstr(int argno, char **pp)
   int addr;
   if(argint(argno, &addr) < 0)
     return -1;
-  return fetchstr(curproc[cpu()], addr, pp);
+  return fetchstr(cp, addr, pp);
 }
 
 extern int sys_chdir(void);
@@ -133,7 +131,6 @@ static int (*syscalls[])(void) = {
 void
 syscall(void)
 {
-  struct proc *cp = curproc[cpu()];
   int num = cp->tf->eax;
 
   if(num >= 0 && num < NELEM(syscalls) && syscalls[num])
diff --git a/sysfile.c b/sysfile.c
index ecddd2b..a73f250 100644
--- a/sysfile.c
+++ b/sysfile.c
@@ -22,7 +22,6 @@ argfd(int argno, int *pfd, struct file **pf)
 {
   int fd;
   struct file *f;
-  struct proc *cp = curproc[cpu()];
 
   if(argint(argno, &fd) < 0)
     return -1;
@@ -41,7 +40,6 @@ static int
 fdalloc(struct file *f)
 {
   int fd;
-  struct proc *cp = curproc[cpu()];
 
   for(fd = 0; fd < NOFILE; fd++){
     if(cp->ofile[fd] == 0){
@@ -58,7 +56,6 @@ sys_pipe(void)
   int *fd;
   struct file *rf = 0, *wf = 0;
   int fd0, fd1;
-  struct proc *cp = curproc[cpu()];
 
   if(argptr(0, (void*)&fd, 2*sizeof fd[0]) < 0)
     return -1;
@@ -109,7 +106,7 @@ sys_close(void)
   
   if(argfd(0, &fd, &f) < 0)
     return -1;
-  curproc[cpu()]->ofile[fd] = 0;
+  cp->ofile[fd] = 0;
   fileclose(f);
   return 0;
 }
@@ -242,7 +239,6 @@ sys_mkdir(void)
 int
 sys_chdir(void)
 {
-  struct proc *cp = curproc[cpu()];
   struct inode *ip;
   char *path;
 
@@ -316,7 +312,6 @@ sys_link(void)
 int
 sys_exec(void)
 {
-  struct proc *cp = curproc[cpu()];
   uint sz=0, ap, sp, p1, p2;
   int i, nargs, argbytes, len;
   struct inode *ip;
diff --git a/sysproc.c b/sysproc.c
index 14b85c5..9d599de 100644
--- a/sysproc.c
+++ b/sysproc.c
@@ -20,7 +20,7 @@ sys_fork(void)
 {
   struct proc *np;
 
-  if((np = copyproc(curproc[cpu()])) == 0)
+  if((np = copyproc(cp)) == 0)
     return -1;
   np->state = RUNNABLE;
   return np->pid;
@@ -52,7 +52,7 @@ sys_kill(void)
 int
 sys_getpid(void)
 {
-  return curproc[cpu()]->pid;
+  return cp->pid;
 }
 
 int
@@ -60,7 +60,6 @@ sys_sbrk(void)
 {
   int addr;
   int n;
-  struct proc *cp = curproc[cpu()];
 
   if(argint(0, &n) < 0)
     return -1;
diff --git a/trap.c b/trap.c
index 0c4a956..7a47516 100644
--- a/trap.c
+++ b/trap.c
@@ -31,7 +31,6 @@ void
 trap(struct trapframe *tf)
 {
   int v = tf->trapno;
-  struct proc *cp = curproc[cpu()];
 
   if(v == T_SYSCALL){
     if(cp->killed)
-- 
GitLab