Skip to content

Commit

Permalink
♻️ seperate files into folders
Browse files Browse the repository at this point in the history
  • Loading branch information
ianchen-tw committed Apr 2, 2021
1 parent 1b3872f commit 0679472
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
kernel_impl = impl-c
# kernel_impl = impl-rs

OPT_BUILD_BOOTLOADER = 0
OPT_BUILD_BOOTLOADER = 1
bootloader_impl = bootloader


Expand Down
5 changes: 2 additions & 3 deletions impl-c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ OBJCOPY := $(TOOLCHAIN_PREFIX)-objcopy

SRC_DIR = src

LDSCRIPT = $(SRC_DIR)/linker.ld
LDSCRIPT = kernel/linker.ld
LDFLAGS = -nostdlib

SRC = $(wildcard **/*.S) $(wildcard **/*.c)
OBJS = $(wildcard *.o)

CFLAGS = -Wall -I include -nostartfiles -ffreestanding
CFLAGS = -Wall -Iinclude -nostartfiles -ffreestanding
CFLAGS += -g -ggdb

TARGET = kernel8.img
Expand All @@ -24,7 +24,6 @@ all:

.PHONY: kernel
kernel:

$(LD) $(LDFLAGS) $(OBJS) -T $(LDSCRIPT) -o $(TARGET_ELF)
$(OBJCOPY) -O binary $(TARGET_ELF) $(TARGET)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
132 changes: 1 addition & 131 deletions impl-c/mm/mem.c → impl-c/mm/buddy.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "mem.h"
#include "bool.h"
#include "list.h"
#include "mem.h"
#include "uart.h"
#include <stddef.h>

