From 314f411b13588207d6f8bceadc2d3121b5a8b6e6 Mon Sep 17 00:00:00 2001 From: AJR Date: Sat, 16 Feb 2019 21:27:06 -0500 Subject: [PATCH] Eliminate qsort usage in debugger (nw) --- src/emu/debug/debugcmd.cpp | 12 +++---- src/emu/debug/dvbpoints.cpp | 51 +++++++++++----------------- src/emu/debug/dvbpoints.h | 2 +- src/emu/debug/dvwpoints.cpp | 67 ++++++++++++++----------------------- src/emu/debug/dvwpoints.h | 2 +- 5 files changed, 52 insertions(+), 82 deletions(-) diff --git a/src/emu/debug/debugcmd.cpp b/src/emu/debug/debugcmd.cpp index 05fb6acbddd..7f650a2e356 100644 --- a/src/emu/debug/debugcmd.cpp +++ b/src/emu/debug/debugcmd.cpp @@ -21,6 +21,7 @@ #include "natkeyboard.h" #include "render.h" #include +#include #include @@ -3215,13 +3216,6 @@ void debugger_commands::execute_memdump(int ref, const std::vector execute_symlist - execute the symlist command -------------------------------------------------*/ -static int CLIB_DECL symbol_sort_compare(const void *item1, const void *item2) -{ - const char *str1 = *(const char **)item1; - const char *str2 = *(const char **)item2; - return strcmp(str1, str2); -} - void debugger_commands::execute_symlist(int ref, const std::vector ¶ms) { device_t *cpu = nullptr; @@ -3257,7 +3251,9 @@ void debugger_commands::execute_symlist(int ref, const std::vector /* sort the symbols */ if (count > 1) - qsort((void *)namelist, count, sizeof(namelist[0]), symbol_sort_compare); + std::sort(&namelist[0], &namelist[count], [](const char *item1, const char *item2) { + return strcmp(item1, item2) < 0; + }); /* iterate over symbols and print out relevant ones */ for (symnum = 0; symnum < count; symnum++) diff --git a/src/emu/debug/dvbpoints.cpp b/src/emu/debug/dvbpoints.cpp index 6bc4a008c5f..3b76c7d26fe 100644 --- a/src/emu/debug/dvbpoints.cpp +++ b/src/emu/debug/dvbpoints.cpp @@ -12,79 +12,68 @@ #include "debugger.h" #include "dvbpoints.h" +#include #include // Sorting functors for the qsort function -static int cIndexAscending(const void* a, const void* b) +static bool cIndexAscending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { - const device_debug::breakpoint* left = *(device_debug::breakpoint**)a; - const device_debug::breakpoint* right = *(device_debug::breakpoint**)b; - return left->index() - right->index(); + return a->index() < b->index(); } -static int cIndexDescending(const void* a, const void* b) +static bool cIndexDescending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { return cIndexAscending(b, a); } -static int cEnabledAscending(const void* a, const void* b) +static bool cEnabledAscending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { - const device_debug::breakpoint* left = *(device_debug::breakpoint**)a; - const device_debug::breakpoint* right = *(device_debug::breakpoint**)b; - return (left->enabled() ? 1 : 0) - (right->enabled() ? 1 : 0); + return !a->enabled() && b->enabled(); } -static int cEnabledDescending(const void* a, const void* b) +static bool cEnabledDescending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { return cEnabledAscending(b, a); } -static int cCpuAscending(const void* a, const void* b) +static bool cCpuAscending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { - const device_debug::breakpoint* left = *(device_debug::breakpoint**)a; - const device_debug::breakpoint* right = *(device_debug::breakpoint**)b; - return strcmp(left->debugInterface()->device().tag(), right->debugInterface()->device().tag()); + return strcmp(a->debugInterface()->device().tag(), b->debugInterface()->device().tag()) < 0; } -static int cCpuDescending(const void* a, const void* b) +static bool cCpuDescending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { return cCpuAscending(b, a); } -static int cAddressAscending(const void* a, const void* b) +static bool cAddressAscending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { - const device_debug::breakpoint* left = *(device_debug::breakpoint**)a; - const device_debug::breakpoint* right = *(device_debug::breakpoint**)b; - return (left->address() > right->address()) ? 1 : (left->address() < right->address()) ? -1 : 0; + return a->address() < b->address(); } -static int cAddressDescending(const void* a, const void* b) +static bool cAddressDescending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { return cAddressAscending(b, a); } -static int cConditionAscending(const void* a, const void* b) +static bool cConditionAscending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { - const device_debug::breakpoint* left = *(device_debug::breakpoint**)a; - const device_debug::breakpoint* right = *(device_debug::breakpoint**)b; - return strcmp(left->condition(), right->condition()); + return strcmp(a->condition(), b->condition()) < 0; } -static int cConditionDescending(const void* a, const void* b) +static bool cConditionDescending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { return cConditionAscending(b, a); } -static int cActionAscending(const void* a, const void* b) +static bool cActionAscending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { - const device_debug::breakpoint* left = *(device_debug::breakpoint**)a; - const device_debug::breakpoint* right = *(device_debug::breakpoint**)b; - return strcmp(left->action(), right->action()); + return strcmp(a->action(), b->action()) < 0; } -static int cActionDescending(const void* a, const void* b) +static bool cActionDescending(const device_debug::breakpoint *a, const device_debug::breakpoint *b) { return cActionAscending(b, a); } @@ -210,7 +199,7 @@ void debug_view_breakpoints::gather_breakpoints() // And now for the sort if (!m_buffer.empty()) - qsort(&m_buffer[0], m_buffer.size(), sizeof(device_debug::breakpoint *), m_sortType); + std::stable_sort(m_buffer.begin(), m_buffer.end(), m_sortType); } diff --git a/src/emu/debug/dvbpoints.h b/src/emu/debug/dvbpoints.h index 5d74549fd66..2d3ae38fa58 100644 --- a/src/emu/debug/dvbpoints.h +++ b/src/emu/debug/dvbpoints.h @@ -48,7 +48,7 @@ private: // internal state - int (*m_sortType)(void const *, void const *); + bool (*m_sortType)(const device_debug::breakpoint *, const device_debug::breakpoint *); std::vector m_buffer; }; diff --git a/src/emu/debug/dvwpoints.cpp b/src/emu/debug/dvwpoints.cpp index 2ab1fc6a0bf..b0584b5d61c 100644 --- a/src/emu/debug/dvwpoints.cpp +++ b/src/emu/debug/dvwpoints.cpp @@ -11,102 +11,87 @@ #include "emu.h" #include "dvwpoints.h" +#include #include -static int cIndexAscending(const void* a, const void* b) +static bool cIndexAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return left->index() - right->index(); + return a->index() < b->index(); } -static int cIndexDescending(const void* a, const void* b) +static bool cIndexDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cIndexAscending(b, a); } -static int cEnabledAscending(const void* a, const void* b) +static bool cEnabledAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return (left->enabled() ? 1 : 0) - (right->enabled() ? 1 : 0); + return !a->enabled() && b->enabled(); } -static int cEnabledDescending(const void* a, const void* b) +static bool cEnabledDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cEnabledAscending(b, a); } -static int cCpuAscending(const void* a, const void* b) +static bool cCpuAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return strcmp(left->debugInterface()->device().tag(), right->debugInterface()->device().tag()); + return strcmp(a->debugInterface()->device().tag(), b->debugInterface()->device().tag()) < 0; } -static int cCpuDescending(const void* a, const void* b) +static bool cCpuDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cCpuAscending(b, a); } -static int cSpaceAscending(const void* a, const void* b) +static bool cSpaceAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return strcmp(left->space().name(), right->space().name()); + return strcmp(a->space().name(), b->space().name()) < 0; } -static int cSpaceDescending(const void* a, const void* b) +static bool cSpaceDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cSpaceAscending(b, a); } -static int cAddressAscending(const void* a, const void* b) +static bool cAddressAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return (left->address() > right->address()) ? 1 : (left->address() < right->address()) ? -1 : 0; + return a->address() < b->address(); } -static int cAddressDescending(const void* a, const void* b) +static bool cAddressDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cAddressAscending(b, a); } -static int cTypeAscending(const void* a, const void* b) +static bool cTypeAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return int(left->type()) - int(right->type()); + return int(a->type()) < int(b->type()); } -static int cTypeDescending(const void* a, const void* b) +static bool cTypeDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cTypeAscending(b, a); } -static int cConditionAscending(const void* a, const void* b) +static bool cConditionAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return strcmp(left->condition(), right->condition()); + return strcmp(a->condition(), b->condition()) < 0; } -static int cConditionDescending(const void* a, const void* b) +static bool cConditionDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cConditionAscending(b, a); } -static int cActionAscending(const void* a, const void* b) +static bool cActionAscending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { - const device_debug::watchpoint* left = *(device_debug::watchpoint**)a; - const device_debug::watchpoint* right = *(device_debug::watchpoint**)b; - return left->action().compare(right->action()); + return a->action() < b->action(); } -static int cActionDescending(const void* a, const void* b) +static bool cActionDescending(const device_debug::watchpoint *a, const device_debug::watchpoint *b) { return cActionAscending(b, a); } @@ -236,7 +221,7 @@ void debug_view_watchpoints::gather_watchpoints() // And now for the sort if (!m_buffer.empty()) - qsort(&m_buffer[0], m_buffer.size(), sizeof(device_debug::watchpoint *), m_sortType); + std::stable_sort(m_buffer.begin(), m_buffer.end(), m_sortType); } diff --git a/src/emu/debug/dvwpoints.h b/src/emu/debug/dvwpoints.h index b1707b7d5b2..26b7c676e0e 100644 --- a/src/emu/debug/dvwpoints.h +++ b/src/emu/debug/dvwpoints.h @@ -43,7 +43,7 @@ private: // internal state - int (*m_sortType)(void const *, void const *); + bool (*m_sortType)(const device_debug::watchpoint *, const device_debug::watchpoint *); std::vector m_buffer; };