kernel+libos+libluna: Avoid copying and reallocation when creating Strings
All checks were successful
continuous-integration/drone/push Build is passing
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:
parent
498e20561f
commit
bd757d204e
@ -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)
|
||||
|
@ -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() };
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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() };
|
||||
}
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user