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

View File

@ -3,6 +3,7 @@
#include <BAN/Formatter.h> #include <BAN/Formatter.h>
#include <BAN/Variant.h> #include <BAN/Variant.h>
#include <errno.h>
#include <string.h> #include <string.h>
#if defined(__is_kernel) #if defined(__is_kernel)
@ -29,6 +30,14 @@ namespace BAN
result.m_error_code = 0xFF; result.m_error_code = 0xFF;
return result; 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; } uint8_t get_error_code() const { return m_error_code; }
const char* get_message() const { return m_message; } const char* get_message() const { return m_message; }

View File

@ -123,7 +123,7 @@ namespace BAN
auto& bucket = get_bucket(key); auto& bucket = get_bucket(key);
auto result = bucket.emplace_back(key, forward<Args>(args)...); auto result = bucket.emplace_back(key, forward<Args>(args)...);
if (result.is_error()) if (result.is_error())
return Error::from_string("HashMap: Could not allocate memory"); return Error::from_errno(ENOMEM);
m_size++; m_size++;
return {}; return {};
} }
@ -212,7 +212,7 @@ namespace BAN
size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2); size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2);
Vector<LinkedList<Entry>> new_buckets; Vector<LinkedList<Entry>> new_buckets;
if (new_buckets.resize(new_bucket_count).is_error()) 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 // 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. // 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(); size_type bucket_index = HASH()(entry.key) % new_buckets.size();
if (new_buckets[bucket_index].push_back(entry).is_error()) 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); size_type new_bucket_count = BAN::Math::max<size_type>(bucket_count, m_buckets.size() * 2);
Vector<Vector<T>> new_buckets; Vector<Vector<T>> new_buckets;
if (new_buckets.resize(new_bucket_count).is_error()) 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 // 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. // 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(); size_type bucket_index = HASH()(key) % new_buckets.size();
if (new_buckets[bucket_index].push_back(key).is_error()) 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)); Node* node = (Node*)BAN::allocator(sizeof(Node));
if (node == nullptr) if (node == nullptr)
return Error::from_string("LinkedList: Could not allocate memory"); return Error::from_errno(ENOMEM);
return node; return node;
} }

View File

