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.
74 lines
1.4 KiB
C++
74 lines
1.4 KiB
C++
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|