Expand Down Expand Up @@ -126,8 +126,6 @@ void buddy_init(BuddyAllocater *alloc, Frame *frame_arr) {
alloc->frame_array[i].exp = -1;
alloc->frame_array[i].addr =
(void *)(((long)i << FRAME_SHIFT) + MEMORY_START);
alloc->frame_array[i].list_base.next = NULL;
alloc->frame_array[i].list_base.prev = NULL;
}
for (int i = 0; i < BUDDY_NUM_FREE_LISTS; i++) {
list_init(&alloc->free_lists[i]);
Expand All @@ -137,131 +135,3 @@ void buddy_init(BuddyAllocater *alloc, Frame *frame_arr) {
root_frame->exp = BUDDY_MAX_EXPONENT;
list_push(&root_frame->list_base, &alloc->free_lists[BUDDY_MAX_EXPONENT]);
}

void *slab_alloc(SlabAllocator *alloc) {
Frame *frame;

if (alloc->cur_frame == NULL) {
if (!list_empty(&alloc->partial_list)) {
frame = (Frame *)list_pop(&alloc->partial_list);
alloc->cur_frame = frame;
uart_println("slab: Recycle frame from partial filled slab");
} else {
frame = buddy_alloc(alloc->frame_allocator, 0);
if (frame == NULL)
return NULL;
uart_println("slab: Request frame from buddy system");
frame->slab_allocator = alloc;
alloc->cur_frame = frame;
for (int i = 0; i < SLAB_MAX_SLOTS; i++) {
frame->slot_available[i] = 1;
}
frame->free_slot_remains = alloc->max_slab_num_obj;
// frame->free_slot_remains = 5;
uart_println(" slot remains: %d, max:%d", frame->free_slot_remains,
alloc->max_slab_num_obj);
}
}
frame = alloc->cur_frame;

void *addr;

// find a space for allocation
for (int i = 0; i < SLAB_MAX_SLOTS; i++) {
if (frame->slot_available[i] == 1) {
addr = frame->addr + i * alloc->unit_size;
frame->slot_available[i] = 0;
frame->free_slot_remains -= 1;
break;
}
}

if (frame->free_slot_remains <= 0) {
// if the page is full, move it to the full-list
list_push(&frame->list_base, &alloc->full_list);
alloc->cur_frame = NULL;
uart_println("==== slab frame is full");
}
return addr;
}

void slab_free(void *obj) {
// get frame address first
int arr_index = (long)(obj - MEMORY_START) >> FRAME_SHIFT;
struct Frame *frame = &Frames[arr_index];
struct SlabAllocator *alloc = frame->slab_allocator;

int obj_index = (((long)(obj - MEMORY_START) & ((1 << FRAME_SHIFT) - 1)) /
alloc->unit_size);

if (frame->slot_available[obj_index] == 1) {
uart_println("!!Free after free");
}
uart_println("slab: Free object");
frame->slot_available[obj_index] = 1;
frame->free_slot_remains += 1;
if (frame != alloc->cur_frame) {
list_del(&frame->list_base);
if (frame->slot_available > 0) {
list_push(&frame->list_base, &alloc->partial_list);
} else {
uart_println("slab: release block");
buddy_free(alloc->frame_allocator, frame);
}
}
}

void KAllocManager_init() {
AllocationManager *am = &KAllocManager;
buddy_init(am->frame_allocator, Frames);

SlabAllocator *slab_alloc;
for (int i = SLAB_OBJ_MIN_SIZE_EXP; i <= SLAB_OBJ_MAX_SIZE_EXP; i++) {
slab_alloc = &am->obj_allocator_list[i - SLAB_OBJ_MIN_SIZE_EXP];
slab_alloc->unit_size = 1 << i;
slab_alloc->max_slab_num_obj = 4096 / slab_alloc->unit_size;
slab_alloc->frame_allocator = am->frame_allocator;
slab_alloc->cur_frame = NULL;
list_init(&slab_alloc->partial_list);
list_init(&slab_alloc->full_list);
}
}

void KAllocManager_show_status() { buddy_dump(KAllocManager.frame_allocator); }

void *kalloc(int size) {
void *addr;
if (size <= (1 << SLAB_OBJ_MAX_SIZE_EXP)) {
for (int i = SLAB_OBJ_MIN_SIZE_EXP; i < SLAB_OBJ_MAX_SIZE_EXP; i++) {
if (size < 1 << i) {
uart_println("Allocation from slab allocator, size: %d", size);
addr = slab_alloc(
&KAllocManager.obj_allocator_list[i - SLAB_OBJ_MIN_SIZE_EXP]);
return addr;
}
}
}
// allcation using buddy system
for (int i = 0; i < BUDDY_MAX_EXPONENT; i++) {
if ((i << FRAME_SHIFT) >= size) {
uart_println("Allocate Request exp:%d", i);
Frame *frame = buddy_alloc(KAllocManager.frame_allocator, i);
uart_println("Allocated addr:%x, frame_idx:%d", frame->addr,
frame->arr_index);
return frame->addr;
}
}
return NULL;
}
void kfree(void *addr) {
// Get frame from address provided
int arr_index = (long)(addr - MEMORY_START) >> FRAME_SHIFT;
Frame *frame = &Frames[arr_index];
if (frame->slab_allocator) {
slab_free(addr);
} else {
uart_println("Free Request addr:%x, frame_idx:%d", frame->addr,
frame->arr_index);
buddy_free(KAllocManager.frame_allocator, frame);
}
}
70 changes: 70 additions & 0 deletions impl-c/mm/kalloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "bool.h"
#include "list.h"
#include "mem.h"
#include "uart.h"
#include <stddef.h>

void KAllocManager_init() {
AllocationManager *am = &KAllocManager;

buddy_init(am->frame_allocator, Frames);
for (int i = 0; i < (1 << BUDDY_MAX_EXPONENT); i++) {
Frames[i].list_base.next = NULL;
Frames[i].list_base.prev = NULL;
for (int j = 0; j < (SLAB_MAX_SLOTS >> 3); j++) {
Frames[i].slot_available[j] = 0;
}
Frames[i].slab_allocator = NULL;
Frames[i].free_slot_remains = 0;
}

SlabAllocator *slab_alloc;
for (int i = SLAB_OBJ_MIN_SIZE_EXP; i <= SLAB_OBJ_MAX_SIZE_EXP; i++) {
slab_alloc = &am->obj_allocator_list[i - SLAB_OBJ_MIN_SIZE_EXP];
slab_alloc->unit_size = 1 << i;
slab_alloc->max_slab_num_obj = 4096 / slab_alloc->unit_size;
slab_alloc->frame_allocator = am->frame_allocator;
slab_alloc->cur_frame = NULL;
list_init(&slab_alloc->partial_list);
list_init(&slab_alloc->full_list);
}
}

void KAllocManager_show_status() { buddy_dump(KAllocManager.frame_allocator); }

void *kalloc(int size) {
void *addr;
if (size <= (1 << SLAB_OBJ_MAX_SIZE_EXP)) {
for (int i = SLAB_OBJ_MIN_SIZE_EXP; i < SLAB_OBJ_MAX_SIZE_EXP; i++) {
if (size < 1 << i) {
uart_println("Allocation from slab allocator, size: %d", size);
addr = slab_alloc(
&KAllocManager.obj_allocator_list[i - SLAB_OBJ_MIN_SIZE_EXP]);
return addr;
}
}
}
// allcation using buddy system
for (int i = 0; i < BUDDY_MAX_EXPONENT; i++) {
if ((i << FRAME_SHIFT) >= size) {
uart_println("Allocate Request exp:%d", i);
Frame *frame = buddy_alloc(KAllocManager.frame_allocator, i);
uart_println("Allocated addr:%x, frame_idx:%d", frame->addr,
frame->arr_index);
return frame->addr;
}
}
return NULL;
}
void kfree(void *addr) {
// Get frame from address provided
int arr_index = (long)(addr - MEMORY_START) >> FRAME_SHIFT;
Frame *frame = &Frames[arr_index];
if (frame->slab_allocator) {
slab_free(addr);
} else {
uart_println("Free Request addr:%x, frame_idx:%d", frame->addr,
frame->arr_index);
buddy_free(KAllocManager.frame_allocator, frame);
}
}
78 changes: 78 additions & 0 deletions impl-c/mm/slab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "bool.h"
#include "list.h"
#include "mem.h"
#include "uart.h"
#include <stddef.h>

void *slab_alloc(SlabAllocator *alloc) {
Frame *frame;

if (alloc->cur_frame == NULL) {
if (!list_empty(&alloc->partial_list)) {
frame = (Frame *)list_pop(&alloc->partial_list);
alloc->cur_frame = frame;
uart_println("slab: Recycle frame from partial filled slab");
} else {
frame = buddy_alloc(alloc->frame_allocator, 0);
if (frame == NULL)
return NULL;
uart_println("slab: Request frame from buddy system");
frame->slab_allocator = alloc;
alloc->cur_frame = frame;
for (int i = 0; i < SLAB_MAX_SLOTS; i++) {
frame->slot_available[i] = 1;
}
frame->free_slot_remains = alloc->max_slab_num_obj;
// frame->free_slot_remains = 5;
uart_println(" slot remains: %d, max:%d", frame->free_slot_remains,
alloc->max_slab_num_obj);
}
}
frame = alloc->cur_frame;

void *addr;

// find a space for allocation
for (int i = 0; i < SLAB_MAX_SLOTS; i++) {
if (frame->slot_available[i] == 1) {
addr = frame->addr + i * alloc->unit_size;
frame->slot_available[i] = 0;
frame->free_slot_remains -= 1;
break;
}
}

if (frame->free_slot_remains <= 0) {
// if the page is full, move it to the full-list
list_push(&frame->list_base, &alloc->full_list);
alloc->cur_frame = NULL;
uart_println("==== slab frame is full");
}
return addr;
}

void slab_free(void *obj) {
// get frame address first
int arr_index = (long)(obj - MEMORY_START) >> FRAME_SHIFT;
struct Frame *frame = &Frames[arr_index];
struct SlabAllocator *alloc = frame->slab_allocator;

int obj_index = (((long)(obj - MEMORY_START) & ((1 << FRAME_SHIFT) - 1)) /
alloc->unit_size);

if (frame->slot_available[obj_index] == 1) {
uart_println("!!Free after free");
}
uart_println("slab: Free object");
frame->slot_available[obj_index] = 1;
frame->free_slot_remains += 1;
if (frame != alloc->cur_frame) {
list_del(&frame->list_base);
if (frame->slot_available > 0) {
list_push(&frame->list_base, &alloc->partial_list);
} else {
uart_println("slab: release block");
buddy_free(alloc->frame_allocator, frame);
}
}
}

0 comments on commit 0679472

Please sign in to comment.