1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 19:35:08 +00:00

StringBuilder: Use a ByteBuffer internally instead of a Vector<String>.

This commit is contained in:
Andreas Kling 2019-01-18 03:27:51 +01:00
parent 074edffc44
commit 9d7da26b4e
6 changed files with 58 additions and 37 deletions

View file

@ -47,6 +47,8 @@ public:
m_size = size; m_size = size;
} }
void grow(size_t size);
private: private:
enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt }; enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt };
explicit Buffer(size_t); // For ConstructionMode=Uninitialized explicit Buffer(size_t); // For ConstructionMode=Uninitialized
@ -87,7 +89,19 @@ inline Buffer<T>::Buffer(T* elements, size_t size, ConstructionMode mode)
} else if (mode == Wrap) { } else if (mode == Wrap) {
m_owned = false; m_owned = false;
} }
}
template<typename T>
inline void Buffer<T>::grow(size_t size)
{
ASSERT(size > m_size);
ASSERT(m_owned);
T* new_elements = static_cast<T*>(kmalloc(size * sizeof(T)));
memcpy(new_elements, m_elements, m_size * sizeof(T));
T* old_elements = m_elements;
m_elements = new_elements;
m_size = size;
kfree(old_elements);
} }
template<typename T> template<typename T>

View file

@ -73,6 +73,14 @@ public:
return copy(offset_pointer(offset), size); return copy(offset_pointer(offset), size);
} }
void grow(size_t size)
{
if (!m_impl)
m_impl = Buffer<byte>::create_uninitialized(size);
else
m_impl->grow(size);
}
private: private:
explicit ByteBuffer(RetainPtr<Buffer<byte>>&& impl) explicit ByteBuffer(RetainPtr<Buffer<byte>>&& impl)
: m_impl(move(impl)) : m_impl(move(impl))

View file

@ -38,7 +38,7 @@ bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
StringBuilder builder; StringBuilder builder;
for (auto& cpart : canonical_parts) { for (auto& cpart : canonical_parts) {
builder.append('/'); builder.append('/');
builder.append(move(cpart)); builder.append(cpart);
} }
m_string = builder.build(); m_string = builder.build();
return true; return true;

View file

@ -1,22 +1,30 @@
#include "StringBuilder.h" #include "StringBuilder.h"
#include <LibC/stdarg.h> #include <LibC/stdarg.h>
#include "printf.cpp" #include "printf.cpp"
#include <AK/StdLibExtras.h>
namespace AK { namespace AK {
void StringBuilder::append(String&& str) inline void StringBuilder::will_append(size_t size)
{ {
m_strings.append(move(str)); if ((m_length + size) > m_buffer.size())
m_buffer.grow(max(16u, m_buffer.size() * 2 + size));
} }
void StringBuilder::append(const String& str) void StringBuilder::append(const String& str)
{ {
m_strings.append(str); if (str.is_empty())
return;
will_append(str.length());
memcpy(m_buffer.pointer() + m_length, str.characters(), str.length());
m_length += str.length();
} }
void StringBuilder::append(char ch) void StringBuilder::append(char ch)
{ {
m_strings.append(StringImpl::create(&ch, 1)); will_append(1);
m_buffer.pointer()[m_length] = ch;
m_length += 1;
} }
void StringBuilder::appendf(const char* fmt, ...) void StringBuilder::appendf(const char* fmt, ...)
@ -29,27 +37,15 @@ void StringBuilder::appendf(const char* fmt, ...)
va_end(ap); va_end(ap);
} }
ByteBuffer StringBuilder::to_byte_buffer()
{
m_buffer.trim(m_length);
return m_buffer;
}
String StringBuilder::build() String StringBuilder::build()
{ {
auto strings = move(m_strings); return String((const char*)m_buffer.pointer(), m_length);
if (strings.is_empty())
return String::empty();
size_t sizeNeeded = 0;
for (auto& string : strings)
sizeNeeded += string.length();
char* buffer;
auto impl = StringImpl::create_uninitialized(sizeNeeded, buffer);
if (!impl)
return String();
for (auto& string : strings) {
memcpy(buffer, string.characters(), string.length());
buffer += string.length();
}
*buffer = '\0';
return String(move(impl));
} }
} }

View file

@ -11,14 +11,17 @@ public:
~StringBuilder() { } ~StringBuilder() { }
void append(const String&); void append(const String&);
void append(String&&);
void append(char); void append(char);
void appendf(const char*, ...); void appendf(const char*, ...);
String build(); String build();
ByteBuffer to_byte_buffer();
private: private:
Vector<String> m_strings; void will_append(size_t);
ByteBuffer m_buffer;
size_t m_length { 0 };
}; };
} }

