libluna: Add a very simple Buffer class, to help implement tmpfs files
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
6fbf97292a
commit
89bea931cd
@ -10,6 +10,7 @@ set(FREESTANDING_SOURCES
|
|||||||
src/Units.cpp
|
src/Units.cpp
|
||||||
src/SystemError.cpp
|
src/SystemError.cpp
|
||||||
src/Bitmap.cpp
|
src/Bitmap.cpp
|
||||||
|
src/Buffer.cpp
|
||||||
src/Stack.cpp
|
src/Stack.cpp
|
||||||
src/OwnedStringView.cpp
|
src/OwnedStringView.cpp
|
||||||
src/Utf8.cpp
|
src/Utf8.cpp
|
||||||
|
40
libluna/include/luna/Buffer.h
Normal file
40
libluna/include/luna/Buffer.h
Normal 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
40
libluna/src/Buffer.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user