Moving certain aspects into their own static library
Problem: As our kernel grows we need more complex datastructures and functions these would come from the standard C/C++ library with normal programs. The kernel is a freestanding programme and has no access to standard libraries. Solution: We build a mini version of the standard C/C++ library which will contain the datastructures and functions we want. This library can then be statically linked into our kernel binary. Making it a statically linked library also gives more structure to the project. Keeping these random functions and datastructures in the kernel just clutters the kernel source code with less relevant source code.
This commit is contained in:
33
source/CoreLib/Makefile
Normal file
33
source/CoreLib/Makefile
Normal file
@ -0,0 +1,33 @@
|
||||
CPP = ${HOME}/opt/cross/bin/i686-elf-g++
|
||||
CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra
|
||||
|
||||
SRC_DIR = .
|
||||
BUILD_DIR = build
|
||||
OBJ_FOLDER = bin
|
||||
OUTPUTFILE = $(BUILD_DIR)/libCoreLib.a
|
||||
|
||||
OFILES = $(OBJ_FOLDER)/memory.o $(OBJ_FOLDER)/path.o $(OBJ_FOLDER)/stack.o $(OBJ_FOLDER)/string.o $(OBJ_FOLDER)/stringview.o
|
||||
|
||||
|
||||
.phony: all
|
||||
all: $(OUTPUTFILE)
|
||||
cp *.h build/include/
|
||||
|
||||
$(OUTPUTFILE): $(OFILES)
|
||||
ar -rc $(OUTPUTFILE) $(OFILES)
|
||||
|
||||
$(OBJ_FOLDER)/memory.o: Memory.cpp
|
||||
$(CPP) -c Memory.cpp -o $(OBJ_FOLDER)/memory.o $(CFLAGS)
|
||||
|
||||
$(OBJ_FOLDER)/path.o: Path.cpp
|
||||
$(CPP) -c Path.cpp -o $(OBJ_FOLDER)/path.o $(CFLAGS)
|
||||
|
||||
$(OBJ_FOLDER)/stack.o: Stack.cpp
|
||||
$(CPP) -c Stack.cpp -o $(OBJ_FOLDER)/stack.o $(CFLAGS)
|
||||
|
||||
$(OBJ_FOLDER)/string.o: String.cpp
|
||||
$(CPP) -c String.cpp -o $(OBJ_FOLDER)/string.o $(CFLAGS)
|
||||
|
||||
$(OBJ_FOLDER)/stringview.o: StringView.cpp
|
||||
$(CPP) -c StringView.cpp -o $(OBJ_FOLDER)/stringview.o $(CFLAGS)
|
||||
|
58
source/CoreLib/Memory.cpp
Normal file
58
source/CoreLib/Memory.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
||||
#include "Memory.h"
|
||||
|
||||
void* memset (void* ptr, int value, size_t num)
|
||||
{
|
||||
for( int i = 0; i < num; i++ )
|
||||
{
|
||||
unsigned char* data = (unsigned char*)ptr+ i;
|
||||
*data = (unsigned char)value;
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
int memcmp( const void* ptr1, const void* ptr2, size_t num)
|
||||
{
|
||||
const unsigned char * cs = (const unsigned char*) ptr1;
|
||||
const unsigned char * ct = (const unsigned char*) ptr2;
|
||||
|
||||
|
||||
for (int i = 0 ; i < num ; i++, cs++, ct++ ){
|
||||
if( *cs < *ct){
|
||||
return -1;
|
||||
} else if( *cs > *ct){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
size_t strlen(const char* str) {
|
||||
size_t len = 0;
|
||||
while(str[len]){
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int strncmp ( const char* str1, const char* str2, size_t num ){
|
||||
for( int i = 0; i < num ; i++){
|
||||
|
||||
if( str1[i] < str2[i]){
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( str1[i] > str2[i] ){
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
12
source/CoreLib/Memory.h
Normal file
12
source/CoreLib/Memory.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void* memset (void* ptr, int value, size_t num);
|
||||
|
||||
int memcmp( const void* ptr1, const void* ptr2, size_t num);
|
||||
|
||||
size_t strlen(const char* str);
|
||||
|
||||
int strncmp ( const char* str1, const char* str2, size_t num );
|
24
source/CoreLib/Path.cpp
Normal file
24
source/CoreLib/Path.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
||||
#include "Path.h"
|
||||
|
||||
Path::Path(String path)
|
||||
: path(path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
StringView Path::getbasename()
|
||||
{
|
||||
unsigned int path_length = path.length();
|
||||
int i = path_length;
|
||||
while (path[i] != '/')
|
||||
i--;
|
||||
|
||||
return {path,static_cast<unsigned int>(i +1), path_length};
|
||||
}
|
||||
|
||||
char* Path::str() {
|
||||
return path.str();
|
||||
}
|
22
source/CoreLib/Path.h
Normal file
22
source/CoreLib/Path.h
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include "StringView.h"
|
||||
|
||||
|
||||
class Path{
|
||||
public:
|
||||
explicit Path(String path);
|
||||
|
||||
StringView getbasename();
|
||||
|
||||
char* str();
|
||||
|
||||
private:
|
||||
String path;
|
||||
|
||||
};
|
||||
|
||||
|
3
source/CoreLib/Stack.cpp
Normal file
3
source/CoreLib/Stack.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
73
source/CoreLib/Stack.h
Normal file
73
source/CoreLib/Stack.h
Normal file
@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
#include "../kernel/memory/KernelHeap.h"
|
||||
#include <stdint.h>
|
||||
|
||||
template <typename T>
|
||||
class Stack {
|
||||
public:
|
||||
inline Stack() {
|
||||
elements = (T[MAX_STACK_SIZE]) malloc(MAX_STACK_SIZE * sizeof(T));
|
||||
num = 0;
|
||||
|
||||
}
|
||||
|
||||
inline void Push(T element){
|
||||
num++;
|
||||
if(num > MAX_STACK_SIZE)
|
||||
grow();
|
||||
|
||||
element[num] = element;
|
||||
}
|
||||
|
||||
|
||||
inline T Pop()
|
||||
{
|
||||
T temp = elements[num];
|
||||
num --;
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
inline bool isEmpty()
|
||||
{
|
||||
return num == 0;
|
||||
}
|
||||
|
||||
inline bool isFull()
|
||||
{
|
||||
return num == MAX_STACK_SIZE;
|
||||
}
|
||||
|
||||
|
||||
inline int count()
|
||||
{
|
||||
return num;
|
||||
}
|
||||
|
||||
inline ~Stack()
|
||||
{
|
||||
free(elements);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
unsigned int MAX_STACK_SIZE;
|
||||
T[MAX_STACK_SIZE] elements;
|
||||
unsigned int num;
|
||||
|
||||
inline void grow (){
|
||||
MAX_STACK_SIZE = MAX_STACK_SIZE + (int)(MAX_STACK_SIZE / 4);
|
||||
T[] new_elements =(T[MAX_STACK_SIZE]) malloc(MAX_STACK_SIZE * sizeof(T));
|
||||
|
||||
for ( int i = 0; i < num ; i++){
|
||||
new_elements[i] = elements[i];
|
||||
}
|
||||
free(elements);
|
||||
elements = new_elements;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
39
source/CoreLib/String.cpp
Normal file
39
source/CoreLib/String.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "String.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
String::String(char* characters)
|
||||
: chars(characters)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
char* String::str(){
|
||||
return chars;
|
||||
}
|
||||
|
||||
unsigned int String::length ()
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while ( chars[i] != '\0'){
|
||||
i++;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
// Returns a null character if size exceeds limits
|
||||
char String::operator[] (size_t idx)
|
||||
{
|
||||
if( idx > this->length())
|
||||
return '\0';
|
||||
|
||||
return chars[idx];
|
||||
}
|
||||
|
||||
const char String::operator[](size_t idx) const {
|
||||
return (const char) chars[idx];
|
||||
}
|
||||
|
18
source/CoreLib/String.h
Normal file
18
source/CoreLib/String.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
|
||||
class String {
|
||||
public:
|
||||
String(char* characters);
|
||||
String(String&) = default;
|
||||
unsigned int length();
|
||||
|
||||
char* str ();
|
||||
char operator[](size_t index) ;
|
||||
const char operator[](size_t idx) const;
|
||||
|
||||
protected:
|
||||
char* chars;
|
||||
|
||||
|
||||
};
|
14
source/CoreLib/StringView.cpp
Normal file
14
source/CoreLib/StringView.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
||||
#include "StringView.h"
|
||||
|
||||
StringView::StringView(String string, unsigned int start, unsigned int end)
|
||||
: String(string), begin(start), end(end)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
char* StringView::str(){
|
||||
//TODO: Not implemented
|
||||
}
|
14
source/CoreLib/StringView.h
Normal file
14
source/CoreLib/StringView.h
Normal file
@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by nigel on 19/02/23.
|
||||
//
|
||||
#pragma once
|
||||
#include "String.h"
|
||||
|
||||
class StringView : String {
|
||||
public:
|
||||
StringView(String string, unsigned int start, unsigned int end );
|
||||
char* str ();
|
||||
private:
|
||||
unsigned int begin;
|
||||
unsigned int end;
|
||||
};
|
BIN
source/CoreLib/bin/memory.o
Normal file
BIN
source/CoreLib/bin/memory.o
Normal file
Binary file not shown.
BIN
source/CoreLib/bin/path.o
Normal file
BIN
source/CoreLib/bin/path.o
Normal file
Binary file not shown.
BIN
source/CoreLib/bin/stack.o
Normal file
BIN
source/CoreLib/bin/stack.o
Normal file
Binary file not shown.
BIN
source/CoreLib/bin/string.o
Normal file
BIN
source/CoreLib/bin/string.o
Normal file
Binary file not shown.
BIN
source/CoreLib/bin/stringview.o
Normal file
BIN
source/CoreLib/bin/stringview.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user