added part 4
This commit is contained in:
8
Part4/10_scheduler/libc/function.h
Normal file
8
Part4/10_scheduler/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
|
5
Part4/10_scheduler/libc/globals.c
Normal file
5
Part4/10_scheduler/libc/globals.c
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "../cpu/types.h"
|
||||
#include "globals.h"
|
||||
u32 free_mem_addr;
|
||||
u32 global_id;
|
||||
u32 memory_limit;
|
9
Part4/10_scheduler/libc/globals.h
Normal file
9
Part4/10_scheduler/libc/globals.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef GLOBAL_H
|
||||
#define GLOBAL_H
|
||||
|
||||
#include "../cpu/types.h"
|
||||
extern u32 free_mem_addr;
|
||||
extern u32 global_id;
|
||||
extern u32 memory_limit;
|
||||
|
||||
#endif // GLOBAL_H
|
43
Part4/10_scheduler/libc/mem.c
Normal file
43
Part4/10_scheduler/libc/mem.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
// See mem.h for free_mem_addr global declaration
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
char c[16];
|
||||
hex_to_ascii( (u32) *phys_addr, c);
|
||||
kprint("Inside kmalloc phys_addr = ");
|
||||
kprint(c);
|
||||
kprint("\n");
|
||||
|
||||
/* Save also the physical address */
|
||||
*phys_addr = free_mem_addr;
|
||||
|
||||
hex_to_ascii( (u32) *phys_addr, c);
|
||||
kprint("Inside kmalloc phys_addr = ");
|
||||
kprint(c);
|
||||
kprint("\n");
|
||||
|
||||
u32 ret = free_mem_addr;
|
||||
free_mem_addr += size; /* Remember to increment the pointer */
|
||||
return ret;
|
||||
}
|
22
Part4/10_scheduler/libc/mem.h
Normal file
22
Part4/10_scheduler/libc/mem.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef MEM_H
|
||||
#define MEM_H
|
||||
|
||||
#include "../cpu/types.h"
|
||||
#include "../libc/string.h"
|
||||
#include "../drivers/screen.h"
|
||||
#include "globals.h"
|
||||
|
||||
typedef struct _node {
|
||||
u32 id;
|
||||
u32 base;
|
||||
u32 size;
|
||||
struct _node *next;
|
||||
struct _node *previous;
|
||||
} node;
|
||||
|
||||
void memory_copy(u8 *source, u8 *dest, int nbytes);
|
||||
void memory_set(u8 *dest, u8 val, u32 len);
|
||||
|
||||
u32 kmalloc(u32 size, int align, u32 *phys_addr);
|
||||
|
||||
#endif
|
79
Part4/10_scheduler/libc/string.c
Normal file
79
Part4/10_scheduler/libc/string.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "string.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[]) {
|
||||
str[0] = 0;
|
||||
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 > 0x0A) append(str, tmp - 0x0a + 'a');
|
||||
else append(str, tmp + '0');
|
||||
}
|
||||
|
||||
tmp = n & 0xF;
|
||||
if (tmp >= 0x0A) append(str, tmp - 0x0a + 'a');
|
||||
else append(str, tmp + '0');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* 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];
|
||||
}
|
14
Part4/10_scheduler/libc/string.h
Normal file
14
Part4/10_scheduler/libc/string.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef STRINGS_H
|
||||
#define STRINGS_H
|
||||
|
||||
#include "../cpu/types.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
|
Reference in New Issue
Block a user