mirror of
https://github.com/cfenollosa/os-tutorial.git
synced 2025-12-17 12:24:37 +03:00
lesson 22, malloc
This commit is contained in:
1
22-malloc/libc/function.h
Symbolic link
1
22-malloc/libc/function.h
Symbolic link
@@ -0,0 +1 @@
|
||||
../../21-shell/libc/function.h
|
||||
33
22-malloc/libc/mem.c
Normal file
33
22-malloc/libc/mem.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "mem.h"
|
||||
|
||||
void memory_copy(u8 *source, u8 *dest, int nbytes) {
|
||||
int i;
|
||||
for (i = 0; i < nbytes; i++) {
|
||||
*(dest + i) = *(source + i);
|
||||
}
|
||||
}
|
||||
|
||||
void memory_set(u8 *dest, u8 val, u32 len) {
|
||||
u8 *temp = (u8 *)dest;
|
||||
for ( ; len != 0; len--) *temp++ = val;
|
||||
}
|
||||
|
||||
/* This should be computed at link time, but a hardcoded
|
||||
* value is fine for now. Remember that our kernel starts
|
||||
* at 0x1000 as defined on the Makefile */
|
||||
u32 free_mem_addr = 0x10000;
|
||||
/* Implementation is just a pointer to some free memory which
|
||||
* keeps growing */
|
||||
u32 kmalloc(u32 size, int align, u32 *phys_addr) {
|
||||
/* Pages are aligned to 4K, or 0x1000 */
|
||||
if (align == 1 && (free_mem_addr & 0xFFFFF000)) {
|
||||
free_mem_addr &= 0xFFFFF000;
|
||||
free_mem_addr += 0x1000;
|
||||
}
|
||||
/* Save also the physical address */
|
||||
if (phys_addr) *phys_addr = free_mem_addr;
|
||||
|
||||
u32 ret = free_mem_addr;
|
||||
free_mem_addr += size; /* Remember to increment the pointer */
|
||||
return ret;
|
||||
}
|
||||
12
22-malloc/libc/mem.h
Normal file
12
22-malloc/libc/mem.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef MEM_H
|
||||
#define MEM_H
|
||||
|
||||
#include "../cpu/types.h"
|
||||
|
||||
void memory_copy(u8 *source, u8 *dest, int nbytes);
|
||||
void memory_set(u8 *dest, u8 val, u32 len);
|
||||
|
||||
/* At this stage there is no 'free' implemented. */
|
||||
u32 kmalloc(u32 size, int align, u32 *phys_addr);
|
||||
|
||||
#endif
|
||||
1
22-malloc/libc/string.c
Symbolic link
1
22-malloc/libc/string.c
Symbolic link
@@ -0,0 +1 @@
|
||||
../../21-shell/libc/string.c
|
||||
1
22-malloc/libc/string.h
Symbolic link
1
22-malloc/libc/string.h
Symbolic link
@@ -0,0 +1 @@
|
||||
../../21-shell/libc/string.h
|
||||
Reference in New Issue
Block a user