mirror of
https://github.com/cfenollosa/os-tutorial.git
synced 2024-10-27 20:34:19 +00:00
lesson 22
This commit is contained in:
parent
293f556a7e
commit
1ec003f2c4
@ -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
|
subsequent `kmalloc()`'s produce a new address which is
|
||||||
aligned 4096 bytes or 0x1000 from the previous one.
|
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.
|
The rest of the files are unchanged from last lesson.
|
||||||
|
@ -1 +0,0 @@
|
|||||||
../21-shell/cpu
|
|
@ -1 +0,0 @@
|
|||||||
../21-shell/drivers
|
|
@ -20,10 +20,10 @@ void user_input(char *input) {
|
|||||||
/* Lesson 22: Code to test kmalloc, the rest is unchanged */
|
/* Lesson 22: Code to test kmalloc, the rest is unchanged */
|
||||||
u32 phys_addr;
|
u32 phys_addr;
|
||||||
u32 page = kmalloc(1000, 1, &phys_addr);
|
u32 page = kmalloc(1000, 1, &phys_addr);
|
||||||
char page_str[16];
|
char page_str[16] = "";
|
||||||
int_to_ascii(page, page_str);
|
hex_to_ascii(page, page_str);
|
||||||
char phys_str[16];
|
char phys_str[16] = "";
|
||||||
int_to_ascii(phys_addr, phys_str);
|
hex_to_ascii(phys_addr, phys_str);
|
||||||
kprint("Page: ");
|
kprint("Page: ");
|
||||||
kprint(page_str);
|
kprint(page_str);
|
||||||
kprint(", physical address: ");
|
kprint(", physical address: ");
|
||||||
|
@ -1 +0,0 @@
|
|||||||
../../21-shell/libc/function.h
|
|
8
22-malloc/libc/function.h
Normal file
8
22-malloc/libc/function.h
Normal file
@ -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
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef MEM_H
|
#ifndef MEM_H
|
||||||
#define MEM_H
|
#define MEM_H
|
||||||
|
|
||||||
#include "../cpu/types.h"
|
#include "../cpu/type.h"
|
||||||
|
|
||||||
void memory_copy(u8 *source, u8 *dest, int nbytes);
|
void memory_copy(u8 *source, u8 *dest, int nbytes);
|
||||||
void memory_set(u8 *dest, u8 val, u32 len);
|
void memory_set(u8 *dest, u8 val, u32 len);
|
||||||
|
@ -1 +0,0 @@
|
|||||||
../../21-shell/libc/string.c
|
|
77
22-malloc/libc/string.c
Normal file
77
22-malloc/libc/string.c
Normal file
@ -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 s1<s2, 0 if s1==s2, >0 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];
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
../../21-shell/libc/string.h
|
|
12
22-malloc/libc/string.h
Normal file
12
22-malloc/libc/string.h
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user