Skip to content

Commit

Permalink
chore: do fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
alissa-tung committed Feb 9, 2023
1 parent b79f4d8 commit 7e6ca8d
Show file tree
Hide file tree
Showing 60 changed files with 2,337 additions and 3,146 deletions.
47 changes: 16 additions & 31 deletions kernel/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// * Only one process at a time can use a buffer,
// so do not keep them longer than necessary.


#include "types.h"
#include "param.h"
#include "spinlock.h"
Expand All @@ -33,17 +32,15 @@ struct {
struct buf head;
} bcache;

void
binit(void)
{
void binit(void) {
struct buf *b;

initlock(&bcache.lock, "bcache");

// Create linked list of buffers
bcache.head.prev = &bcache.head;
bcache.head.next = &bcache.head;
for(b = bcache.buf; b < bcache.buf+NBUF; b++){
for (b = bcache.buf; b < bcache.buf + NBUF; b++) {
b->next = bcache.head.next;
b->prev = &bcache.head;
initsleeplock(&b->lock, "buffer");
Expand All @@ -55,16 +52,14 @@ binit(void)
// Look through buffer cache for block on device dev.
// If not found, allocate a buffer.
// In either case, return locked buffer.
static struct buf*
bget(uint dev, uint blockno)
{
static struct buf *bget(uint dev, uint blockno) {
struct buf *b;

acquire(&bcache.lock);

// Is the block already cached?
for(b = bcache.head.next; b != &bcache.head; b = b->next){
if(b->dev == dev && b->blockno == blockno){
for (b = bcache.head.next; b != &bcache.head; b = b->next) {
if (b->dev == dev && b->blockno == blockno) {
b->refcnt++;
release(&bcache.lock);
acquiresleep(&b->lock);
Expand All @@ -74,8 +69,8 @@ bget(uint dev, uint blockno)

// Not cached.
// Recycle the least recently used (LRU) unused buffer.
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
if(b->refcnt == 0) {
for (b = bcache.head.prev; b != &bcache.head; b = b->prev) {
if (b->refcnt == 0) {
b->dev = dev;
b->blockno = blockno;
b->valid = 0;
Expand All @@ -89,34 +84,28 @@ bget(uint dev, uint blockno)
}

// Return a locked buf with the contents of the indicated block.
struct buf*
bread(uint dev, uint blockno)
{
struct buf *bread(uint dev, uint blockno) {
struct buf *b;

b = bget(dev, blockno);
if(!b->valid) {
if (!b->valid) {
virtio_disk_rw(b, 0);
b->valid = 1;
}
return b;
}

// Write b's contents to disk. Must be locked.
void
bwrite(struct buf *b)
{
if(!holdingsleep(&b->lock))
void bwrite(struct buf *b) {
if (!holdingsleep(&b->lock))
panic("bwrite");
virtio_disk_rw(b, 1);
}

// Release a locked buffer.
// Move to the head of the most-recently-used list.
void
brelse(struct buf *b)
{
if(!holdingsleep(&b->lock))
void brelse(struct buf *b) {
if (!holdingsleep(&b->lock))
panic("brelse");

releasesleep(&b->lock);
Expand All @@ -132,22 +121,18 @@ brelse(struct buf *b)
bcache.head.next->prev = b;
bcache.head.next = b;
}

release(&bcache.lock);
}

void
bpin(struct buf *b) {
void bpin(struct buf *b) {
acquire(&bcache.lock);
b->refcnt++;
release(&bcache.lock);
}

void
bunpin(struct buf *b) {
void bunpin(struct buf *b) {
acquire(&bcache.lock);
b->refcnt--;
release(&bcache.lock);
}


5 changes: 2 additions & 3 deletions kernel/buf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct buf {
int valid; // has data been read from disk?
int disk; // does disk "own" buf?
int valid; // has data been read from disk?
int disk; // does disk "own" buf?
uint dev;
uint blockno;
struct sleeplock lock;
Expand All @@ -9,4 +9,3 @@ struct buf {
struct buf *next;
uchar data[BSIZE];
};

72 changes: 32 additions & 40 deletions kernel/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,44 @@
#include "proc.h"

#define BACKSPACE 0x100
#define C(x) ((x)-'@') // Control-x
#define C(x) ((x) - '@') // Control-x

//
// send one character to the uart.
// called by printf(), and to echo input characters,
// but not from write().
//
void
consputc(int c)
{
if(c == BACKSPACE){
void consputc(int c) {
if (c == BACKSPACE) {
// if the user typed backspace, overwrite with a space.
uartputc_sync('\b'); uartputc_sync(' '); uartputc_sync('\b');
uartputc_sync('\b');
uartputc_sync(' ');
uartputc_sync('\b');
} else {
uartputc_sync(c);
}
}

struct {
struct spinlock lock;

// input
#define INPUT_BUF_SIZE 128
char buf[INPUT_BUF_SIZE];
uint r; // Read index
uint w; // Write index
uint e; // Edit index
uint r; // Read index
uint w; // Write index
uint e; // Edit index
} cons;

//
// user write()s to the console go here.
//
int
consolewrite(int user_src, uint64 src, int n)
{
int consolewrite(int user_src, uint64 src, int n) {
int i;

for(i = 0; i < n; i++){
for (i = 0; i < n; i++) {
char c;
if(either_copyin(&c, user_src, src+i, 1) == -1)
if (either_copyin(&c, user_src, src + i, 1) == -1)
break;
uartputc(c);
}
Expand All @@ -76,20 +74,18 @@ consolewrite(int user_src, uint64 src, int n)
// user_dist indicates whether dst is a user
// or kernel address.
//
int
consoleread(int user_dst, uint64 dst, int n)
{
int consoleread(int user_dst, uint64 dst, int n) {
uint target;
int c;
char cbuf;

target = n;
acquire(&cons.lock);
while(n > 0){
while (n > 0) {
// wait until interrupt handler has put some
// input into cons.buffer.
while(cons.r == cons.w){
if(killed(myproc())){
while (cons.r == cons.w) {
if (killed(myproc())) {
release(&cons.lock);
return -1;
}
Expand All @@ -98,8 +94,8 @@ consoleread(int user_dst, uint64 dst, int n)

c = cons.buf[cons.r++ % INPUT_BUF_SIZE];

if(c == C('D')){ // end-of-file
if(n < target){
if (c == C('D')) { // end-of-file
if (n < target) {
// Save ^D for next time, to make sure
// caller gets a 0-byte result.
cons.r--;
Expand All @@ -109,13 +105,13 @@ consoleread(int user_dst, uint64 dst, int n)

// copy the input byte to the user-space buffer.
cbuf = c;
if(either_copyout(user_dst, dst, &cbuf, 1) == -1)
if (either_copyout(user_dst, dst, &cbuf, 1) == -1)
break;

dst++;
--n;

if(c == '\n'){
if (c == '\n') {
// a whole line has arrived, return to
// the user-level read().
break;
Expand All @@ -132,31 +128,29 @@ consoleread(int user_dst, uint64 dst, int n)
// do erase/kill processing, append to cons.buf,
// wake up consoleread() if a whole line has arrived.
//
void
consoleintr(int c)
{
void consoleintr(int c) {
acquire(&cons.lock);

switch(c){
case C('P'): // Print process list.
switch (c) {
case C('P'): // Print process list.
procdump();
break;
case C('U'): // Kill line.
while(cons.e != cons.w &&
cons.buf[(cons.e-1) % INPUT_BUF_SIZE] != '\n'){
case C('U'): // Kill line.
while (cons.e != cons.w &&
cons.buf[(cons.e - 1) % INPUT_BUF_SIZE] != '\n') {
cons.e--;
consputc(BACKSPACE);
}
break;
case C('H'): // Backspace
case '\x7f': // Delete key
if(cons.e != cons.w){
if (cons.e != cons.w) {
cons.e--;
consputc(BACKSPACE);
}
break;
default:
if(c != 0 && cons.e-cons.r < INPUT_BUF_SIZE){
if (c != 0 && cons.e - cons.r < INPUT_BUF_SIZE) {
c = (c == '\r') ? '\n' : c;

// echo back to the user.
Expand All @@ -165,7 +159,7 @@ consoleintr(int c)
// store for consumption by consoleread().
cons.buf[cons.e++ % INPUT_BUF_SIZE] = c;

if(c == '\n' || c == C('D') || cons.e-cons.r == INPUT_BUF_SIZE){
if (c == '\n' || c == C('D') || cons.e - cons.r == INPUT_BUF_SIZE) {
// wake up consoleread() if a whole line (or end-of-file)
// has arrived.
cons.w = cons.e;
Expand All @@ -174,13 +168,11 @@ consoleintr(int c)
}
break;
}

release(&cons.lock);
}

void
consoleinit(void)
{
void consoleinit(void) {
initlock(&cons.lock, "cons");

uartinit();
Expand Down
Loading

0 comments on commit 7e6ca8d

Please sign in to comment.