BarinkOS/source/CoreLib/Memory.cpp
Nigel b07b4f0d38 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.
2023-02-19 23:38:32 +01:00

58 lines
1009 B
C++

//
// 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;
}