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:
@@ -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; }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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]));
|
||||
|
||||
@@ -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]));
|
||||
|
||||
Reference in New Issue
Block a user