@ -73,7 +73,7 @@ namespace BAN
{ {
uint32_t* count = new uint32_t(1); uint32_t* count = new uint32_t(1);
if (!count) if (!count)
return Error::from_string("RefCounted: Could not allocate memory"); return Error::from_errno(ENOMEM);
return RefCounted<T>((T*)data, count); return RefCounted<T>((T*)data, count);
} }
@ -82,10 +82,10 @@ namespace BAN
{ {
uint32_t* count = new uint32_t(1); uint32_t* count = new uint32_t(1);
if (!count) if (!count)
return Error::from_string("RefCounted: Could not allocate memory"); return Error::from_errno(ENOMEM);
T* data = new T(forward<Args>(args)...); T* data = new T(forward<Args>(args)...);
if (!data) if (!data)
return Error::from_string("RefCounted: Could not allocate memory"); return Error::from_errno(ENOMEM);
return RefCounted<T>(data, count); 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); size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
T* new_data = (T*)BAN::allocator(new_cap * sizeof(T)); T* new_data = (T*)BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr) 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++) for (size_type i = 0; i < m_size; i++)
{ {
new (new_data + i) T(move(m_data[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); size_type new_cap = BAN::Math::max<size_type>(size, m_capacity * 2);
T* new_data = (T*)BAN::allocator(new_cap * sizeof(T)); T* new_data = (T*)BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr) 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++) for (size_type i = 0; i < m_size; i++)
{ {
new (new_data + i) T(move(m_data[i])); new (new_data + i) T(move(m_data[i]));

View File

@ -228,7 +228,7 @@ namespace Kernel
BAN::ErrorOr<BAN::Vector<uint8_t>> Ext2Inode::read_all() BAN::ErrorOr<BAN::Vector<uint8_t>> Ext2Inode::read_all()
{ {
if (ifdir()) if (ifdir())
return BAN::Error::from_string("Inode is a directory"); return BAN::Error::from_errno(EISDIR);
BAN::Vector<uint8_t> data_buffer; BAN::Vector<uint8_t> data_buffer;
TRY(data_buffer.resize(m_inode.size)); TRY(data_buffer.resize(m_inode.size));
@ -256,7 +256,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefCounted<Inode>> Ext2Inode::directory_find(BAN::StringView file_name) BAN::ErrorOr<BAN::RefCounted<Inode>> Ext2Inode::directory_find(BAN::StringView file_name)
{ {
if (!ifdir()) if (!ifdir())
return BAN::Error::from_string("Inode is not a directory"); return BAN::Error::from_errno(ENOTDIR);
BAN::RefCounted<Inode> result; BAN::RefCounted<Inode> result;
BAN::Function<BAN::ErrorOr<bool>(const BAN::Vector<uint8_t>&)> function( BAN::Function<BAN::ErrorOr<bool>(const BAN::Vector<uint8_t>&)> function(
@ -272,7 +272,7 @@ namespace Kernel
{ {
Ext2Inode* inode = new Ext2Inode(m_fs, TRY(m_fs->read_inode(entry->inode)), entry_name); Ext2Inode* inode = new Ext2Inode(m_fs, TRY(m_fs->read_inode(entry->inode)), entry_name);
if (inode == nullptr) if (inode == nullptr)
return BAN::Error::from_string("Could not allocate Ext2Inode"); return BAN::Error::from_errno(ENOMEM);
result = TRY(BAN::RefCounted<Inode>::adopt(inode)); result = TRY(BAN::RefCounted<Inode>::adopt(inode));
return false; return false;
} }
@ -285,13 +285,13 @@ namespace Kernel
TRY(for_each_block(function)); TRY(for_each_block(function));
if (result) if (result)
return result; return result;
return BAN::Error::from_string("Could not find the asked inode"); return BAN::Error::from_errno(ENOENT);
} }
BAN::ErrorOr<BAN::Vector<BAN::RefCounted<Inode>>> Ext2Inode::directory_inodes() BAN::ErrorOr<BAN::Vector<BAN::RefCounted<Inode>>> Ext2Inode::directory_inodes()
{ {
if (!ifdir()) if (!ifdir())
return BAN::Error::from_string("Inode is not a directory"); return BAN::Error::from_errno(ENOTDIR);
BAN::Vector<BAN::RefCounted<Inode>> inodes; BAN::Vector<BAN::RefCounted<Inode>> inodes;
BAN::Function<BAN::ErrorOr<bool>(const BAN::Vector<uint8_t>&)> function( BAN::Function<BAN::ErrorOr<bool>(const BAN::Vector<uint8_t>&)> function(
@ -309,7 +309,7 @@ namespace Kernel
Ext2Inode* inode = new Ext2Inode(m_fs, BAN::move(current_inode), entry_name); Ext2Inode* inode = new Ext2Inode(m_fs, BAN::move(current_inode), entry_name);
if (inode == nullptr) if (inode == nullptr)
return BAN::Error::from_string("Could not allocate memory for Ext2Inode"); return BAN::Error::from_errno(ENOMEM);
TRY(inodes.push_back(TRY(BAN::RefCounted<Inode>::adopt(inode)))); TRY(inodes.push_back(TRY(BAN::RefCounted<Inode>::adopt(inode))));
} }
entry_addr += entry->rec_len; entry_addr += entry->rec_len;
@ -327,7 +327,7 @@ namespace Kernel
{ {
Ext2FS* ext2fs = new Ext2FS(partition); Ext2FS* ext2fs = new Ext2FS(partition);
if (ext2fs == nullptr) if (ext2fs == nullptr)
return BAN::Error::from_string("Could not allocate Ext2FS"); return BAN::Error::from_errno(ENOMEM);
TRY(ext2fs->initialize_superblock()); TRY(ext2fs->initialize_superblock());
TRY(ext2fs->initialize_block_group_descriptors()); TRY(ext2fs->initialize_block_group_descriptors());
TRY(ext2fs->initialize_root_inode()); TRY(ext2fs->initialize_root_inode());
@ -343,7 +343,7 @@ namespace Kernel
{ {
uint8_t* superblock_buffer = (uint8_t*)kmalloc(1024); uint8_t* superblock_buffer = (uint8_t*)kmalloc(1024);
if (superblock_buffer == nullptr) if (superblock_buffer == nullptr)
return BAN::Error::from_string("Could not allocate memory for superblocks"); BAN::Error::from_errno(ENOMEM);
BAN::ScopeGuard _([superblock_buffer] { kfree(superblock_buffer); }); BAN::ScopeGuard _([superblock_buffer] { kfree(superblock_buffer); });
uint32_t lba = 1024 / sector_size; uint32_t lba = 1024 / sector_size;
@ -399,7 +399,7 @@ namespace Kernel
uint8_t* block_group_descriptor_table_buffer = (uint8_t*)kmalloc(block_group_descriptor_table_sector_count * sector_size); uint8_t* block_group_descriptor_table_buffer = (uint8_t*)kmalloc(block_group_descriptor_table_sector_count * sector_size);
if (block_group_descriptor_table_buffer == nullptr) if (block_group_descriptor_table_buffer == nullptr)
return BAN::Error::from_string("Could not allocate memory for block group descriptor table"); return BAN::Error::from_errno(ENOMEM);
BAN::ScopeGuard _([block_group_descriptor_table_buffer] { kfree(block_group_descriptor_table_buffer); }); BAN::ScopeGuard _([block_group_descriptor_table_buffer] { kfree(block_group_descriptor_table_buffer); });
TRY(m_partition.read_sectors( TRY(m_partition.read_sectors(
@ -428,10 +428,9 @@ namespace Kernel
BAN::ErrorOr<void> Ext2FS::initialize_root_inode() BAN::ErrorOr<void> Ext2FS::initialize_root_inode()
{ {
Ext2Inode* root_inode = new Ext2Inode(this, TRY(read_inode(Ext2::Enum::ROOT_INO)), ""); Ext2Inode* root_inode = new Ext2Inode(this, TRY(read_inode(Ext2::Enum::ROOT_INO)), "");
if (root_inode == nullptr) if (root_inode == nullptr)
return BAN::Error::from_string("Could not allocate Ext2Inode"); return BAN::Error::from_errno(ENOMEM);
m_root_inode = TRY(BAN::RefCounted<Inode>::adopt(root_inode)); m_root_inode = TRY(BAN::RefCounted<Inode>::adopt(root_inode));
#if EXT2_DEBUG_PRINT #if EXT2_DEBUG_PRINT

View File

@ -15,7 +15,7 @@ namespace Kernel
ASSERT(s_instance == nullptr); ASSERT(s_instance == nullptr);
s_instance = new VirtualFileSystem(); s_instance = new VirtualFileSystem();
if (s_instance == nullptr) if (s_instance == nullptr)
return BAN::Error::from_string("Could not allocate the Virtual File System"); return BAN::Error::from_errno(ENOMEM);
return s_instance->initialize_impl(); return s_instance->initialize_impl();
} }

View File

@ -271,9 +271,6 @@ argument_done:
return TTY_PRINTLN("{}", directory_or_error.error()); return TTY_PRINTLN("{}", directory_or_error.error());
auto directory = directory_or_error.release_value(); auto directory = directory_or_error.release_value();
if (!directory->ifdir())
return TTY_PRINTLN("Given path does not point to a directory");
auto inodes_or_error = directory->directory_inodes(); auto inodes_or_error = directory->directory_inodes();
if (inodes_or_error.is_error()) if (inodes_or_error.is_error())
return TTY_PRINTLN("{}", inodes_or_error.error()); return TTY_PRINTLN("{}", inodes_or_error.error());

View File

@ -54,7 +54,7 @@ namespace Kernel
{ {
ATAController* controller = new ATAController(device); ATAController* controller = new ATAController(device);
if (controller == nullptr) if (controller == nullptr)
return BAN::Error::from_string("Could not allocate memory for ATAController"); return BAN::Error::from_errno(ENOMEM);
TRY(controller->initialize()); TRY(controller->initialize());
return controller; return controller;
} }
@ -256,7 +256,7 @@ namespace Kernel
if (status & ATA_STATUS_ERR) if (status & ATA_STATUS_ERR)
return error(); return error();
if (status & ATA_STATUS_DF) if (status & ATA_STATUS_DF)
return BAN::Error::from_string("Device fault"); return BAN::Error::from_errno(EIO);
status = read(ATA_PORT_STATUS); status = read(ATA_PORT_STATUS);
} }

View File

@ -41,9 +41,10 @@ string/memcpy.o \
string/memmove.o \ string/memmove.o \
string/memset.o \ string/memset.o \
string/strcmp.o \ string/strcmp.o \
string/strncmp.o \
string/strcpy.o \ string/strcpy.o \
string/strerror.o \
string/strlen.o \ string/strlen.o \
string/strncmp.o \
string/strncpy.o \ string/strncpy.o \
string/strstr.o \ string/strstr.o \

16
libc/include/errno.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <sys/cdefs.h>
#define ENOMEM 1
#define EINVAL 2
#define ENOTDIR 3
#define EISDIR 4
#define ENOENT 5
#define EIO 6
__BEGIN_DECLS
extern int errno;
__END_DECLS

View File

@ -19,4 +19,6 @@ char* strncpy(char* __restrict, const char* __restrict, size_t);
char* strstr(const char*, const char*); char* strstr(const char*, const char*);
char* strerror(int);
__END_DECLS __END_DECLS

32
libc/string/strerror.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <errno.h>
#include <string.h>
int errno = 0;
char* strerror(int error)
{
switch (error)
{
case ENOMEM:
return "Cannot allocate memory";
case EINVAL:
return "Invalid argument";
case EISDIR:
return "Is a directory";
case ENOTDIR:
return "Not a directory";
case ENOENT:
return "No such file or directory";
case EIO:
return "Input/output error";
default:
break;
}
// FIXME: sprintf
//static char buffer[26];
//sprintf(buffer, "Unknown error %d", error);
//return buffer;
errno = EINVAL;
return "Unknown error";
}