1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:37:44 +00:00

Kernel: Add KBufferBuilder, similar to StringBuilder but for KBuffer

This class works by eagerly allocating 1MB of virtual memory but only
adding physical pages on demand. In other words, when you append to it,
its memory usage will increase by 1 page whenever you append across a
page boundary (4KB.)
This commit is contained in:
Andreas Kling 2019-08-06 20:04:12 +02:00
parent 83fdad25ed
commit f083031a27
3 changed files with 95 additions and 0 deletions

28
Kernel/KBufferBuilder.h Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <AK/AKString.h>
#include <Kernel/KBuffer.h>
#include <stdarg.h>
class KBufferBuilder {
public:
using OutputType = KBuffer;
explicit KBufferBuilder();
~KBufferBuilder() {}
void append(const StringView&);
void append(char);
void append(const char*, int);
void appendf(const char*, ...);
void appendvf(const char*, va_list);
KBuffer build();
private:
bool can_append(size_t) const;
u8* insertion_ptr() { return m_buffer.data() + m_size; }
KBuffer m_buffer;
size_t m_size { 0 };
};