Exercise 2.9.1
This commit is contained in:
parent
a3dc50bff7
commit
ca88006b5b
8 changed files with 81 additions and 1 deletions
4
Makefile
4
Makefile
|
@ -28,7 +28,8 @@ OBJS = \
|
|||
$K/sysfile.o \
|
||||
$K/kernelvec.o \
|
||||
$K/plic.o \
|
||||
$K/virtio_disk.o
|
||||
$K/virtio_disk.o \
|
||||
$K/freeram.o
|
||||
|
||||
# riscv64-unknown-elf- or riscv64-linux-gnu-
|
||||
# perhaps in /opt/riscv/bin
|
||||
|
@ -132,6 +133,7 @@ UPROGS=\
|
|||
$U/_ln\
|
||||
$U/_ls\
|
||||
$U/_mkdir\
|
||||
$U/_freeram\
|
||||
$U/_rm\
|
||||
$U/_sh\
|
||||
$U/_stressfs\
|
||||
|
|
|
@ -187,3 +187,6 @@ void virtio_disk_intr(void);
|
|||
|
||||
// number of elements in fixed-size array
|
||||
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
|
||||
|
||||
// freeram.c exercise 2.9.1
|
||||
uint64 freeram(void);
|
||||
|
|
34
kernel/freeram.c
Normal file
34
kernel/freeram.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include "types.h"
|
||||
#include "param.h"
|
||||
#include "memlayout.h"
|
||||
#include "spinlock.h"
|
||||
#include "riscv.h"
|
||||
#include "defs.h"
|
||||
|
||||
struct run {
|
||||
struct run *next;
|
||||
};
|
||||
|
||||
extern struct {
|
||||
struct spinlock lock;
|
||||
struct run *freelist;
|
||||
} kmem;
|
||||
|
||||
uint64
|
||||
sys_freeram(void)
|
||||
{
|
||||
struct run *r;
|
||||
uint64 free_pages = 0;
|
||||
|
||||
acquire(&kmem.lock);
|
||||
|
||||
// loop over free pages' list
|
||||
r = kmem.freelist;
|
||||
while (r != 0) {
|
||||
free_pages++;
|
||||
r = r->next;
|
||||
}
|
||||
release(&kmem.lock);
|
||||
|
||||
return free_pages * PGSIZE;
|
||||
}
|
|
@ -101,6 +101,7 @@ extern uint64 sys_unlink(void);
|
|||
extern uint64 sys_link(void);
|
||||
extern uint64 sys_mkdir(void);
|
||||
extern uint64 sys_close(void);
|
||||
extern uint64 sys_freeram(void); // exercise 2.9.1
|
||||
|
||||
// An array mapping syscall numbers from syscall.h
|
||||
// to the function that handles the system call.
|
||||
|
@ -126,6 +127,7 @@ static uint64 (*syscalls[])(void) = {
|
|||
[SYS_link] sys_link,
|
||||
[SYS_mkdir] sys_mkdir,
|
||||
[SYS_close] sys_close,
|
||||
[SYS_freeram] sys_freeram, // exercise 2.9.1
|
||||
};
|
||||
|
||||
void
|
||||
|
|
|
@ -20,3 +20,4 @@
|
|||
#define SYS_link 19
|
||||
#define SYS_mkdir 20
|
||||
#define SYS_close 21
|
||||
#define SYS_freeram 22 // exercise 2.9.1
|
||||
|
|
36
user/freeram.c
Normal file
36
user/freeram.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/riscv.h" // to get PGSIZE
|
||||
#include "user/user.h"
|
||||
|
||||
#define KIBIBYTES 1024
|
||||
#define MEBIBYTES (1024 * 1024)
|
||||
void
|
||||
display_memory(void)
|
||||
{
|
||||
uint64 free_ram, npages, nm, nk;
|
||||
free_ram = freeram();
|
||||
npages = free_ram / PGSIZE;
|
||||
nm = free_ram / MEBIBYTES;
|
||||
nk = free_ram / KIBIBYTES;
|
||||
printf("%ld bytes (%ld MiB or %ld KiB), %ld pages ", free_ram, nm, nk, npages);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
void *mem;
|
||||
|
||||
display_memory();
|
||||
printf("free memory at the start\n");
|
||||
|
||||
mem = malloc(PGSIZE * 1000);
|
||||
display_memory();
|
||||
printf("after 1000*PGSIZE mem alloc\n");
|
||||
|
||||
free(mem);
|
||||
display_memory();
|
||||
printf("after dealloc\n");
|
||||
|
||||
exit(0);
|
||||
}
|
|
@ -22,6 +22,7 @@ int getpid(void);
|
|||
char* sbrk(int);
|
||||
int sleep(int);
|
||||
int uptime(void);
|
||||
uint64 freeram(void); // exercise 2.9.1
|
||||
|
||||
// ulib.c
|
||||
int stat(const char*, struct stat*);
|
||||
|
|
|
@ -36,3 +36,4 @@ entry("getpid");
|
|||
entry("sbrk");
|
||||
entry("sleep");
|
||||
entry("uptime");
|
||||
entry("freeram");
|
||||
|
|
Loading…
Add table
Reference in a new issue