kernel+libos+libluna: Avoid copying and reallocation when creating Strings
All checks were successful
continuous-integration/drone/push Build is passing

This is done by reusing the existing buffers in Vector and Buffer instances.
This commit is contained in:
apio 2023-07-02 19:30:25 +02:00
parent 498e20561f
commit bd757d204e
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 12 additions and 5 deletions

View File

@ -431,7 +431,7 @@ namespace MemoryManager
TRY(result.try_append(0)); // null terminator
return String::from_cstring(result.data());
return String { result.release_data() };
}
bool validate_access(const void* mem, usize size, int flags)

View File

@ -152,6 +152,6 @@ namespace Base64
u8 nul_byte = '\0';
TRY(buf.append_data(&nul_byte, 1));
return String::from_cstring((char*)buf.data());
return String { (char*)buf.release_data() };
}
}

View File

@ -52,3 +52,10 @@ Result<void> Buffer::append_data(const u8* data, usize size)
return {};
}
u8* Buffer::release_data()
{
u8* data = m_data;
m_data = nullptr;
return data;
}

View File

@ -140,7 +140,7 @@ Result<String> String::vformat(StringView fmt, va_list ap)
TRY(buf.try_append(0));
return from_cstring(buf.data());
return String { buf.release_data() };
}
Result<String> String::from_cstring(const char* str)

View File

@ -61,5 +61,5 @@ Result<String> StringBuilder::string()
char nul = '\0';
TRY(m_data.append_data((u8*)&nul, 1));
return String::from_cstring((const char*)m_data.data());
return String { (char*)m_data.release_data() };
}

View File

@ -143,7 +143,7 @@ namespace os
TRY(data.try_append('\0'));
return String::from_cstring(data.data());
return String { data.release_data() };
}
Result<String> File::read_all_as_string()