Speed up devprop_generate_string by using a XBuffer instead of a String.

Create a S8Catf in XBuffer.
This commit is contained in:
jief666 2021-04-08 11:01:20 +03:00
parent c11a90046a
commit 277f3e8619
7 changed files with 73 additions and 28 deletions

View File

@ -7710,14 +7710,14 @@ SetDevices (LOADER_ENTRY *Entry)
if (StringDirty) {
EFI_PHYSICAL_ADDRESS BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS; //0xFE000000;
XString8 newDeviceProperties = devprop_generate_string(device_inject_string);
XBuffer<char> newDeviceProperties = devprop_generate_string(device_inject_string);
size_t binaryPropSize = hex2bin(newDeviceProperties, NULL, 0);
if ( binaryPropSize > MAX_UINT32 ) {
MsgLog("devprop_generate_string(device_inject_string) is too big");
newDeviceProperties.setEmpty();
}else{
DBG("stringlength = %zu\n", newDeviceProperties.length());
DBG("stringlength = %zu\n", newDeviceProperties.size());
UINTN nbPages = EFI_SIZE_TO_PAGES(binaryPropSize);
Status = gBS->AllocatePages (

View File

@ -97,6 +97,10 @@ BOOLEAN IsHexDigit (CHAR8 c) {
#ifdef __cplusplus
size_t hex2bin(const XBuffer<char>& buffer, uint8_t *out, size_t outlen)
{
return hex2bin(buffer.data(), buffer.size(), out, outlen);
}
size_t hex2bin(const XString8& s, uint8_t *out, size_t outlen)
{

View File

@ -47,6 +47,7 @@ UINT8 hexstrtouint8 (CONST CHAR8* buf); //one or two hex letters to one by
#ifdef __cplusplus
#include "../cpp_foundation/XString.h"
#include "../cpp_foundation/XBuffer.h"
template <typename T, enable_if( is_char_ptr(T) || is___String(T) )>
size_t hex2bin(const T hex, size_t hexlen, uint8_t *out, size_t outlen)
@ -98,6 +99,7 @@ size_t hex2bin(const T hex, size_t hexlen, uint8_t *out, size_t outlen)
}
size_t hex2bin(const XString8& s, uint8_t *out, size_t outlen);
size_t hex2bin(const XBuffer<char>& buffer, uint8_t *out, size_t outlen);
size_t hex2bin(const XStringW& s, uint8_t *out, size_t outlen);

View File

@ -281,36 +281,37 @@ bool devprop_add_value(DevPropDevice *device, const XString8& nm, const XBuffer<
return devprop_add_value(device, nm.data(), vl.data(), vl.size());
}
XString8 devprop_generate_string(DevPropString *StringBuf)
XBuffer<char> devprop_generate_string(DevPropString *StringBuf)
{
UINTN len = StringBuf->length * 2;
INT32 i = 0;
UINT32 x = 0;
XString8 buffer;
XBuffer<char> buffer;
buffer.dataSized(len+1);
// DBG("devprop_generate_string\n");
buffer.S8Catf("%08X%08X%04hX%04hX", SwapBytes32(StringBuf->length), StringBuf->WHAT2, SwapBytes16(StringBuf->numentries), StringBuf->WHAT3);
buffer.S8Catf("%08X%08X%04hX%04hX", SwapBytes32(StringBuf->length), StringBuf->WHAT2, SwapBytes16(StringBuf->numentries), StringBuf->WHAT3);
while(i < StringBuf->numentries) {
UINT8 *dataptr = StringBuf->entries[i]->data;
buffer.S8Catf("%08X%04hX%04hX", SwapBytes32(StringBuf->entries[i]->length),
buffer.S8Catf("%08X%04hX%04hX", SwapBytes32(StringBuf->entries[i]->length),
SwapBytes16(StringBuf->entries[i]->numentries), StringBuf->entries[i]->WHAT2); //FIXME: wrong buffer sizes!
buffer.S8Catf("%02hhX%02hhX%04hX%08X%08X", StringBuf->entries[i]->acpi_dev_path.type,
buffer.S8Catf("%02hhX%02hhX%04hX%08X%08X", StringBuf->entries[i]->acpi_dev_path.type,
StringBuf->entries[i]->acpi_dev_path.subtype,
SwapBytes16(StringBuf->entries[i]->acpi_dev_path.length),
SwapBytes32(StringBuf->entries[i]->acpi_dev_path._HID),
SwapBytes32(StringBuf->entries[i]->acpi_dev_path._UID));
for(x = 0; x < StringBuf->entries[i]->num_pci_devpaths; x++) {
buffer.S8Catf("%02hhX%02hhX%04hX%02hhX%02hhX", StringBuf->entries[i]->pci_dev_path[x].type,
buffer.S8Catf("%02hhX%02hhX%04hX%02hhX%02hhX", StringBuf->entries[i]->pci_dev_path[x].type,
StringBuf->entries[i]->pci_dev_path[x].subtype,
SwapBytes16(StringBuf->entries[i]->pci_dev_path[x].length),
StringBuf->entries[i]->pci_dev_path[x].function,
StringBuf->entries[i]->pci_dev_path[x].device);
}
buffer.S8Catf("%02hhX%02hhX%04hX", StringBuf->entries[i]->path_end.type,
buffer.S8Catf("%02hhX%02hhX%04hX", StringBuf->entries[i]->path_end.type,
StringBuf->entries[i]->path_end.subtype,
SwapBytes16(StringBuf->entries[i]->path_end.length));
@ -322,6 +323,7 @@ XString8 devprop_generate_string(DevPropString *StringBuf)
return buffer;
}
void devprop_free_string(DevPropString *StringBuf)
{
INT32 i;

View File

@ -122,7 +122,7 @@ DevPropString *devprop_create_string(void);
DevPropDevice *devprop_add_device_pci(DevPropString *string, pci_dt_t *PciDt, EFI_DEVICE_PATH_PROTOCOL *DevicePath);
BOOLEAN devprop_add_value(DevPropDevice *device, CONST CHAR8 *nm, const UINT8 *vl, UINTN len); // to be removed
bool devprop_add_value(DevPropDevice *device, const XString8& nm, const XBuffer<uint8_t>& vl);
XString8 devprop_generate_string(DevPropString *string);
XBuffer<char> devprop_generate_string(DevPropString *string);
void devprop_free_string(DevPropString *string);
BOOLEAN set_eth_props(pci_dt_t *eth_dev);

View File

@ -223,6 +223,36 @@ public:
void cat(double d) { ncat(&d, sizeof(d)); };
void cat(void* p) { ncat(&p, sizeof(p)); };
protected:
static void transmitS8Printf(const char* buf, unsigned int nbchar, void* context)
{
((XBuffer<T>*)(context))->ncat(buf, nbchar);
}
public:
void vS8Catf(const char* format, XTOOLS_VA_LIST va)
{
vprintf_with_callback(format, va, transmitS8Printf, this);
}
void S8Catf(const char* format, ...) __attribute__((__format__(__printf__, 2, 3)))
{
XTOOLS_VA_LIST va;
XTOOLS_VA_START (va, format);
vS8Catf(format, va);
XTOOLS_VA_END(va);
}
T* forgetDataWithoutFreeing()
{
T* ret = data();
m_allocatedSize = 0;
XRBuffer<T>::_RData = _WData = NULL;
XRBuffer<T>::m_size = 0;
XRBuffer<T>::_Index = 0;
return ret;
}
// void cat(const XString8 &aXString8);
void cat(const XBuffer &unXBuffer) { ncat(unXBuffer.Length()); ncat(unXBuffer.Data(), unXBuffer.Length()); }
void deleteAtPos(unsigned int pos, unsigned int count=1);
@ -247,7 +277,7 @@ void XBuffer<T>::Initialize(const T* p, size_t count, size_t index)
if ( p!=NULL && count>0 )
{
m_allocatedSize = count;
_WData = (unsigned char*)malloc(m_allocatedSize);
_WData = (T*)malloc(m_allocatedSize);
if ( !_WData ) {
#ifdef DEBUG
panic("XBuffer<T>::Initialize(%zu) : malloc returned NULL. System halted\n", count);
@ -278,7 +308,7 @@ void XBuffer<T>::CheckSize(size_t nNewSize, size_t nGrowBy)
if ( m_allocatedSize < nNewSize )
{
nNewSize += nGrowBy;
_WData = (unsigned char*)Xrealloc(_WData, nNewSize, m_allocatedSize);
_WData = (T*)Xrealloc(_WData, nNewSize*sizeof(T), m_allocatedSize);
if ( !_WData ) {
#ifdef DEBUG
panic("XBuffer<T>::CheckSize(%zu, %zu) : Xrealloc(%" PRIuPTR " %zu, %zu) returned NULL. System halted\n", nNewSize, nGrowBy, uintptr_t(_WData), nNewSize, m_allocatedSize);
@ -490,6 +520,4 @@ bool XBuffer<T>::ReadFromBuf(const char *buf, size_t *idx, size_t count)
}
}
#endif

View File

@ -8,20 +8,29 @@ int XBuffer_tests()
// printf("XBuffer_tests -> Enter\n");
#endif
XBuffer<UINT8> xb_uint8;
{
XBuffer<UINT8> xb_uint8;
void* p = (void*)1;
char* p2 = (char*)2;
xb_uint8.cat(p);
xb_uint8.cat(p2);
xb_uint8.cat(uintptr_t(0));
XBuffer<UINT8> xb_uint8_2;
xb_uint8_2.cat(uintptr_t(1));
xb_uint8_2.cat(uintptr_t(2));
xb_uint8_2.cat(uintptr_t(0));
if ( xb_uint8_2 != xb_uint8 ) return 1;
}
{
XBuffer<UINT8> xb_uint8;
xb_uint8.S8Catf("%s %d %ls", "s1", 1, L"ls1");
xb_uint8.S8Catf("%s %d %ls", "s2", 2, L"ls2");
if ( memcmp(xb_uint8.data(), "s1 1 ls1s2 2 ls2", strlen("s1 1 ls1 s2 2 ls2")) != 0 ) return 2;
}
void* p = (void*)1;
char* p2 = (char*)2;
xb_uint8.cat(p);
xb_uint8.cat(p2);
xb_uint8.cat(uintptr_t(0));
XBuffer<UINT8> xb_uint8_2;
xb_uint8_2.cat(uintptr_t(1));
xb_uint8_2.cat(uintptr_t(2));
xb_uint8_2.cat(uintptr_t(0));
if ( xb_uint8_2 != xb_uint8 ) return 1;
return 0;
}