Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Aug 4, 2024
1 parent 4a856e7 commit 4e3d2d0
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 56 deletions.
55 changes: 27 additions & 28 deletions far/PluginA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "platform.version.hpp"

// Common:
#include "common/algorithm.hpp"
#include "common/function_ref.hpp"
#include "common/null_iterator.hpp"
#include "common/scope_exit.hpp"
Expand Down Expand Up @@ -775,16 +774,16 @@ static void ConvertPanelItemToAnsi(const PluginPanelItem &PanelItem, oldfar::Plu
*PanelItemA.FindData.cAlternateFileName = {};
}

static oldfar::PluginPanelItem* ConvertPanelItemsArrayToAnsi(const PluginPanelItem *PanelItemW, size_t ItemsNumber)
static std::span<oldfar::PluginPanelItem> ConvertPanelItemsArrayToAnsi(std::span<PluginPanelItem const> const Items)
{
auto Result = std::make_unique<oldfar::PluginPanelItem[]>(ItemsNumber);
auto Result = std::make_unique<oldfar::PluginPanelItem[]>(Items.size());

for (const auto& [Item, AnsiItem]: zip(std::span(PanelItemW, ItemsNumber), std::span(Result.get(), ItemsNumber)))
for (const auto& [Item, AnsiItem]: zip(Items, std::span(Result.get(), Items.size())))
{
ConvertPanelItemToAnsi(Item, AnsiItem);
}

return Result.release();
return { Result.release(), Items.size() };
}

static void FreeUnicodePanelItem(PluginPanelItem *PanelItem, size_t ItemsNumber)
Expand Down Expand Up @@ -5195,17 +5194,17 @@ WARNING_POP()
if (exception_handling_in_progress() || !has(es))
return es;

const auto PanelItemA = ConvertPanelItemsArrayToAnsi(Info->PanelItem, Info->ItemsNumber);
const auto PanelItemA = ConvertPanelItemsArrayToAnsi({ Info->PanelItem, Info->ItemsNumber });
char DestA[oldfar::NM];
(void)encoding::oem::get_bytes(Info->DestPath, DestA);
int OpMode = 0;
SecondFlagsToFirst(Info->OpMode, OpMode, OperationModesMap);
ExecuteFunction(es, Info->hPanel, PanelItemA, static_cast<int>(Info->ItemsNumber), Info->Move, DestA, OpMode);
UpdatePluginPanelItemFlags(PanelItemA, Info->PanelItem, Info->ItemsNumber);
ExecuteFunction(es, Info->hPanel, PanelItemA.data(), static_cast<int>(PanelItemA.size()), Info->Move, DestA, OpMode);
UpdatePluginPanelItemFlags(PanelItemA.data(), Info->PanelItem, Info->ItemsNumber);
static wchar_t DestW[oldfar::NM];
(void)encoding::oem::get_chars(DestA, DestW);
Info->DestPath = DestW;
FreePanelItemA({ PanelItemA, Info->ItemsNumber });
FreePanelItemA(PanelItemA);
return es;
}

Expand All @@ -5215,12 +5214,12 @@ WARNING_POP()
if (exception_handling_in_progress() || !has(es))
return es;

const auto PanelItemA = ConvertPanelItemsArrayToAnsi(Info->PanelItem, Info->ItemsNumber);
const auto PanelItemA = ConvertPanelItemsArrayToAnsi({ Info->PanelItem, Info->ItemsNumber });
int OpMode = 0;
SecondFlagsToFirst(Info->OpMode, OpMode, OperationModesMap);
ExecuteFunction(es, Info->hPanel, PanelItemA, static_cast<int>(Info->ItemsNumber), Info->Move, OpMode);
UpdatePluginPanelItemFlags(PanelItemA, Info->PanelItem, Info->ItemsNumber);
FreePanelItemA({ PanelItemA, Info->ItemsNumber });
ExecuteFunction(es, Info->hPanel, PanelItemA.data(), static_cast<int>(PanelItemA.size()), Info->Move, OpMode);
UpdatePluginPanelItemFlags(PanelItemA.data(), Info->PanelItem, Info->ItemsNumber);
FreePanelItemA(PanelItemA);
return es;
}

Expand All @@ -5230,12 +5229,12 @@ WARNING_POP()
if (exception_handling_in_progress() || !has(es))
return es;

const auto PanelItemA = ConvertPanelItemsArrayToAnsi(Info->PanelItem, Info->ItemsNumber);
const auto PanelItemA = ConvertPanelItemsArrayToAnsi({ Info->PanelItem, Info->ItemsNumber });
int OpMode = 0;
SecondFlagsToFirst(Info->OpMode, OpMode, OperationModesMap);
ExecuteFunction(es, Info->hPanel, PanelItemA, static_cast<int>(Info->ItemsNumber), OpMode);
UpdatePluginPanelItemFlags(PanelItemA, Info->PanelItem, Info->ItemsNumber);
FreePanelItemA({ PanelItemA, Info->ItemsNumber });
ExecuteFunction(es, Info->hPanel, PanelItemA.data(), static_cast<int>(PanelItemA.size()), OpMode);
UpdatePluginPanelItemFlags(PanelItemA.data(), Info->PanelItem, Info->ItemsNumber);
FreePanelItemA(PanelItemA);
return es;
}

