libluna: Add a very simple Buffer class, to help implement tmpfs files
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-03-11 00:27:08 +01:00
parent 6fbf97292a
commit 89bea931cd
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 81 additions and 0 deletions

View File

@ -10,6 +10,7 @@ set(FREESTANDING_SOURCES
src/Units.cpp
src/SystemError.cpp
src/Bitmap.cpp
src/Buffer.cpp
src/Stack.cpp
src/OwnedStringView.cpp
src/Utf8.cpp

View File

@ -0,0 +1,40 @@
#pragma once
#include <luna/Result.h>
#include <luna/Types.h>
class Buffer
{
public:
Buffer();
~Buffer();
Buffer(Buffer&& other);
Buffer(const Buffer& other) = delete; // For now.
static Result<Buffer> create_sized(usize size);
Result<void> try_resize(usize new_size);
Result<u8*> create_slice_at_end(usize size);
u8* data()
{
return m_data;
}
const u8* data() const
{
return m_data;
}
usize size() const
{
return m_size;
}
private:
Buffer(u8* data, usize size);
u8* m_data { nullptr };
usize m_size { 0 };
};

40
libluna/src/Buffer.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <luna/Buffer.h>
#include <luna/Heap.h>
Buffer::Buffer()
{
}
Buffer::Buffer(u8* data, usize size) : m_data(data), m_size(size)
{
}
Buffer::Buffer(Buffer&& other) : m_data(other.data()), m_size(other.size())
{
other.m_data = nullptr;
}
Buffer::~Buffer()
{
if (m_data) free_impl(m_data);
}
Result<Buffer> Buffer::create_sized(usize size)
{
u8* data = (u8*)TRY(malloc_impl(size));
return Buffer { data, size };
}
Result<void> Buffer::try_resize(usize new_size)
{
m_data = (u8*)TRY(realloc_impl(m_data, new_size));
m_size = new_size;
return {};
}
Result<u8*> Buffer::create_slice_at_end(usize size)
{
usize old_size = m_size;
TRY(try_resize(m_size + size));
return m_data + old_size;
}