Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions kernel/include/kclib/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MEMORY_H
#define MEMORY_H

#include <stddef.h>
#include <stdint.h>

void* kmemcpy (void*, const void*, size_t);
void* kmemset (void*, int, size_t);
void* kmemmove (void*, const void*, size_t);
int kmemcmp (const void*, const void*, size_t);

#endif
2 changes: 1 addition & 1 deletion kernel/include/stdio.h → kernel/include/kclib/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
#include <stddef.h>
#include <stdint.h>

void printf (const char*, ...);
void kprintf (const char*, ...);

#endif
15 changes: 15 additions & 0 deletions kernel/include/kclib/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef STRING_H
#define STRING_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

void kreverse (char* str);
size_t kstrlen (const char*);
void kitos (int32_t, char*, uint32_t);
void kulitos (uint64_t, char*, uint32_t);
int kstrcmp (const char* a, const char* b);
char* kstrdup (const char* s);

#endif
2 changes: 0 additions & 2 deletions kernel/include/kernel/idt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#ifndef IDT_H
#define IDT_H

#include <kernel/serial.h>
#include <stdint.h>
#include <string.h>

typedef struct __attribute__ ((packed)) {
uint16_t size;
Expand Down
12 changes: 0 additions & 12 deletions kernel/include/memory.h

This file was deleted.

15 changes: 0 additions & 15 deletions kernel/include/string.h

This file was deleted.

10 changes: 5 additions & 5 deletions kernel/src/memory.c → kernel/src/kclib/memory.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include <memory.h>
#include <kclib/memory.h>

/*!
Copy memory chunk of size n.
Expand All @@ -8,7 +8,7 @@ Copy memory chunk of size n.
@param src source
@param n width of data
*/
void* memcpy (void* dest, const void* src, size_t n) {
void* kmemcpy (void* dest, const void* src, size_t n) {
uint8_t* pdest = (uint8_t*)dest;
const uint8_t* psrc = (const uint8_t*)src;

Expand All @@ -25,7 +25,7 @@ Set memory chunk of size n to be value c.
@param c value to set memory to
@param n size of memory chunk
*/
void* memset (void* s, int c, size_t n) {
void* kmemset (void* s, int c, size_t n) {
uint8_t* p = (uint8_t*)s;

for (size_t i = 0; i < n; i++)
Expand All @@ -41,7 +41,7 @@ Move memory chunk.
@param src source
@param n width of data
*/
void* memmove (void* dest, const void* src, size_t n) {
void* kmemmove (void* dest, const void* src, size_t n) {
uint8_t* pdest = (uint8_t*)dest;
const uint8_t* psrc = (const uint8_t*)src;

Expand All @@ -63,7 +63,7 @@ Compare memory chunks.
@param n size of both chunks
@return -1 if less, 0 if equal and 1 if greater
*/
int memcmp (const void* s1, const void* s2, size_t n) {
int kmemcmp (const void* s1, const void* s2, size_t n) {
const uint8_t* p1 = (const uint8_t*)s1;
const uint8_t* p2 = (const uint8_t*)s2;

Expand Down
32 changes: 16 additions & 16 deletions kernel/src/stdio.c → kernel/src/kclib/stdio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <kclib/stdio.h>
#include <kclib/string.h>
#include <kernel/console.h>
#include <stdio.h>
#include <string.h>

/*!
Handle the different cases of %-- in printf
Expand All @@ -10,39 +10,39 @@ Handle the different cases of %-- in printf
@param args pointer to the arguments printf received
@param ul whether the format prints an unsigned/long character
*/
static void fmtprintf (const char* format, size_t* i, va_list* args, bool ul) {
static void kfmtprintf (const char* format, size_t* i, va_list* args, bool ul) {
switch (format[*i]) {
case 's': {
const char* str = va_arg (*args, const char*);
putstr (str, strlen (str));
putstr (str, kstrlen (str));
} break;
case 'd':
case 'i': {
if (ul) {
char buf[65] = {0};
ulitos (va_arg (*args, uint64_t), buf, 10);
putstr (buf, strlen (buf));
kulitos (va_arg (*args, uint64_t), buf, 10);
putstr (buf, kstrlen (buf));
} else {
char buf[33] = {0};
itos (va_arg (*args, int32_t), buf, 10);
putstr (buf, strlen (buf));
kitos (va_arg (*args, int32_t), buf, 10);
putstr (buf, kstrlen (buf));
}
} break;
case 'x': {
if (ul) {
char buf[65] = {0};
ulitos (va_arg (*args, uint64_t), buf, 16);
putstr (buf, strlen (buf));
kulitos (va_arg (*args, uint64_t), buf, 16);
putstr (buf, kstrlen (buf));
} else {
char buf[33] = {0};
itos (va_arg (*args, int32_t), buf, 16);
putstr (buf, strlen (buf));
kitos (va_arg (*args, int32_t), buf, 16);
putstr (buf, kstrlen (buf));
}
} break;
case 'u':
case 'l': {
(*i)++;
fmtprintf (format, i, args, true);
kfmtprintf (format, i, args, true);
} break;
default: {
putchar ('%');
Expand All @@ -56,17 +56,17 @@ Print a formatted string to the screen.

@param format formatted string to print
*/
void printf (const char* format, ...) {
void kprintf (const char* format, ...) {
va_list args;
va_start (args, format);

bool buf = get_update_on_putch ();
set_update_on_putch (false);
for (size_t i = 0; i < strlen (format); i++) {
for (size_t i = 0; i < kstrlen (format); i++) {
switch (format[i]) {
case '%': {
i++;
fmtprintf (format, &i, &args, false);
kfmtprintf (format, &i, &args, false);
} break;
case '\t':
size_t idx = get_idx ();
Expand Down
22 changes: 11 additions & 11 deletions kernel/src/string.c → kernel/src/kclib/string.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <kclib/string.h>
#include <liballoc/liballoc.h>
#include <string.h>

/*!
Get the length of a standard string (terminating with 0).

@param str pointer to string
@return string size
*/
size_t strlen (const char* str) {
size_t kstrlen (const char* str) {
size_t ret = 0;
while (str[ret] != 0)
ret++;
Expand All @@ -19,8 +19,8 @@ Reverse a standard string (terminating with 0).

@param str string to reverse
*/
void reverse (char* str) {
int len = strlen (str), start = 0, end = len - 1;
void kreverse (char* str) {
int len = kstrlen (str), start = 0, end = len - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
Expand All @@ -37,7 +37,7 @@ Convert integer to representative string with base b.
@param buf memory to save integer
@param b base
*/
void itos (int32_t i, char* buf, uint32_t b) {
void kitos (int32_t i, char* buf, uint32_t b) {
int ctr = 0;
bool negative = false;
if (i < 0) {
Expand All @@ -54,7 +54,7 @@ void itos (int32_t i, char* buf, uint32_t b) {
if (ctr == 0) buf[0] = '0';
if (negative) buf[ctr++] = '-';
buf[ctr + 1] = 0;
reverse (buf);
kreverse (buf);
}

/*!
Expand All @@ -64,7 +64,7 @@ Convert long integer to representative string with base b.
@param buf memory to save integer
@param b base
*/
void ulitos (uint64_t i, char* buf, uint32_t b) {
void kulitos (uint64_t i, char* buf, uint32_t b) {
int ctr = 0;
bool negative = false;
do {
Expand All @@ -77,7 +77,7 @@ void ulitos (uint64_t i, char* buf, uint32_t b) {
if (ctr == 0) buf[0] = '0';
if (negative) buf[ctr++] = '-';
buf[ctr++] = 0;
reverse (buf);
kreverse (buf);
}

/*!
Expand All @@ -86,7 +86,7 @@ void ulitos (uint64_t i, char* buf, uint32_t b) {
* @param b second string
* @return whether strings are equal
*/
int strcmp (const char* a, const char* b) {
int kstrcmp (const char* a, const char* b) {
for (int i = 0; a[i] != 0 && b[i] != 0; i++)
if (a[i] != b[i]) return 1;
return 0;
Expand All @@ -96,9 +96,9 @@ int strcmp (const char* a, const char* b) {
* Duplicate a string using the kernel allocator.
* Returns a newly allocated copy or nullptr on failure.
*/
char* strdup (const char* s) {
char* kstrdup (const char* s) {
if (!s) return nullptr;
size_t len = strlen (s);
size_t len = kstrlen (s);
char* dup = (char*)kmalloc (len + 1);
if (!dup) return nullptr;
for (size_t i = 0; i <= len; i++)
Expand Down
5 changes: 2 additions & 3 deletions kernel/src/kernel/console.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <kclib/memory.h>
#include <kclib/string.h>
#include <kernel/console.h>
#include <kernel/graphics.h>
#include <kernel/hardfonts/classic.h>
#include <kernel/serial.h>
#include <memory.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#define CONSOLE_HEIGHT 80
#define CONSOLE_WIDTH 140
Expand Down
9 changes: 4 additions & 5 deletions kernel/src/kernel/elf.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <kclib/memory.h>
#include <kclib/stdio.h>
#include <kclib/string.h>
#include <kernel/elf.h>
#include <kernel/error.h>
#include <kernel/fs/vfs.h>
#include <kernel/memmgt.h>
#include <kernel/syscall.h>
#include <liballoc/liballoc.h>
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

bool verify_elf_loadable (elf64_header_t* elf) {
if (!elf) return false;
Expand Down Expand Up @@ -76,7 +75,7 @@ int load_elf (const char* filepath, process* target_process, uintptr_t* entry_po
do_syscall (SYSCALL_SYS_READ, fd, ph->p_vaddr, ph->p_filesz);

if (ph->p_memsz > ph->p_filesz)
memset ((void*)(ph->p_vaddr + ph->p_filesz), 0, ph->p_memsz - ph->p_filesz);
kmemset ((void*)(ph->p_vaddr + ph->p_filesz), 0, ph->p_memsz - ph->p_filesz);
}

init_break = ((init_break + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
Expand Down
Loading
Loading