Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assorted LLVM inspired changes #930

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
40 changes: 24 additions & 16 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static void print_help_hint() {
}

//* A simple argument parser
void argumentParser(const int argc, char **argv) {
static void argumentParser(const int argc, char **argv) {
for(int i = 1; i < argc; i++) {
const string argument = argv[i];
if (is_in(argument, "-h", "--help")) {
Expand Down Expand Up @@ -301,7 +301,8 @@ void term_resize(bool force) {
}
}
min_size = Term::get_min_size(boxes);
minWidth = min_size.at(0), minHeight = min_size.at(1);
minWidth = min_size.at(0);
minHeight = min_size.at(1);
}
else if (not Term::refresh()) break;
}
Expand All @@ -321,8 +322,7 @@ void clean_quit(int sig) {
pthread_cancel(Runner::runner_id);
}
#else
struct timespec ts;
ts.tv_sec = 5;
constexpr struct timespec ts { .tv_sec = 5, .tv_nsec = 0 };
if (pthread_timedjoin_np(Runner::runner_id, nullptr, &ts) != 0) {
Logger::warning("Failed to join _runner thread on exit!");
pthread_cancel(Runner::runner_id);
Expand Down Expand Up @@ -359,23 +359,23 @@ void clean_quit(int sig) {
}

//* Handler for SIGTSTP; stops threads, restores terminal and sends SIGSTOP
void _sleep() {
static void _sleep() {
Runner::stop();
Term::restore();
std::raise(SIGSTOP);
}

//* Handler for SIGCONT; re-initialize terminal and force a resize event
void _resume() {
static void _resume() {
Term::init();
term_resize(true);
}

void _exit_handler() {
static void _exit_handler() {
clean_quit(-1);
}

void _signal_handler(const int sig) {
static void _signal_handler(const int sig) {
switch (sig) {
case SIGINT:
if (Runner::active) {
Expand Down Expand Up @@ -414,7 +414,7 @@ void _signal_handler(const int sig) {
}

//* Config init
void init_config(){
static void init_config(){
atomic_lock lck(Global::init_conf);
vector<string> load_warnings;
Config::load(Config::conf_file, load_warnings);
Expand Down Expand Up @@ -464,14 +464,18 @@ namespace Runner {
pthread_mutex_t& pt_mutex;
public:
int status;
thread_lock(pthread_mutex_t& mtx) : pt_mutex(mtx) {
explicit thread_lock(pthread_mutex_t& mtx) : pt_mutex(mtx) {
pthread_mutex_init(&pt_mutex, nullptr);
status = pthread_mutex_lock(&pt_mutex);
}
~thread_lock() {
~thread_lock() noexcept {
if (status == 0)
pthread_mutex_unlock(&pt_mutex);
}
thread_lock(const thread_lock& other) = delete;
thread_lock& operator=(const thread_lock& other) = delete;
thread_lock(thread_lock&& other) = delete;
thread_lock& operator=(thread_lock&& other) = delete;
};

//* Wrapper for raising privileges when using SUID bit
Expand All @@ -482,10 +486,14 @@ namespace Runner {
if (Global::real_uid != Global::set_uid)
this->status = seteuid(Global::set_uid);
}
~gain_priv() {
~gain_priv() noexcept {
if (status == 0)
status = seteuid(Global::real_uid);
}
gain_priv(const gain_priv& other) = delete;
gain_priv& operator=(const gain_priv& other) = delete;
gain_priv(gain_priv&& other) = delete;
gain_priv& operator=(gain_priv&& other) = delete;
};

string output;
Expand Down Expand Up @@ -514,8 +522,8 @@ namespace Runner {
class MyNumPunct : public std::numpunct<char>
{
protected:
virtual char do_thousands_sep() const { return '\''; }
virtual std::string do_grouping() const { return "\03"; }
virtual char do_thousands_sep() const override { return '\''; }
virtual std::string do_grouping() const override { return "\03"; }
};


Expand All @@ -530,7 +538,7 @@ namespace Runner {

struct runner_conf current_conf;

void debug_timer(const char* name, const int action) {
static void debug_timer(const char* name, const int action) {
switch (action) {
case collect_begin:
debug_times[name].at(collect) = time_micros();
Expand All @@ -555,7 +563,7 @@ namespace Runner {
}

//? ------------------------------- Secondary thread: async launcher and drawing ----------------------------------
void * _runner(void *) {
static void * _runner(void *) {
//? Block some signals in this thread to avoid deadlock from any signal handlers trying to stop this thread
sigemptyset(&mask);
// sigaddset(&mask, SIGINT);
Expand Down
6 changes: 3 additions & 3 deletions src/btop_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ namespace Config {

for (const auto& box : ssplit(preset, ',')) {
const auto& vals = ssplit(box, ':');
if (vals.at(0) == "cpu") set("cpu_bottom", (vals.at(1) == "0" ? false : true));
else if (vals.at(0) == "mem") set("mem_below_net", (vals.at(1) == "0" ? false : true));
else if (vals.at(0) == "proc") set("proc_left", (vals.at(1) == "0" ? false : true));
if (vals.at(0) == "cpu") set("cpu_bottom", (vals.at(1) != "0"));
else if (vals.at(0) == "mem") set("mem_below_net", (vals.at(1) != "0"));
else if (vals.at(0) == "proc") set("proc_left", (vals.at(1) != "0"));
set("graph_symbol_" + vals.at(0), vals.at(2));
}

Expand Down
5 changes: 2 additions & 3 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ namespace Draw {
//* Meter class ------------------------------------------------------------------------------------------------------------>
Meter::Meter() {}

Meter::Meter(const int width, const string& color_gradient, bool invert)
: width(width), color_gradient(color_gradient), invert(invert) {}
Meter::Meter(const int width, string color_gradient, bool invert)
: width(width), color_gradient(std::move(color_gradient)), invert(invert) {}

string Meter::operator()(int value) {
if (width < 1) return "";
Expand Down Expand Up @@ -2239,4 +2239,3 @@ namespace Draw {
}
}
}

6 changes: 3 additions & 3 deletions src/btop_draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ namespace Draw {
class TextEdit {
size_t pos{};
size_t upos{};
bool numeric;
bool numeric = false;
public:
string text;
TextEdit();
TextEdit(string text, bool numeric=false);
explicit TextEdit(string text, bool numeric=false);
bool command(const string& key);
string operator()(const size_t limit=0);
void clear();
Expand All @@ -92,7 +92,7 @@ namespace Draw {
array<string, 101> cache;
public:
Meter();
Meter(const int width, const string& color_gradient, bool invert = false);
Meter(const int width, string color_gradient, bool invert = false);

//* Return a string representation of the meter with given value
string operator()(int value);
Expand Down
2 changes: 1 addition & 1 deletion src/btop_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ namespace Input {
}
else if (is_in(key, "f", "/")) {
Config::flip("proc_filtering");
Proc::filter = { Config::getS("proc_filter") };
Proc::filter = Draw::TextEdit{Config::getS("proc_filter")};
old_filter = Proc::filter.text;
}
else if (key == "e") {
Expand Down
16 changes: 8 additions & 8 deletions src/btop_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ namespace Menu {
Switch
};

int signalChoose(const string& key) {
static int signalChoose(const string& key) {
auto s_pid = (Config::getB("show_detailed") and Config::getI("selected_pid") == 0 ? Config::getI("detailed_pid") : Config::getI("selected_pid"));
static int x{};
static int y{};
Expand Down Expand Up @@ -986,7 +986,7 @@ namespace Menu {
return (redraw ? Changed : retval);
}

int sizeError(const string& key) {
static int sizeError(const string& key) {
if (redraw) {
vector<string> cont_vec;
cont_vec.push_back(Fx::b + Theme::g("used")[100] + "Error:" + Theme::c("main_fg") + Fx::ub);
Expand All @@ -1008,7 +1008,7 @@ namespace Menu {
return NoChange;
}

int signalSend(const string& key) {
static int signalSend(const string& key) {
auto s_pid = (Config::getB("show_detailed") and Config::getI("selected_pid") == 0 ? Config::getI("detailed_pid") : Config::getI("selected_pid"));
if (s_pid == 0) return Closed;
if (redraw) {
Expand Down Expand Up @@ -1048,7 +1048,7 @@ namespace Menu {
return NoChange;
}

int signalReturn(const string& key) {
static int signalReturn(const string& key) {
if (redraw) {
vector<string> cont_vec;
cont_vec.push_back(Fx::b + Theme::g("used")[100] + "Failure:" + Theme::c("main_fg") + Fx::ub);
Expand Down Expand Up @@ -1080,7 +1080,7 @@ namespace Menu {
return NoChange;
}

int mainMenu(const string& key) {
static int mainMenu(const string& key) {
enum MenuItems { Options, Help, Quit };
static int y{};
static int selected{};
Expand Down Expand Up @@ -1159,7 +1159,7 @@ namespace Menu {
return (redraw ? Changed : retval);
}

int optionsMenu(const string& key) {
static int optionsMenu(const string& key) {
enum Predispositions { isBool, isInt, isString, is2D, isBrowseable, isEditable};
static int y{};
static int x{};
Expand Down Expand Up @@ -1450,7 +1450,7 @@ namespace Menu {
auto cy = y+9;
for (int c = 0, i = max(0, item_height * page); c++ < item_height and i < (int)categories[selected_cat].size(); i++) {
const auto& option = categories[selected_cat][i][0];
const auto& value = (option == "color_theme" ? (string) fs::path(Config::getS("color_theme")).stem() : Config::getAsString(option));
const auto& value = (option == "color_theme" ? fs::path(Config::getS("color_theme")).stem().string() : Config::getAsString(option));

out += Mv::to(cy++, x + 1) + (c-1 == selected ? Theme::c("selected_bg") + Theme::c("selected_fg") : Theme::c("title"))
+ Fx::b + cjust(capitalize(s_replace(option, "_", " "))
Expand Down Expand Up @@ -1511,7 +1511,7 @@ namespace Menu {
return (redraw ? Changed : retval);
}

int helpMenu(const string& key) {
static int helpMenu(const string& key) {
static int y{};
static int x{};
static int height{};
Expand Down
6 changes: 5 additions & 1 deletion src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ namespace Net {
int status;
public:
IfAddrsPtr() { status = getifaddrs(&ifaddr); }
~IfAddrsPtr() { freeifaddrs(ifaddr); }
~IfAddrsPtr() noexcept { freeifaddrs(ifaddr); }
IfAddrsPtr(const IfAddrsPtr &) = delete;
IfAddrsPtr& operator=(IfAddrsPtr& other) = delete;
IfAddrsPtr(IfAddrsPtr &&) = delete;
IfAddrsPtr& operator=(IfAddrsPtr&& other) = delete;
[[nodiscard]] constexpr auto operator()() -> struct ifaddrs* { return ifaddr; }
[[nodiscard]] constexpr auto get() -> struct ifaddrs* { return ifaddr; }
[[nodiscard]] constexpr auto get_status() const noexcept -> int { return status; };
Expand Down
11 changes: 8 additions & 3 deletions src/btop_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace Term {
struct winsize wsize {};
if (uses_dev_tty || ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsize) < 0 || (wsize.ws_col == 0 && wsize.ws_row == 0)) {
Logger::error(R"(Couldn't determine terminal size of "STDOUT_FILENO"!)");
auto dev_tty = open("/dev/tty", O_RDONLY);
auto dev_tty = open("/dev/tty", O_RDONLY | O_CLOEXEC);
if (dev_tty != -1) {
ioctl(dev_tty, TIOCGWINSZ, &wsize);
close(dev_tty);
Expand Down Expand Up @@ -523,7 +523,7 @@ namespace Tools {
else this->atom.store(true);
}

atomic_lock::~atomic_lock() {
atomic_lock::~atomic_lock() noexcept {
this->atom.store(false);
}

Expand Down Expand Up @@ -556,6 +556,7 @@ namespace Tools {
string hostname() {
char host[HOST_NAME_MAX];
gethostname(host, HOST_NAME_MAX);
host[HOST_NAME_MAX - 1] = '\0';
return string{host};
}

Expand Down Expand Up @@ -653,11 +654,15 @@ namespace Logger {
this->status = seteuid(Global::real_uid);
}
}
~lose_priv() {
~lose_priv() noexcept {
if (status == 0) {
status = seteuid(Global::set_uid);
}
}
lose_priv(const lose_priv& other) = delete;
lose_priv& operator=(const lose_priv& other) = delete;
lose_priv(lose_priv&& other) = delete;
lose_priv& operator=(lose_priv&& other) = delete;
};

void set(const string& level) {
Expand Down
27 changes: 16 additions & 11 deletions src/btop_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ namespace Tools {

class MyNumPunct : public std::numpunct<char> {
protected:
virtual char do_thousands_sep() const { return '\''; }
virtual std::string do_grouping() const { return "\03"; }
virtual char do_thousands_sep() const override { return '\''; }
virtual std::string do_grouping() const override { return "\03"; }
};

size_t wide_ulen(const string& str);
Expand Down Expand Up @@ -364,7 +364,7 @@ namespace Tools {
#ifdef BTOP_DEBUG
const T& safeVal(const std::vector<T>& vec, const size_t& index, const T& fallback = T{}, std::source_location loc = std::source_location::current()) {
if (index < vec.size()) {
return vec.at(index);
return vec[index];
} else {
Logger::error(fmt::format("safeVal() called with invalid index: [{}] in file: {} on line: {}", index, loc.file_name(), loc.line()));
return fallback;
Expand All @@ -373,7 +373,7 @@ namespace Tools {
#else
const T& safeVal(const std::vector<T>& vec, const size_t& index, const T& fallback = T{}) {
if (index < vec.size()) {
return vec.at(index);
return vec[index];
} else {
Logger::error(fmt::format("safeVal() called with invalid index: [{}] (Compile btop with DEBUG=true for more extensive logging!)", index));
return fallback;
Expand Down Expand Up @@ -410,8 +410,12 @@ namespace Tools {
atomic<bool>& atom;
bool not_true{};
public:
atomic_lock(atomic<bool>& atom, bool wait = false);
~atomic_lock();
explicit atomic_lock(atomic<bool>& atom, bool wait = false);
~atomic_lock() noexcept;
atomic_lock(const atomic_lock& other) = delete;
atomic_lock& operator=(const atomic_lock& other) = delete;
atomic_lock(atomic_lock&& other) = delete;
atomic_lock& operator=(atomic_lock&& other) = delete;
};

//* Read a complete file and return as a string
Expand All @@ -433,13 +437,17 @@ namespace Tools {
bool running{};
std::locale custom_locale = std::locale(std::locale::classic(), new Tools::MyNumPunct);
vector<string> report_buffer{};
public:
string name{};
bool delayed_report{};
Logger::Level log_level = Logger::DEBUG;
public:
DebugTimer() = default;
DebugTimer(const string name, bool start = true, bool delayed_report = true);
explicit DebugTimer(const string name, bool start = true, bool delayed_report = true);
~DebugTimer();
DebugTimer(const DebugTimer& other) = delete;
DebugTimer& operator=(const DebugTimer& other) = delete;
DebugTimer(DebugTimer&& other) = delete;
DebugTimer& operator=(DebugTimer&& other) = delete;

void start();
void stop(bool report = true);
Expand All @@ -453,6 +461,3 @@ namespace Tools {
};

}



Loading
Loading