Expand All @@ -5262,11 +5261,11 @@ WARNING_POP()
if (exception_handling_in_progress() || !has(es))
return es;

const auto PanelItemA = ConvertPanelItemsArrayToAnsi(Info->PanelItem, Info->ItemsNumber);
const auto PanelItemA = ConvertPanelItemsArrayToAnsi({ Info->PanelItem, Info->ItemsNumber });
int OpMode = 0;
SecondFlagsToFirst(Info->OpMode, OpMode, OperationModesMap);
ExecuteFunction(es, Info->hPanel, PanelItemA, static_cast<int>(Info->ItemsNumber), OpMode);
FreePanelItemA({ PanelItemA, Info->ItemsNumber });
ExecuteFunction(es, Info->hPanel, PanelItemA.data(), static_cast<int>(PanelItemA.size()), OpMode);
FreePanelItemA(PanelItemA);
return es;
}

Expand All @@ -5276,9 +5275,9 @@ WARNING_POP()
if (exception_handling_in_progress() || !has(es))
return es;

const auto PanelItemA = ConvertPanelItemsArrayToAnsi(Info->PanelItem, Info->ItemsNumber);
ExecuteFunction(es, Info->hPanel, PanelItemA, static_cast<int>(Info->ItemsNumber));
FreePanelItemA({ PanelItemA, Info->ItemsNumber });
const auto PanelItemA = ConvertPanelItemsArrayToAnsi({ Info->PanelItem, Info->ItemsNumber });
ExecuteFunction(es, Info->hPanel, PanelItemA.data(), static_cast<int>(PanelItemA.size()));
FreePanelItemA(PanelItemA);
return es;
}

Expand Down Expand Up @@ -5372,11 +5371,11 @@ WARNING_POP()
if (exception_handling_in_progress() || !has(es))
return es;

const auto Item1A = ConvertPanelItemsArrayToAnsi(Info->Item1, 1);
const auto Item2A = ConvertPanelItemsArrayToAnsi(Info->Item2, 1);
ExecuteFunction(es, Info->hPanel, Item1A, Item2A, Info->Mode);
FreePanelItemA({ Item1A, 1 });
FreePanelItemA({ Item2A, 1 });
const auto Item1A = ConvertPanelItemsArrayToAnsi({ Info->Item1, 1 });
const auto Item2A = ConvertPanelItemsArrayToAnsi({ Info->Item2, 1 });
ExecuteFunction(es, Info->hPanel, Item1A.data(), Item2A.data(), Info->Mode);
FreePanelItemA(Item1A);
FreePanelItemA(Item2A);
return es;
}

Expand Down
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 2024-08-04 20:51:46+01:00 - build 6354

1. Refactoring.

--------------------------------------------------------------------------------
drkns 2024-07-28 14:35:18+01:00 - build 6353

Expand Down
6 changes: 6 additions & 0 deletions far/encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ size_t MultibyteCodepageDecoder::GetChar(std::string_view const Str, wchar_t& Ch
}
}

char raw_eol::to(uintptr_t Codepage, wchar_t WideChar)
{
char Char;
return encoding::get_bytes(Codepage, { &WideChar, 1 }, { &Char, 1 })? Char : WideChar;
}

static size_t utf8_get_bytes(string_view Str, std::span<char> Buffer);

static size_t get_bytes_impl(uintptr_t const Codepage, string_view const Str, std::span<char> Buffer, encoding::diagnostics* const Diagnostics)
Expand Down
6 changes: 1 addition & 5 deletions far/encoding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,7 @@ class [[nodiscard]] raw_eol
[[nodiscard]] auto lf() const { return m_Lf; }

private:
static char to(uintptr_t Codepage, wchar_t WideChar)
{
char Char;
return encoding::get_bytes(Codepage, { &WideChar, 1 }, { &Char, 1 })? Char : WideChar;
}
static char to(uintptr_t Codepage, wchar_t WideChar);

char
m_Cr{'\r'},
Expand Down
30 changes: 18 additions & 12 deletions far/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "log.hpp"
#include "char_width.hpp"
#include "string_sort.hpp"
#include "datetime.hpp"

// Platform:
#include "platform.hpp"
Expand Down Expand Up @@ -512,19 +513,24 @@ static void log_process_exit_code(execute_info const& Info, os::handle const& Pr
return;
}

LOG(
ExitCode == EXIT_SUCCESS?
logging::level::debug :
logging::level::warning,
L"Exit code: {}"sv,
ExitCode
);
string ElapsedTime;
if (os::chrono::time_point CreationTime; os::chrono::get_process_creation_time(Process.native_handle(), CreationTime))
ElapsedTime = ConvertDurationToHMS(os::chrono::nt_clock::now() - CreationTime);
else
LOGWARNING(L"get_process_creation_time(): {}"sv, os::last_error());

