Skip to content

Commit

Permalink
malloc() works
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Apr 6, 2022
1 parent db9aff7 commit e983d20
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 40 deletions.
3 changes: 2 additions & 1 deletion apps/app.lds
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SECTIONS
.heap (NOLOAD) : ALIGN(8) {
PROVIDE( __heap_start = . );
. = __heap_size;
PROVIDE( __heap_end = . );
} >ram :ram

PROVIDE( __heap_end = 0x08008000 );
}
9 changes: 5 additions & 4 deletions earth/earth.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ extern char data_rom_start, data_ram_start, data_ram_end;
struct earth *earth = (void*)GRASS_STACK_TOP;

int main() {
memset(&bss_start, 0, &bss_end - &bss_start);
int data_size = &data_ram_end - &data_ram_start;
memcpy(&data_ram_start, &data_rom_start, data_size);

for (int i = 0; i < (&bss_end - &bss_start); i++)
((char*)&bss_start)[i] = 0;
for (int i = 0; i < (&data_ram_end - &data_ram_start); i++)
((char*)&data_ram_start)[i] = ((char*)&data_rom_start)[i];

earth_init();

INFO("Start to load the grass layer");
Expand Down
10 changes: 5 additions & 5 deletions earth/earth.lds
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENTRY(_enter)

MEMORY
{
ram (arw!xi) : ORIGIN = 0x08000000, LENGTH = 0x2000
ram (arw!xi) : ORIGIN = 0x08000000, LENGTH = 0x2800
rom (irx!wa) : ORIGIN = 0x20400000, LENGTH = 0x400000
}

Expand All @@ -18,7 +18,7 @@ PHDRS

SECTIONS
{
__heap_size = 0x800;
__heap_size = 0x1000;

/* ROM Sections */
.init : {
Expand Down Expand Up @@ -52,9 +52,9 @@ SECTIONS
.heap (NOLOAD) : ALIGN(8) {
PROVIDE( __heap_start = . );
. += __heap_size;
PROVIDE( __heap_end = . );
} >ram :ram
} >ram :ram
PROVIDE( __heap_end = 0x08002800 );

PROVIDE( bss_start = ADDR(.bss) );
PROVIDE( bss_end = ADDR(.bss) + SIZEOF(.bss) );
PROVIDE( data_rom_start = LOADADDR(.data) );
Expand Down
2 changes: 1 addition & 1 deletion grass/grass.lds
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ SECTIONS
.heap (NOLOAD) : ALIGN(8) {
PROVIDE( __heap_start = . );
. = __heap_size;
PROVIDE( __heap_end = . );
} >ram :ram
PROVIDE( __heap_end = 0x08005000 );
}
8 changes: 0 additions & 8 deletions library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,3 @@ extern struct earth *earth;
#define FATAL earth->tty_fatal
#define SUCCESS earth->tty_success
#define CRITICAL earth->tty_critical

#undef malloc
#define malloc my_alloc
void* my_alloc(unsigned int size);

#undef free
#define free my_free
void my_free(void* ptr);
2 changes: 1 addition & 1 deletion library/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ int treedisk_create(block_store_t *below, unsigned int below_ino, unsigned int n
INFO("treedisk: Created a new filesystem with %d inodes\n", ninodes);
}
else {
INFO("treedisk: Attempted to create a new filesystem, but one already exists with %lu inodes",
INFO("treedisk: A filesystem already exists with %lu inodes",
superblock.superblock.n_inodeblocks * INODES_PER_BLOCK);
}

Expand Down
25 changes: 6 additions & 19 deletions library/libc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,23 @@
*/

/* Author: Yunhao Zhang
* Description: EGOS uses my_alloc() and my_free() instead of
* the malloc() and free() in metal-libc in order to reduce the
* size of the code segment; This file is a naive implementation
* and a better one is left to students as an exercise.
* Description: system support to C library function malloc()
*/

#include "egos.h"

/* heap region is defined in the memory layout scripts */
extern char __heap_start, __heap_end;
static char* brk = &__heap_start;

char* brk = &__heap_start;

char *_sbrk(int size) {
if (brk + size >= &__heap_end) return (void*)-1;
if ((brk + size) > (&__heap_end)) {
earth->tty_write("_sbrk: heap is full\r\n", 21);
while(1);
}

char *old_brk = brk;
brk += size;
for (int i = 0; i < size; i++) old_brk[i] = 0;

return old_brk;
}


void* my_alloc(unsigned int size) {
if (brk + size >= &__heap_end) FATAL("Heap is full");

char* old_brk = brk;
brk += size;
return old_brk;
}

void my_free(void* ptr) {}
2 changes: 1 addition & 1 deletion library/libc/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/* Author: Yunhao Zhang
* Description: system support to C library functions such as printf
* Description: system support to C library functions like printf()
*/

#include "egos.h"
Expand Down

0 comments on commit e983d20

Please sign in to comment.