Skip to content

Commit

Permalink
Free process heap on exit. Display free memory size on status.
Browse files Browse the repository at this point in the history
  • Loading branch information
pykello committed Aug 11, 2014
1 parent bc5c8e1 commit ebfc1a7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/kalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ void *kalloc();
void kfree(void *page);
void *kalloc1k();
void kfree1k(void *page);
uint32_t get_free_memory_size(void);

#endif
1 change: 1 addition & 0 deletions include/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void proc_init(void);
struct Process *proc_create(void);
void proc_free(struct Process *proc);
void proc_expand_memory(struct Process *proc, int page_count);
void proc_shrink_memory(struct Process *proc, int page_count);
bool proc_load(struct Process *proc, char **proc_image, int page_count);
void proc_switch(struct Process *proc);

Expand Down
21 changes: 21 additions & 0 deletions kernel/kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ void kfree1k(void *page)
free_list_1k = page_list_prepend(free_list_1k, page);
}

uint32_t get_free_memory_size(void) {
uint32_t result = 0;
struct PageList *current_page = NULL;

/* iterate over free 1k pages */
current_page = free_list_1k;
while (current_page != NULL) {
result += 1024;
current_page = current_page->next;
}

/* iterate over free 4k pages */
current_page = free_list_4k;
while (current_page != NULL) {
result += 4096;
current_page = current_page->next;
}

return result;
}

/*
* page_list_prepend adds the given page to the beginning of the page list
* and returns the address of the new page list.
Expand Down
9 changes: 9 additions & 0 deletions kernel/monitor/mon_status.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <monitor.h>
#include <kalloc.h>
#include <klib.h>
#include <lib/string.h>
#include <system.h>
Expand All @@ -12,6 +13,7 @@ static void print_control_register(void);
static void print_cpsr_status(void);
static void print_supervisor_sp(void);
static void print_irq_sp(void);
static void print_free_memory(void);
static uint32_t get_bits(uint32_t n, uint32_t bitmask);

/* mon_status displays information about the current status of system. */
Expand All @@ -28,6 +30,7 @@ int mon_status(int argc, char **argv)
print_cpsr_status();
print_supervisor_sp();
print_irq_sp();
print_free_memory();

return 0;
}
Expand Down Expand Up @@ -131,6 +134,12 @@ static void print_irq_sp(void)
irq_stack_start - irq_stack_current);
}

static void print_free_memory(void)
{
uint32_t free_memory_size = get_free_memory_size();
kprintf("free memory size: %d (bytes)\n", free_memory_size);
}

static uint32_t get_bits(uint32_t n, uint32_t bitmask)
{
uint32_t result = 0;
Expand Down
19 changes: 19 additions & 0 deletions kernel/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void proc_free(struct Process *proc)
{
kfree(proc->kernel_stack);
kfree(proc->user_stack);
proc_shrink_memory(proc, proc->heap_size / PAGE_SIZE);
free_vm_page_tables(proc->vm);
}

Expand All @@ -93,6 +94,24 @@ void proc_expand_memory(struct Process *proc, int page_count)
}
}

void proc_shrink_memory(struct Process *proc, int page_count)
{
int i = 0;

for (i = 0; i < page_count; i++) {
uint32_t virtual_addr = proc->heap_size - PAGE_SIZE;
uint32_t physical_addr = resolve_physical_address(proc->vm,
virtual_addr);
uint32_t kernel_addr = P2V(physical_addr);
kfree((void *) kernel_addr);

proc->heap_size -= PAGE_SIZE;
if (proc->heap_size == 0) {
break;
}
}
}

bool proc_load(struct Process *proc, char **proc_image, int page_count)
{
int prog_header_offset = 0;
Expand Down

0 comments on commit ebfc1a7

Please sign in to comment.