diff options
Diffstat (limited to 'Source/Kernel/Library')
-rw-r--r-- | Source/Kernel/Library/Bitset.class.cpp | 62 | ||||
-rw-r--r-- | Source/Kernel/Library/Bitset.class.h | 31 | ||||
-rw-r--r-- | Source/Kernel/Library/OrderedArray.class.cpp | 54 | ||||
-rw-r--r-- | Source/Kernel/Library/OrderedArray.class.h | 29 | ||||
-rw-r--r-- | Source/Kernel/Library/String.class.cpp | 261 | ||||
-rw-r--r-- | Source/Kernel/Library/String.class.h | 47 | ||||
-rw-r--r-- | Source/Kernel/Library/Vector.class.cpp | 103 | ||||
-rw-r--r-- | Source/Kernel/Library/Vector.class.h | 35 |
8 files changed, 622 insertions, 0 deletions
diff --git a/Source/Kernel/Library/Bitset.class.cpp b/Source/Kernel/Library/Bitset.class.cpp new file mode 100644 index 0000000..ec4e62c --- /dev/null +++ b/Source/Kernel/Library/Bitset.class.cpp @@ -0,0 +1,62 @@ +#include "Bitset.class.h" + +Bitset::Bitset() { +} + +Bitset::Bitset(u32int size) { + init(size, (u32int*)Mem::kalloc(INDEX_FROM_BIT(size))); +} + +Bitset::Bitset(u32int size, u32int *ptr) { + init(size, ptr); +} + +Bitset::~Bitset() { + Mem::kfree(m_data); +} + +void Bitset::init(u32int size, u32int *ptr) { + m_size = size; + m_data = ptr; + for (u32int i = 0; i < INDEX_FROM_BIT(m_size); i++) { + m_data[i] = 0; + } + m_usedCount = 0; +} + +void Bitset::setBit(u32int number) { + u32int idx = INDEX_FROM_BIT(number); + u32int off = OFFSET_FROM_BIT(number); + m_data[idx] |= (0x1 << off); + m_usedCount++; +} + +void Bitset::clearBit(u32int number) { + u32int idx = INDEX_FROM_BIT(number); + u32int off = OFFSET_FROM_BIT(number); + m_data[idx] &= ~(0x1 << off); + m_usedCount--; +} + +bool Bitset::testBit(u32int number) { + u32int idx = INDEX_FROM_BIT(number); + u32int off = OFFSET_FROM_BIT(number); + return (m_data[idx] & (0x1 << off)); +} + +u32int Bitset::firstFreeBit() { + for (u32int i = 0; i < INDEX_FROM_BIT(m_size); i++) { + if (m_data[i] != 0xFFFFFFFF) { + for (int j = 0; j < 32; j++) { + if (!(m_data[i] & (0x1 << j))) { + return (i * 4 * 8) + j; + } + } + } + } + return (u32int) - 1; +} + +u32int Bitset::usedBits() { + return m_usedCount; +} diff --git a/Source/Kernel/Library/Bitset.class.h b/Source/Kernel/Library/Bitset.class.h new file mode 100644 index 0000000..75fde24 --- /dev/null +++ b/Source/Kernel/Library/Bitset.class.h @@ -0,0 +1,31 @@ +#ifndef DEF_BITSET_CLASS_H +#define DEF_BITSET_CLASS_H + +#include <Core/common.wtf.h> + +#define INDEX_FROM_BIT(a) (a/(8*4)) +#define OFFSET_FROM_BIT(a) (a%(8*4)) + +class Bitset { + private: + u32int m_size; + u32int *m_data; + u32int m_usedCount; + + public: + Bitset(); + Bitset(u32int size); + Bitset(u32int size, u32int *ptr); + ~Bitset(); + + void init(u32int size, u32int *ptr); + + void setBit(u32int number); + void clearBit(u32int number); + bool testBit(u32int number); + u32int firstFreeBit(); + + u32int usedBits(); +}; + +#endif diff --git a/Source/Kernel/Library/OrderedArray.class.cpp b/Source/Kernel/Library/OrderedArray.class.cpp new file mode 100644 index 0000000..8b8f24f --- /dev/null +++ b/Source/Kernel/Library/OrderedArray.class.cpp @@ -0,0 +1,54 @@ +template <typename T> +OrderedArray<T>::OrderedArray(u32int max_size) { + m_array = (T*)Memory::alloc(max_size * sizeof(T*)); + m_size = 0; + m_maxSize = max_size; +} + +template <typename T> +OrderedArray<T>::OrderedArray(T **addr, u32int max_size) { + m_array = addr; + memset((u8int*)addr, 0, max_size * sizeof(T*)); + m_size = 0; + m_maxSize = max_size; +} + +template <typename T> +OrderedArray<T>::~OrderedArray() { + //Free memory +} + +template <typename T> +void OrderedArray<T>::insert(T *element) { + if (m_size == m_maxSize) return; //Array is full + u32int iterator = 0; + while (iterator < m_size && *(m_array[iterator]) < *element) { + iterator++; + } + if (iterator == m_size) { + m_array[m_size++] = element; + } else { + u32int pos = iterator; + while (iterator < m_size) { + iterator++; + m_array[iterator] = m_array[iterator - 1]; + } + m_size++; + m_array[pos] = element; + } +} + +template <typename T> +T *OrderedArray<T>::lookup(int index) { + return m_array[index]; +} + +template <typename T> +void OrderedArray<T>::remove(int index) { + m_size--; + while (index < m_size) { + m_array[index] = m_array[index + 1]; + index++; + } +} + diff --git a/Source/Kernel/Library/OrderedArray.class.h b/Source/Kernel/Library/OrderedArray.class.h new file mode 100644 index 0000000..2a5acdd --- /dev/null +++ b/Source/Kernel/Library/OrderedArray.class.h @@ -0,0 +1,29 @@ +#ifndef DEF_ORDARRAY_CLASS +#define DEF_ORDARRAY_CLASS + +#include <Core/common.wtf.h> + +template <typename T> +class OrderedArray { + private: + T *m_array[]; + u32int m_size; + u32int m_maxSize; + + public: + OrderedArray(u32int max_size); + OrderedArray(T **addr, u32int max_size); + ~OrderedArray(); + + u32int size() { return m_size; } + + void insert(T *element); + T *lookup(int index); + void remove(int index); + + T *operator[] (int index) { return lookup(index); } +}; + +#include "OrderedArray.class.cpp" + +#endif diff --git a/Source/Kernel/Library/String.class.cpp b/Source/Kernel/Library/String.class.cpp new file mode 100644 index 0000000..dc763bd --- /dev/null +++ b/Source/Kernel/Library/String.class.cpp @@ -0,0 +1,261 @@ +#include "String.class.h" +#include <Library/Vector.class.h> + +using namespace CMem; //strlen and memcpy + +String String::hex(u32int number) { + String ret; + ret.m_length = 10; + ret.m_string = (char*)Mem::kalloc(11); + ret.m_string[0] = '0'; + ret.m_string[1] = 'x'; + ret.m_string[10] = 0; + + char hexdigits[] = "0123456789ABCDEF"; + for (unsigned int j = 0; j < 8; j++) { + ret.m_string[j + 2] = hexdigits[(number & 0xF0000000) >> 28]; + number = number << 4; + } + return ret; +} + +String String::number(s32int number) { + if (number == 0) return String("0"); + bool negative = false; + if (number < 0) { + negative = true; + number = 0 - number; + } + u32int order = 0, temp = number; + char numbers[] = "0123456789"; + while (temp > 0) { + order++; + temp /= 10; + } + + String ret; + ret.m_length = order; + ret.m_string = (char*)Mem::kalloc(order + 1); + + for (u32int i = order; i > 0; i--) { + ret.m_string[i - 1] = numbers[number % 10]; + number /= 10; + } + + ret.m_string[order] = 0; + + if (negative) return String("-") += ret; + + return ret; +} + +String::String() { + m_string = 0; + m_length = 0; +} + +String::String(char* string) { + m_length = strlen(string); + m_string = (char*)Mem::kalloc(m_length + 1); + for (u32int i = 0; i < m_length; i++) { + m_string[i] = string[i]; + } + m_string[m_length] = 0; +} + +String::String(const String &other) { + m_length = other.m_length; + m_string = (char*)Mem::kalloc(m_length + 1); + for (u32int i = 0; i < m_length; i++) { + m_string[i] = other.m_string[i]; + } + m_string[m_length] = 0; +} + +String::~String() { + if (m_string != 0) Mem::kfree(m_string); +} + +void String::operator= (const String &other) { + m_length = other.m_length; + if (m_string != 0) Mem::kfree(m_string); + m_string = (char*)Mem::kalloc(m_length + 1); + for (u32int i = 0; i < m_length; i++) { + m_string[i] = other.m_string[i]; + } + m_string[m_length] = 0; +} + +void String::operator= (char* string) { + m_length = strlen(string); + if (m_string != 0) Mem::kfree(m_string); + m_string = (char*)Mem::kalloc(m_length + 1); + for (u32int i = 0; i < m_length; i++) { + m_string[i] = string[i]; + } + m_string[m_length] = 0; +} + +bool String::operator== (String &other) { + if (m_length != other.m_length) return false; + for (u32int i = 0; i < m_length; i++) { + if (m_string[i] != other.m_string[i]) return false; + } + return true; +} + +bool String::operator== (char* string) { + if (m_length != strlen(string)) return false; + for (u32int i = 0; i < m_length; i++) { + if (m_string[i] != string[i]) return false; + } + return true; +} + +String& String::operator+= (String &other) { + char* newdata = (char*)Mem::kalloc(m_length + other.m_length + 1); + for (u32int i = 0; i < m_length; i++) { + newdata[i] = m_string[i]; + } + for (u32int i = 0; i < other.m_length; i++) { + newdata[i + m_length] = other.m_string[i]; + } + if (m_string != 0) Mem::kfree(m_string); + m_string = newdata; + m_length += other.m_length; + m_string[m_length] = 0; + return *this; +} + +String& String::operator+= (char* other) { + char* newdata = (char*)Mem::kalloc(m_length + strlen(other) + 1); + for (u32int i = 0; i < m_length; i++) { + newdata[i] = m_string[i]; + } + for (u32int i = 0; i < strlen(other); i++) { + newdata[i + m_length] = other[i]; + } + if (m_string != 0) Mem::kfree(m_string); + m_string = newdata; + m_length += strlen(other); + m_string[m_length] = 0; + return *this; +} + +String& String::operator+= (char other) { + char* newdata = (char*)Mem::kalloc(m_length + 2); + for (u32int i = 0; i < m_length; i++) { + newdata[i] = m_string[i]; + } + if (m_string != 0) Mem::kfree(m_string); + m_string = newdata; + m_string[m_length] = other; + m_length++; + m_string[m_length] = 0; + return *this; +} + +String& String::operator+ (String &other) { //Can be optimized + String ret(*this); + return (ret += other); +} + +String& String::operator+ (char* other) { //Can be optimized + String ret(*this); + return (ret += other); +} + +String& String::operator+ (char other) { //Can be optimized + String ret(*this); + return (ret += other); +} + +String::operator char* () { + if (m_string == 0) return ""; + return m_string; +} + +s32int String::toInt() { + if (m_string == 0) return 0; + s32int pos = 0, number = 0; + bool negative = false; + if (m_string[0] == '-') { + negative = true; + pos = 1; + } + while (m_string[pos] >= '0' && m_string[pos] <= '9') { + number *= 10; + number += (m_string[pos] - '0'); + pos++; + } + if (negative) return 0 - number; + return number; +} + +u32int String::toInt16() { + if (m_string == 0) return 0; + u32int pos = 0, number = 0; + if (m_string[0] == '0' && m_string[1] == 'x') pos = 2; + while (1) { + char c = m_string[pos]; + pos++; + if (c >= 'a' && c <= 'f') c -= ('a' - 'A'); //To uppercase + if (c >= '0' && c <= '9') { + number *= 16; + number += (c - '0'); + continue; + } + if (c >= 'A' && c <= 'F') { + number *= 16; + number += (c - 'A' + 10); + continue; + } + break; + } + return number; +} + +char& String::operator[] (int index) { + return m_string[index]; +} + +u32int String::size() { + return m_length; +} + +void String::clear() { + Mem::kfree(m_string); + m_length = 0; + m_string = 0; +} + +bool String::empty() { + return (m_length == 0); +} + +Vector<String> String::split(char c) { + Vector<String> ret; + ret.push(String("")); + for (u32int i = 0; i < m_length; i++) { + if (m_string[i] == c) { + ret.push(String("")); + } else { + ret.back() += m_string[i]; + } + } + return ret; +} + +String String::substr(s32int start, s32int size) { + if (start < 0) start = m_length - start; + if (size < 0) { //this fucks + start = start + size; + size = 0 - size; + } + String ret; + ret.m_string = (char*)Mem::kalloc(size + 1); + ret.m_length = size; + memcpy((u8int*)ret.m_string, (const u8int*)(m_string + start), size); + ret.m_string[size] = 0; + return ret; +} diff --git a/Source/Kernel/Library/String.class.h b/Source/Kernel/Library/String.class.h new file mode 100644 index 0000000..58237f0 --- /dev/null +++ b/Source/Kernel/Library/String.class.h @@ -0,0 +1,47 @@ +#ifndef DEF_STRING_CLASS +#define DEF_STRING_CLASS + +#include <Core/common.wtf.h> + +template <typename T> class Vector; + +class String { + private: + char *m_string; + u32int m_length; + + public: + static String hex(u32int number); + static String number(s32int number); + + String(char* string); + String(); + String(const String &other); + ~String(); + + void operator= (const String &other); + void operator= (char* string); + + bool operator== (String &other); + bool operator== (char* string); + String &operator+= (String &other); + String &operator+= (char* other); + String &operator+= (char other); + String &operator+ (String &other); + String &operator+ (char* other); + String &operator+ (char other); + operator char* (); + s32int toInt(); + u32int toInt16(); //From HEX + char& operator[] (int index); + + u32int size(); + void clear(); + bool empty(); + + Vector<String> split(char c); + + String substr(s32int start, s32int size); +}; + +#endif diff --git a/Source/Kernel/Library/Vector.class.cpp b/Source/Kernel/Library/Vector.class.cpp new file mode 100644 index 0000000..aaa777f --- /dev/null +++ b/Source/Kernel/Library/Vector.class.cpp @@ -0,0 +1,103 @@ +using namespace CMem; //strlen and memcpy + +template <typename T> +Vector<T>::Vector() { + m_data = 0; + m_size = 0; +} + +template <typename T> +Vector<T>::Vector(u32int size) { + m_data = new T[size]; + m_size = size; +} + +template <typename T> +Vector<T>::Vector(u32int size, T value) { + //m_data = (T*)Mem::kalloc(size * sizeof(T)); + m_data = new T[size](value); + m_size = size; + /*for (u32int i = 0; i < size; i++) { + m_data[i] = new(&m_data[i]) T(value); + }*/ +} + +template <typename T> +Vector<T>::Vector(const Vector<T> &other) { + m_size = other.m_size; + m_data = (T*)Mem::kalloc(m_size * sizeof(T)); + for (u32int i = 0; i < m_size; i++) { + new(&m_data[i]) T(other.m_data[i]); + } +} + +template <typename T> +Vector<T>::~Vector() { + if (m_data != 0) delete[] m_data; +} + +template <typename T> +T& Vector<T>::operator[] (u32int index) { + return m_data[index]; +} + +template <typename T> +void Vector<T>::push(T element) { + T* newdata = (T*)Mem::kalloc((m_size + 1) * sizeof(T)); + memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T)); + new(&newdata[m_size]) T(element); //Construct by copy + m_size++; + Mem::kfree(m_data); + m_data = newdata; +} + +/* template <typename T> +void Vector<T>::push(T& element) { + T* newdata = (T*)Memory::alloc((m_size + 1) * sizeof(T)); + memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T)); + //memcpy((u8int*)newdata + (m_size * sizeof(T)), (const u8int*) element, sizeof(T)); //Copy + new(&newdata[m_size]) T(element); //Construct by copy + m_size++; + Memory::free(m_data); + m_data = newdata; +} */ + +template <typename T> +void Vector<T>::pop() { + m_size--; + //delete(&m_data[m_size], &m_data[m_size]); //implicitly call destructor with placement delete + m_data[m_size].~T(); //Call destructor + T* newdata = (T*)Mem::kalloc(m_size * sizeof(T)); + memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T)); + Mem::kfree(m_data); + m_data = newdata; +} + +template <typename T> +T& Vector<T>::back() { + return m_data[m_size - 1]; +} + +template <typename T> +T& Vector<T>::front() { + return m_data[0]; +} + + +template <typename T> +u32int Vector<T>::size() { + return m_size; +} + +template <typename T> +bool Vector<T>::empty() { + return m_size == 0; +} + +template <typename T> +void Vector<T>::clear() { + if (empty()) return; + Mem::kfree(m_data); + m_data = 0; + m_size = 0; +} diff --git a/Source/Kernel/Library/Vector.class.h b/Source/Kernel/Library/Vector.class.h new file mode 100644 index 0000000..9763d2c --- /dev/null +++ b/Source/Kernel/Library/Vector.class.h @@ -0,0 +1,35 @@ +#ifndef DEF_VECTOR_CLASS +#define DEF_VECTOR_CLASS + +#include <Core/common.wtf.h> + +template <typename T> +class Vector { + private: + T *m_data; + u32int m_size; + + public: + Vector(); + Vector(u32int size); + Vector(u32int size, T value); + Vector(const Vector<T> &other); + ~Vector(); + + T& operator[] (u32int index); + + void push(T element); + //void push(T& element); + void pop(); + + T& back(); + T& front(); + + u32int size(); + bool empty(); + void clear(); +}; + +#include "Vector.class.cpp" + +#endif |