if (ExitCode != EXIT_SUCCESS && ExitCode != EXIT_FAILURE)
{
LOGWARNING(L"{}"sv, os::format_error(ExitCode));
LOGWARNING(L"{}"sv, os::format_ntstatus(ExitCode));
}
if (ExitCode == EXIT_SUCCESS)
LOGINFO(L"Command [{}] took {}"sv, Info.Command, ElapsedTime);
else
LOGWARNING(
L"Command [{}] took {} and failed (exit code {}{})"sv,
Info.Command,
ElapsedTime,
ExitCode,
ExitCode == EXIT_FAILURE?
L""sv :
far::format(L", {}"sv, os::error_state{ExitCode, static_cast<NTSTATUS>(ExitCode)}.to_string())
);

console.command_finished(ExitCode);

Expand Down
3 changes: 3 additions & 0 deletions far/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ namespace

auto get_thread_id()
{
if (const auto ThreadName = os::debug::get_thread_name(GetCurrentThread()); !ThreadName.empty())
return far::format(L"{}|{}"sv, GetCurrentThreadId(), ThreadName);

return str(GetCurrentThreadId());
}

Expand Down
15 changes: 14 additions & 1 deletion far/memcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ class checker

m_AllocatedMemorySize += Block->TotalSize;
m_AllocatedPayloadSize += Block->DataSize;

++m_OverallAllocations;
m_OverallSize += Block->DataSize;
}

void unregister_block(memory_block const* const Block)
Expand Down Expand Up @@ -314,6 +317,11 @@ class checker

void print_summary() const
{
if constexpr ((false))
{
std::wcout << far::format(L"\nAllocations: {}\nSize: {}\n"sv, m_OverallAllocations, m_OverallSize);
}

if (!m_AllocatedMemorySize)
return;

Expand Down Expand Up @@ -360,7 +368,7 @@ class checker
Message = concat(
L"--------------------------------------------------------------------------------\n"sv,
str(Data), L", "sv, format_type(i->AllocationType, Size),
L"\nData: "sv, BlobToHexString({ static_cast<std::byte const*>(Data), std::min(Size, Width / 3) }, L' '),
L"\nData: "sv, BlobToHexString(view_bytes(Data, std::min(Size, Width / 3)), L' '),
L"\nAnsi: "sv, printable_ansi_string(Data, std::min(Size, Width)),
L"\nWide: "sv, printable_wide_string(Data, std::min(Size, Width * sizeof(wchar_t))),
L"\nStack:\n"sv);
Expand All @@ -384,11 +392,16 @@ class checker

os::critical_section m_CS;

// These can go up and down and should be 0 in the end
intptr_t m_CallNewDeleteVector{};
intptr_t m_CallNewDeleteScalar{};
size_t m_AllocatedMemorySize{};
size_t m_AllocatedPayloadSize{};

// These can only grow and can be used for a rough performance estimation
size_t m_OverallAllocations{};
size_t m_OverallSize{};

bool m_Enabled{true};
};

Expand Down
22 changes: 14 additions & 8 deletions far/platform.clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "encoding.hpp"
#include "eol.hpp"
#include "log.hpp"
#include "wm_listener.hpp"

// Platform:
#include "platform.chrono.hpp"
Expand Down Expand Up @@ -163,15 +164,22 @@ namespace os::clipboard
bool open()
{
// Clipboard is a shared resource
const size_t Attempts = 5;
const auto Attempts = 5uz;
const auto Delay = 100ms;

const auto ServiceWindow = wm_listener::service_window();
const auto NewOwner = ServiceWindow? ServiceWindow : console.GetWindow();

error_state Error;

for (const auto i: std::views::iota(0uz, Attempts))
{
// TODO: this is bad, we should use a real window handle
if (OpenClipboard(console.GetWindow()))
if (OpenClipboard(NewOwner))
return true;

const auto Error = last_error();
Error = last_error();

LOGDEBUG(L"OpenClipboard(): {}"sv, Error);

if (Error.Win32Error == ERROR_ACCESS_DENIED)
{
Expand All @@ -185,12 +193,10 @@ namespace os::clipboard
}
}

LOGDEBUG(L"OpenClipboard(): {}"sv, Error);

os::chrono::sleep_for((i + 1) * 50ms);
chrono::sleep_for((i + 1) * Delay);
}

LOGERROR(L"OpenClipboard(): {}"sv, last_error());
LOGERROR(L"OpenClipboard(): {}"sv, Error);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6353
6354
5 changes: 5 additions & 0 deletions far/wm_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ void wm_listener::disable_power_notifications()
m_PowerNotify.reset();
}

HWND wm_listener::service_window()
{
return m_Hwnd;
}

wm_listener::wm_listener()
{
os::event ReadyEvent(os::event::type::automatic, os::event::state::nonsignaled);
Expand Down
4 changes: 3 additions & 1 deletion far/wm_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class wm_listener: noncopyable
void enable_power_notifications();
void disable_power_notifications();

static HWND service_window();

private:
void WindowThreadRoutine(const os::event& ReadyEvent);

HWND m_Hwnd{};
static inline HWND m_Hwnd{};
std::exception_ptr m_ExceptionPtr;
os::thread m_Thread;
struct powernotify_deleter
Expand Down

0 comments on commit 4e3d2d0

Please sign in to comment.