Skip to content

Commit

Permalink
make new code like old code
Browse files Browse the repository at this point in the history
Variable declarations at top of function,
separate from initialization.

Use == 0 instead of ! for checking pointers.

Consistent spacing around {, *, casts.

Declare 0-parameter functions as (void) not ().

Integer valued functions return -1 on failure, 0 on success.
  • Loading branch information
rsc committed Jan 11, 2011
1 parent 2406796 commit 1a81e38
Show file tree
Hide file tree
Showing 21 changed files with 227 additions and 199 deletions.
2 changes: 1 addition & 1 deletion bootmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bootmain(void)
// Load each program segment (ignores ph flags).
ph = (struct proghdr*)((uchar*)elf + elf->phoff);
eph = ph + elf->phnum;
for(; ph < eph; ph++) {
for(; ph < eph; ph++){
va = (uchar*)ph->va;
readseg(va, ph->filesz, ph->offset);
if(ph->memsz > ph->filesz)
Expand Down
5 changes: 3 additions & 2 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ printint(int xx, int base, int sgn)
{
static char digits[] = "0123456789abcdef";
char buf[16];
int i = 0, neg = 0;
int i, neg;
uint x;

if(sgn && xx < 0){
if(sgn && (neg = xx < 0)){
neg = 1;
x = -xx;
} else
x = xx;

i = 0;
do{
buf[i++] = digits[x % base];
}while((x /= base) != 0);
Expand Down
8 changes: 4 additions & 4 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void ioapicinit(void);
// kalloc.c
char* kalloc(void);
void kfree(char*);
void kinit();
void kinit(void);

// kbd.c
void kbdintr(void);
Expand Down Expand Up @@ -117,8 +117,8 @@ void getcallerpcs(void*, uint*);
int holding(struct spinlock*);
void initlock(struct spinlock*, char*);
void release(struct spinlock*);
void pushcli();
void popcli();
void pushcli(void);
void popcli(void);

// string.c
int memcmp(const void*, const void*, uint);
Expand Down Expand Up @@ -164,7 +164,7 @@ void inituvm(pde_t*, char*, uint);
int loaduvm(pde_t*, char*, struct inode *, uint, uint);
pde_t* copyuvm(pde_t*,uint);
void switchuvm(struct proc*);
void switchkvm();
void switchkvm(void);
int copyout(pde_t *pgdir, uint va, void *buf, uint len);

// number of elements in fixed-size array
Expand Down
24 changes: 12 additions & 12 deletions exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,47 @@ int
exec(char *path, char **argv)
{
char *s, *last;
int i, off;
uint sz = 0;
int i, off, argc;
uint sz, sp, strings[MAXARG];
struct elfhdr elf;
struct inode *ip = 0;
struct inode *ip;
struct proghdr ph;
pde_t *pgdir = 0, *oldpgdir;
pde_t *pgdir, *oldpgdir;

if((ip = namei(path)) == 0)
return -1;
ilock(ip);
pgdir = 0;

// Check ELF header
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
goto bad;
if(elf.magic != ELF_MAGIC)
goto bad;

if(!(pgdir = setupkvm()))
if((pgdir = setupkvm()) == 0)
goto bad;

// Load program into memory.
sz = 0;
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
goto bad;
if(ph.type != ELF_PROG_LOAD)
continue;
if(ph.memsz < ph.filesz)
goto bad;
if(!(sz = allocuvm(pgdir, sz, ph.va + ph.memsz)))
if((sz = allocuvm(pgdir, sz, ph.va + ph.memsz)) == 0)
goto bad;
if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
if(loaduvm(pgdir, (char*)ph.va, ip, ph.offset, ph.filesz) < 0)
goto bad;
}
iunlockput(ip);
ip = 0;

// Allocate a one-page stack at the next page boundary
sz = PGROUNDUP(sz);
if(!(sz = allocuvm(pgdir, sz, sz + PGSIZE)))
if((sz = allocuvm(pgdir, sz, sz + PGSIZE)) == 0)
goto bad;

// initialize stack content:
Expand All @@ -64,24 +66,22 @@ exec(char *path, char **argv)
// argc -- argc argument to main()
// ffffffff -- return PC for main() call

uint sp = sz;
sp = sz;

// count arguments
int argc;
for(argc = 0; argv[argc]; argc++)
;
if(argc >= MAXARG)
goto bad;

// push strings and remember where they are
uint strings[MAXARG];
for(i = argc - 1; i >= 0; --i){
sp -= strlen(argv[i]) + 1;
strings[i] = sp;
copyout(pgdir, sp, argv[i], strlen(argv[i]) + 1);
}

#define PUSH(x) { int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }
#define PUSH(x){ int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }

PUSH(0); // argv[argc] is zero

Expand Down
4 changes: 2 additions & 2 deletions ide.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ iderw(struct buf *b)
if((b->flags & (B_VALID|B_DIRTY)) == B_VALID)
panic("iderw: nothing to do");
if(b->dev != 0 && !havedisk1)
panic("idrw: ide disk 1 not present");
panic("iderw: ide disk 1 not present");

acquire(&idelock);

Expand All @@ -147,7 +147,7 @@ iderw(struct buf *b)

// Wait for request to finish.
// Assuming will not sleep too long: ignore proc->killed.
while((b->flags & (B_VALID|B_DIRTY)) != B_VALID) {
while((b->flags & (B_VALID|B_DIRTY)) != B_VALID){
sleep(b, &idelock);
}

Expand Down
14 changes: 8 additions & 6 deletions kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ extern char end[]; // first address after kernel loaded from ELF file
void
kinit(void)
{
char *p;

initlock(&kmem.lock, "kmem");
char *p = (char*)PGROUNDUP((uint)end);
for( ; p + PGSIZE - 1 < (char*) PHYSTOP; p += PGSIZE)
p = (char*)PGROUNDUP((uint)end);
for(; p + PGSIZE - 1 < (char*)PHYSTOP; p += PGSIZE)
kfree(p);
}

Expand All @@ -39,14 +41,14 @@ kfree(char *v)
{
struct run *r;

if(((uint) v) % PGSIZE || v < end || (uint)v >= PHYSTOP)
if((uint)v % PGSIZE || v < end || (uint)v >= PHYSTOP)
panic("kfree");

// Fill with junk to catch dangling refs.
memset(v, 1, PGSIZE);

acquire(&kmem.lock);
r = (struct run *) v;
r = (struct run*)v;
r->next = kmem.freelist;
kmem.freelist = r;
release(&kmem.lock);
Expand All @@ -56,7 +58,7 @@ kfree(char *v)
// Returns a pointer that the kernel can use.
// Returns 0 if the memory cannot be allocated.
char*
kalloc()
kalloc(void)
{
struct run *r;

Expand All @@ -65,6 +67,6 @@ kalloc()
if(r)
kmem.freelist = r->next;
release(&kmem.lock);
return (char*) r;
return (char*)r;
}

30 changes: 15 additions & 15 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

static void bootothers(void);
static void mpmain(void);
void jkstack(void) __attribute__((noreturn));
void jmpkstack(void) __attribute__((noreturn));
void mainc(void);

// Bootstrap processor starts running C code here.
Expand All @@ -20,19 +20,20 @@ main(void)
lapicinit(mpbcpu());
seginit(); // set up segments
kinit(); // initialize memory allocator
jkstack(); // call mainc() on a properly-allocated stack
jmpkstack(); // call mainc() on a properly-allocated stack
}

void
jkstack(void)
jmpkstack(void)
{
char *kstack = kalloc();
if(!kstack)
panic("jkstack\n");
char *top = kstack + PGSIZE;
asm volatile("movl %0,%%esp" : : "r" (top));
asm volatile("call mainc");
panic("jkstack");
char *kstack, *top;

kstack = kalloc();
if(kstack == 0)
panic("jmpkstack kalloc");
top = kstack + PGSIZE;
asm volatile("movl %0,%%esp; call mainc" : : "r" (top));
panic("jmpkstack");
}

// Set up hardware and software.
Expand Down Expand Up @@ -67,7 +68,7 @@ mainc(void)
static void
mpmain(void)
{
if(cpunum() != mpbcpu()) {
if(cpunum() != mpbcpu()){
seginit();
lapicinit(cpunum());
}
Expand All @@ -87,9 +88,9 @@ bootothers(void)
struct cpu *c;
char *stack;

// Write bootstrap code to unused memory at 0x7000. The linker has
// placed the start of bootother.S there.
code = (uchar *) 0x7000;
// Write bootstrap code to unused memory at 0x7000.
// The linker has placed the image of bootother.S in _binary_bootother_start.
code = (uchar*)0x7000;
memmove(code, _binary_bootother_start, (uint)_binary_bootother_size);

for(c = cpus; c < cpus+ncpu; c++){
Expand All @@ -110,4 +111,3 @@ bootothers(void)
;
}
}

24 changes: 12 additions & 12 deletions mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ushort
xshort(ushort x)
{
ushort y;
uchar *a = (uchar*) &y;
uchar *a = (uchar*)&y;
a[0] = x;
a[1] = x >> 8;
return y;
Expand All @@ -45,7 +45,7 @@ uint
xint(uint x)
{
uint y;
uchar *a = (uchar*) &y;
uchar *a = (uchar*)&y;
a[0] = x;
a[1] = x >> 8;
a[2] = x >> 16;
Expand Down Expand Up @@ -177,7 +177,7 @@ winode(uint inum, struct dinode *ip)

bn = i2b(inum);
rsect(bn, buf);
dip = ((struct dinode*) buf) + (inum % IPB);
dip = ((struct dinode*)buf) + (inum % IPB);
*dip = *ip;
wsect(bn, buf);
}
Expand All @@ -191,7 +191,7 @@ rinode(uint inum, struct dinode *ip)

bn = i2b(inum);
rsect(bn, buf);
dip = ((struct dinode*) buf) + (inum % IPB);
dip = ((struct dinode*)buf) + (inum % IPB);
*ip = *dip;
}

Expand Down Expand Up @@ -231,7 +231,7 @@ balloc(int used)
printf("balloc: first %d blocks have been allocated\n", used);
assert(used < 512);
bzero(buf, 512);
for(i = 0; i < used; i++) {
for(i = 0; i < used; i++){
buf[i/8] = buf[i/8] | (0x1 << (i%8));
}
printf("balloc: write bitmap block at sector %zu\n", ninodes/IPB + 3);
Expand All @@ -243,7 +243,7 @@ balloc(int used)
void
iappend(uint inum, void *xp, int n)
{
char *p = (char*) xp;
char *p = (char*)xp;
uint fbn, off, n1;
struct dinode din;
char buf[512];
Expand All @@ -256,24 +256,24 @@ iappend(uint inum, void *xp, int n)
while(n > 0){
fbn = off / 512;
assert(fbn < MAXFILE);
if(fbn < NDIRECT) {
if(xint(din.addrs[fbn]) == 0) {
if(fbn < NDIRECT){
if(xint(din.addrs[fbn]) == 0){
din.addrs[fbn] = xint(freeblock++);
usedblocks++;
}
x = xint(din.addrs[fbn]);
} else {
if(xint(din.addrs[NDIRECT]) == 0) {
if(xint(din.addrs[NDIRECT]) == 0){
// printf("allocate indirect block\n");
din.addrs[NDIRECT] = xint(freeblock++);
usedblocks++;
}
// printf("read indirect block\n");
rsect(xint(din.addrs[NDIRECT]), (char*) indirect);
if(indirect[fbn - NDIRECT] == 0) {
rsect(xint(din.addrs[NDIRECT]), (char*)indirect);
if(indirect[fbn - NDIRECT] == 0){
indirect[fbn - NDIRECT] = xint(freeblock++);
usedblocks++;
wsect(xint(din.addrs[NDIRECT]), (char*) indirect);
wsect(xint(din.addrs[NDIRECT]), (char*)indirect);
}
x = xint(indirect[fbn-NDIRECT]);
}
Expand Down
Loading

0 comments on commit 1a81e38

Please sign in to comment.