Kernel+LibC: Add some errno codes

Kernel now returns ENOMEM and other errnos, so we dont have to write
error messages
This commit is contained in:
Bananymous
2023-03-02 21:10:44 +02:00
parent 90a7268e5a
commit 52aa98ba25
16 changed files with 87 additions and 31 deletions

View File

@@ -230,7 +230,7 @@ namespace BAN
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
void* new_data = BAN::allocator(new_cap);
if (new_data == nullptr)
return Error::from_string("String: Could not allocate memory");
return Error::from_errno(ENOMEM);
if (m_data)
memcpy(new_data, m_data, m_size + 1);
BAN::deallocator(m_data);

View File

@@ -3,6 +3,7 @@
#include <BAN/Formatter.h>
#include <BAN/Variant.h>
#include <errno.h>
#include <string.h>
#if defined(__is_kernel)
@@ -29,6 +30,14 @@ namespace BAN
result.m_error_code = 0xFF;
return result;
}
static Error from_errno(int error)
{
Error result;
strncpy(result.m_message, strerror(error), sizeof(m_message));
result.m_message[sizeof(result.m_message) - 1] = '\0';
result.m_error_code = error;
return result;
}
uint8_t get_error_code() const { return m_error_code; }
const char* get_message() const { return m_message; }

View File

@@ -123,7 +123,7 @@ namespace BAN
auto& bucket = get_bucket(key);
auto result = bucket.emplace_back(key, forward<Args>(args)...);
if (result.is_error())
return Error::from_string("HashMap: Could not allocate memory");
return Error::from_errno(ENOMEM);
m_size++;
return {};
}
@@ -212,7 +212,7 @@ namespace BAN
size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2);
Vector<LinkedList<Entry>> new_buckets;
if (new_buckets.resize(new_bucket_count).is_error())
return Error::from_string("HashMap: Could not allocate memory");
return Error::from_errno(ENOMEM);
// NOTE: We have to copy the old entries to the new entries and not move
// since we might run out of memory half way through.
@@ -222,7 +222,7 @@ namespace BAN
{
size_type bucket_index = HASH()(entry.key) % new_buckets.size();
if (new_buckets[bucket_index].push_back(entry).is_error())
return Error::from_string("HashMap: Could not allocate memory");
return Error::from_errno(ENOMEM);
}
}

View File

@@ -197,7 +197,7 @@ namespace BAN
size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2);
Vector<Vector<T>> new_buckets;
if (new_buckets.resize(new_bucket_count).is_error())
return Error::from_string("HashSet: Could not allocate memory");
return Error::from_errno(ENOMEM);
// NOTE: We have to copy the old keys to the new keys and not move
// since we might run out of memory half way through.
@@ -207,7 +207,7 @@ namespace BAN
{
size_type bucket_index = HASH()(key) % new_buckets.size();
if (new_buckets[bucket_index].push_back(key).is_error())
return Error::from_string("HashSet: Could not allocate memory");
return Error::from_errno(ENOMEM);
}
}

View File

@@ -288,7 +288,7 @@ namespace BAN
{
Node* node = (Node*)BAN::allocator(sizeof(Node));
if (node == nullptr)
return Error::from_string("LinkedList: Could not allocate memory");
return Error::from_errno(ENOMEM);
return node;
}

View File

@@ -73,7 +73,7 @@ namespace BAN
{
uint32_t* count = new uint32_t(1);
if (!count)
return Error::from_string("RefCounted: Could not allocate memory");
return Error::from_errno(ENOMEM);
return RefCounted<T>((T*)data, count);
}
@@ -82,10 +82,10 @@ namespace BAN
{
uint32_t* count = new uint32_t(1);
if (!count)
return Error::from_string("RefCounted: Could not allocate memory");
return Error::from_errno(ENOMEM);
T* data = new T(forward<Args>(args)...);
if (!data)
return Error::from_string("RefCounted: Could not allocate memory");
return Error::from_errno(ENOMEM);
return RefCounted<T>(data, count);
}

View File

@@ -205,7 +205,7 @@ namespace BAN
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
T* new_data = (T*)BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr)
return Error::from_string("Queue: Could not allocate memory");
return Error::from_errno(ENOMEM);
for (size_type i = 0; i < m_size; i++)
{
new (new_data + i) T(move(m_data[i]));

View File

@@ -400,7 +400,7 @@ namespace BAN
size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
T* new_data = (T*)BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr)
return Error::from_string("Vector: Could not allocate memory");
return Error::from_errno(ENOMEM);
for (size_type i = 0; i < m_size; i++)
{
new (new_data + i) T(move(m_data[i]));