From 277f3e8619941581a5edba104a9fae22fc9c2230 Mon Sep 17 00:00:00 2001 From: jief666 Date: Thu, 8 Apr 2021 11:01:20 +0300 Subject: [PATCH] Speed up devprop_generate_string by using a XBuffer instead of a String. Create a S8Catf in XBuffer. --- rEFIt_UEFI/Platform/Settings.cpp | 4 +-- rEFIt_UEFI/Platform/Utils.cpp | 4 +++ rEFIt_UEFI/Platform/Utils.h | 2 ++ rEFIt_UEFI/Platform/device_inject.cpp | 18 ++++++----- rEFIt_UEFI/Platform/device_inject.h | 2 +- rEFIt_UEFI/cpp_foundation/XBuffer.h | 36 +++++++++++++++++++--- rEFIt_UEFI/cpp_unit_test/XBuffer_tests.cpp | 35 +++++++++++++-------- 7 files changed, 73 insertions(+), 28 deletions(-) diff --git a/rEFIt_UEFI/Platform/Settings.cpp b/rEFIt_UEFI/Platform/Settings.cpp index 3c183fed5..d6d7f8f0a 100755 --- a/rEFIt_UEFI/Platform/Settings.cpp +++ b/rEFIt_UEFI/Platform/Settings.cpp @@ -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 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 ( diff --git a/rEFIt_UEFI/Platform/Utils.cpp b/rEFIt_UEFI/Platform/Utils.cpp index 1b75d1a45..81ecbdf5b 100644 --- a/rEFIt_UEFI/Platform/Utils.cpp +++ b/rEFIt_UEFI/Platform/Utils.cpp @@ -97,6 +97,10 @@ BOOLEAN IsHexDigit (CHAR8 c) { #ifdef __cplusplus +size_t hex2bin(const XBuffer& 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) { diff --git a/rEFIt_UEFI/Platform/Utils.h b/rEFIt_UEFI/Platform/Utils.h index 0f697c149..03adae508 100644 --- a/rEFIt_UEFI/Platform/Utils.h +++ b/rEFIt_UEFI/Platform/Utils.h @@ -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 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& buffer, uint8_t *out, size_t outlen); size_t hex2bin(const XStringW& s, uint8_t *out, size_t outlen); diff --git a/rEFIt_UEFI/Platform/device_inject.cpp b/rEFIt_UEFI/Platform/device_inject.cpp index e1a9cf067..6b373bcf3 100644 --- a/rEFIt_UEFI/Platform/device_inject.cpp +++ b/rEFIt_UEFI/Platform/device_inject.cpp @@ -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 devprop_generate_string(DevPropString *StringBuf) { + UINTN len = StringBuf->length * 2; INT32 i = 0; UINT32 x = 0; - XString8 buffer; + XBuffer 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; diff --git a/rEFIt_UEFI/Platform/device_inject.h b/rEFIt_UEFI/Platform/device_inject.h index 0dff08cb1..0efad600b 100644 --- a/rEFIt_UEFI/Platform/device_inject.h +++ b/rEFIt_UEFI/Platform/device_inject.h @@ -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& vl); -XString8 devprop_generate_string(DevPropString *string); +XBuffer devprop_generate_string(DevPropString *string); void devprop_free_string(DevPropString *string); BOOLEAN set_eth_props(pci_dt_t *eth_dev); diff --git a/rEFIt_UEFI/cpp_foundation/XBuffer.h b/rEFIt_UEFI/cpp_foundation/XBuffer.h index e4b8dfd7d..19d3248d8 100755 --- a/rEFIt_UEFI/cpp_foundation/XBuffer.h +++ b/rEFIt_UEFI/cpp_foundation/XBuffer.h @@ -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*)(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::_RData = _WData = NULL; + XRBuffer::m_size = 0; + XRBuffer::_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::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::Initialize(%zu) : malloc returned NULL. System halted\n", count); @@ -278,7 +308,7 @@ void XBuffer::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::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::ReadFromBuf(const char *buf, size_t *idx, size_t count) } } - - #endif diff --git a/rEFIt_UEFI/cpp_unit_test/XBuffer_tests.cpp b/rEFIt_UEFI/cpp_unit_test/XBuffer_tests.cpp index 2a49d87f0..a774883c9 100644 --- a/rEFIt_UEFI/cpp_unit_test/XBuffer_tests.cpp +++ b/rEFIt_UEFI/cpp_unit_test/XBuffer_tests.cpp @@ -8,20 +8,29 @@ int XBuffer_tests() // printf("XBuffer_tests -> Enter\n"); #endif - XBuffer xb_uint8; + { + XBuffer 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 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 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 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; }