View file

@ -42,7 +42,7 @@ ByteBuffer procfs$pid_fds(Process& process)
continue; continue;
builder.appendf("% 3u %s\n", i, descriptor->absolute_path().characters()); builder.appendf("% 3u %s\n", i, descriptor->absolute_path().characters());
} }
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$pid_vm(Process& process) ByteBuffer procfs$pid_vm(Process& process)
@ -58,7 +58,7 @@ ByteBuffer procfs$pid_vm(Process& process)
region->committed(), region->committed(),
region->name.characters()); region->name.characters());
} }
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$pid_vmo(Process& process) ByteBuffer procfs$pid_vmo(Process& process)
@ -87,7 +87,7 @@ ByteBuffer procfs$pid_vmo(Process& process)
} }
builder.appendf("\n"); builder.appendf("\n");
} }
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$pid_stack(Process& process) ByteBuffer procfs$pid_stack(Process& process)
@ -111,7 +111,7 @@ ByteBuffer procfs$pid_stack(Process& process)
unsigned offset = symbol.address - symbol.ksym->address; unsigned offset = symbol.address - symbol.ksym->address;
builder.appendf("%p %s +%u\n", symbol.address, symbol.ksym->name, offset); builder.appendf("%p %s +%u\n", symbol.address, symbol.ksym->name, offset);
} }
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$pid_regs(Process& process) ByteBuffer procfs$pid_regs(Process& process)
@ -130,7 +130,7 @@ ByteBuffer procfs$pid_regs(Process& process)
builder.appendf("flg: %x\n", tss.eflags); builder.appendf("flg: %x\n", tss.eflags);
builder.appendf("sp: %w:%x\n", tss.ss, tss.esp); builder.appendf("sp: %w:%x\n", tss.ss, tss.esp);
builder.appendf("pc: %w:%x\n", tss.cs, tss.eip); builder.appendf("pc: %w:%x\n", tss.cs, tss.eip);
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$pid_exe(Process& process) ByteBuffer procfs$pid_exe(Process& process)
@ -195,7 +195,7 @@ ByteBuffer procfs$mm()
builder.appendf("VMO count: %u\n", MM.m_vmos.size()); builder.appendf("VMO count: %u\n", MM.m_vmos.size());
builder.appendf("Free physical pages: %u\n", MM.m_free_physical_pages.size()); builder.appendf("Free physical pages: %u\n", MM.m_free_physical_pages.size());
builder.appendf("Free supervisor physical pages: %u\n", MM.m_free_supervisor_physical_pages.size()); builder.appendf("Free supervisor physical pages: %u\n", MM.m_free_supervisor_physical_pages.size());
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$mounts() ByteBuffer procfs$mounts()
@ -210,7 +210,7 @@ ByteBuffer procfs$mounts()
else else
builder.appendf("%u:%u\n", mount.host().fsid(), mount.host().index()); builder.appendf("%u:%u\n", mount.host().fsid(), mount.host().index());
}); });
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$cpuinfo() ByteBuffer procfs$cpuinfo()
@ -273,7 +273,7 @@ ByteBuffer procfs$cpuinfo()
copy_brand_string_part_to_buffer(2); copy_brand_string_part_to_buffer(2);
builder.appendf("brandstr: \"%s\"\n", buffer); builder.appendf("brandstr: \"%s\"\n", buffer);
} }
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$kmalloc() ByteBuffer procfs$kmalloc()
@ -287,7 +287,7 @@ ByteBuffer procfs$kmalloc()
sum_alloc, sum_alloc,
sum_free sum_free
); );
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$summary() ByteBuffer procfs$summary()
@ -310,7 +310,7 @@ ByteBuffer procfs$summary()
process->tty() ? strrchr(process->tty()->tty_name().characters(), '/') + 1 : "n/a", process->tty() ? strrchr(process->tty()->tty_name().characters(), '/') + 1 : "n/a",
process->name().characters()); process->name().characters());
} }
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
ByteBuffer procfs$inodes() ByteBuffer procfs$inodes()
@ -323,7 +323,7 @@ ByteBuffer procfs$inodes()
String path = vfs.absolute_path(*inode); String path = vfs.absolute_path(*inode);
builder.appendf("Inode{K%x} %02u:%08u (%u) %s\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count(), path.characters()); builder.appendf("Inode{K%x} %02u:%08u (%u) %s\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count(), path.characters());
} }
return builder.build().to_byte_buffer(); return builder.to_byte_buffer();
} }
bool ProcFS::initialize() bool ProcFS::initialize()