From 32fbf76326ba45d6c06f6b3655877dbec21da2fd Mon Sep 17 00:00:00 2001 From: jief Date: Sat, 18 Nov 2023 11:04:54 +0100 Subject: [PATCH] Update BaseLib.h with macro BASE_LIST_FOR_EACH. Introduce class apd (automatic pointer destruction) Introduce MemoryTracker Weak symbols in MemoryAllocationLib to be able to redefine them (see MemoryTracker). Embed LegacyOS instead of a pointer. Fixed a lot of memory leaks. --- .../CloverX64.xcodeproj/project.pbxproj | 17 + rEFIt_UEFI/Platform/BasicIO.h | 1 + rEFIt_UEFI/Platform/DataHubCpu.cpp | 5 +- rEFIt_UEFI/Platform/Edid.cpp | 5 +- rEFIt_UEFI/Platform/Hibernate.cpp | 2 +- rEFIt_UEFI/Platform/KextList.cpp | 2 +- rEFIt_UEFI/Platform/Nvram.cpp | 54 +-- rEFIt_UEFI/Platform/Nvram.h | 2 +- rEFIt_UEFI/Platform/Utils.cpp | 4 +- rEFIt_UEFI/Platform/Utils.h | 4 +- rEFIt_UEFI/Platform/Volume.cpp | 2 +- rEFIt_UEFI/Platform/Volume.h | 18 +- rEFIt_UEFI/Platform/plist/TagArray.cpp | 28 +- rEFIt_UEFI/Platform/plist/TagArray.h | 6 +- rEFIt_UEFI/Platform/plist/TagBool.cpp | 22 +- rEFIt_UEFI/Platform/plist/TagBool.h | 6 +- rEFIt_UEFI/Platform/plist/TagData.cpp | 22 +- rEFIt_UEFI/Platform/plist/TagData.h | 6 +- rEFIt_UEFI/Platform/plist/TagDate.cpp | 22 +- rEFIt_UEFI/Platform/plist/TagDate.h | 6 +- rEFIt_UEFI/Platform/plist/TagDict.cpp | 30 +- rEFIt_UEFI/Platform/plist/TagDict.h | 14 +- rEFIt_UEFI/Platform/plist/TagFloat.cpp | 22 +- rEFIt_UEFI/Platform/plist/TagFloat.h | 6 +- rEFIt_UEFI/Platform/plist/TagInt64.cpp | 22 +- rEFIt_UEFI/Platform/plist/TagInt64.h | 6 +- rEFIt_UEFI/Platform/plist/TagKey.cpp | 22 +- rEFIt_UEFI/Platform/plist/TagKey.h | 6 +- rEFIt_UEFI/Platform/plist/TagString8.cpp | 22 +- rEFIt_UEFI/Platform/plist/TagString8.h | 6 +- rEFIt_UEFI/Platform/plist/plist.cpp | 32 +- rEFIt_UEFI/Platform/plist/plist.h | 14 +- rEFIt_UEFI/PlatformEFI/BasicIO.cpp | 13 + .../PlatformEFI/cpp_util/globals_ctor.cpp | 9 +- .../PlatformEFI/cpp_util/globals_dtor.cpp | 2 + .../cpp_util/operatorNewDelete.cpp | 6 +- rEFIt_UEFI/PlatformEFI/include/Platform.h | 1 + rEFIt_UEFI/PlatformEFI/posix/limits.h | 35 ++ rEFIt_UEFI/PlatformEFI/posix/stdint.h | 36 +- rEFIt_UEFI/Settings/ConfigManager.cpp | 7 +- rEFIt_UEFI/Settings/ConfigPlist/SMBIOSPlist.h | 6 +- rEFIt_UEFI/Settings/Self.h | 5 +- rEFIt_UEFI/cpp_foundation/XToolsCommon.h | 15 +- rEFIt_UEFI/cpp_foundation/apd.h | 74 ++++ rEFIt_UEFI/cpp_lib/MemoryTracker.cpp | 391 ++++++++++++++++++ rEFIt_UEFI/cpp_lib/MemoryTracker.h | 98 +++++ rEFIt_UEFI/cpp_lib/XmlLiteParser.h | 3 +- rEFIt_UEFI/cpp_lib/undefinable.h | 2 + .../cpp_unit_test/MemoryTracker_test.cpp | 132 ++++++ rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.h | 3 + rEFIt_UEFI/cpp_unit_test/all_tests.cpp | 47 +-- rEFIt_UEFI/cpp_unit_test/plist_tests.cpp | 3 + rEFIt_UEFI/entry_scan/legacy.cpp | 28 +- rEFIt_UEFI/entry_scan/loader.cpp | 11 +- rEFIt_UEFI/gui/menu_items/menu_items.cpp | 17 +- rEFIt_UEFI/gui/menu_items/menu_items.h | 10 +- rEFIt_UEFI/gui/shared_with_menu.cpp | 2 +- rEFIt_UEFI/gui/shared_with_menu.h | 2 +- rEFIt_UEFI/libeg/VectorGraphics.cpp | 2 +- rEFIt_UEFI/libeg/XTheme.cpp | 4 +- rEFIt_UEFI/libeg/nanosvg.cpp | 59 +-- rEFIt_UEFI/refit/lib.cpp | 285 +++++++------ rEFIt_UEFI/refit/main.cpp | 181 ++++++-- rEFIt_UEFI/refit/menu.cpp | 3 - 64 files changed, 1453 insertions(+), 475 deletions(-) create mode 100644 rEFIt_UEFI/cpp_foundation/apd.h create mode 100644 rEFIt_UEFI/cpp_lib/MemoryTracker.cpp create mode 100644 rEFIt_UEFI/cpp_lib/MemoryTracker.h create mode 100644 rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.cpp create mode 100644 rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.h diff --git a/Xcode/CloverX64/CloverX64.xcodeproj/project.pbxproj b/Xcode/CloverX64/CloverX64.xcodeproj/project.pbxproj index 1bb2a56e5..e7970b801 100644 --- a/Xcode/CloverX64/CloverX64.xcodeproj/project.pbxproj +++ b/Xcode/CloverX64/CloverX64.xcodeproj/project.pbxproj @@ -7,6 +7,12 @@ objects = { /* Begin PBXBuildFile section */ + 9A0064A52B08298E0016E3EB /* MemoryTracker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0064A32B08298D0016E3EB /* MemoryTracker.cpp */; }; + 9A0064A62B08298E0016E3EB /* MemoryTracker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0064A32B08298D0016E3EB /* MemoryTracker.cpp */; }; + 9A0064A72B08298E0016E3EB /* MemoryTracker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0064A32B08298D0016E3EB /* MemoryTracker.cpp */; }; + 9A0064A82B08298E0016E3EB /* MemoryTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A0064A42B08298D0016E3EB /* MemoryTracker.h */; }; + 9A0064A92B08298E0016E3EB /* MemoryTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A0064A42B08298D0016E3EB /* MemoryTracker.h */; }; + 9A0064AA2B08298E0016E3EB /* MemoryTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A0064A42B08298D0016E3EB /* MemoryTracker.h */; }; 9A27545B263801930095D456 /* Self.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A275455263801920095D456 /* Self.h */; }; 9A27545C263801930095D456 /* Self.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A275455263801920095D456 /* Self.h */; }; 9A27545D263801930095D456 /* Self.h in Headers */ = {isa = PBXBuildFile; fileRef = 9A275455263801920095D456 /* Self.h */; }; @@ -1097,6 +1103,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 9A0064A32B08298D0016E3EB /* MemoryTracker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryTracker.cpp; sourceTree = ""; }; + 9A0064A42B08298D0016E3EB /* MemoryTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryTracker.h; sourceTree = ""; }; 9A275455263801920095D456 /* Self.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Self.h; sourceTree = ""; }; 9A275456263801920095D456 /* SelfOem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SelfOem.h; sourceTree = ""; }; 9A275457263801930095D456 /* SelfOem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SelfOem.cpp; sourceTree = ""; }; @@ -1686,6 +1694,8 @@ 9A8787D126186896000B9362 /* cpp_lib */ = { isa = PBXGroup; children = ( + 9A0064A32B08298D0016E3EB /* MemoryTracker.cpp */, + 9A0064A42B08298D0016E3EB /* MemoryTracker.h */, 9AF4D932263004E200487D15 /* XmlLiteArrayTypes.cpp */, 9AF4D930263004E200487D15 /* XmlLiteArrayTypes.h */, 9AF4D92B263004E100487D15 /* XmlLiteCompositeTypes.cpp */, @@ -2209,6 +2219,7 @@ 9A878B1926186897000B9362 /* cpu.h in Headers */, 9A878CA526186898000B9362 /* XArray.h in Headers */, 9A878C3026186898000B9362 /* MSKEK.h in Headers */, + 9A0064A82B08298E0016E3EB /* MemoryTracker.h in Headers */, 9A878A6826186897000B9362 /* AmlGenerator.h in Headers */, 9A878B4326186897000B9362 /* Volumes.h in Headers */, 9A878C7226186898000B9362 /* screen.h in Headers */, @@ -2484,6 +2495,7 @@ 9A2754B3263802230095D456 /* ConfigPlistAbstract.h in Headers */, 9A878BC226186898000B9362 /* XIcon.h in Headers */, 9A878C3126186898000B9362 /* MSKEK.h in Headers */, + 9A0064A92B08298E0016E3EB /* MemoryTracker.h in Headers */, 9A878C4C26186898000B9362 /* menu_globals.h in Headers */, 9A878A8D26186897000B9362 /* Settings.h in Headers */, 9A8789D326186897000B9362 /* VolumeTypes.h in Headers */, @@ -2691,6 +2703,7 @@ 9A2754B4263802230095D456 /* ConfigPlistAbstract.h in Headers */, 9A878BC326186898000B9362 /* XIcon.h in Headers */, 9A878C3226186898000B9362 /* MSKEK.h in Headers */, + 9A0064AA2B08298E0016E3EB /* MemoryTracker.h in Headers */, 9A878C4D26186898000B9362 /* menu_globals.h in Headers */, 9A878A8E26186897000B9362 /* Settings.h in Headers */, 9A8789D426186897000B9362 /* VolumeTypes.h in Headers */, @@ -2920,6 +2933,7 @@ 9ACD7ACB299304690095F00A /* load_icns.c in Sources */, 9A878B6D26186897000B9362 /* egemb_icons_dark.cpp in Sources */, 9A878B2226186897000B9362 /* VersionString.cpp in Sources */, + 9A0064A52B08298E0016E3EB /* MemoryTracker.cpp in Sources */, 9A878B0426186897000B9362 /* MemoryOperation.c in Sources */, 9ACBC043264484A5001EB94B /* config-test.cpp in Sources */, 9A878A2326186897000B9362 /* xml.cpp in Sources */, @@ -3037,6 +3051,7 @@ 9A878A5D26186897000B9362 /* kext_patcher.cpp in Sources */, 9A87895826186897000B9362 /* LoadOptions_test.cpp in Sources */, 9A878A1B26186897000B9362 /* plist.cpp in Sources */, + 9A0064A62B08298E0016E3EB /* MemoryTracker.cpp in Sources */, 9A8789FA26186897000B9362 /* TagFloat.cpp in Sources */, 9A87893426186897000B9362 /* globals_ctor.cpp in Sources */, 9A878ABD26186897000B9362 /* Nvram.cpp in Sources */, @@ -3203,6 +3218,7 @@ 9A878A5E26186897000B9362 /* kext_patcher.cpp in Sources */, 9A87895926186897000B9362 /* LoadOptions_test.cpp in Sources */, 9A878A1C26186897000B9362 /* plist.cpp in Sources */, + 9A0064A72B08298E0016E3EB /* MemoryTracker.cpp in Sources */, 9A8789FB26186897000B9362 /* TagFloat.cpp in Sources */, 9A87893526186897000B9362 /* globals_ctor.cpp in Sources */, 9A878ABE26186897000B9362 /* Nvram.cpp in Sources */, @@ -3404,6 +3420,7 @@ DISABLE_PRINTLIB, OC_TARGET_NOOPT, DISABLE_SECURE_BOOT, + IS_UEFI_MODULE, ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES; diff --git a/rEFIt_UEFI/Platform/BasicIO.h b/rEFIt_UEFI/Platform/BasicIO.h index 8cf0c831d..2257d5ef9 100644 --- a/rEFIt_UEFI/Platform/BasicIO.h +++ b/rEFIt_UEFI/Platform/BasicIO.h @@ -10,6 +10,7 @@ XBool ReadAllKeyStrokes(void); +void PauseForKey(); void PauseForKey(const XString8& msg); //void DebugPause(void); diff --git a/rEFIt_UEFI/Platform/DataHubCpu.cpp b/rEFIt_UEFI/Platform/DataHubCpu.cpp index db4d781bd..6301a4a71 100644 --- a/rEFIt_UEFI/Platform/DataHubCpu.cpp +++ b/rEFIt_UEFI/Platform/DataHubCpu.cpp @@ -38,6 +38,7 @@ #include // Only use angled for Platform, else, xcode project won't compile +#include "../cpp_lib/MemoryTracker.h" #include "../include/OSTypes.h" #include "Nvram.h" #include "platformdata.h" @@ -498,7 +499,9 @@ SetVariablesForOSX(LOADER_ENTRY *Entry) void AddSMCkey(SMC_KEY Key, SMC_DATA_SIZE Size, SMC_KEY_TYPE Type, SMC_DATA *Data) { - if (gAppleSmc && (gAppleSmc->Signature == NON_APPLE_SMC_SIGNATURE)) { + if (gAppleSmc && (gAppleSmc->Signature == NON_APPLE_SMC_SIGNATURE)) + { + MemoryStopRecord msr; // Do not record allocation made by SmcAddKey gAppleSmc->SmcAddKey(gAppleSmc, Key, Size, Type, 0xC0); gAppleSmc->SmcWriteValue(gAppleSmc, Key, Size, Data); } diff --git a/rEFIt_UEFI/Platform/Edid.cpp b/rEFIt_UEFI/Platform/Edid.cpp index ab2d38bf7..19a9377b2 100644 --- a/rEFIt_UEFI/Platform/Edid.cpp +++ b/rEFIt_UEFI/Platform/Edid.cpp @@ -42,14 +42,11 @@ EFI_STATUS InitializeEdidOverride () { EFI_STATUS Status; - EFI_EDID_OVERRIDE_PROTOCOL *EdidOverride; - - EdidOverride = (__typeof__(EdidOverride))AllocateCopyPool(sizeof(EFI_EDID_OVERRIDE_PROTOCOL), &gEdidOverride); Status = gBS->InstallMultipleProtocolInterfaces ( &gImageHandle, &gEfiEdidOverrideProtocolGuid, - EdidOverride, + &gEdidOverride, NULL ); if (EFI_ERROR(Status)) { diff --git a/rEFIt_UEFI/Platform/Hibernate.cpp b/rEFIt_UEFI/Platform/Hibernate.cpp index 418383c3f..bfda57b1a 100644 --- a/rEFIt_UEFI/Platform/Hibernate.cpp +++ b/rEFIt_UEFI/Platform/Hibernate.cpp @@ -452,7 +452,7 @@ GetSleepImageLocation(IN REFIT_VOLUME *Volume, REFIT_VOLUME **SleepImageVolume, } } } - PrefDict->FreeTag(); + PrefDict->ReleaseTag(); } } diff --git a/rEFIt_UEFI/Platform/KextList.cpp b/rEFIt_UEFI/Platform/KextList.cpp index 43485547a..e0db16932 100644 --- a/rEFIt_UEFI/Platform/KextList.cpp +++ b/rEFIt_UEFI/Platform/KextList.cpp @@ -59,7 +59,7 @@ XStringW GetBundleVersion(const XStringW& pathUnderSelf) if (InfoPlistPtr) { FreePool(InfoPlistPtr); } - if ( InfoPlistDict ) InfoPlistDict->FreeTag(); + if ( InfoPlistDict ) InfoPlistDict->ReleaseTag(); return CFBundleVersion; } diff --git a/rEFIt_UEFI/Platform/Nvram.cpp b/rEFIt_UEFI/Platform/Nvram.cpp index 8f6bc8b62..30fe503fb 100755 --- a/rEFIt_UEFI/Platform/Nvram.cpp +++ b/rEFIt_UEFI/Platform/Nvram.cpp @@ -6,6 +6,7 @@ */ #include // Only use angled for Platform, else, xcode project won't compile +#include "../cpp_lib/MemoryTracker.h" #include "../include/OSTypes.h" #include "Nvram.h" #include "BootOptions.h" @@ -34,7 +35,7 @@ TagDict* gNvramDict; // // always contains original efi-boot-device-data -EFI_DEVICE_PATH *gEfiBootDeviceData; +apd gEfiBootDeviceData; // if gEfiBootDeviceData starts with MemoryMapped node, then gBootCampHD = "BootCampHD" var, otherwise == NULL EFI_DEVICE_PATH *gBootCampHD; @@ -536,8 +537,12 @@ GetSmcKeys (XBool WriteToSMC) DBG("%02hhX ", *((UINT8*)Data + Index)); } DBG("\n"); - if (gAppleSmc && WriteToSMC) { - Status = gAppleSmc->SmcAddKey(gAppleSmc, KeyFromName(Name), (SMC_DATA_SIZE)DataSize, TypeFromName(Name), 0xC0); + if (gAppleSmc && WriteToSMC) + { + { + MemoryStopRecord msr; + Status = gAppleSmc->SmcAddKey(gAppleSmc, KeyFromName(Name), (SMC_DATA_SIZE)DataSize, TypeFromName(Name), 0xC0); + } if (!EFI_ERROR(Status)) { Status = gAppleSmc->SmcWriteValue(gAppleSmc, KeyFromName(Name), (SMC_DATA_SIZE)DataSize, Data); // DBG("Write to AppleSMC status=%s\n", efiStrError(Status)); @@ -552,21 +557,25 @@ GetSmcKeys (XBool WriteToSMC) NKey[3] = NumKey & 0xFF; NKey[2] = (NumKey >> 8) & 0xFF; //key, size, type, attr DBG("Registered %lld SMC keys\n", NumKey); - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("#KEY"), 4, SmcKeyTypeUint32, 0xC0); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("#KEY"), 4, (SMC_DATA *)&NKey); - } - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Adr"), 4, SmcKeyTypeUint32, 0x08); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Adr"), 4, (SMC_DATA *)&SAdr); - } - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Num"), 1, SmcKeyTypeUint8, 0x08); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Num"), 1, (SMC_DATA *)&SNum); - } - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("RMde"), 1, SmcKeyTypeChar, 0xC0); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("RMde"), 1, (SMC_DATA *)&Mode); + + { + MemoryStopRecord msr; + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("#KEY"), 4, SmcKeyTypeUint32, 0xC0); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("#KEY"), 4, (SMC_DATA *)&NKey); + } + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Adr"), 4, SmcKeyTypeUint32, 0x08); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Adr"), 4, (SMC_DATA *)&SAdr); + } + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Num"), 1, SmcKeyTypeUint8, 0x08); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Num"), 1, (SMC_DATA *)&SNum); + } + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("RMde"), 1, SmcKeyTypeChar, 0xC0); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("RMde"), 1, (SMC_DATA *)&Mode); + } } } FreePool(Name); @@ -791,14 +800,13 @@ GetEfiBootDeviceFromNvram () return EFI_SUCCESS; } - gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData))GetNvramVariable(L"efi-boot-next-data", gEfiAppleBootGuid, NULL, &Size); + gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData.get()))GetNvramVariable(L"efi-boot-next-data", gEfiAppleBootGuid, NULL, &Size); if (gEfiBootDeviceData != NULL) { DBG("Got efi-boot-next-data size=%lld\n", Size); if (IsDevicePathValid(gEfiBootDeviceData, Size)) { // DBG(" - efi-boot-next-data: %ls\n", FileDevicePathToStr (gEfiBootDeviceData)); } else { DBG(" - device path for efi-boot-next-data is invalid\n"); - FreePool(gEfiBootDeviceData); gEfiBootDeviceData = NULL; } } @@ -808,9 +816,9 @@ GetEfiBootDeviceFromNvram () EFI_STATUS Status; Status = GetVariable2 (L"aptiofixflag", gEfiAppleBootGuid, &Value, &Size2); if (EFI_ERROR(Status)) { - gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData))GetNvramVariable(L"efi-boot-device-data", gEfiAppleBootGuid, NULL, &Size); + gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData.get()))GetNvramVariable(L"efi-boot-device-data", gEfiAppleBootGuid, NULL, &Size); } else { - gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData))GetNvramVariable(L"specialbootdevice", gEfiAppleBootGuid, NULL, &Size); + gEfiBootDeviceData = (__typeof__(gEfiBootDeviceData.get()))GetNvramVariable(L"specialbootdevice", gEfiAppleBootGuid, NULL, &Size); } if (gEfiBootDeviceData != NULL) { @@ -1394,7 +1402,7 @@ FindStartupDiskVolume ( //DBG(" checking legacy entry %d. %ls\n", Index, LegacyEntry.Title); //DBG(" %ls\n", DevicePathToStr (Volume->DevicePath)); //DBG(" OSType = %d\n", Volume->OSType); - if (Volume->LegacyOS->Type == OSTYPE_WIN) { + if (Volume->LegacyOS.Type == OSTYPE_WIN) { // that's the one - legacy win partition DBG(" - found legacy entry %lld. '%ls', Volume '%ls'\n", Index, LegacyEntry.Title.s(), Volume->VolName.wc_str()); return Index; diff --git a/rEFIt_UEFI/Platform/Nvram.h b/rEFIt_UEFI/Platform/Nvram.h index 1f00cc670..098a3469f 100644 --- a/rEFIt_UEFI/Platform/Nvram.h +++ b/rEFIt_UEFI/Platform/Nvram.h @@ -15,7 +15,7 @@ extern EFI_GUID gEfiBootDeviceGuid; -extern EFI_DEVICE_PATH_PROTOCOL *gEfiBootDeviceData; +extern apd gEfiBootDeviceData; diff --git a/rEFIt_UEFI/Platform/Utils.cpp b/rEFIt_UEFI/Platform/Utils.cpp index 87a3af3a3..d584fc744 100755 --- a/rEFIt_UEFI/Platform/Utils.cpp +++ b/rEFIt_UEFI/Platform/Utils.cpp @@ -142,7 +142,7 @@ UINT32 GetCrc32(UINT8 *Buffer, UINTN Size) return x; } -#ifndef UNIT_TESTS_MACOS +//#ifndef UNIT_TESTS_MACOS extern "C" { #include @@ -157,7 +157,7 @@ void displayFreeMemory(const XString8& prefix) DebugLog(1, "--> %s: Firmware has %llu free pages (%llu in lower 4 GB)\n", prefix.c_str(), TotalMemory, LowMemory); } -#endif +//#endif XBool haveError = false; diff --git a/rEFIt_UEFI/Platform/Utils.h b/rEFIt_UEFI/Platform/Utils.h index 7b96bd429..1b49c578c 100755 --- a/rEFIt_UEFI/Platform/Utils.h +++ b/rEFIt_UEFI/Platform/Utils.h @@ -130,10 +130,10 @@ extern XBool haveError; #endif // __cplusplus -#ifndef UNIT_TESTS_MACOS +//#ifndef UNIT_TESTS_MACOS extern "C" { void displayFreeMemory(const XString8& prefix); } -#endif +//#endif #endif // _UTILS_H_ diff --git a/rEFIt_UEFI/Platform/Volume.cpp b/rEFIt_UEFI/Platform/Volume.cpp index 5615a160a..6f1486209 100644 --- a/rEFIt_UEFI/Platform/Volume.cpp +++ b/rEFIt_UEFI/Platform/Volume.cpp @@ -102,7 +102,7 @@ GetRootUUID (IN REFIT_VOLUME *Volume) } } - Dict->FreeTag(); + Dict->ReleaseTag(); FreePool(PlistBuffer); } diff --git a/rEFIt_UEFI/Platform/Volume.h b/rEFIt_UEFI/Platform/Volume.h index 25419f170..5210f8518 100644 --- a/rEFIt_UEFI/Platform/Volume.h +++ b/rEFIt_UEFI/Platform/Volume.h @@ -10,6 +10,7 @@ #include "../include/VolumeTypes.h" #include +#include "../cpp_foundation/apd.h" #include "../cpp_foundation/XString.h" #include "../libeg/libeg.h" #include "../Platform/guid.h" @@ -17,15 +18,10 @@ class LEGACY_OS { public: - UINT8 Type; - XStringW IconName; - XStringW Name; - - LEGACY_OS() : Type(0), IconName(), Name() {} - LEGACY_OS(const LEGACY_OS& other) = delete; // Can be defined if needed - const LEGACY_OS& operator = ( const LEGACY_OS & ) = delete; // Can be defined if needed - ~LEGACY_OS() {} -} ; + UINT8 Type = 0; + XStringW IconName = XStringW(); + XStringW Name = XStringW(); +}; class REFIT_VOLUME { public: @@ -36,7 +32,7 @@ public: XStringW VolName = XStringW(); // comes from EfiLibFileSystemInfo, EfiLibFileSystemVolumeLabelInfo, "EFI" if gEfiPartTypeSystemPartGuid or "Unknown HD" XStringW VolLabel = XStringW(); // comes from \\.VolumeLabel.txt, or empty. UINT8 DiskKind = 0; - LEGACY_OS *LegacyOS = 0; + LEGACY_OS LegacyOS = LEGACY_OS(); XBool Hidden = false; UINT8 BootType = 0; XBool IsAppleLegacy = false; @@ -46,7 +42,7 @@ public: EFI_BLOCK_IO *BlockIO = 0; UINT64 BlockIOOffset = 0; EFI_BLOCK_IO *WholeDiskBlockIO = 0; - EFI_DEVICE_PATH *WholeDiskDevicePath = 0; + apd WholeDiskDevicePath = NULL; EFI_HANDLE WholeDiskDeviceHandle = 0; MBR_PARTITION_INFO *MbrPartitionTable = 0; UINT32 DriveCRC32 = 0; diff --git a/rEFIt_UEFI/Platform/plist/TagArray.cpp b/rEFIt_UEFI/Platform/plist/TagArray.cpp index cb0848109..49bed8b8e 100755 --- a/rEFIt_UEFI/Platform/plist/TagArray.cpp +++ b/rEFIt_UEFI/Platform/plist/TagArray.cpp @@ -44,7 +44,9 @@ #include "TagArray.h" +#ifdef TagStruct_USE_CACHE XObjArray TagArray::tagsFree; +#endif XBool TagArray::operator == (const TagStruct& other) const { @@ -79,26 +81,28 @@ XBool TagArray::debugIsEqual(const TagStruct& other, const XString8& label) cons } -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagArray* TagArray::getEmptyTag() { TagArray* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagArray; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagArray::FreeTag() +void TagArray::ReleaseTag() { //while ( tagIdx < _dictOrArrayContent.notEmpty() ) { // _dictOrArrayContent[0].FreeTag(); @@ -107,10 +111,18 @@ void TagArray::FreeTag() // this loop is better because removing objects from the end don't do any memory copying. for (size_t tagIdx = _arrayContent.size() ; tagIdx > 0 ; ) { tagIdx--; - _arrayContent[tagIdx].FreeTag(); +#ifdef TagStruct_USE_CACHE + _arrayContent[tagIdx].ReleaseTag(); _arrayContent.RemoveWithoutFreeingAtIndex(tagIdx); +#else + _arrayContent.RemoveAtIndex(tagIdx); +#endif } +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } const TagStruct* TagArray::elementAt(size_t idx) const diff --git a/rEFIt_UEFI/Platform/plist/TagArray.h b/rEFIt_UEFI/Platform/plist/TagArray.h index d350eba9a..a6cc0f37c 100644 --- a/rEFIt_UEFI/Platform/plist/TagArray.h +++ b/rEFIt_UEFI/Platform/plist/TagArray.h @@ -12,10 +12,12 @@ class TagArray : public TagStruct { - static XObjArray tagsFree; XObjArray _arrayContent; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagArray() : _arrayContent() {} TagArray(const TagArray& other) = delete; // Can be defined if needed @@ -31,7 +33,7 @@ public: virtual XBool isArray() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Array"_XS8; } static TagArray* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagBool.cpp b/rEFIt_UEFI/Platform/plist/TagBool.cpp index a00ba2304..2375d1a00 100755 --- a/rEFIt_UEFI/Platform/plist/TagBool.cpp +++ b/rEFIt_UEFI/Platform/plist/TagBool.cpp @@ -44,31 +44,39 @@ #include "TagBool.h" +#ifdef TagStruct_USE_CACHE XObjArray TagBool::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagBool* TagBool::getEmptyTag() { TagBool* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagBool; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagBool::FreeTag() +void TagBool::ReleaseTag() { value = false; +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagBool::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagBool.h b/rEFIt_UEFI/Platform/plist/TagBool.h index 412001fc7..f4dc4843d 100644 --- a/rEFIt_UEFI/Platform/plist/TagBool.h +++ b/rEFIt_UEFI/Platform/plist/TagBool.h @@ -12,10 +12,12 @@ class TagBool : public TagStruct { - static XObjArray tagsFree; XBool value; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagBool() : value(false) {} TagBool(const TagBool& other) = delete; // Can be defined if needed @@ -30,7 +32,7 @@ public: virtual XBool isBool() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Bool"_XS8; } static TagBool* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagData.cpp b/rEFIt_UEFI/Platform/plist/TagData.cpp index 9eb3ecc4e..d80893e3b 100755 --- a/rEFIt_UEFI/Platform/plist/TagData.cpp +++ b/rEFIt_UEFI/Platform/plist/TagData.cpp @@ -46,31 +46,39 @@ #include "TagData.h" +#ifdef TagStruct_USE_CACHE XObjArray TagData::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagData* TagData::getEmptyTag() { TagData* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagData; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagData::FreeTag() +void TagData::ReleaseTag() { dataBuffer.setEmpty(); +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagData::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagData.h b/rEFIt_UEFI/Platform/plist/TagData.h index 1e97a47e0..395194012 100644 --- a/rEFIt_UEFI/Platform/plist/TagData.h +++ b/rEFIt_UEFI/Platform/plist/TagData.h @@ -13,10 +13,12 @@ class TagData : public TagStruct { - static XObjArray tagsFree; XBuffer dataBuffer; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagData() : dataBuffer() {} TagData(const TagData& other) = delete; // Can be defined if needed @@ -31,7 +33,7 @@ public: virtual XBool isData() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Data"_XS8; } static TagData* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagDate.cpp b/rEFIt_UEFI/Platform/plist/TagDate.cpp index da5690c9a..44e382550 100755 --- a/rEFIt_UEFI/Platform/plist/TagDate.cpp +++ b/rEFIt_UEFI/Platform/plist/TagDate.cpp @@ -45,31 +45,39 @@ #include "TagDate.h" +#ifdef TagStruct_USE_CACHE XObjArray TagDate::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagDate* TagDate::getEmptyTag() { TagDate* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagDate; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagDate::FreeTag() +void TagDate::ReleaseTag() { string.setEmpty(); +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagDate::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagDate.h b/rEFIt_UEFI/Platform/plist/TagDate.h index 0637804c2..b82c39757 100644 --- a/rEFIt_UEFI/Platform/plist/TagDate.h +++ b/rEFIt_UEFI/Platform/plist/TagDate.h @@ -12,10 +12,12 @@ class TagDate : public TagStruct { - static XObjArray tagsFree; XString8 string; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagDate() : string() {} TagDate(const TagDate& other) = delete; // Can be defined if needed @@ -30,7 +32,7 @@ public: virtual XBool isDict() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Dict"_XS8; } static TagDate* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagDict.cpp b/rEFIt_UEFI/Platform/plist/TagDict.cpp index 3cea17da8..734cbbbed 100755 --- a/rEFIt_UEFI/Platform/plist/TagDict.cpp +++ b/rEFIt_UEFI/Platform/plist/TagDict.cpp @@ -44,35 +44,47 @@ #include "TagDict.h" -XObjArray TagDict::tagsFree; +#ifdef TagStruct_USE_CACHE +TagsDictFreeArray TagDict::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagDict* TagDict::getEmptyTag() { TagDict* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagDict; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagDict::FreeTag() +void TagDict::ReleaseTag() { for (size_t tagIdx = _dictContent.size() ; tagIdx > 0 ; ) { tagIdx--; - _dictContent[tagIdx].FreeTag(); +#ifdef TagStruct_USE_CACHE + _dictContent[tagIdx].ReleaseTag(); _dictContent.RemoveWithoutFreeingAtIndex(tagIdx); +#else + _dictContent.RemoveAtIndex(tagIdx); +#endif } +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagDict::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagDict.h b/rEFIt_UEFI/Platform/plist/TagDict.h index 7845d957a..93c318bce 100644 --- a/rEFIt_UEFI/Platform/plist/TagDict.h +++ b/rEFIt_UEFI/Platform/plist/TagDict.h @@ -10,12 +10,22 @@ #include "plist.h" +class TagsDictFreeArray : public XObjArray +{ +public: + ~TagsDictFreeArray () { + NOP; + } +}; + class TagDict : public TagStruct { - static XObjArray tagsFree; XObjArray _dictContent; public: +#ifdef TagStruct_USE_CACHE + static TagsDictFreeArray tagsFree; +#endif TagDict() : _dictContent() {} TagDict(const TagDict& other) = delete; // Can be defined if needed @@ -31,7 +41,7 @@ public: virtual XBool isDict() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Dict"_XS8; } static TagDict* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagFloat.cpp b/rEFIt_UEFI/Platform/plist/TagFloat.cpp index e3a9ec639..25c7de5f2 100755 --- a/rEFIt_UEFI/Platform/plist/TagFloat.cpp +++ b/rEFIt_UEFI/Platform/plist/TagFloat.cpp @@ -45,32 +45,40 @@ #include "TagFloat.h" +#ifdef TagStruct_USE_CACHE XObjArray TagFloat::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagFloat* TagFloat::getEmptyTag() { TagFloat* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagFloat; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagFloat::FreeTag() +void TagFloat::ReleaseTag() { value = 0; +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagFloat::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagFloat.h b/rEFIt_UEFI/Platform/plist/TagFloat.h index 231221618..4dc98be06 100644 --- a/rEFIt_UEFI/Platform/plist/TagFloat.h +++ b/rEFIt_UEFI/Platform/plist/TagFloat.h @@ -12,10 +12,12 @@ class TagFloat : public TagStruct { - static XObjArray tagsFree; float value; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagFloat() : value(0) {} TagFloat(const TagFloat& other) = delete; // Can be defined if needed @@ -30,7 +32,7 @@ public: virtual XBool isFloat() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Float"_XS8; } static TagFloat* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagInt64.cpp b/rEFIt_UEFI/Platform/plist/TagInt64.cpp index a42b59861..8dafd0d52 100755 --- a/rEFIt_UEFI/Platform/plist/TagInt64.cpp +++ b/rEFIt_UEFI/Platform/plist/TagInt64.cpp @@ -45,32 +45,40 @@ #include "TagInt64.h" +#ifdef TagStruct_USE_CACHE XObjArray TagInt64::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagInt64* TagInt64::getEmptyTag() { TagInt64* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagInt64; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagInt64::FreeTag() +void TagInt64::ReleaseTag() { value = 0; +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagInt64::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagInt64.h b/rEFIt_UEFI/Platform/plist/TagInt64.h index de6c1625d..b6f2b4a6b 100644 --- a/rEFIt_UEFI/Platform/plist/TagInt64.h +++ b/rEFIt_UEFI/Platform/plist/TagInt64.h @@ -12,10 +12,12 @@ class TagInt64 : public TagStruct { - static XObjArray tagsFree; INT64 value; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagInt64() : value(0) {} TagInt64(const TagInt64& other) = delete; // Can be defined if needed @@ -30,7 +32,7 @@ public: virtual XBool isInt64() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Int64"_XS8; } static TagInt64* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagKey.cpp b/rEFIt_UEFI/Platform/plist/TagKey.cpp index 7de4872af..de4902699 100755 --- a/rEFIt_UEFI/Platform/plist/TagKey.cpp +++ b/rEFIt_UEFI/Platform/plist/TagKey.cpp @@ -45,32 +45,40 @@ #include "TagKey.h" +#ifdef TagStruct_USE_CACHE XObjArray TagKey::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagKey* TagKey::getEmptyTag() { TagKey* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagKey; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagKey::FreeTag() +void TagKey::ReleaseTag() { _string.setEmpty(); +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagKey::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagKey.h b/rEFIt_UEFI/Platform/plist/TagKey.h index 844fcb7bd..9834cb3d0 100644 --- a/rEFIt_UEFI/Platform/plist/TagKey.h +++ b/rEFIt_UEFI/Platform/plist/TagKey.h @@ -12,10 +12,12 @@ class TagKey : public TagStruct { - static XObjArray tagsFree; XString8 _string; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagKey() : _string() {} TagKey(const TagKey& other) = delete; // Can be defined if needed @@ -30,7 +32,7 @@ public: virtual XBool isKey() const { return true; } virtual const XString8 getTypeAsXString8() const { return "Key"_XS8; } static TagKey* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/TagString8.cpp b/rEFIt_UEFI/Platform/plist/TagString8.cpp index 69dcad8e9..c334d8b57 100755 --- a/rEFIt_UEFI/Platform/plist/TagString8.cpp +++ b/rEFIt_UEFI/Platform/plist/TagString8.cpp @@ -44,32 +44,40 @@ #include "TagString8.h" +#ifdef TagStruct_USE_CACHE XObjArray TagString::tagsFree; +#endif -//UINTN newtagcount = 0; -//UINTN tagcachehit = 0; TagString* TagString::getEmptyTag() { TagString* tag; +#ifdef TagStruct_USE_CACHE if ( tagsFree.size() > 0 ) { tag = &tagsFree[0]; tagsFree.RemoveWithoutFreeingAtIndex(0); -//tagcachehit++; -//DBG("tagcachehit=%lld\n", tagcachehit); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachehit++; + #endif return tag; } +#endif tag = new TagString; -//newtagcount += 1; -//DBG("newtagcount=%lld\n", newtagcount); + #ifdef TagStruct_COUNT_CACHEHITMISS + cachemiss++; + #endif return tag; } -void TagString::FreeTag() +void TagString::ReleaseTag() { _string.setEmpty(); +#ifdef TagStruct_USE_CACHE tagsFree.AddReference(this, true); +#else + delete this; +#endif } XBool TagString::operator == (const TagStruct& other) const diff --git a/rEFIt_UEFI/Platform/plist/TagString8.h b/rEFIt_UEFI/Platform/plist/TagString8.h index 3341e5657..b1b9fedbc 100644 --- a/rEFIt_UEFI/Platform/plist/TagString8.h +++ b/rEFIt_UEFI/Platform/plist/TagString8.h @@ -12,10 +12,12 @@ class TagString : public TagStruct { - static XObjArray tagsFree; XString8 _string; public: +#ifdef TagStruct_USE_CACHE + static XObjArray tagsFree; +#endif TagString() : _string() {} TagString(const TagString& other) = delete; // Can be defined if needed @@ -30,7 +32,7 @@ public: virtual XBool isString() const { return true; } virtual const XString8 getTypeAsXString8() const { return "String8"_XS8; } static TagString* getEmptyTag(); - virtual void FreeTag(); + virtual void ReleaseTag(); virtual void sprintf(unsigned int ident, XString8* s) const; diff --git a/rEFIt_UEFI/Platform/plist/plist.cpp b/rEFIt_UEFI/Platform/plist/plist.cpp index 69031a41c..8a4bb9ecd 100755 --- a/rEFIt_UEFI/Platform/plist/plist.cpp +++ b/rEFIt_UEFI/Platform/plist/plist.cpp @@ -29,6 +29,7 @@ */ //Slice - rewrite for UEFI with more functions like Copyright (c) 2003 Apple Computer #include // Only use angled for Platform, else, xcode project won't compile +#include "../../cpp_foundation/apd.h" #include "../b64cdecode.h" #include "plist.h" #include "../../libeg/FloatLib.h" @@ -76,6 +77,25 @@ EFI_STATUS FixDataMatchingTag( CHAR8* buffer, CONST CHAR8* tag,UINT32* lenPtr); #include "TagInt64.h" #include "TagString8.h" +#ifdef TagStruct_COUNT_CACHEHITMISS +size_t TagStruct::cachemiss = 0; +size_t TagStruct::cachehit = 0; +#endif + +void TagStruct::EmptyCache() +{ +#ifdef TagStruct_USE_CACHE + TagArray::tagsFree.setEmpty(); + TagBool::tagsFree.setEmpty(); + TagData::tagsFree.setEmpty(); + TagDate::tagsFree.setEmpty(); + TagDict::tagsFree.setEmpty(); + TagFloat::tagsFree.setEmpty(); + TagInt64::tagsFree.setEmpty(); + TagKey::tagsFree.setEmpty(); + TagString::tagsFree.setEmpty(); +#endif +} XBool TagStruct::debugIsEqual(const TagStruct& other, const XString8& label) const { @@ -140,7 +160,7 @@ EFI_STATUS ParseXML(const UINT8* buffer, TagDict** dict, size_t bufSize) UINT32 length = 0; UINT32 pos = 0; TagStruct* tag = NULL; - CHAR8* configBuffer = NULL; + apd configBuffer = NULL; size_t bufferSize = 0; UINTN i; @@ -154,7 +174,7 @@ EFI_STATUS ParseXML(const UINT8* buffer, TagDict** dict, size_t bufSize) return EFI_INVALID_PARAMETER; } - configBuffer = (__typeof__(configBuffer))malloc(bufferSize+1); + configBuffer = (CHAR8*)malloc(bufferSize+1); memset(configBuffer, 0, bufferSize+1); if(configBuffer == NULL) { return EFI_OUT_OF_RESOURCES; @@ -185,7 +205,7 @@ EFI_STATUS ParseXML(const UINT8* buffer, TagDict** dict, size_t bufSize) break; } - tag->FreeTag(); + tag->ReleaseTag(); tag = NULL; } // FreePool(configBuffer); @@ -383,7 +403,7 @@ EFI_STATUS __ParseTagList(XBool isArray, CHAR8* buffer, TagStruct** tag, UINT32 } if (EFI_ERROR(Status)) { - dictOrArrayTag->FreeTag(); + dictOrArrayTag->ReleaseTag(); return Status; } } @@ -506,7 +526,7 @@ EFI_STATUS ParseTagInteger(CHAR8* buffer, TagStruct** tag, UINT32* lenPtr) else { MsgLog("ParseTagInteger hex error (0x%hhX) in buffer %s\n", *val, buffer); // getchar(); - tmpTag->FreeTag(); + tmpTag->ReleaseTag(); return EFI_UNSUPPORTED; } } @@ -523,7 +543,7 @@ EFI_STATUS ParseTagInteger(CHAR8* buffer, TagStruct** tag, UINT32* lenPtr) if (*val < '0' || *val > '9') { MsgLog("ParseTagInteger decimal error (0x%hhX) in buffer %s\n", *val, buffer); // getchar(); - tmpTag->FreeTag(); + tmpTag->ReleaseTag(); return EFI_UNSUPPORTED; } integer = (integer * 10) + (*val++ - '0'); diff --git a/rEFIt_UEFI/Platform/plist/plist.h b/rEFIt_UEFI/Platform/plist/plist.h index d4ab0b1bd..ab5e331c9 100644 --- a/rEFIt_UEFI/Platform/plist/plist.h +++ b/rEFIt_UEFI/Platform/plist/plist.h @@ -26,6 +26,9 @@ #include "../../include/TagTypes.h" +#define TagStruct_USE_CACHE +//#define TagStruct_COUNT_CACHEHITMISS + class TagDict; class TagKey; class TagString; @@ -40,15 +43,18 @@ class TagStruct { public: +#ifdef TagStruct_COUNT_CACHEHITMISS + static size_t cachemiss; + static size_t cachehit; +#endif + static void EmptyCache(); + TagStruct() {} TagStruct(const TagStruct& other) = delete; // Can be defined if needed const TagStruct& operator = ( const TagStruct & ) = delete; // Can be defined if needed virtual ~TagStruct() { } -// static TagStruct* getEmptyTag(); -// static TagStruct* getEmptyDictTag(); -// static TagStruct* getEmptyArrayTag(); - virtual void FreeTag() = 0; + virtual void ReleaseTag() = 0; virtual XBool operator == (const TagStruct& other) const = 0; virtual XBool operator != (const TagStruct& other) const { return !(*this == other); }; diff --git a/rEFIt_UEFI/PlatformEFI/BasicIO.cpp b/rEFIt_UEFI/PlatformEFI/BasicIO.cpp index 5f9487b1c..9cb3ca05e 100644 --- a/rEFIt_UEFI/PlatformEFI/BasicIO.cpp +++ b/rEFIt_UEFI/PlatformEFI/BasicIO.cpp @@ -39,6 +39,19 @@ XBool ReadAllKeyStrokes(void) return GotKeyStrokes; } +void PauseForKey() +{ + UINTN index; + + if (ReadAllKeyStrokes()) { // remove buffered key strokes + gBS->Stall(5000000); // 5 seconds delay + ReadAllKeyStrokes(); // empty the buffer again + } + + gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &index); + ReadAllKeyStrokes(); // empty the buffer to protect the menu +} + void PauseForKey(const XString8& msg) { UINTN index; diff --git a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp index 3afbb4f19..6a7882f99 100755 --- a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp +++ b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp @@ -103,13 +103,16 @@ void construct_globals_objects(EFI_HANDLE ImageHandle) // unsigned char* cp = (unsigned char*)p; // for (size_t i=0 ; i<2467264 ; i +=16) DBG("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",cp[0]>=32 ? cp[i+0] : '.',cp[i+1]>=32 ? cp[i+1] : '.',cp[i+2]>=32 ? cp[i+2] : '.',cp[i+3]>=32 ? cp[i+3] : '.',cp[i+4]>=32 ? cp[i+4] : '.',cp[i+5]>=32 ? cp[i+5] : '.',cp[i+6]>=32 ? cp[i+6] : '.',cp[i+7]>=32 ? cp[i+7] : '.',cp[i+8]>=32 ? cp[i+8] : '.',cp[i+9]>=32 ? cp[i+9] : '.',cp[i+10]>=32 ? cp[i+10] : '.',cp[i+11]>=32 ? cp[i+11] : '.',cp[i+12]>=32 ? cp[i+12] : '.',cp[i+13]>=32 ? cp[i+13] : '.',cp[i+14]>=32 ? cp[i+14] : '.',cp[i+15]>=32 ? cp[i+15] : '.'); // egSaveFile(&self.getCloverDir(), L"dump.bin", LoadedImage->ImageBase, 3000000); -#endif - DBG("CTORS %llX(%lld), offset from file beginning %llX(%lld) size %ld\n", (UINTN)p, (UINTN)p, (UINTN)p - (UINTN)LoadedImage->ImageBase, (UINTN)p - (UINTN)LoadedImage->ImageBase, pend-p ); +#else + DBG("CTORS %llX(%lld), size %ld\n", (UINTN)p, (UINTN)p, pend-p ); +#endif + size_t ctor_idx = 0; while ( p < pend ) { - DBG("CTOR %llX(%lld)\n", (UINTN)p[0], (UINTN)p[0]); + DBG("CTOR %zu adr=%llX(%lld)\n", ctor_idx, (UINTN)p[0], (UINTN)p[0]); (*p)(); p++; + ctor_idx++; } // DBG("CTOR %X %d\n", (UINTN)__CTOR_LIST__, (UINTN)__CTOR_LIST__); // __do_init(); diff --git a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_dtor.cpp b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_dtor.cpp index 4e8c6b549..fcaabb7f9 100755 --- a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_dtor.cpp +++ b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_dtor.cpp @@ -1,4 +1,5 @@ #include // Only use angled for Platform, else, xcode project won't compile +#include "../../cpp_lib/MemoryTracker.h" #if 0 #define DBG(...) DebugLog(2, __VA_ARGS__) @@ -57,6 +58,7 @@ extern "C" int atexit(void(__cdecl *destructor_func)(void)) DBG("atexit(%p, %p, %p, %d)\n", destructor_func, objptr, dso, sizeof(atexit_func_entry_t)); #endif + MemoryStopRecord msr; if ( !atexit_func_entry_array ) { DBG("atexit : allocate\n"); diff --git a/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp b/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp index daf9a1029..1aee545b8 100755 --- a/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp +++ b/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp @@ -48,7 +48,7 @@ void operator delete ( void* ptr ) noexcept { // ++operator_delete_count1; // MemLogf(false, 0, "operator delete(%llx) %lld\n", uintptr_t(ptr), operator_delete_count1); - return FreePool(ptr); + FreePool(ptr); } #ifdef _MSC_VER @@ -59,7 +59,7 @@ void operator delete (void * ptr, UINTN count) { // ++operator_delete_count2; // MemLogf(false, 0, "operator delete(%llx, %lld) %lld\n", uintptr_t(ptr), count, operator_delete_count2); - return FreePool(ptr); + FreePool(ptr); } @@ -71,7 +71,7 @@ void operator delete[](void * ptr, UINTN count) { // ++operator_delete_count3; // MemLogf(false, 0, "operator delete[](%llx, %lld) %lld\n", uintptr_t(ptr), count, operator_delete_count3); - return FreePool(ptr); + FreePool(ptr); } diff --git a/rEFIt_UEFI/PlatformEFI/include/Platform.h b/rEFIt_UEFI/PlatformEFI/include/Platform.h index 28830f6ab..b87ef03f1 100755 --- a/rEFIt_UEFI/PlatformEFI/include/Platform.h +++ b/rEFIt_UEFI/PlatformEFI/include/Platform.h @@ -21,6 +21,7 @@ Headers collection for procedures #include "../cpp_foundation/XObjArray.h" #include "../include/remove_ref.h" #include "../cpp_lib/undefinable.h" +#include "../cpp_foundation/apd.h" #endif #include "../include/OneLinerMacros.h" diff --git a/rEFIt_UEFI/PlatformEFI/posix/limits.h b/rEFIt_UEFI/PlatformEFI/posix/limits.h index 31fe61ee7..d6181aeaf 100644 --- a/rEFIt_UEFI/PlatformEFI/posix/limits.h +++ b/rEFIt_UEFI/PlatformEFI/posix/limits.h @@ -1,2 +1,37 @@ //#define UINT_MAX #define SIZE_T_MAX MAX_UINTN + +#define INT8_MIN (-128) +#define INT16_MIN (-32768) +#define INT32_MIN (-2147483647 - 1) +#define INT64_MIN (-9223372036854775807LL - 1) + +#define INT8_MAX 127 +#define INT16_MAX 32767 +#define INT32_MAX 2147483647 +#define INT64_MAX 9223372036854775807LL + +#define UINT8_MAX 0xff /* 255U */ +#define UINT16_MAX 0xffff /* 65535U */ +#define UINT32_MAX 0xffffffff /* 4294967295U */ +#define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */ + +#define CHAR_MIN (-128) +#define SCHAR_MIN (-128) +#define SHRT_MIN (-32768) +#define INT_MIN INT32_MIN +#define LONG_MIN INT64_MIN +#define LLONG_MIN INT64_MIN + +#define CHAR_MAX 127 +#define SCHAR_MAX 127 +#define SHRT_MAX 32767 +#define INT_MAX INT32_MAX +#define LONG_MAX INT64_MAX +#define LLONG_MAX INT64_MAX + +#define UCHAR_MAX 255 +#define USHRT_MAX 65535 +#define UINT_MAX UINT32_MAX +#define ULONG_MAX UINT64_MAX +#define ULLONG_MAX UINT64_MAX diff --git a/rEFIt_UEFI/PlatformEFI/posix/stdint.h b/rEFIt_UEFI/PlatformEFI/posix/stdint.h index 90be436a3..2748c0a42 100644 --- a/rEFIt_UEFI/PlatformEFI/posix/stdint.h +++ b/rEFIt_UEFI/PlatformEFI/posix/stdint.h @@ -7,46 +7,12 @@ static_assert(sizeof(char) == 1, "sizeof(char) != 1"); static_assert(sizeof(short) == 2, "sizeof(short) != 2"); static_assert(sizeof(int) == 4, "sizeof(int) != 4"); -//static_assert(sizeof(long) == 8, "sizeof(long) != 8"); // Jief : I think this break on Windows. Conditional compilation rquired to restore Windows compatibility +static_assert(sizeof(long) == 8, "sizeof(long) != 8"); // Jief : I think this break on Windows. Conditional compilation required to restore Windows compatibility //so why EDK2 never used "long". It uses INT32. static_assert(sizeof(long long) == 8, "sizeof(long long) != 8"); static_assert(true, "true"); #endif -#define INT8_MIN (-128) -#define INT16_MIN (-32768) -#define INT32_MIN (-2147483647 - 1) -#define INT64_MIN (-9223372036854775807LL - 1) - -#define INT8_MAX 127 -#define INT16_MAX 32767 -#define INT32_MAX 2147483647 -#define INT64_MAX 9223372036854775807LL - -#define UINT8_MAX 0xff /* 255U */ -#define UINT16_MAX 0xffff /* 65535U */ -#define UINT32_MAX 0xffffffff /* 4294967295U */ -#define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */ - -#define CHAR_MIN (-128) -#define SCHAR_MIN (-128) -#define SHRT_MIN (-32768) -#define INT_MIN INT32_MIN -#define LONG_MIN INT64_MIN -#define LLONG_MIN INT64_MIN - -#define CHAR_MAX 127 -#define SCHAR_MAX 127 -#define SHRT_MAX 32767 -#define INT_MAX INT32_MAX -#define LONG_MAX INT64_MAX -#define LLONG_MAX INT64_MAX - -#define UCHAR_MAX 255 -#define USHRT_MAX 65535 -#define UINT_MAX UINT32_MAX -#define ULONG_MAX UINT64_MAX -#define ULLONG_MAX UINT64_MAX typedef UINT8 uint8_t; typedef UINT16 uint16_t; diff --git a/rEFIt_UEFI/Settings/ConfigManager.cpp b/rEFIt_UEFI/Settings/ConfigManager.cpp index 77aca3408..687437025 100644 --- a/rEFIt_UEFI/Settings/ConfigManager.cpp +++ b/rEFIt_UEFI/Settings/ConfigManager.cpp @@ -5,6 +5,8 @@ * Author: jief */ +#include +#include "../cpp_lib/MemoryTracker.h" #include "ConfigManager.h" #include "../Settings/SelfOem.h" #include "../refit/lib.h" @@ -484,7 +486,8 @@ EFI_STATUS LoadPlist(const XStringW& ConfName, C* plist) { EFI_STATUS Status = EFI_NOT_FOUND; UINTN Size = 0; - UINT8* ConfigPtr = NULL; + UINT8* ConfigPtr = NULL; // not needed. but avoids warning + apd apdConfigPtr; // XStringW ConfigPlistPath; // XStringW ConfigOemPath; @@ -503,6 +506,7 @@ EFI_STATUS LoadPlist(const XStringW& ConfName, C* plist) configPlistPath = SWPrintf("%ls\\%ls.plist", selfOem.getOemFullPath().wc_str(), ConfName.wc_str()); if (FileExists (&selfOem.getOemDir(), configFilename)) { Status = egLoadFile(&selfOem.getOemDir(), configFilename.wc_str(), &ConfigPtr, &Size); + apdConfigPtr = ConfigPtr; // This will automatically destruct ConfigPtr when the method exit. if (EFI_ERROR(Status)) { DBG("Cannot find %ls at path (%s): '%ls', trying '%ls'\n", configFilename.wc_str(), efiStrError(Status), selfOem.getOemFullPath().wc_str(), self.getCloverDirFullPath().wc_str()); }else{ @@ -514,6 +518,7 @@ EFI_STATUS LoadPlist(const XStringW& ConfName, C* plist) configPlistPath = SWPrintf("%ls\\%ls.plist", self.getCloverDirFullPath().wc_str(), ConfName.wc_str()); if ( FileExists(&self.getCloverDir(), configFilename.wc_str())) { Status = egLoadFile(&self.getCloverDir(), configFilename.wc_str(), &ConfigPtr, &Size); + apdConfigPtr = ConfigPtr; // This will automatically destruct ConfigPtr when the method exit. } if (EFI_ERROR(Status)) { DBG("Cannot find %ls at path '%ls' : %s\n", configFilename.wc_str(), self.getCloverDirFullPath().wc_str(), efiStrError(Status)); diff --git a/rEFIt_UEFI/Settings/ConfigPlist/SMBIOSPlist.h b/rEFIt_UEFI/Settings/ConfigPlist/SMBIOSPlist.h index 4beaf8e92..f5cf1b3de 100755 --- a/rEFIt_UEFI/Settings/ConfigPlist/SMBIOSPlist.h +++ b/rEFIt_UEFI/Settings/ConfigPlist/SMBIOSPlist.h @@ -43,9 +43,9 @@ #include "ConfigPlistAbstract.h" -extern XStringWArray ThemeNameArray; -extern XStringWArray ConfigsList; -extern XStringWArray DsdtsList; +//extern XStringWArray ThemeNameArray; +//extern XStringWArray ConfigsList; +//extern XStringWArray DsdtsList; extern XStringWArray SmbiosList; diff --git a/rEFIt_UEFI/Settings/Self.h b/rEFIt_UEFI/Settings/Self.h index 8ec542852..e4592d1f7 100644 --- a/rEFIt_UEFI/Settings/Self.h +++ b/rEFIt_UEFI/Settings/Self.h @@ -9,6 +9,7 @@ #define PLATFORM_SELF_H_ #include "../cpp_foundation/XString.h" +#include "../cpp_foundation/apd.h" extern "C" { #include #include @@ -26,8 +27,8 @@ public: protected: EFI_HANDLE m_SelfImageHandle {}; // this efi. - EFI_LOADED_IMAGE* m_SelfLoadedImage {}; // this efi. - EFI_DEVICE_PATH* m_SelfDevicePath {}; // path to device containing this efi. + EFI_LOADED_IMAGE* m_SelfLoadedImage = NULL; // this efi. + apd m_SelfDevicePath = NULL; // path to device containing this efi. // XStringW m_SelfDevicePathAsXStringW; // path to device containing this efi. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* m_SelfSimpleVolume {}; // Volume containing this efi. diff --git a/rEFIt_UEFI/cpp_foundation/XToolsCommon.h b/rEFIt_UEFI/cpp_foundation/XToolsCommon.h index fd9c8f07d..7429e8e0b 100644 --- a/rEFIt_UEFI/cpp_foundation/XToolsCommon.h +++ b/rEFIt_UEFI/cpp_foundation/XToolsCommon.h @@ -12,7 +12,8 @@ #ifdef __cplusplus #include // size_t -#include // CHAR_MIN etc. +#include // CHAR_MIN etc. +#include // UINT16_MAX etc. @@ -22,14 +23,19 @@ template< class T > struct _xtools__remove_ref { typedef T type; template< class T > struct _xtools__remove_const { typedef T type; }; template< class T > struct _xtools__remove_const { typedef T type; }; template< class T > struct _xtools__remove_const { typedef T& type; }; -template< class T > struct _xtools__remove_const { typedef T* type; }; +template< class T > struct _xtools__remove_const { typedef T* type; }; // this match type with [] and [size] + +template< class T > struct _xtools__remove_ptr { typedef T type; }; +template< class T > struct _xtools__remove_ptr { typedef T type; }; +template< class T > struct _xtools__remove_ptr { typedef T type; }; +template struct _xtools__remove_ptr { typedef T type; }; template< class T > struct _xtools__remove_const_ptr { typedef T type; }; template< class T > struct _xtools__remove_const_ptr { typedef T type; }; template< class T > struct _xtools__remove_const_ptr { typedef T type; }; template< class T > struct _xtools__remove_const_ptr { typedef T type; }; -template< class T, int n > struct _xtools__remove_const_ptr { typedef T type; }; -template< class T, int n > struct _xtools__remove_const_ptr { typedef T type; }; +template< class T, size_t n > struct _xtools__remove_const_ptr { typedef T type; }; +template< class T, size_t n > struct _xtools__remove_const_ptr { typedef T type; }; @@ -217,6 +223,7 @@ struct _xtools__has_type_member> : _xtool #define remove_const(x) typename _xtools__remove_const::type +#define remove_ptr(x) typename _xtools__remove_ptr::type #define unsigned_type(x) typename _xtools__make_unsigned::type #define remove_ref(x) typename _xtools__remove_ref::type #define remove_const_ptr(x) typename _xtools__remove_const_ptr::type diff --git a/rEFIt_UEFI/cpp_foundation/apd.h b/rEFIt_UEFI/cpp_foundation/apd.h new file mode 100644 index 000000000..09a770873 --- /dev/null +++ b/rEFIt_UEFI/cpp_foundation/apd.h @@ -0,0 +1,74 @@ +/* + * apd.h + * + * Created on: Nov 11, 2023 + * Author: jief + */ + +#ifndef CPP_FOUNDATION_APD_H_ +#define CPP_FOUNDATION_APD_H_ + + +template +class apd +{ +protected: + T t = 0; +public: + using type = T; + + apd() : t(NULL) {}; + apd(T _t) : t(_t) {}; + apd(const apd& other) { + panic("You must NOT copy an apd object. Use shared_ptr instead\n"); + } + apd& operator=(const apd& other) { + panic("You must NOT copy an apd object. Use shared_ptr instead\n"); + return *this; + } + + apd& operator=(T _t) { + delete t; + t = _t; + return *this; + } + + constexpr bool isNull() const { return t == NULL; } + constexpr bool notNull() const { return !isNull(); } + + remove_ptr(T)& operator * () { + return *t; + } + T operator -> () { + return t; + } + T* operator & () + { + return &t; + } + + T& get() { + return t; + } + const T& get() const { + return t; + } + + operator T () { + return t; + } + operator const T () const { + return t; + } + + /* [] */ + template + remove_ptr(T)& operator [](IntegralType i) const { return t[i]; } + + + ~apd() { + delete t; + } +}; + +#endif /* CPP_FOUNDATION_APD_H_ */ diff --git a/rEFIt_UEFI/cpp_lib/MemoryTracker.cpp b/rEFIt_UEFI/cpp_lib/MemoryTracker.cpp new file mode 100644 index 000000000..0829f1cbd --- /dev/null +++ b/rEFIt_UEFI/cpp_lib/MemoryTracker.cpp @@ -0,0 +1,391 @@ +/* + * MemoryTracker.cpp + * + * Created on: Nov 9, 2023 + * Author: jief + */ + +#include + +#include "MemoryTracker.h" +#include "../cpp_foundation/XString.h" + +#ifndef DEBUG_ALL +#define DEBUG_MT 1 +#else +#define DEBUG_MT DEBUG_ALL +#endif + +#if DEBUG_MT == 0 +#define DBG(...) +#else +#define DBG(...) DebugLog(DEBUG_MT, __VA_ARGS__) +#endif + +#ifdef MEMORY_TRACKER_ENABLED + + +uint64_t MT_alloc_count = 0; +bool MT_recording = false; + +MTArray allocatedPtrArray; +MTArray allocatedPtrInfoArray; + +void MT_import(const void* p) +{ +// printf("MT_import ptr %llx\n", uintptr_t(p)); + MT_recording = false; + auto count = MT_alloc_count; + allocatedPtrArray.Add(uintptr_t(p)); // this can allocate memory, so don't record that. + allocatedPtrInfoArray.Add(count); // this can allocate memory, so don't record that. + MT_recording = true; +} +// +//void MT_delete(const void* p) +//{ +// if ( p ) { +// uintptr_t ref2 = uintptr_t(p); +// auto idx = allocatedPtrArray.indexOf(ref2); +// if ( idx == MAX_XSIZE ) { +//// printf("MT_delete unknown ptr %llx\n", uintptr_t(p)); +// }else{ +////if ( allocatedPtrInfoArray[idx]->alloc_idx == 15579 ) printf("MT_delete(0x%llx) - %lld\n", uintptr_t(p), allocatedPtrInfoArray[idx]->alloc_idx); +// allocatedPtrArray.RemoveAtIndex(idx); +// allocatedPtrInfoArray.RemoveAtIndex(idx); +// } +// } +//} + + +extern "C" { + +void* real_malloc(size_t size); +void* real_type_malloc(EFI_MEMORY_TYPE MemoryType, size_t size); +void* my_malloc(size_t size); +void real_free(void*); +void my_free(void*); + +//#define MAGIC_BEGINNING 0xDEADBEEF32659821 +//#define MAGIC_END 0x1245788956231973 + +#define MAGIC_BEGINNING 0xDEADBEEFBBBBBBBB +#define MAGIC_END 0xEEEEEEEEDEADBEEF + +uint64_t MT_count_to_break_into = 0; + +#if defined(IS_UEFI_MODULE) + +VOID * +InternalAllocatePool ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN AllocationSize + ) +#else + +void* my_malloc(size_t AllocationSize) + +#endif +{ + VOID *Memory = NULL; + +#ifdef JIEF_DEBUG +if ( MT_alloc_count == 4028 || MT_alloc_count == MT_count_to_break_into ) { +NOP; +} +#endif + if ( MT_recording ) + { + Memory = real_type_malloc(MemoryType, AllocationSize+24); + if ( !Memory ) return NULL; + + *(uint64_t*)Memory = AllocationSize; + + // void* p = ((uint64_t*)Memory + 1) ; + + *((uint64_t*)Memory + 1) = MAGIC_BEGINNING; + *(uint64_t*)( ((uint8_t*)Memory) + 16 + AllocationSize ) = MAGIC_END; + if ( MT_recording ) { + MT_import( ((uint8_t*)Memory) + 16 ); + ++MT_alloc_count; // do this only if !MT_recording because MT_import is doing an allocation. + } + return ((uint8_t*)Memory) + 16; + + }else{ + Memory = real_type_malloc(MemoryType, AllocationSize); + return Memory; + } +} + + +#if defined(IS_UEFI_MODULE) + +void (EFIAPI FreePool)(IN void *MemoryPlus16) + +#else + +void my_free(IN void *MemoryPlus16) + +#endif +{ + if ( !MemoryPlus16 ) return; + + uintptr_t ref2 = uintptr_t(MemoryPlus16); + auto idx = allocatedPtrArray.indexOf(ref2); + if ( idx == MAX_XSIZE ) { + //DBG("Delete non recorded ptr %llx\n", uintptr_t(MemoryPlus16)); + real_free(MemoryPlus16); + return; + } + + uint8_t* Memory = ((uint8_t*)MemoryPlus16) - 16; + uint64_t AllocationSize = *(uint64_t*)Memory; + + if ( *((uint64_t*)Memory + 1) != MAGIC_BEGINNING ) { + DBG("Buffer underrun for ptr %llx count %lld\n", uintptr_t(MemoryPlus16), allocatedPtrInfoArray[idx]); + } + if ( *(uint64_t*)( ((uint8_t*)Memory) + 16 + AllocationSize ) != MAGIC_END ) { + DBG("Buffer overrun for ptr %llx count %lld\n", uintptr_t(MemoryPlus16), allocatedPtrInfoArray[idx]); + } + allocatedPtrArray.RemoveAtIndex(idx); + allocatedPtrInfoArray.RemoveAtIndex(idx); + real_free( Memory ); +} + +} // extern "C" + + +void MemoryTrackerCheck() +{ + size_t nb_ptr = allocatedPtrArray.length(); + DBG("-- %zu pointers :\n", nb_ptr); + for( size_t idx=0 ; idx < nb_ptr ; ++idx ) + { + uint8_t* Memory = ((uint8_t*)allocatedPtrArray[idx]) - 16; + uint64_t AllocationSize = *(uint64_t*)Memory; + + if ( *((uint64_t*)Memory + 1) != MAGIC_BEGINNING ) { + DBG("Buffer underrun for ptr %llx count %lld\n", uintptr_t(Memory + 16), allocatedPtrInfoArray[idx]); + } + if ( *(uint64_t*)( ((uint8_t*)Memory) + 16 + AllocationSize ) != MAGIC_END ) { + DBG("Buffer overrun for ptr %llx count %lld\n", uintptr_t(Memory + 16), allocatedPtrInfoArray[idx]); + } + } +} + +void MemoryTrackerInit() +{ + MT_alloc_count = 0; + MT_recording = false; +} + + +/* + * In fact, the hook is already in place. We just have to activate it now. + */ +void MemoryTrackerInstallHook() +{ + MT_recording = true; +} + +void MT_outputDanglingPtr() +{ + size_t nb_ptr = allocatedPtrArray.length(); + DBG("-- %zu lost pointer :\n", nb_ptr); + for( size_t idx=0 ; idx < nb_ptr ; ++idx ) { + DBG(" Dangling ptr %llx count=%lld\n", allocatedPtrArray[idx], allocatedPtrInfoArray[idx]); + } +} +uint64_t MT_getAllocCount() { return MT_alloc_count; }; +uint64_t MT_getDanglingPtrCount() { return allocatedPtrArray.length(); }; + + + +MemoryStopRecord::MemoryStopRecord() : recording(MT_recording) { MT_recording = false; }; +MemoryStopRecord::~MemoryStopRecord() { MT_recording = recording; }; + + + + + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// +// MTArray +// +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +template +MTArray::MTArray() : m_data(0), m_len(0), m_allocatedSize(4096) +{ + m_data = (TYPE*)real_malloc(4096 * sizeof(TYPE)); + if ( !m_data ) { +#ifdef JIEF_DEBUG + panic("MTArray:: MTArray() : OldAllocatePool(%zu) returned NULL. System halted\n", 4096 * sizeof(TYPE)); +#endif + } +} + + +template +size_t MTArray::indexOf(TYPE& e) const +{ + size_t i; + + for ( i=0 ; i +void MTArray::CheckSize(size_t nNewSize, size_t nGrowBy) +{ +//MTArray_DBG("CheckSize: m_len=%d, m_size=%d, nGrowBy=%d, nNewSize=%d\n", m_len, m_size, nGrowBy, nNewSize); + if ( nNewSize > m_allocatedSize ) { + nNewSize += nGrowBy; + TYPE* new_data; + new_data = (TYPE*)real_malloc(nNewSize * sizeof(TYPE)); + if ( !new_data ) { +#ifdef JIEF_DEBUG + panic("MTArray::CheckSize(nNewSize=%zu, nGrowBy=%zu) : OldAllocatePool(%zu, %lu, %" PRIuPTR ") returned NULL. System halted\n", nNewSize, nGrowBy, m_allocatedSize, nNewSize*sizeof(TYPE), (uintptr_t)m_data); +#endif + } + memcpy(new_data, m_data, m_allocatedSize * sizeof(TYPE)); + real_free(m_data); + m_data = new_data; +// memset(&_Data[_Size], 0, (nNewSize-_Size) * sizeof(TYPE)); // Could help for debugging, but zeroing in not needed. + m_allocatedSize = nNewSize; + } +} + +/* CheckSize() */ +template +void MTArray::CheckSize(size_t nNewSize) +{ + CheckSize(nNewSize, 4096); +} + +/* Add(TYPE, size_t) */ +template +size_t MTArray::Add(const TYPE newElement, size_t count) +{ +// MTArray_DBG("size_t MTArray::Add(const TYPE newElement, size_t count) -> Enter. count=%d _Len=%d _Size=%d\n", count, m_len, m_size); + size_t i; + + CheckSize(m_len+count); + for ( i=0 ; i +void MTArray::RemoveAtIndex(size_t nIndex) +{ + if ( nIndex < m_len ) { + if ( nIndex nIndex > m_len\n"); + #endif +} + + +/* FreeAndRemoveAtIndex(size_t) */ +template +void MTArray::FreeAndRemoveAtIndex(size_t nIndex) +{ + if ( nIndex < m_len ) { +#ifdef UNIT_TESTS_MACOS // won't be needed soon as I'll improve the EFI mock + free(m_data[nIndex]); +#else + OldFreePool(m_data[nIndex]); +#endif + if ( nIndex nIndex > m_len\n"); + #endif +} + + +#undef malloc +#undef free + + +void* real_type_malloc(EFI_MEMORY_TYPE MemoryType, size_t size) +{ + #if defined(IS_UEFI_MODULE) + void* Memory; + EFI_STATUS Status = gBS->AllocatePool(MemoryType, size, &Memory); // AllocatePool in MemoryAllocationLib uses EfiBootServicesData, not EfiConventionalMemory + if (EFI_ERROR(Status)) return NULL; + return Memory; + #else + return malloc(AllocationSize); + #endif +} +void* real_malloc(size_t size) { return real_type_malloc(EfiBootServicesData, size); } + +void real_free(void* p) +{ + #if defined(IS_UEFI_MODULE) + EFI_STATUS Status = gBS->FreePool(p); + if (EFI_ERROR(Status)) { + // What to do ? + } + #else + free(p); + #endif +} + + + +#if !defined(IS_UEFI_MODULE) + +extern "C" { + +#if !defined(IS_UEFI_MODULE) +void* malloc(size_t size); +void free(void* p); +#endif + +} //extern C + +#endif // !defined(IS_UEFI_MODULE) + + +#else // MEMORY_TRACKER_ENABLED + +void MemoryTrackerInit() {}; +void MemoryTrackerInstallHook() {}; +void MT_outputDanglingPtr() {}; +uint64_t MT_getAllocCount() { return 0; }; +uint64_t MT_getDanglingPtrCount() { return 0; }; +MemoryStopRecord::MemoryStopRecord() {}; +MemoryStopRecord::~MemoryStopRecord() {}; +void MemoryTrackerCheck() {}; + + +#undef malloc +#undef free + +void* my_malloc(size_t size) +{ + return malloc(size); +} + +void my_free(void* p) +{ + free(p); +} + + +#endif// MEMORY_TRACKER_ENABLED diff --git a/rEFIt_UEFI/cpp_lib/MemoryTracker.h b/rEFIt_UEFI/cpp_lib/MemoryTracker.h new file mode 100644 index 000000000..0f4308793 --- /dev/null +++ b/rEFIt_UEFI/cpp_lib/MemoryTracker.h @@ -0,0 +1,98 @@ +/* + * MemoryTracker.h + * + * Created on: Nov 9, 2023 + * Author: jief + */ + +#ifndef CPP_LIB_MEMORYTRACKER_H_ +#define CPP_LIB_MEMORYTRACKER_H_ + +#ifdef JIEF_DEBUG +#define MEMORY_TRACKER_ENABLED +#endif + + + +extern "C" { +void MemoryTrackerInit(); +void MemoryTrackerInstallHook(); +} +void MT_outputDanglingPtr(); +uint64_t MT_getAllocCount(); +uint64_t MT_getDanglingPtrCount(); +void MemoryTrackerCheck(); + +class MemoryStopRecord +{ +protected: + bool recording; +public: + MemoryStopRecord(); + ~MemoryStopRecord(); +}; + + + +#ifdef MEMORY_TRACKER_ENABLED +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// +// MTArray +// +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + + +template +class MTArray +{ + protected: + TYPE* m_data; + size_t m_len; + size_t m_allocatedSize; + + public: + + MTArray(); + + size_t length() const { return m_len; } + +////-------------------------------------------------- + + TYPE &operator[](size_t index) + { + #ifdef JIEF_DEBUG + if ( index < 0 ) { + panic("MTArray::ElementAt(int) -> Operator [] : index < 0"); + } + if ( (unsigned int)index >= m_len ) { // cast safe, index > 0 + panic("MTArray::ElementAt(int) -> Operator [] : index > m_len"); + } + #endif + return m_data[index]; + } + + void CheckSize(size_t nNewSize); + void CheckSize(size_t nNewSize, size_t nGrowBy); + + size_t Add(const TYPE newElement, size_t count = 1); + void RemoveAtIndex(size_t nIndex); + void FreeAndRemoveAtIndex(size_t nIndex); + size_t indexOf(TYPE& e) const; +}; + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// +// +// +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +extern MTArray allocatedPtrArray; +extern MTArray allocatedPtrInfoArray; + + +#endif + + + +#endif /* CPP_LIB_MEMORYTRACKER_H_ */ diff --git a/rEFIt_UEFI/cpp_lib/XmlLiteParser.h b/rEFIt_UEFI/cpp_lib/XmlLiteParser.h index 8f01d34d5..097bde4a0 100755 --- a/rEFIt_UEFI/cpp_lib/XmlLiteParser.h +++ b/rEFIt_UEFI/cpp_lib/XmlLiteParser.h @@ -8,6 +8,7 @@ #ifndef __XML_LITE_H__ #define __XML_LITE_H__ +#include "../cpp_foundation/apd.h" #include "../cpp_foundation/XBool.h" #include "../cpp_foundation/XStringArray.h" #include "../cpp_lib/XmlLiteSimpleTypes.h" @@ -73,7 +74,7 @@ class XmlLiteParser { friend class XmlParserPosition; - char* p_start = NULL; + apd p_start = NULL; char* p_end = NULL; XmlParserPosition currentPos = XmlParserPosition(); XObjArray XmlParserMessageArray = XObjArray(); diff --git a/rEFIt_UEFI/cpp_lib/undefinable.h b/rEFIt_UEFI/cpp_lib/undefinable.h index be0025e1a..070cf5c97 100644 --- a/rEFIt_UEFI/cpp_lib/undefinable.h +++ b/rEFIt_UEFI/cpp_lib/undefinable.h @@ -8,6 +8,8 @@ #ifndef CPP_LIB_DEF_TYPES_H_ #define CPP_LIB_DEF_TYPES_H_ +#include "../cpp_foundation/XBool.h" + template class undefinable { diff --git a/rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.cpp b/rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.cpp new file mode 100644 index 000000000..56ea2038b --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.cpp @@ -0,0 +1,132 @@ +#include // Only use angled for Platform, else, xcode project won't compile +#include "../cpp_lib/MemoryTracker.h" +#include "../cpp_foundation/XStringArray.h" + + +static int breakpoint(int v) +{ + return v; +} + +// +//class TestAlloc { +//public: +//// MemoryAllocation4Object objma; +// void* ptr1 = 0; +// void* ptr2 = 0; +// +// TestAlloc() {}; +// ~TestAlloc() { +//// objma.free(ptr1, "ptr1"_XS8); +// MT_delete(ptr1, ""_XS8); +// }; +// +// void* testAlloc2() +// { +// printf("--TestAlloc.testAlloc2 - begin\n"); +//// MemoryAllocation ma(__FILE_NAME__, __PRETTY_FUNCTION__, __LINE__); +// ptr1 = MT_alloc(10, "ptr1 (supposed to be a memory leak)"_XS8); (void)ptr1; +//// objma.takeOwnership(ptr1, "ptr1"_XS8); +// void* ptrl1 = MT_alloc(10, "ptrl1"_XS8); (void)ptrl1; +// MT_delete(ptrl1, "ptrl1"_XS8); +// void* ptrl2 = MT_alloc(10, "ptrl2 bis"_XS8); +// printf("--TestAlloc.testAlloc2 - end\n"); +//// MT_export(q2); +//// return ma.return_(ptrl2); +// return ptrl2; +// } +//}; +// +//void* testAlloc2() +//{ +// printf("--testAlloc2 - begin\n"); +// +// MemoryAllocation ma(__FILE__, __PRETTY_FUNCTION__, __LINE__); +// void* p2 = MT_alloc(10, "p2 (supposed to be a memory leak)"_XS8); (void)p2; +// void* q2 = MT_alloc(10, "q2"_XS8); (void)q2; +// MT_delete(q2, "q2"_XS8); +// q2 = MT_alloc(10, "q2 bis"_XS8); +// +// +// printf("--testAlloc2 - end\n"); +// return ma.return_(q2); +//} +// +//void testAlloc1() +//{ +// printf("--testAlloc1 - begin\n"); +// MemoryAllocation ma(__FILE__, __PRETTY_FUNCTION__, __LINE__); +// void* p = MT_alloc(10, "p (supposed to be a memory leak)"_XS8); (void)p; +// void* q = MT_alloc(10, "q"_XS8); (void)q; +// void* t = testAlloc2(); (void)t; +// MT_delete(q, "q"_XS8); +// q = MT_alloc(10, "q bis (supposed to be a memory leak)"_XS8); +// printf("--testAlloc1 - end\n"); +//} +// +// +//TestAlloc g_ta; +// + +void f_p1void(void* p) +{ + void* q = p; + (void)q; +} + +int MemoryTracker_tests() +{ +#ifdef MEMORY_TRACKER_ENABLED + void* p; (void)p; + +// int t1_v = 3; +//// apd apdTest1 = &t1_v; // this would crash because this ptr cannot be freed +// apd apdTest1 = new int(t1_v); // this is ok +// int* q = &(*apdTest1); +//// int* qq = *apdTest1; +// int* const* r = &apdTest1; +// **r = 7; +// +// void* u = apdTest1; +// auto uuu = &apdTest1.get(); +// +// f_p1void(apdTest1); +// +// auto apdTest1_get = apdTest1.get(); +// auto apdTest1_adr_of_get = &apdTest1.get(); +// auto apdTest1_getAdrOfPtr = &apdTest1; + + + MemoryTrackerInstallHook(); + + { + new char(); + char* pp = (char*)malloc(2); + *(pp-1) = 0; + free(pp); + } + { + new char(); + char* pp = (char*)malloc(2); + *(pp+3) = 0; + free(pp); + } + MT_outputDanglingPtr(); + if ( MT_getDanglingPtrCount() != 2 ) return breakpoint(1); + +// testAlloc1(); +// printf("------------------\n"); +// { +// TestAlloc ta; +// p = ta.testAlloc2(); +// MT_delete(p, "return ta.testAlloc2()"_XS8); +// } +// printf("------------------\n"); +// p = g_ta.testAlloc2(); +// MT_delete(p, "return g_ta.testAlloc2()"_XS8); +// MT_outputDanglingPtr(); +// printf("------------------\n"); +#endif + return 0; +} + diff --git a/rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.h b/rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.h new file mode 100644 index 000000000..95fc17b6c --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/MemoryTracker_test.h @@ -0,0 +1,3 @@ + + +int MemoryTracker_tests(); diff --git a/rEFIt_UEFI/cpp_unit_test/all_tests.cpp b/rEFIt_UEFI/cpp_unit_test/all_tests.cpp index 5218ae8ce..902f97195 100755 --- a/rEFIt_UEFI/cpp_unit_test/all_tests.cpp +++ b/rEFIt_UEFI/cpp_unit_test/all_tests.cpp @@ -24,6 +24,7 @@ #include "config-test.h" #include "XToolsCommon_test.h" #include "guid_tests.h" +#include "MemoryTracker_test.h" #if defined(JIEF_DEBUG) && defined(CLOVER_BUILD) #include "printlib-test.h" @@ -45,37 +46,16 @@ bool all_tests() int ret; - ret = XString_tests(); + ret = ParseXML_tests(); if ( ret != 0 ) { - printf("XString_tests() failed at test %d\n", ret); + printf("ParseXML_tests() failed at test %d\n", ret); all_ok = false; } - ret = XStringArray_tests(); + ret = MemoryTracker_tests(); if ( ret != 0 ) { - printf("XStringArray_tests() failed at test %d\n", ret); + printf("MemoryTracker_tests() failed at test %d\n", ret); all_ok = false; } - ret = XToolsCommon_tests(); - if ( ret != 0 ) { - printf("XToolsCommon_tests() failed at test %d\n", ret); - all_ok = false; - } -ret = xml_lite_tests(); -if ( ret != 0 ) { - printf("xml_lite_tests() failed at test %d\n", ret); - all_ok = false; -} -ret = strcasecmp_tests(); -if ( ret != 0 ) { - printf("strncmp_tests() failed at test %d\n", ret); - all_ok = false; -} -// ret = config_plist_tests(); -// if ( ret != 0 ) { -// printf("config_plist_tests() failed at test %d\n", ret); -// all_ok = false; -// } -// // ret = XUINTN_tests(); // if ( ret != 0 ) { // printf("XUINTN_tests() failed at test %d\n", ret); @@ -83,7 +63,7 @@ if ( ret != 0 ) { // } #endif -#if defined(JIEF_DEBUG) +#if !defined(JIEF_DEBUG) #if defined(JIEF_DEBUG) && defined(CLOVER_BUILD) ret = printlib_tests(); @@ -121,12 +101,12 @@ if ( ret != 0 ) { } ret = strcasecmp_tests(); if ( ret != 0 ) { - printf("strncmp_tests() failed at test %d\n", ret); + printf("strcasecmp_tests() failed at test %d\n", ret); all_ok = false; } ret = XToolsCommon_tests(); if ( ret != 0 ) { - printf("printlib_tests() failed at test %d\n", ret); + printf("XToolsCommon_tests() failed at test %d\n", ret); all_ok = false; } ret = XArray_tests(); @@ -161,17 +141,17 @@ if ( ret != 0 ) { } ret = find_replace_mask_Clover_tests(); if ( ret != 0 ) { - printf("xml_lite_tests() failed at test %d\n", ret); + printf("find_replace_mask_Clover_tests() failed at test %d\n", ret); all_ok = false; } ret = find_replace_mask_OC_tests(); if ( ret != 0 ) { - printf("xml_lite_tests() failed at test %d\n", ret); + printf("find_replace_mask_OC_tests() failed at test %d\n", ret); all_ok = false; } ret = ParseXML_tests(); if ( ret != 0 ) { - printf("plist_tests() failed at test %d\n", ret); + printf("ParseXML_tests() failed at test %d\n", ret); all_ok = false; } ret = MacOsVersion_tests(); @@ -179,6 +159,11 @@ if ( ret != 0 ) { printf("MacOsVersion_tests() failed at test %d\n", ret); all_ok = false; } + ret = xml_lite_tests(); + if ( ret != 0 ) { + printf("xml_lite_tests() failed at test %d\n", ret); + all_ok = false; + } #endif diff --git a/rEFIt_UEFI/cpp_unit_test/plist_tests.cpp b/rEFIt_UEFI/cpp_unit_test/plist_tests.cpp index df15833e0..2be023e6d 100755 --- a/rEFIt_UEFI/cpp_unit_test/plist_tests.cpp +++ b/rEFIt_UEFI/cpp_unit_test/plist_tests.cpp @@ -912,6 +912,9 @@ int ParseXML_tests() { TagDict* dict = NULL; EFI_STATUS Status = ParseXML((UINT8*)config_all, &dict, (UINT32)strlen(config_all)); + dict->ReleaseTag(); + TagStruct::EmptyCache(); + if ( !EFI_ERROR(Status) ) { XString8 s; dict->sprintf(0, &s); diff --git a/rEFIt_UEFI/entry_scan/legacy.cpp b/rEFIt_UEFI/entry_scan/legacy.cpp index 15119a932..60fd817b8 100755 --- a/rEFIt_UEFI/entry_scan/legacy.cpp +++ b/rEFIt_UEFI/entry_scan/legacy.cpp @@ -72,7 +72,7 @@ DBG(" AddLegacyEntry:\n"); } DBG(" FullTitle=%ls\n", FullTitle.wc_str()); DBG(" LoaderTitle=%ls\n", _LoaderTitle.wc_str()); -DBG(" Volume->LegacyOS->Name=%ls\n", Volume->LegacyOS->Name.wc_str()); +DBG(" Volume->LegacyOS.Name=%ls\n", Volume->LegacyOS.Name.wc_str()); // Ignore this loader if it's device path is already present in another loader for (UINTN i = 0; i < MainMenu.Entries.size(); ++i) { @@ -88,17 +88,17 @@ DBG(" Volume->LegacyOS->Name=%ls\n", Volume->LegacyOS->Name.wc_str()); XStringW LoaderTitle; if ( _LoaderTitle.isEmpty() ) { - LoaderTitle = Volume->LegacyOS->Name; + LoaderTitle = Volume->LegacyOS.Name; }else{ LoaderTitle = _LoaderTitle; } XStringW LTitle; if (LoaderTitle.isEmpty()) { - if (Volume->LegacyOS->Name.notEmpty()) { - LTitle.takeValueFrom(Volume->LegacyOS->Name); - if (Volume->LegacyOS->Name[0] == 'W' || Volume->LegacyOS->Name[0] == 'L') - ShortcutLetter = (wchar_t)Volume->LegacyOS->Name[0]; // cast safe because value is 'W' or 'L' + if (Volume->LegacyOS.Name.notEmpty()) { + LTitle.takeValueFrom(Volume->LegacyOS.Name); + if (Volume->LegacyOS.Name[0] == 'W' || Volume->LegacyOS.Name[0] == 'L') + ShortcutLetter = (wchar_t)Volume->LegacyOS.Name[0]; // cast safe because value is 'W' or 'L' } else LTitle = L"Legacy OS"_XSW; } else @@ -128,15 +128,15 @@ DBG(" Volume->LegacyOS->Name=%ls\n", Volume->LegacyOS->Name.wc_str()); if ( Image && !Image->isEmpty() ) { Entry->Image = *Image; } else { - Entry->Image = ThemeX->LoadOSIcon(Volume->LegacyOS->IconName); + Entry->Image = ThemeX->LoadOSIcon(Volume->LegacyOS.IconName); if (Entry->Image.Image.isEmpty()) { Entry->Image = ThemeX->GetIcon("os_win"_XS8); //we have no legacy.png } } -// DBG("IconName=%ls\n", Volume->LegacyOS->IconName); +// DBG("IconName=%ls\n", Volume->LegacyOS.IconName); - Entry->DriveImage = (DriveImage != NULL) ? *DriveImage : ScanVolumeDefaultIcon(Volume, Volume->LegacyOS->Type, Volume->DevicePath); + Entry->DriveImage = (DriveImage != NULL) ? *DriveImage : ScanVolumeDefaultIcon(Volume, Volume->LegacyOS.Type, Volume->DevicePath); // DBG("HideBadges=%d Volume=%ls\n", GlobalConfig.HideBadges, Volume->VolName); // DBG("Title=%ls OSName=%ls OSIconName=%ls\n", LoaderTitle, Volume->OSName, Volume->OSIconName); @@ -165,7 +165,7 @@ DBG(" Volume->LegacyOS->Name=%ls\n", Volume->LegacyOS->Name.wc_str()); if (Custom.settings.Volume.notEmpty()) { if ( !Volume->DevicePathString.contains(Custom.settings.Volume) && !Volume->VolName.contains(Custom.settings.Volume) ) { if (Custom.settings.Type != 0) { - if (Custom.settings.Type == Volume->LegacyOS->Type) { + if (Custom.settings.Type == Volume->LegacyOS.Type) { Entry->Hidden = true; } } else { @@ -173,7 +173,7 @@ DBG(" Volume->LegacyOS->Name=%ls\n", Volume->LegacyOS->Name.wc_str()); } } } else if (Custom.settings.Type != 0) { - if (Custom.settings.Type == Volume->LegacyOS->Type) { + if (Custom.settings.Type == Volume->LegacyOS.Type) { Entry->Hidden = true; } } @@ -201,7 +201,7 @@ DBG(" Volume->LegacyOS->Name=%ls\n", Volume->LegacyOS->Name.wc_str()); SubScreen->AddMenuEntry(&MenuEntryReturn, false); Entry->SubScreen = SubScreen; MainMenu.AddMenuEntry(Entry, true); -// DBG(" added '%ls' OSType=%d Icon=%ls\n", Entry->Title, Volume->LegacyOS->Type, Volume->LegacyOS->IconName); +// DBG(" added '%ls' OSType=%d Icon=%ls\n", Entry->Title, Volume->LegacyOS.Type, Volume->LegacyOS.IconName); return true; } @@ -363,11 +363,11 @@ void AddCustomLegacy(void) continue; } // Check if the volume should be of certain os type - if ((Custom.settings.Type != 0) && (Custom.settings.Type != Volume->LegacyOS->Type)) { + if ((Custom.settings.Type != 0) && (Custom.settings.Type != Volume->LegacyOS.Type)) { DBG("skipped because wrong type\n"); continue; } - } else if ((Custom.settings.Type != 0) && (Custom.settings.Type != Volume->LegacyOS->Type)) { + } else if ((Custom.settings.Type != 0) && (Custom.settings.Type != Volume->LegacyOS.Type)) { DBG("skipped because wrong type\n"); continue; } diff --git a/rEFIt_UEFI/entry_scan/loader.cpp b/rEFIt_UEFI/entry_scan/loader.cpp index d994c43b8..4903577f5 100644 --- a/rEFIt_UEFI/entry_scan/loader.cpp +++ b/rEFIt_UEFI/entry_scan/loader.cpp @@ -501,7 +501,7 @@ MacOsVersion GetOSVersion(int LoaderType, const EFI_GUID& APFSTargetUUID, const } } } - Dict->FreeTag(); + Dict->ReleaseTag(); } } } @@ -555,7 +555,7 @@ MacOsVersion GetOSVersion(int LoaderType, const EFI_GUID& APFSTargetUUID, const } } } - Dict->FreeTag(); + Dict->ReleaseTag(); } } @@ -672,7 +672,7 @@ MacOsVersion GetOSVersion(int LoaderType, const EFI_GUID& APFSTargetUUID, const } } } - Dict->FreeTag(); + Dict->ReleaseTag(); } } } @@ -771,7 +771,7 @@ MacOsVersion GetOSVersion(int LoaderType, const EFI_GUID& APFSTargetUUID, const } } } - Dict->FreeTag(); + Dict->ReleaseTag(); } } } @@ -816,7 +816,7 @@ MacOsVersion GetOSVersion(int LoaderType, const EFI_GUID& APFSTargetUUID, const } } } - Dict->FreeTag(); + Dict->ReleaseTag(); } else if (FileExists (Volume->RootDir, L"\\com.apple.recovery.boot\\boot.efi")) { // Special case - com.apple.recovery.boot/boot.efi exists but SystemVersion.plist doesn't --> 10.9 recovery OSVersion = "10.9"_XS8; @@ -1745,6 +1745,7 @@ XString8 GetAuthRootDmg(const EFI_FILE& dir, const XStringW& path) } } if ( PlistBuffer ) FreePool(PlistBuffer); + if ( Dict ) Dict->ReleaseTag(); return returnValue; } diff --git a/rEFIt_UEFI/gui/menu_items/menu_items.cpp b/rEFIt_UEFI/gui/menu_items/menu_items.cpp index a7a244779..d3d8dddb1 100644 --- a/rEFIt_UEFI/gui/menu_items/menu_items.cpp +++ b/rEFIt_UEFI/gui/menu_items/menu_items.cpp @@ -35,6 +35,7 @@ #include // Only use angled for Platform, else, xcode project won't compile #include "menu_items.h" +#include "../REFIT_MENU_SCREEN.h" #include "../../libeg/libeg.h" #include "../../refit/lib.h" @@ -45,17 +46,21 @@ #include "../../libeg/XPointer.h" #endif +REFIT_ABSTRACT_MENU_ENTRY::~REFIT_ABSTRACT_MENU_ENTRY() { + delete SubScreen; +}; + REFIT_MENU_ENTRY_CLOVER* REFIT_MENU_ENTRY_CLOVER::getPartiallyDuplicatedEntry() const { REFIT_MENU_ENTRY_CLOVER* DuplicateEntry = new REFIT_MENU_ENTRY_CLOVER; - DuplicateEntry->AtClick = ActionEnter; + DuplicateEntry->AtClick = ActionEnter; DuplicateEntry->Volume = Volume; DuplicateEntry->DevicePathString= DevicePathString; DuplicateEntry->LoadOptions = LoadOptions; DuplicateEntry->LoaderPath = LoaderPath; DuplicateEntry->VolName = VolName; - DuplicateEntry->DevicePath = DevicePath; + DuplicateEntry->DevicePath = DuplicateDevicePath(DevicePath); DuplicateEntry->Flags = Flags; return DuplicateEntry; } @@ -64,16 +69,16 @@ LOADER_ENTRY* LOADER_ENTRY::getPartiallyDuplicatedEntry() const { LOADER_ENTRY* DuplicateEntry = new LOADER_ENTRY; - DuplicateEntry->AtClick = ActionEnter; + DuplicateEntry->AtClick = ActionEnter; DuplicateEntry->Volume = Volume; DuplicateEntry->DevicePathString= DevicePathString; DuplicateEntry->LoadOptions = LoadOptions; DuplicateEntry->LoaderPath = LoaderPath; - DuplicateEntry->DisplayedVolName = DisplayedVolName; - DuplicateEntry->DevicePath = DevicePath; + DuplicateEntry->DisplayedVolName= DisplayedVolName; + DuplicateEntry->DevicePath = DuplicateDevicePath(DevicePath); DuplicateEntry->Flags = Flags; DuplicateEntry->LoaderType = LoaderType; - DuplicateEntry->macOSVersion = macOSVersion; + DuplicateEntry->macOSVersion = macOSVersion; DuplicateEntry->BuildVersion = BuildVersion; // CopyKernelAndKextPatches(&DuplicateEntry->KernelAndKextPatches, &KernelAndKextPatches); DuplicateEntry->KernelAndKextPatches = KernelAndKextPatches; diff --git a/rEFIt_UEFI/gui/menu_items/menu_items.h b/rEFIt_UEFI/gui/menu_items/menu_items.h index 80837e7d6..adb495819 100644 --- a/rEFIt_UEFI/gui/menu_items/menu_items.h +++ b/rEFIt_UEFI/gui/menu_items/menu_items.h @@ -115,7 +115,7 @@ class REFIT_ABSTRACT_MENU_ENTRY ACTION AtDoubleClick = ActionNone; ACTION AtRightClick = ActionNone; ACTION AtMouseOver = ActionNone; - REFIT_MENU_SCREEN *SubScreen = NULL; + REFIT_MENU_SCREEN* SubScreen = NULL; // we can't use apd because of circular reference. We would need to include REFIT_MENU_SCREEN.h, but REFIT_MENU_SCREEN.h includes menu_items.h virtual XIcon* getDriveImage() { return nullptr; }; virtual XIcon* getBadgeImage() { return nullptr; }; @@ -150,7 +150,7 @@ class REFIT_ABSTRACT_MENU_ENTRY REFIT_ABSTRACT_MENU_ENTRY(const REFIT_ABSTRACT_MENU_ENTRY&) { panic("not yet defined"); } REFIT_ABSTRACT_MENU_ENTRY& operator=(const REFIT_ABSTRACT_MENU_ENTRY&) { panic("not yet defined"); } - virtual ~REFIT_ABSTRACT_MENU_ENTRY() {}; // virtual destructor : this is vital + virtual ~REFIT_ABSTRACT_MENU_ENTRY(); // virtual destructor : this is vital }; @@ -288,7 +288,7 @@ class REFIT_ABSTRACT_MENU_ENTRY public: UINT8 NoMemset; //HACK - some non zero value UINT16 Flags; - EFI_DEVICE_PATH *DevicePath; + apd DevicePath; void StartTool(); @@ -307,7 +307,7 @@ class REFIT_ABSTRACT_MENU_ENTRY public: REFIT_VOLUME *Volume; XStringW VolName; - EFI_DEVICE_PATH *DevicePath; + apd DevicePath; UINT16 Flags; REFIT_MENU_ENTRY_CLOVER() : REFIT_MENU_ITEM_ABSTRACT_ENTRY_LOADER(), Volume(NULL), VolName(), DevicePath(NULL), Flags(0) {}; @@ -356,7 +356,7 @@ class REFIT_ABSTRACT_MENU_ENTRY XStringW DisplayedVolName; XStringW OSName = XStringW(); - EFI_DEVICE_PATH *DevicePath; + apd DevicePath; UINT16 Flags; UINT8 LoaderType; MacOsVersion macOSVersion; diff --git a/rEFIt_UEFI/gui/shared_with_menu.cpp b/rEFIt_UEFI/gui/shared_with_menu.cpp index cac20b21b..25ddd5d24 100644 --- a/rEFIt_UEFI/gui/shared_with_menu.cpp +++ b/rEFIt_UEFI/gui/shared_with_menu.cpp @@ -68,7 +68,7 @@ CONST XString8 ArgOptional[NUM_OPT] = { }; CONST CHAR16 *VBIOS_BIN = L"misc\\c0000.bin"; -INPUT_ITEM *InputItems = NULL; +INPUT_ITEM InputItems[135]; // INPUT_ITEM is a class, therefore default initialization is taking place. INTN TextStyle; //why global? It will be class SCREEN member UINT32 EncodeOptions(const XString8Array& Options) diff --git a/rEFIt_UEFI/gui/shared_with_menu.h b/rEFIt_UEFI/gui/shared_with_menu.h index 9d2708b69..52f853acd 100644 --- a/rEFIt_UEFI/gui/shared_with_menu.h +++ b/rEFIt_UEFI/gui/shared_with_menu.h @@ -17,7 +17,7 @@ extern INTN TextStyle; //why global? It will be class SCREEN member extern CONST CHAR16 *VBIOS_BIN; -extern INPUT_ITEM *InputItems; +extern INPUT_ITEM InputItems[135]; void DecodeOptions(REFIT_MENU_ITEM_BOOTNUM *Entry); UINT32 EncodeOptions(const XString8Array& Options); diff --git a/rEFIt_UEFI/libeg/VectorGraphics.cpp b/rEFIt_UEFI/libeg/VectorGraphics.cpp index ea1e57335..decbd14d0 100755 --- a/rEFIt_UEFI/libeg/VectorGraphics.cpp +++ b/rEFIt_UEFI/libeg/VectorGraphics.cpp @@ -153,7 +153,7 @@ displayFreeMemory("XTheme::ParseSVGXTheme begin"_XS8); if ( nsvg__nbDanglingPtr() > 0 ) { DBG("There is already dangling ptr. nano svg memory leak test not done\n"); }else{ - char* buffer2 = (char*)malloc(Size); + apd buffer2 = (char*)malloc(Size); memcpy(buffer2, buffer, Size); nvsg__memoryallocation_verbose = false; NSVGparser* p = nsvg__parse(buffer2, 72, 1.f); //the buffer will be modified, it is how nanosvg works diff --git a/rEFIt_UEFI/libeg/XTheme.cpp b/rEFIt_UEFI/libeg/XTheme.cpp index e834691c7..c3dd847ef 100644 --- a/rEFIt_UEFI/libeg/XTheme.cpp +++ b/rEFIt_UEFI/libeg/XTheme.cpp @@ -185,7 +185,7 @@ finish: } } } - ThemeDict->FreeTag(); + ThemeDict->ReleaseTag(); if (!ThemeX->Daylight) { Status = StartupSoundPlay(&ThemeX->getThemeDir(), L"sound_night.wav"); @@ -819,6 +819,7 @@ XIcon& XTheme::GetIconAlt(INTN Id, INTN Alt) //if not found then take embedded XIcon *NewIcon = new XIcon(Id, true); if (NewIcon->Image.isEmpty()) { // check for embedded with ID=Alt + delete NewIcon; NewIcon = new XIcon(Alt, true); } if (!NewIcon->Image.isEmpty()) { @@ -826,6 +827,7 @@ XIcon& XTheme::GetIconAlt(INTN Id, INTN Alt) //if not found then take embedded Icons[IdFound].Image = NewIcon->Image; Icons[IdFound].ImageNight = NewIcon->ImageNight; } + delete NewIcon; // there is probably a way better way to do this. By initializing directly Icons[IdFound].Image instead of using dynamically allocated copy. } } diff --git a/rEFIt_UEFI/libeg/nanosvg.cpp b/rEFIt_UEFI/libeg/nanosvg.cpp index 214a7313d..0464e1012 100755 --- a/rEFIt_UEFI/libeg/nanosvg.cpp +++ b/rEFIt_UEFI/libeg/nanosvg.cpp @@ -44,6 +44,7 @@ //#include "../include/OneLinerMacros.h" #include "../Platform/Utils.h" #include "BmLib.h" +#include "../include/OneLinerMacros.h" #ifndef DEBUG_ALL #define DEBUG_SVG 1 @@ -111,20 +112,31 @@ #include "../cpp_foundation/XString.h" int nvsg__memoryallocation_verbose = false; +uint64_t nsvg__alloc_count = 0; XArray nsvg__allocatedPtr; +XArray nsvg__allocatedPtrIdx; #ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE XObjArray nsvg__allocatedPtrMsg; #endif +void nsvg__alloc_insert(void* p, const XString8& msg) +{ +//if ( nsvg__alloc_count == 36602 ) { +//NOP; +//} + nsvg__allocatedPtr.Add(uintptr_t(p)); + nsvg__allocatedPtrIdx.Add(nsvg__alloc_count++); +#ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE + nsvg__allocatedPtrMsg.AddCopy(msg, true); +#endif +} + void* nsvg__alloc(UINTN size, const XString8& msg) { void* buffer = AllocatePool(size); if ( nvsg__memoryallocation_verbose ) DBG("nsvg__alloc(%lld) - %s = %llx\n", size, msg.c_str(), uintptr_t(buffer)); - nsvg__allocatedPtr.Add(uintptr_t(buffer)); -#ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE - nsvg__allocatedPtrMsg.AddCopy(XString8(msg), true); -#endif + nsvg__alloc_insert(buffer, msg); return buffer; } @@ -132,10 +144,7 @@ void* nsvg__alloczero(UINTN size, const XString8& msg) { void* buffer = AllocateZeroPool(size); if ( nvsg__memoryallocation_verbose ) DBG("nsvg__alloczero(%lld) - %s = %llx\n", size, msg.c_str(), uintptr_t(buffer)); - nsvg__allocatedPtr.Add(uintptr_t(buffer)); -#ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE - nsvg__allocatedPtrMsg.AddCopy(XString8(msg), true); -#endif + nsvg__alloc_insert(buffer, msg); return buffer; } @@ -143,10 +152,7 @@ void* nsvg__alloccopy(UINTN size, const void* ref, const XString8& msg) { void* buffer = AllocateCopyPool(size, ref); if ( nvsg__memoryallocation_verbose ) DBG("nsvg__alloccopy(%lld, %llx) - %s = %llx\n", size, uintptr_t(ref), msg.c_str(), uintptr_t(buffer)); - nsvg__allocatedPtr.Add(uintptr_t(buffer)); -#ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE - nsvg__allocatedPtrMsg.AddCopy(XString8(msg), true); -#endif + nsvg__alloc_insert(buffer, msg); return buffer; } @@ -158,34 +164,35 @@ void* nsvg__realloc(UINTN oldsize, UINTN newsize, void* ref, const XString8& msg void* buffer = ReallocatePool(oldsize, newsize, ref); if ( nvsg__memoryallocation_verbose ) DBG("nsvg__realloc(%lld, %lld, %llx) - %s = %llx\n", oldsize, newsize, uintptr_t(ref), msg.c_str(), uintptr_t(buffer)); nsvg__allocatedPtr.RemoveAtIndex(idx); + nsvg__allocatedPtrIdx.RemoveAtIndex(idx); #ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE nsvg__allocatedPtrMsg.RemoveAtIndex(idx); #endif - nsvg__allocatedPtr.Add(uintptr_t(buffer)); -#ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE - nsvg__allocatedPtrMsg.AddCopy(XString8(msg), true); -#endif + nsvg__alloc_insert(buffer, msg); return buffer; } void nsvg__delete(void* buffer, const XString8& msg) { -//if ( ThemeX->Icons.length() > 0 && buffer == ThemeX->Icons[0].ImageSVG ) { -// DBG("stop"); -//} uintptr_t ref2 = uintptr_t(buffer); auto idx = nsvg__allocatedPtr.indexOf(ref2); if ( idx == MAX_XSIZE ) { log_technical_bug("nsvg__delete %llx", uintptr_t(buffer)); } - EFI_STATUS Status = gBS->FreePool(buffer); +// EFI_STATUS Status = gBS->FreePool(buffer); +// if ( EFI_ERROR(Status) ) { +// log_technical_bug("nsvg__delete %llx", uintptr_t(buffer)); +// } + EFI_STATUS Status = 0; (void)Status; + FreePool(buffer); #ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE - if ( nvsg__memoryallocation_verbose ) DBG("nsvg__delete(%llx) - allocation msg %s - %s - Status = %s\n", uintptr_t(buffer), nsvg__allocatedPtrMsg[idx].c_str(), msg.c_str(), efiStrError(Status)); + if ( nvsg__memoryallocation_verbose ) DBG("nsvg__delete(%llx) - count=%lld - allocation msg %s - %s - Status = %s\n", uintptr_t(buffer), nsvg__allocatedPtrIdx[idx], nsvg__allocatedPtrMsg[idx].c_str(), msg.c_str(), efiStrError(Status)); #else - if ( nvsg__memoryallocation_verbose ) DBG("nsvg__delete(%llx) - %s - Status = %s\n", uintptr_t(buffer), msg.c_str(), efiStrError(Status)); + if ( nvsg__memoryallocation_verbose ) DBG("nsvg__delete[%zu](%llx) - %s - Status = %s\n", idx, uintptr_t(buffer), msg.c_str(), efiStrError(Status)); #endif nsvg__allocatedPtr.RemoveAtIndex(idx); + nsvg__allocatedPtrIdx.RemoveAtIndex(idx); #ifdef NANOSVG_MEMORY_ALLOCATION_TRACE_VERBOSE nsvg__allocatedPtrMsg.RemoveAtIndex(idx); #endif @@ -199,11 +206,11 @@ size_t nsvg__nbDanglingPtr() void nsvg__outputDanglingPtr() { - for(size_t i=0;ipaths); } } -// nsvg__delete(shape, "nsvg__deleteShapes"_XS8); // TODO : BUG, it doesn't boot anymore if we do the delete. Something is wrong. + nsvg__delete(shape, "nsvg__deleteShapes"_XS8); shape = snext; } } diff --git a/rEFIt_UEFI/refit/lib.cpp b/rEFIt_UEFI/refit/lib.cpp index 86e8332b4..89982d6ea 100644 --- a/rEFIt_UEFI/refit/lib.cpp +++ b/rEFIt_UEFI/refit/lib.cpp @@ -291,8 +291,8 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) //CHAR16 *kind = NULL; Volume->HasBootCode = false; - Volume->LegacyOS->IconName.setEmpty(); - Volume->LegacyOS->Name.setEmpty(); + Volume->LegacyOS.IconName.setEmpty(); + Volume->LegacyOS.Name.setEmpty(); // Volume->BootType = BOOTING_BY_MBR; //default value Volume->BootType = BOOTING_BY_EFI; *Bootable = false; @@ -357,18 +357,18 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) if (AsciiStrStr((CHAR8*)&SectorBuffer[i], "APPLE")) { // StrCpy(Volume->VolName, volumeName); DBG(" Found AppleDVD\n"); - Volume->LegacyOS->Type = OSTYPE_OSX; + Volume->LegacyOS.Type = OSTYPE_OSX; Volume->BootType = BOOTING_BY_CD; - Volume->LegacyOS->IconName = L"mac"_XSW; + Volume->LegacyOS.IconName = L"mac"_XSW; break; } } else if (SectorBuffer[i] == 'M') { if (AsciiStrStr((CHAR8*)&SectorBuffer[i], "MICROSOFT")) { // StrCpy(Volume->VolName, volumeName); DBG(" Found Windows DVD\n"); - Volume->LegacyOS->Type = OSTYPE_WIN; + Volume->LegacyOS.Type = OSTYPE_WIN; Volume->BootType = BOOTING_BY_CD; - Volume->LegacyOS->IconName = L"win"_XSW; + Volume->LegacyOS.IconName = L"win"_XSW; break; } @@ -378,9 +378,9 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) // StrCpy(Volume->VolName, volumeName); DBG(" Found Linux DVD\n"); - Volume->LegacyOS->Type = OSTYPE_LIN; + Volume->LegacyOS.Type = OSTYPE_LIN; Volume->BootType = BOOTING_BY_CD; - Volume->LegacyOS->IconName = L"linux"_XSW; + Volume->LegacyOS.IconName = L"linux"_XSW; break; } } @@ -395,9 +395,9 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) *Bootable = true; Volume->HasBootCode = true; // DBG("The volume has bootcode\n"); - Volume->LegacyOS->IconName = L"legacy"; - Volume->LegacyOS->Name = L"Legacy"; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"legacy"; + Volume->LegacyOS.Name = L"Legacy"; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } // */ @@ -408,24 +408,24 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) CompareMem(SectorBuffer + 3, "SYSLINUX", 8) == 0 || FindMem(SectorBuffer, 2048, "ISOLINUX", 8) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"linux"_XSW; - Volume->LegacyOS->Name = L"Linux"_XSW; - Volume->LegacyOS->Type = OSTYPE_LIN; + Volume->LegacyOS.IconName = L"linux"_XSW; + Volume->LegacyOS.Name = L"Linux"_XSW; + Volume->LegacyOS.Type = OSTYPE_LIN; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "Geom\0Hard Disk\0Read\0 Error", 26) >= 0) { // GRUB Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"grub,linux"_XSW; - Volume->LegacyOS->Name = L"Linux"_XSW; + Volume->LegacyOS.IconName = L"grub,linux"_XSW; + Volume->LegacyOS.Name = L"Linux"_XSW; Volume->BootType = BOOTING_BY_PBR; /* } else if ((*((UINT32 *)(SectorBuffer)) == 0x4d0062e9 && *((UINT16 *)(SectorBuffer + 510)) == 0xaa55) || FindMem(SectorBuffer, 2048, "BOOT ", 10) >= 0) { //reboot Clover Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"clover"; - Volume->LegacyOS->Name = L"Clover"; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"clover"; + Volume->LegacyOS.Name = L"Clover"; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; // DBG("Detected Clover FAT32 bootcode\n"); */ @@ -435,79 +435,79 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) *((UINT16 *)(SectorBuffer + 510)) == 0xaa55) || FindMem(SectorBuffer, 2048, "Starting the BTX loader", 23) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"freebsd,linux"_XSW; - Volume->LegacyOS->Name = L"FreeBSD"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"freebsd,linux"_XSW; + Volume->LegacyOS.Name = L"FreeBSD"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "!Loading", 8) >= 0 || FindMem(SectorBuffer, 2048, "/cdboot\0/CDBOOT\0", 16) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"openbsd,linux"_XSW; - Volume->LegacyOS->Name = L"OpenBSD"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"openbsd,linux"_XSW; + Volume->LegacyOS.Name = L"OpenBSD"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "Not a bootxx image", 18) >= 0 || *((UINT32 *)(SectorBuffer + 1028)) == 0x7886b6d1) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"netbsd,linux"_XSW; - Volume->LegacyOS->Name = L"NetBSD"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"netbsd,linux"_XSW; + Volume->LegacyOS.Name = L"NetBSD"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 2048, "NTLDR", 5) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"win"_XSW; - Volume->LegacyOS->Name = L"Windows"_XSW; - Volume->LegacyOS->Type = OSTYPE_WIN; + Volume->LegacyOS.IconName = L"win"_XSW; + Volume->LegacyOS.Name = L"Windows"_XSW; + Volume->LegacyOS.Type = OSTYPE_WIN; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 2048, "BOOTMGR", 7) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"vista,win"_XSW; - Volume->LegacyOS->Name = L"Windows"_XSW; - Volume->LegacyOS->Type = OSTYPE_WIN; + Volume->LegacyOS.IconName = L"vista,win"_XSW; + Volume->LegacyOS.Name = L"Windows"_XSW; + Volume->LegacyOS.Type = OSTYPE_WIN; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "CPUBOOT SYS", 11) >= 0 || FindMem(SectorBuffer, 512, "KERNEL SYS", 11) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"freedos,win"_XSW; - Volume->LegacyOS->Name = L"FreeDOS"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"freedos,win"_XSW; + Volume->LegacyOS.Name = L"FreeDOS"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "OS2LDR", 6) >= 0 || FindMem(SectorBuffer, 512, "OS2BOOT", 7) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"ecomstation"_XSW; - Volume->LegacyOS->Name = L"eComStation"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"ecomstation"_XSW; + Volume->LegacyOS.Name = L"eComStation"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "Be Boot Loader", 14) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"beos"_XSW; - Volume->LegacyOS->Name = L"BeOS"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"beos"_XSW; + Volume->LegacyOS.Name = L"BeOS"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "yT Boot Loader", 14) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"zeta"_XSW; - Volume->LegacyOS->Name = L"ZETA"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"zeta"_XSW; + Volume->LegacyOS.Name = L"ZETA"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } else if (FindMem(SectorBuffer, 512, "\x04" "beos\x06" "system\x05" "zbeos", 18) >= 0 || FindMem(SectorBuffer, 512, "haiku_loader", 12) >= 0) { Volume->HasBootCode = true; - Volume->LegacyOS->IconName = L"haiku"_XSW; - Volume->LegacyOS->Name = L"Haiku"_XSW; - Volume->LegacyOS->Type = OSTYPE_VAR; + Volume->LegacyOS.IconName = L"haiku"_XSW; + Volume->LegacyOS.Name = L"Haiku"_XSW; + Volume->LegacyOS.Type = OSTYPE_VAR; Volume->BootType = BOOTING_BY_PBR; } @@ -519,8 +519,8 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) #if REFIT_DEBUG > 0 DBG(" Result of bootcode detection: %ls %ls (%ls)\n", Volume->HasBootCode ? L"bootable" : L"non-bootable", - Volume->LegacyOS->Name.notEmpty() ? Volume->LegacyOS->Name.wc_str() : L"unknown", - Volume->LegacyOS->IconName.notEmpty() ? Volume->LegacyOS->IconName.wc_str() : L"legacy"); + Volume->LegacyOS.Name.notEmpty() ? Volume->LegacyOS.Name.wc_str() : L"unknown", + Volume->LegacyOS.IconName.notEmpty() ? Volume->LegacyOS.IconName.wc_str() : L"legacy"); #endif if (FindMem(SectorBuffer, 512, "Non-system disk", 15) >= 0) // dummy FAT boot sector @@ -529,9 +529,9 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) #ifdef JIEF_DEBUG ////*Bootable = true; //Volume->HasBootCode = true; -//Volume->LegacyOS->IconName = L"win"_XSW; -//Volume->LegacyOS->Name = L"Windows"_XSW; -//Volume->LegacyOS->Type = OSTYPE_WIN; +//Volume->LegacyOS.IconName = L"win"_XSW; +//Volume->LegacyOS.Name = L"Windows"_XSW; +//Volume->LegacyOS.Type = OSTYPE_WIN; //Volume->BootType = BOOTING_BY_PBR; #endif @@ -564,8 +564,6 @@ static void ScanVolumeBootcode(IN OUT REFIT_VOLUME *Volume, OUT XBool *Bootable) static EFI_STATUS ScanVolume(IN OUT REFIT_VOLUME *Volume) { EFI_STATUS Status; - EFI_DEVICE_PATH *DevicePath, *NextDevicePath; - EFI_DEVICE_PATH *DiskDevicePath, *RemainingDevicePath = NULL; HARDDRIVE_DEVICE_PATH *HdPath = NULL; EFI_HANDLE WholeDiskHandle; UINTN PartialLength = 0; @@ -576,14 +574,16 @@ static EFI_STATUS ScanVolume(IN OUT REFIT_VOLUME *Volume) XBool Bootable; // EFI_INPUT_KEY Key; - // get device path - DiskDevicePath = DevicePathFromHandle(Volume->DeviceHandle); -//Volume->DevicePath = DuplicateDevicePath(DevicePathFromHandle(Volume->DeviceHandle)); - DevicePathSize = GetDevicePathSize (DiskDevicePath); - Volume->DevicePath = (__typeof__(Volume->DevicePath))AllocateAlignedPages(EFI_SIZE_TO_PAGES(DevicePathSize), 64); - CopyMem(Volume->DevicePath, DiskDevicePath, DevicePathSize); - Volume->DevicePathString = FileDevicePathToXStringW(Volume->DevicePath); - + { + // get device path + EFI_DEVICE_PATH* DiskDevicePath = DevicePathFromHandle(Volume->DeviceHandle); + //Volume->DevicePath = DuplicateDevicePath(DevicePathFromHandle(Volume->DeviceHandle)); + DevicePathSize = GetDevicePathSize (DiskDevicePath); + Volume->DevicePath = (__typeof__(Volume->DevicePath))AllocateAlignedPages(EFI_SIZE_TO_PAGES(DevicePathSize), 64); + CopyMem(Volume->DevicePath, DiskDevicePath, DevicePathSize); + Volume->DevicePathString = FileDevicePathToXStringW(Volume->DevicePath); + } + #if REFIT_DEBUG > 0 if (Volume->DevicePath != NULL) { DBG(" %ls\n", FileDevicePathToXStringW(Volume->DevicePath).wc_str()); @@ -622,41 +622,41 @@ static EFI_STATUS ScanVolume(IN OUT REFIT_VOLUME *Volume) ScanVolumeBootcode(Volume, &Bootable); // DBG(" ScanVolumeBootcode success\n"); // detect device type - DevicePath = DuplicateDevicePath(Volume->DevicePath); - while (DevicePath != NULL && !IsDevicePathEndType(DevicePath)) { - NextDevicePath = NextDevicePathNode(DevicePath); - - if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) && - ((DevicePathSubType (DevicePath) == MSG_SATA_DP) || - (DevicePathSubType (DevicePath) == MSG_NVME_NAMESPACE_DP) || - (DevicePathSubType (DevicePath) == MSG_ATAPI_DP))) { + apd DevicePath = DuplicateDevicePath(Volume->DevicePath); + EFI_DEVICE_PATH *NextDevicePath = NextDevicePathNode(DevicePath); + while (NextDevicePath != NULL && !IsDevicePathEndType(NextDevicePath)) + { + if ((DevicePathType (NextDevicePath) == MESSAGING_DEVICE_PATH) && + ((DevicePathSubType (NextDevicePath) == MSG_SATA_DP) || + (DevicePathSubType (NextDevicePath) == MSG_NVME_NAMESPACE_DP) || + (DevicePathSubType (NextDevicePath) == MSG_ATAPI_DP))) { // DBG(" HDD volume\n"); Volume->DiskKind = DISK_KIND_INTERNAL; break; } - if (DevicePathType(DevicePath) == MESSAGING_DEVICE_PATH && - (DevicePathSubType(DevicePath) == MSG_USB_DP || DevicePathSubType(DevicePath) == MSG_USB_CLASS_DP)) { + if (DevicePathType(NextDevicePath) == MESSAGING_DEVICE_PATH && + (DevicePathSubType(NextDevicePath) == MSG_USB_DP || DevicePathSubType(NextDevicePath) == MSG_USB_CLASS_DP)) { // DBG(" USB volume\n"); Volume->DiskKind = DISK_KIND_EXTERNAL; // break; } // FIREWIRE Devices - if (DevicePathType(DevicePath) == MESSAGING_DEVICE_PATH && - (DevicePathSubType(DevicePath) == MSG_1394_DP || DevicePathSubType(DevicePath) == MSG_FIBRECHANNEL_DP)) { + if (DevicePathType(NextDevicePath) == MESSAGING_DEVICE_PATH && + (DevicePathSubType(NextDevicePath) == MSG_1394_DP || DevicePathSubType(NextDevicePath) == MSG_FIBRECHANNEL_DP)) { // DBG(" FireWire volume\n"); Volume->DiskKind = DISK_KIND_FIREWIRE; break; } // CD-ROM Devices - if (DevicePathType(DevicePath) == MEDIA_DEVICE_PATH && - DevicePathSubType(DevicePath) == MEDIA_CDROM_DP) { + if (DevicePathType(NextDevicePath) == MEDIA_DEVICE_PATH && + DevicePathSubType(NextDevicePath) == MEDIA_CDROM_DP) { // DBG(" CD-ROM volume\n"); Volume->DiskKind = DISK_KIND_OPTICAL; //it's impossible break; } // VENDOR Specific Path - if (DevicePathType(DevicePath) == MEDIA_DEVICE_PATH && - DevicePathSubType(DevicePath) == MEDIA_VENDOR_DP) { + if (DevicePathType(NextDevicePath) == MEDIA_DEVICE_PATH && + DevicePathSubType(NextDevicePath) == MEDIA_VENDOR_DP) { // DBG(" Vendor volume\n"); if ( Volume->ApfsFileSystemUUID.isNull() ) { Volume->DiskKind = DISK_KIND_NODISK; // Jief, don't know why DISK_KIND_NODISK in that case. That prevents Recovery badge to appear. If it's not APFS, let's do it like it was before. @@ -664,15 +664,15 @@ static EFI_STATUS ScanVolume(IN OUT REFIT_VOLUME *Volume) break; } // LEGACY CD-ROM - if (DevicePathType(DevicePath) == BBS_DEVICE_PATH && - (DevicePathSubType(DevicePath) == BBS_BBS_DP || DevicePathSubType(DevicePath) == BBS_TYPE_CDROM)) { + if (DevicePathType(NextDevicePath) == BBS_DEVICE_PATH && + (DevicePathSubType(NextDevicePath) == BBS_BBS_DP || DevicePathSubType(NextDevicePath) == BBS_TYPE_CDROM)) { // DBG(" Legacy CD-ROM volume\n"); Volume->DiskKind = DISK_KIND_OPTICAL; break; } // LEGACY HARDDISK - if (DevicePathType(DevicePath) == BBS_DEVICE_PATH && - (DevicePathSubType(DevicePath) == BBS_BBS_DP || DevicePathSubType(DevicePath) == BBS_TYPE_HARDDRIVE)) { + if (DevicePathType(NextDevicePath) == BBS_DEVICE_PATH && + (DevicePathSubType(NextDevicePath) == BBS_BBS_DP || DevicePathSubType(NextDevicePath) == BBS_TYPE_HARDDRIVE)) { // DBG(" Legacy HDD volume\n"); Volume->DiskKind = DISK_KIND_INTERNAL; break; @@ -682,7 +682,7 @@ static EFI_STATUS ScanVolume(IN OUT REFIT_VOLUME *Volume) // diskKind = NVME //#define MSG_NVME_NAMESPACE_DP 0x17 - DevicePath = NextDevicePath; + NextDevicePath = NextDevicePathNode(NextDevicePath); } /* what is the bread? @@ -699,56 +699,56 @@ static EFI_STATUS ScanVolume(IN OUT REFIT_VOLUME *Volume) */ } - - DevicePath = DuplicateDevicePath(Volume->DevicePath); - RemainingDevicePath = DevicePath; //initial value - // - // find the partition device path node - // - while (DevicePath && !IsDevicePathEnd (DevicePath)) { - if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && - (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) { - HdPath = (HARDDRIVE_DEVICE_PATH *)DevicePath; - // break; - } - DevicePath = NextDevicePathNode (DevicePath); - } -// DBG("DevicePath scanned\n"); - if (HdPath) { - // printf("Partition found %s\n", DevicePathToStr((EFI_DEVICE_PATH *)HdPath)); - - PartialLength = (UINTN)((UINT8 *)HdPath - (UINT8 *)(RemainingDevicePath)); - if (PartialLength > 0x1000) { - PartialLength = sizeof(EFI_DEVICE_PATH); //something wrong here but I don't want to be freezed - // return EFI_SUCCESS; + { + apd DevicePath = DuplicateDevicePath(Volume->DevicePath); + EFI_DEVICE_PATH* walkDevicePath = DevicePath; //initial value + // + // find the partition device path node + // + while (walkDevicePath && !IsDevicePathEnd (walkDevicePath)) { + if ((DevicePathType (walkDevicePath) == MEDIA_DEVICE_PATH) && + (DevicePathSubType (walkDevicePath) == MEDIA_HARDDRIVE_DP)) { + HdPath = (HARDDRIVE_DEVICE_PATH *)walkDevicePath; + // break; + } + walkDevicePath = NextDevicePathNode (walkDevicePath); } - DiskDevicePath = (EFI_DEVICE_PATH *)AllocatePool(PartialLength + sizeof(EFI_DEVICE_PATH)); - CopyMem(DiskDevicePath, Volume->DevicePath, PartialLength); - CopyMem((UINT8 *)DiskDevicePath + PartialLength, DevicePath, sizeof(EFI_DEVICE_PATH)); //EndDevicePath - // DBG("WholeDevicePath %ls\n", DevicePathToStr(DiskDevicePath)); - RemainingDevicePath = DiskDevicePath; - Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &WholeDiskHandle); - if (EFI_ERROR(Status)) { - DBG("Can't find WholeDevicePath: %s\n", efiStrError(Status)); - } else { - Volume->WholeDiskDeviceHandle = WholeDiskHandle; - Volume->WholeDiskDevicePath = DuplicateDevicePath(RemainingDevicePath); - // look at the BlockIO protocol - Status = gBS->HandleProtocol(WholeDiskHandle, &gEfiBlockIoProtocolGuid, (void **) &Volume->WholeDiskBlockIO); - if (!EFI_ERROR(Status)) { - // DBG("WholeDiskBlockIO %hhX BlockSize=%d\n", Volume->WholeDiskBlockIO, Volume->WholeDiskBlockIO->Media->BlockSize); - // check the media block size - if (Volume->WholeDiskBlockIO->Media->BlockSize == 2048) - Volume->DiskKind = DISK_KIND_OPTICAL; - + // DBG("DevicePath scanned\n"); + if (HdPath) { + // printf("Partition found %s\n", DevicePathToStr((EFI_DEVICE_PATH *)HdPath)); + + PartialLength = (UINTN)((UINT8 *)HdPath - (UINT8 *)(DevicePath.get())); + if (PartialLength > 0x1000) { + PartialLength = sizeof(EFI_DEVICE_PATH); //something wrong here but I don't want to be freezed + // return EFI_SUCCESS; + } + apd DiskDevicePath = (EFI_DEVICE_PATH *)AllocatePool(PartialLength + sizeof(EFI_DEVICE_PATH)); + CopyMem(DiskDevicePath, Volume->DevicePath, PartialLength); + CopyMem((UINT8 *)DiskDevicePath.get() + PartialLength, walkDevicePath, sizeof(EFI_DEVICE_PATH)); //EndDevicePath + // DBG("WholeDevicePath %ls\n", DevicePathToStr(DiskDevicePath)); + EFI_DEVICE_PATH* RemainingDevicePath = DiskDevicePath; + Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &WholeDiskHandle); + if (EFI_ERROR(Status)) { + DBG("Can't find WholeDevicePath: %s\n", efiStrError(Status)); } else { - Volume->WholeDiskBlockIO = NULL; - // DBG("no WholeDiskBlockIO: %s\n", efiStrError(Status)); - - //CheckError(Status, L"from HandleProtocol"); + Volume->WholeDiskDeviceHandle = WholeDiskHandle; + Volume->WholeDiskDevicePath = DuplicateDevicePath(RemainingDevicePath); + // look at the BlockIO protocol + Status = gBS->HandleProtocol(WholeDiskHandle, &gEfiBlockIoProtocolGuid, (void **) &Volume->WholeDiskBlockIO); + if (!EFI_ERROR(Status)) { + // DBG("WholeDiskBlockIO %hhX BlockSize=%d\n", Volume->WholeDiskBlockIO, Volume->WholeDiskBlockIO->Media->BlockSize); + // check the media block size + if (Volume->WholeDiskBlockIO->Media->BlockSize == 2048) + Volume->DiskKind = DISK_KIND_OPTICAL; + + } else { + Volume->WholeDiskBlockIO = NULL; + // DBG("no WholeDiskBlockIO: %s\n", efiStrError(Status)); + + //CheckError(Status, L"from HandleProtocol"); + } } } - FreePool(DiskDevicePath); } /* else { DBG("HD path is not found\n"); //master volume! @@ -784,16 +784,16 @@ static EFI_STATUS ScanVolume(IN OUT REFIT_VOLUME *Volume) } else if (Volume->VolName.isEmpty()) { Volume->VolName = L"Whole Disc Boot"_XSW; } - if (Volume->LegacyOS->IconName.isEmpty()) - Volume->LegacyOS->IconName = L"legacy"_XSW; + if (Volume->LegacyOS.IconName.isEmpty()) + Volume->LegacyOS.IconName = L"legacy"_XSW; return EFI_SUCCESS; } if ( Volume->ApfsFileSystemUUID.notNull() ) { - APPLE_APFS_CONTAINER_INFO *ApfsContainerInfo; - APPLE_APFS_VOLUME_INFO *ApfsVolumeInfo; - Status = InternalGetApfsSpecialFileInfo(Volume->RootDir, &ApfsVolumeInfo, &ApfsContainerInfo); + apd ApfsContainerInfo; + apd ApfsVolumeInfo; + Status = InternalGetApfsSpecialFileInfo(Volume->RootDir, &ApfsVolumeInfo.get(), &ApfsContainerInfo.get()); if ( !EFI_ERROR(Status) ) { //DBG("Status : %s, APFS role : %x\n", efiStrError(Status), ApfsVolumeInfo->Role); Volume->ApfsRole = ApfsVolumeInfo->Role; @@ -986,7 +986,6 @@ void ScanVolumes(void) for (UINTN HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) { REFIT_VOLUME* Volume = new REFIT_VOLUME; - Volume->LegacyOS = new LEGACY_OS; Volume->DeviceHandle = Handles[HandleIndex]; if (Volume->DeviceHandle == self.getSelfDeviceHandle()) { SelfVolume = Volume; @@ -999,7 +998,7 @@ void ScanVolumes(void) Status = ScanVolume(Volume); if (!EFI_ERROR(Status)) { Volume->Index = HandleIndex; - Volumes.AddReference(Volume, false); + Volumes.AddReference(Volume, true); for (size_t HVi = 0; HVi < gSettings.GUI.HVHideStrings.size(); HVi++) { if ( Volume->DevicePathString.containsIC(gSettings.GUI.HVHideStrings[HVi]) || Volume->VolName.containsIC(gSettings.GUI.HVHideStrings[HVi]) @@ -1010,11 +1009,11 @@ void ScanVolumes(void) } // Guid = FindGPTPartitionGuidInDevicePath(Volume->DevicePath); - if (Volume->LegacyOS->IconName.isEmpty()) { - Volume->LegacyOS->IconName = L"legacy"_XSW; + if (Volume->LegacyOS.IconName.isEmpty()) { + Volume->LegacyOS.IconName = L"legacy"_XSW; } // DBG(" Volume '%ls', LegacyOS '%ls', LegacyIcon(s) '%ls', EFI_GUID = %s\n", -// Volume->VolName, Volume->LegacyOS->Name ? Volume->LegacyOS->Name : L"", Volume->LegacyOS->IconName, Guid.toXString8().c_str()); +// Volume->VolName, Volume->LegacyOS.Name ? Volume->LegacyOS.Name : L"", Volume->LegacyOS.IconName, Guid.toXString8().c_str()); if (SelfVolume == Volume) { DBG(" This is SelfVolume !!\n"); } @@ -1034,7 +1033,7 @@ void ScanVolumes(void) SelfVolume->RootDir = const_cast(&self.getSelfVolumeRootDir()); // TODO : SelfVolume->RootDir should be const ! we should duplicate ? SelfVolume->DiskKind = DISK_KIND_BOOTER; SelfVolume->VolName = L"Clover"_XSW; - SelfVolume->LegacyOS->Type = OSTYPE_EFI; + SelfVolume->LegacyOS.Type = OSTYPE_EFI; SelfVolume->HasBootCode = true; SelfVolume->BootType = BOOTING_BY_PBR; // AddListElement((void ***) &Volumes, &VolumesCount, SelfVolume); @@ -1170,7 +1169,7 @@ void ReinitVolumes(void) // CheckError(Status, L"from LocateDevicePath"); } - if (Volume->WholeDiskDevicePath != NULL) { + if (Volume->WholeDiskDevicePath.notNull()) { // get the handle for that path RemainingDevicePath = DuplicateDevicePath(Volume->WholeDiskDevicePath); Status = gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid, const_cast(&RemainingDevicePath), &WholeDiskHandle); diff --git a/rEFIt_UEFI/refit/main.cpp b/rEFIt_UEFI/refit/main.cpp index fb19b8eb7..63fed31dd 100644 --- a/rEFIt_UEFI/refit/main.cpp +++ b/rEFIt_UEFI/refit/main.cpp @@ -39,12 +39,14 @@ #include #include "../cpp_foundation/XString.h" +#include "../cpp_lib/MemoryTracker.h" #include "../cpp_unit_test/all_tests.h" #include "../entry_scan/entry_scan.h" #include "../libeg/nanosvg.h" #include "../gui/menu_items/menu_globals.h" #include "menu.h" +#include "../Platform/Utils.h" #include "../Platform/Settings.h" #include "../Platform/DataHubCpu.h" #include "../Platform/Events.h" @@ -140,49 +142,108 @@ extern EFI_DXE_SERVICES *gDS; VOID -PrintMemoryMap () +PrintMemoryMap() { EFI_MEMORY_DESCRIPTOR *MemMap; - EFI_MEMORY_DESCRIPTOR *MemMapPtr; UINTN MemMapSize; UINTN MapKey, DescriptorSize; UINT32 DescriptorVersion; - UINT64 Bytes; EFI_STATUS Status; MemMapSize = 0; MemMap = NULL; DescriptorSize = 0; Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion); -// ASSERT (Status == EFI_BUFFER_TOO_SMALL); - MemMapSize += EFI_PAGE_SIZE; if (Status != EFI_BUFFER_TOO_SMALL) { - DBG("GetMemStatus=%s not EFI_BUFFER_TOO_SMALL\n", efiStrError(Status)); + DBG("PrintMemoryMap: GetMemStatus=%s not EFI_BUFFER_TOO_SMALL\n", efiStrError(Status)); + return; } - Status = gBS->AllocatePool (EfiBootServicesData, MemMapSize, (void**)&MemMap); -// ASSERT (Status == EFI_SUCCESS); - if (EFI_ERROR(Status)) return; + MemMapSize += EFI_PAGE_SIZE; + MemMap = (EFI_MEMORY_DESCRIPTOR*)AllocatePool(MemMapSize); + Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion); -// ASSERT (Status == EFI_SUCCESS); - MemMapPtr = MemMap; - DBG("GetMemStatus=%s\n", efiStrError(Status)); -// ASSERT (DescriptorVersion == EFI_MEMORY_DESCRIPTOR_VERSION); - - for (UINTN Index = 0; Index < MemMapSize / DescriptorSize; Index ++) { - Bytes = LShiftU64 (MemMap->NumberOfPages, 12); - DBG ("%016llX-%016llX %08llX %02llX %02llX\n", - MemMap->PhysicalStart, - MemMap->PhysicalStart + Bytes - 1, - MemMap->NumberOfPages, - MemMap->Attribute, - (UINTN)MemMap->Type); - MemMap = (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemMap + DescriptorSize); + if ( EFI_ERROR(Status) ) { + DBG("PrintMemoryMap: GetMemoryMap failed=%s\n", efiStrError(Status)); + return; } - gBS->FreePool(MemMapPtr); + OcPrintMemoryMap(MemMapSize, MemMap, DescriptorSize); + + gBS->FreePool(MemMap); } +void AllocSmallBlocks(UINTN NumberOfPagesMax) +{ + EFI_MEMORY_DESCRIPTOR *MemMap; + UINTN MemMapSize; + UINTN MapKey, DescriptorSize; + UINT32 DescriptorVersion; + EFI_STATUS Status; + + + MemMapSize = 0; + MemMap = NULL; + DescriptorSize = 0; + Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion); + if (Status != EFI_BUFFER_TOO_SMALL) { + DBG("PrintMemoryMap: GetMemStatus=%s not EFI_BUFFER_TOO_SMALL\n", efiStrError(Status)); + return; + } + MemMapSize += EFI_PAGE_SIZE; + MemMap = (EFI_MEMORY_DESCRIPTOR*)AllocatePool(MemMapSize); + + Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion); + if ( EFI_ERROR(Status) ) { + DBG("PrintMemoryMap: GetMemoryMap failed=%s\n", efiStrError(Status)); + return; + } + + for ( + EFI_MEMORY_DESCRIPTOR* EntryWalker = MemMap; + (UINT8 *)EntryWalker < ((UINT8 *)MemMap + MemMapSize); + EntryWalker = NEXT_MEMORY_DESCRIPTOR (EntryWalker, DescriptorSize)) + { + if (EntryWalker->Type != EfiConventionalMemory) { + continue; + } + + if ( (UINTN)EntryWalker->NumberOfPages < NumberOfPagesMax ) + { + gBS->AllocatePages(AllocateAddress, EfiBootServicesData, EntryWalker->NumberOfPages, &EntryWalker->PhysicalStart); + } + } + + FreePool(MemMap); +} + +void AllocSmallBlocks() +{ + CONST EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + +PrintMemoryMap(); + + UINTN size = 64; + UINTN nb = 0; + + AllocSmallBlocks(size); // 252KB + + MemoryAttributesTable = OcGetMemoryAttributes (NULL); + nb = MemoryAttributesTable->NumberOfEntries; + +PrintMemoryMap(); + while ( size <= 2048 && nb > 100 ) { // XNU seems to handle max 128 entries. So let's shrink a little bit under 128 + size *= 2; + AllocSmallBlocks(size); + MemoryAttributesTable = OcGetMemoryAttributes (NULL); + nb = MemoryAttributesTable->NumberOfEntries; +PrintMemoryMap(); + } + if ( size > 2048 ) { + DBG("Cannot shrink memory map enough. Nb entries = %lld\n", nb); + } +} + static EFI_STATUS LoadEFIImageList(IN EFI_DEVICE_PATH **DevicePaths, IN CONST XStringW& ImageTitle, @@ -1218,7 +1279,6 @@ void LOADER_ENTRY::StartLoader() mOpenCoreConfiguration.Uefi.Output.ProvideConsoleGop = gSettings.GUI.ProvideConsoleGop; OC_STRING_ASSIGN(mOpenCoreConfiguration.Uefi.Output.Resolution, XString8(gSettings.GUI.ScreenResolution).c_str()); - if ( OpenRuntimeEfiName.notEmpty() ) { XStringW FileName = SWPrintf("%ls\\%ls\\%ls", self.getCloverDirFullPath().wc_str(), getDriversPath().wc_str(), OpenRuntimeEfiName.wc_str()); EFI_HANDLE DriverHandle; @@ -1663,14 +1723,13 @@ void LOADER_ENTRY::StartLoader() mOpenCoreConfiguration.Kernel.Quirks.ProvideCurrentCpuInfo); -// EFI_STATUS sta = gBS->FreePages(SomePages, 90000); -// DBG("free 90000 pages status=%s\n", efiStrError(sta)); - PrintMemoryMap(); - DBG("Closing log\n"); - if (SavePreBootLog) { - Status = SaveBooterLog(&self.getCloverDir(), PREBOOT_LOG); - } + DBG("Closing log\n"); + if (SavePreBootLog) { + Status = SaveBooterLog(&self.getCloverDir(), PREBOOT_LOG); + } + AllocSmallBlocks(); // shrink memory map; + PrintMemoryMap(); displayFreeMemory("Just before launching image"_XS8); Status = gBS->StartImage (ImageHandle, 0, NULL); // point to OcStartImage from OC @@ -1740,7 +1799,7 @@ void LEGACY_ENTRY::StartLegacy() egClearScreen(&MenuBackgroundPixel); BeginExternalScreen(true/*, L"Booting Legacy OS"*/); XImage BootLogoX; - BootLogoX.LoadXImage(&ThemeX->getThemeDir(), Volume->LegacyOS->IconName); + BootLogoX.LoadXImage(&ThemeX->getThemeDir(), Volume->LegacyOS.IconName); BootLogoX.Draw((UGAWidth - BootLogoX.GetWidth()) >> 1, (UGAHeight - BootLogoX.GetHeight()) >> 1); @@ -1907,7 +1966,7 @@ static void ScanDriverDir(IN CONST CHAR16 *Path, OUT EFI_HANDLE **DriversToConne #undef BOOLEAN_AT_INDEX XStringW FileName = SWPrintf("%ls\\%ls\\%ls", self.getCloverDirFullPath().wc_str(), Path, DirEntry->FileName); - Status = StartEFIImage(FileDevicePath(self.getSelfLoadedImage().DeviceHandle, FileName), NullXString8Array, LStringW(DirEntry->FileName), XStringW().takeValueFrom(DirEntry->FileName), NULL, &DriverHandle); + Status = StartEFIImage(apd(FileDevicePath(self.getSelfLoadedImage().DeviceHandle, FileName)), NullXString8Array, LStringW(DirEntry->FileName), XStringW().takeValueFrom(DirEntry->FileName), NULL, &DriverHandle); if (EFI_ERROR(Status)) { continue; } @@ -1931,6 +1990,7 @@ static void ScanDriverDir(IN CONST CHAR16 *Path, OUT EFI_HANDLE **DriversToConne if (!EFI_ERROR(Status) && DriverBinding != NULL) { DBG(" - driver needs connecting\n"); // standard UEFI driver - we would reconnect after loading - add to array + MemoryStopRecord msr; // DriversArr won't be deallocated because it's passed to RegisterDriversToHighestPriority that keeps it global. if (DriversArrSize == 0) { // new array DriversArrSize = 16; @@ -2260,6 +2320,7 @@ static void LoadDrivers(void) #else ScanDriverDir(L"drivers32", &DriversToConnect, &DriversToConnectNum); #endif + // DriversToConnect is allocated by ScanDriverDir, but DO NOT free it. RegisterDriversToHighestPriority will store it in the global var mPriorityDrivers. VBiosPatchNeeded = gSettings.Graphics.PatchVBios || gSettings.Graphics.PatchVBiosBytes.getVBIOS_PATCH_BYTES_count() > 0; if (VBiosPatchNeeded) { @@ -2622,11 +2683,11 @@ GetListOfThemes () } // -// main entry point +// secondary main entry point // EFI_STATUS EFIAPI -RefitMain (IN EFI_HANDLE ImageHandle, +RefitMainMain (IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable) { EFI_STATUS Status; @@ -2660,7 +2721,7 @@ RefitMain (IN EFI_HANDLE ImageHandle, //#define DEBUG_ERALY_CRASH #ifdef DEBUG_ERALY_CRASH SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Step1"); - PauseForKey("press any key\n"_XS8); +// PauseForKey(); #endif #ifdef DEBUG_ON_SERIAL_PORT @@ -2673,7 +2734,7 @@ RefitMain (IN EFI_HANDLE ImageHandle, #ifdef DEBUG_ERALY_CRASH SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Step2"); - PauseForKey("press any key\n"_XS8); +// PauseForKey(); #endif // if ( !EFI_ERROR(Status) ) { @@ -2685,18 +2746,18 @@ RefitMain (IN EFI_HANDLE ImageHandle, DBG("Clover ImageHandle = %llx\n", (uintptr_t)ImageHandle); #ifdef DEBUG_ERALY_CRASH SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Step3"); - PauseForKey("press any key\n"_XS8); +// PauseForKey(); #endif } #ifdef JIEF_DEBUG - gBS->Stall(2500000); // to give time to gdb to connect -// PauseForKey("press\n"_XS8); + gBS->Stall(5500000); // to give time to gdb to connect +// PauseForKey(); #endif } #ifdef DEBUG_ERALY_CRASH SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Step4"); - PauseForKey("press any key\n"_XS8); +// PauseForKey(); #endif #ifdef CLOVER_BUILD @@ -2705,11 +2766,13 @@ RefitMain (IN EFI_HANDLE ImageHandle, construct_globals_objects(gImageHandle); #endif + MemoryTrackerInstallHook(); + gCPUStructure.TSCCalibr = GetMemLogTscTicksPerSecond(); //ticks for 1second #ifdef DEBUG_ERALY_CRASH SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Step5"); - PauseForKey("press any key\n"_XS8); + PauseForKey(); #endif #ifdef JIEF_DEBUG @@ -2725,7 +2788,7 @@ RefitMain (IN EFI_HANDLE ImageHandle, #ifdef DEBUG_ERALY_CRASH SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Step6"); - PauseForKey("press any key\n"_XS8); + PauseForKey(); #endif // firmware detection @@ -3149,7 +3212,9 @@ RefitMain (IN EFI_HANDLE ImageHandle, gSettings.Boot.FastBoot = false; //Hmm... will never be here } #ifdef JIEF_DEBUG -MainMenu.TimeoutSeconds=60; +MainMenu.TimeoutSeconds=1; +DefaultEntry = NULL; +DefaultIndex = MainMenu.Entries.length()-1; // this should be "Exit Clover" #endif AfterTool = false; gEvent = 0; //clear to cancel loop @@ -3458,6 +3523,9 @@ log_technical_bug("not done yet"); UninitializeConsoleSim (); + delete ThemeX; // do this before destruct_globals_objects() + FreePool(BlankLine); // Convert BlankLine to XStringW instead. + #ifdef CLOVER_BUILD destruct_globals_objects(NULL); #endif @@ -3465,4 +3533,27 @@ log_technical_bug("not done yet"); return EFI_SUCCESS; } +// +// main entry point +// +EFI_STATUS +EFIAPI +RefitMain (IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable) +{ + MemoryTrackerInit(); + + EFI_STATUS Status = RefitMainMain(ImageHandle, SystemTable); + + + DBG("MT_alloc_count=%lld\n", MT_getAllocCount()); + MT_outputDanglingPtr(); + +#ifdef TagStruct_COUNT_CACHEHIT + DBG("cache hits = %zu\n", TagStruct::cachehit); + DBG("cache miss = %zu\n", TagStruct::cachemiss); +#endif + + return Status; +} diff --git a/rEFIt_UEFI/refit/menu.cpp b/rEFIt_UEFI/refit/menu.cpp index 39ff6f826..2ddfff65d 100644 --- a/rEFIt_UEFI/refit/menu.cpp +++ b/rEFIt_UEFI/refit/menu.cpp @@ -109,9 +109,6 @@ void FillInputs(XBool New) tmp[40] = 0; //make it null-terminated UINTN InputItemsCount = 0; - if (New) { - InputItems = new INPUT_ITEM[135]; - } InputItems[InputItemsCount].ItemType = ASString; //0 //even though Ascii we will keep value as Unicode to convert later