diff --git a/22-malloc/README.md b/22-malloc/README.md index 299db30..da2ed0f 100644 --- a/22-malloc/README.md +++ b/22-malloc/README.md @@ -15,4 +15,10 @@ our first page starts at 0x10000 (as hardcoded on `mem.c`) and subsequent `kmalloc()`'s produce a new address which is aligned 4096 bytes or 0x1000 from the previous one. +Note that we added a new `strings.c:hex_to_ascii()` for +nicer printing of hex numbers. + +Another cosmetic modification is to rename `types.c` to +`type.c` for language consistency. + The rest of the files are unchanged from last lesson. diff --git a/22-malloc/cpu b/22-malloc/cpu deleted file mode 120000 index 48a218d..0000000 --- a/22-malloc/cpu +++ /dev/null @@ -1 +0,0 @@ -../21-shell/cpu \ No newline at end of file diff --git a/22-malloc/drivers b/22-malloc/drivers deleted file mode 120000 index d298ec0..0000000 --- a/22-malloc/drivers +++ /dev/null @@ -1 +0,0 @@ -../21-shell/drivers \ No newline at end of file diff --git a/22-malloc/kernel/kernel.c b/22-malloc/kernel/kernel.c index fd6dc7f..ecd034c 100644 --- a/22-malloc/kernel/kernel.c +++ b/22-malloc/kernel/kernel.c @@ -20,10 +20,10 @@ void user_input(char *input) { /* Lesson 22: Code to test kmalloc, the rest is unchanged */ u32 phys_addr; u32 page = kmalloc(1000, 1, &phys_addr); - char page_str[16]; - int_to_ascii(page, page_str); - char phys_str[16]; - int_to_ascii(phys_addr, phys_str); + char page_str[16] = ""; + hex_to_ascii(page, page_str); + char phys_str[16] = ""; + hex_to_ascii(phys_addr, phys_str); kprint("Page: "); kprint(page_str); kprint(", physical address: "); diff --git a/22-malloc/libc/function.h b/22-malloc/libc/function.h deleted file mode 120000 index f0807b6..0000000 --- a/22-malloc/libc/function.h +++ /dev/null @@ -1 +0,0 @@ -../../21-shell/libc/function.h \ No newline at end of file diff --git a/22-malloc/libc/function.h b/22-malloc/libc/function.h new file mode 100644 index 0000000..bf656ed --- /dev/null +++ b/22-malloc/libc/function.h @@ -0,0 +1,8 @@ +#ifndef FUNCTION_H +#define FUNCTION_H + +/* Sometimes we want to keep parameters to a function for later use + * and this is a solution to avoid the 'unused parameter' compiler warning */ +#define UNUSED(x) (void)(x) + +#endif diff --git a/22-malloc/libc/mem.h b/22-malloc/libc/mem.h index 395a527..297ca6e 100644 --- a/22-malloc/libc/mem.h +++ b/22-malloc/libc/mem.h @@ -1,7 +1,7 @@ #ifndef MEM_H #define MEM_H -#include "../cpu/types.h" +#include "../cpu/type.h" void memory_copy(u8 *source, u8 *dest, int nbytes); void memory_set(u8 *dest, u8 val, u32 len); diff --git a/22-malloc/libc/string.c b/22-malloc/libc/string.c deleted file mode 120000 index 1ae6696..0000000 --- a/22-malloc/libc/string.c +++ /dev/null @@ -1 +0,0 @@ -../../21-shell/libc/string.c \ No newline at end of file diff --git a/22-malloc/libc/string.c b/22-malloc/libc/string.c new file mode 100644 index 0000000..9b8a6d3 --- /dev/null +++ b/22-malloc/libc/string.c @@ -0,0 +1,77 @@ +#include "string.h" +#include "../cpu/type.h" + +/** + * K&R implementation + */ +void int_to_ascii(int n, char str[]) { + int i, sign; + if ((sign = n) < 0) n = -n; + i = 0; + do { + str[i++] = n % 10 + '0'; + } while ((n /= 10) > 0); + + if (sign < 0) str[i++] = '-'; + str[i] = '\0'; + + reverse(str); +} + +void hex_to_ascii(int n, char str[]) { + append(str, '0'); + append(str, 'x'); + char zeros = 0; + + s32 tmp; + int i; + for (i = 28; i > 0; i -= 4) { + tmp = (n >> i) & 0xF; + if (tmp == 0 && zeros == 0) continue; + zeros = 1; + if (tmp > 0xA) append(str, tmp - 0xA + 'a'); + else append(str, tmp + '0'); + } + + tmp = n & 0xF; + if (tmp >= 0xA) append(str, tmp - 0xA + 'a'); + else append(str, tmp + '0'); +} + +/* K&R */ +void reverse(char s[]) { + int c, i, j; + for (i = 0, j = strlen(s)-1; i < j; i++, j--) { + c = s[i]; + s[i] = s[j]; + s[j] = c; + } +} + +/* K&R */ +int strlen(char s[]) { + int i = 0; + while (s[i] != '\0') ++i; + return i; +} + +void append(char s[], char n) { + int len = strlen(s); + s[len] = n; + s[len+1] = '\0'; +} + +void backspace(char s[]) { + int len = strlen(s); + s[len-1] = '\0'; +} + +/* K&R + * Returns <0 if s10 if s1>s2 */ +int strcmp(char s1[], char s2[]) { + int i; + for (i = 0; s1[i] == s2[i]; i++) { + if (s1[i] == '\0') return 0; + } + return s1[i] - s2[i]; +} diff --git a/22-malloc/libc/string.h b/22-malloc/libc/string.h deleted file mode 120000 index 3b5dadc..0000000 --- a/22-malloc/libc/string.h +++ /dev/null @@ -1 +0,0 @@ -../../21-shell/libc/string.h \ No newline at end of file diff --git a/22-malloc/libc/string.h b/22-malloc/libc/string.h new file mode 100644 index 0000000..39f3969 --- /dev/null +++ b/22-malloc/libc/string.h @@ -0,0 +1,12 @@ +#ifndef STRINGS_H +#define STRINGS_H + +void int_to_ascii(int n, char str[]); +void hex_to_ascii(int n, char str[]); +void reverse(char s[]); +int strlen(char s[]); +void backspace(char s[]); +void append(char s[], char n); +int strcmp(char s1[], char s2[]); + +#endif