forked from GrassLab/osc2021
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ianchen-tw/lab3-startup-allocator
Lab3 startup allocator
- Loading branch information
Showing
15 changed files
with
565 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
#pragma once | ||
|
||
#define CFG_LOG_ENABLE 0 | ||
/** | ||
* Log | ||
* */ | ||
|
||
#define CFG_LOG_ENABLE 0 | ||
#define CFG_LOG_STARTUP | ||
#define CFG_LOG_KALLOC | ||
|
||
/** | ||
* TEST | ||
* */ | ||
#define CFG_RUN_TEST | ||
// memory management | ||
#define CFG_RUN_STATUP_ALLOC_TEST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "list.h" | ||
#include "mm/alloc.h" | ||
#include "mm/const.h" | ||
#include "mm/frame.h" | ||
#include "mm/startup.h" | ||
#include <stdint.h> | ||
|
||
/** | ||
* Allocation Manager: | ||
* On charge of dynamic memory allocation by using frame/object allocators | ||
* | ||
* Caution: must initialize the startup allocator before using | ||
* any of the function listed below, otherwise the buddy system | ||
* wont initialize properly | ||
* */ | ||
|
||
extern struct AllocationManager KAllocManager; | ||
typedef struct AllocationManager { | ||
SlabAllocator obj_allocator_list[SLAB_NUM_SLAB_SIZES]; | ||
BuddyAllocater frame_allocator; | ||
} AllocationManager; | ||
|
||
// Statically linked to the heap space | ||
// because their lifetimes is equal to the system itself | ||
extern struct Frame Frames[BUDDY_MAX_EXPONENT << 1]; | ||
|
||
// Allocate a memory space to use in kernel space | ||
void *kalloc(int size); | ||
|
||
// Free a memory space | ||
void kfree(void *addr); | ||
|
||
// Initialize dynamic memory allocator | ||
void KAllocManager_init(); | ||
|
||
void KAllocManager_show_status(); | ||
|
||
// Run several allocation/free as an example | ||
void KAllocManager_run_example(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#define SLAB_NUM_SLAB_SIZES 6 | ||
|
||
#define BUDDY_MAX_EXPONENT 18 // 1GB | ||
// #define BUDDY_MAX_EXPONENT 10 | ||
// #define BUDDY_MAX_EXPONENT 5 | ||
|
||
#define BUDDY_NUM_FREE_LISTS (BUDDY_MAX_EXPONENT + 1) | ||
|
||
// #define MEMORY_START 0x90000 | ||
#define MEMORY_START 0x0 | ||
|
||
// SlabAllocator | ||
// manage slabs with the same allocation size, | ||
// A slab is a frame allocated with small objects with same size | ||
// + @SLAB_MAX_SLOTS: The number of pages available for a single slab | ||
// -> Frame is 4kb by architecture. | ||
// CortexA53 is 16 bytes aligned, so the here 16 bytes is set to be | ||
// the minimum size available for objects in slab. | ||
// -> Therefore, the maximum slots in slab is 4096/16 = 256 | ||
#define SLAB_MAX_SLOTS 256 | ||
|
||
// The size of slab range from 16(2^4) to 512(2^9) bytes | ||
#define SLAB_OBJ_MIN_SIZE_EXP 4 | ||
#define SLAB_OBJ_MAX_SIZE_EXP 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
#define FRAME_SHIFT 12 // 4Kb | ||
#define FRAME_ADDR_BASE (1 << FRAME_SHIFT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
#include "bool.h" | ||
#include "cfg.h" | ||
#include "test.h" | ||
|
||
#define STARTUP_MAX_RESERVE_COUNT 10 | ||
|
||
/** | ||
* Startup allocator: | ||
* The very first memory allocator of the opoerating system | ||
* | ||
* The purpose of startup allocator is to reserve spaces on system starup, | ||
* Hand all of it's knowledfe to( memory regions have bee reserved) | ||
* to the dynamic memory allcator. | ||
* | ||
* This allocator is fairly basic, which only allocate but not free spaces at | ||
* all. | ||
* | ||
* */ | ||
|
||
typedef struct MemRegion { | ||
void *addr; | ||
unsigned long size; | ||
} MemRegion; | ||
|
||
typedef struct StartupAllocator { | ||
int num_reserved; | ||
int max_reserved; | ||
struct MemRegion *_reserved; | ||
} StartupAllocator_t; | ||
|
||
extern struct MemRegion ReservedRegions[STARTUP_MAX_RESERVE_COUNT]; | ||
extern StartupAllocator_t StartupAlloc; | ||
|
||
bool is_overlap(MemRegion *a1, MemRegion *a2); | ||
|
||
// Public api: Initialize the starup allocator module | ||
void startup_init(); | ||
|
||
// Request StartupAllocator to allocate a space | ||
// void *startup_alloc(unsigned long size); | ||
|
||
// Reqeust StartupAllocator to reserve an area | ||
bool startup_reserve(void *addr, unsigned long size); | ||
|
||
// Only used for running tests | ||
void test_startup_alloc(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "bool.h" | ||
#include "uart.h" | ||
|
||
#define TEST_FAILED(name, description) \ | ||
{ uart_println("\033[0;31m[TEST][ERR] %s: %s\033[0m", name, description); } | ||
|
||
#define TEST_SUCCESS(name, description) \ | ||
{ uart_println("\033[0;32m[TEST][OK] %s: %s\033[0m", name, description); } | ||
|
||
#define TEST1(test, name, desc) \ | ||
{ \ | ||
if (test() == true) { \ | ||
TEST_SUCCESS(name, desc); \ | ||
} else { \ | ||
TEST_FAILED(name, desc); \ | ||
} \ | ||
} | ||
|
||
static inline void unittest(int (*f)(), char *s, char *de) { TEST1(f, s, de); } | ||
|
||
// Return false once the condition does not meet | ||
#define assert(cond) \ | ||
{ \ | ||
if (!(cond)) { \ | ||
return false; \ | ||
} \ | ||
} | ||
|
||
void run_tests(); | ||
|
||
void test_suite_startup_alloc(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "test.h" | ||
#include "cfg.h" | ||
#include "mm/startup.h" | ||
|
||
void run_tests() { test_startup_alloc(); } |
Oops, something went wrong.