diff --git a/src/base/lnav_log.cc b/src/base/lnav_log.cc index bce5488b..4dda11e1 100644 --- a/src/base/lnav_log.cc +++ b/src/base/lnav_log.cc @@ -107,8 +107,8 @@ const char *lnav_log_crash_dir; nonstd::optional lnav_log_orig_termios; static pthread_mutex_t lnav_log_mutex = PTHREAD_MUTEX_INITIALIZER; -log_state_dumper::log_state_list log_state_dumper::DUMPER_LIST; -log_crash_recoverer::log_crash_list log_crash_recoverer::CRASH_LIST; +std::unordered_set log_state_dumper::DUMPER_LIST; +std::unordered_set log_crash_recoverer::CRASH_LIST; static struct { size_t lr_length; @@ -370,14 +370,8 @@ static void sigabrt(int sig) log_host_info(); - { - log_state_dumper *lsd; - - for (lsd = LIST_FIRST(&log_state_dumper::DUMPER_LIST.lsl_list); - lsd != nullptr; - lsd = LIST_NEXT(lsd, lsd_link)) { - lsd->log_state(); - } + for (auto lsd : log_state_dumper::DUMPER_LIST) { + lsd->log_state(); } if (log_ring.lr_frag_start < (off_t)BUFFER_SIZE) { @@ -392,14 +386,8 @@ static void sigabrt(int sig) } lnav_log_orig_termios | [](auto termios) { - { - log_crash_recoverer *lcr; - - for (lcr = LIST_FIRST(&log_crash_recoverer::CRASH_LIST.lcl_list); - lcr != nullptr; - lcr = LIST_NEXT(lcr, lcr_link)) { - lcr->log_crash_recover(); - } + for (auto lcr : log_crash_recoverer::CRASH_LIST) { + lcr->log_crash_recover(); } tcsetattr(STDOUT_FILENO, TCSAFLUSH, termios); diff --git a/src/base/lnav_log.hh b/src/base/lnav_log.hh index 640188d5..e7048d5c 100644 --- a/src/base/lnav_log.hh +++ b/src/base/lnav_log.hh @@ -38,7 +38,8 @@ #include #include #include -#include + +#include #ifndef lnav_dead2 #define lnav_dead2 __attribute__((noreturn)) @@ -68,53 +69,33 @@ void log_pipe_err(int fd); struct log_state_dumper { public: log_state_dumper() { - LIST_INSERT_HEAD(&DUMPER_LIST.lsl_list, this, lsd_link); + DUMPER_LIST.insert(this); } virtual ~log_state_dumper() { - LIST_REMOVE(this, lsd_link); + DUMPER_LIST.erase(this); }; virtual void log_state() { }; - struct log_state_list { - log_state_list() { - LIST_INIT(&this->lsl_list); - } - - LIST_HEAD(dumper_head, log_state_dumper) lsl_list; - }; - - static log_state_list DUMPER_LIST; - - LIST_ENTRY(log_state_dumper) lsd_link; + static std::unordered_set DUMPER_LIST; }; struct log_crash_recoverer { public: log_crash_recoverer() { - LIST_INSERT_HEAD(&CRASH_LIST.lcl_list, this, lcr_link); + CRASH_LIST.insert(this); } virtual ~log_crash_recoverer() { - LIST_REMOVE(this, lcr_link); + CRASH_LIST.erase(this); }; virtual void log_crash_recover() = 0; - struct log_crash_list { - log_crash_list() { - LIST_INIT(&this->lcl_list); - } - - LIST_HEAD(crash_head, log_crash_recoverer) lcl_list; - }; - - static log_crash_list CRASH_LIST; - - LIST_ENTRY(log_crash_recoverer) lcr_link; + static std::unordered_set CRASH_LIST; }; extern nonstd::optional lnav_log_file; diff --git a/src/shared_buffer.cc b/src/shared_buffer.cc index 10e0f282..5c7c1398 100644 --- a/src/shared_buffer.cc +++ b/src/shared_buffer.cc @@ -75,14 +75,14 @@ bool shared_buffer_ref::subset(shared_buffer_ref &other, off_t offset, size_t le memcpy(this->sb_data, &other.sb_data[offset], len); } else { - LIST_INSERT_HEAD(&this->sb_owner->sb_refs, this, sb_link); + this->sb_owner->add_ref(*this); this->sb_data = &other.sb_data[offset]; } } return true; } -shared_buffer_ref::shared_buffer_ref(shared_buffer_ref &&other) +shared_buffer_ref::shared_buffer_ref(shared_buffer_ref &&other) noexcept { if (other.sb_data == nullptr) { this->sb_owner = nullptr; @@ -102,3 +102,38 @@ shared_buffer_ref::shared_buffer_ref(shared_buffer_ref &&other) other.sb_length = 0; } } + +bool shared_buffer_ref::take_ownership() +{ + if (this->sb_owner != nullptr && this->sb_data != nullptr) { + char *new_data; + + if ((new_data = (char *)malloc(this->sb_length)) == nullptr) { + return false; + } + + memcpy(new_data, this->sb_data, this->sb_length); + this->sb_data = new_data; + this->sb_owner->sb_refs.erase(find(this->sb_owner->sb_refs.begin(), + this->sb_owner->sb_refs.end(), + this)); + this->sb_owner = nullptr; + } + return true; +} + +void shared_buffer_ref::disown() +{ + if (this->sb_owner == nullptr) { + if (this->sb_data != nullptr) { + free(this->sb_data); + } + } else { + this->sb_owner->sb_refs.erase(find(this->sb_owner->sb_refs.begin(), + this->sb_owner->sb_refs.end(), + this)); + } + this->sb_owner = nullptr; + this->sb_data = nullptr; + this->sb_length = 0; +} diff --git a/src/shared_buffer.hh b/src/shared_buffer.hh index 705a382a..a870e67b 100644 --- a/src/shared_buffer.hh +++ b/src/shared_buffer.hh @@ -35,9 +35,9 @@ #include #include #include -#include #include +#include #include "auto_mem.hh" #include "base/lnav_log.hh" @@ -48,7 +48,6 @@ struct shared_buffer_ref { public: shared_buffer_ref(char *data = nullptr, size_t len = 0) : sb_owner(nullptr), sb_data(data), sb_length(len) { - memset(&this->sb_link, 0, sizeof(this->sb_link)); }; ~shared_buffer_ref() { @@ -63,7 +62,7 @@ public: this->copy_ref(other); }; - shared_buffer_ref(shared_buffer_ref &&other); + shared_buffer_ref(shared_buffer_ref &&other) noexcept; shared_buffer_ref &operator=(const shared_buffer_ref &other) { if (this != &other) { @@ -110,36 +109,10 @@ public: bool subset(shared_buffer_ref &other, off_t offset, size_t len); - bool take_ownership() { - if (this->sb_owner != nullptr && this->sb_data != nullptr) { - char *new_data; - - if ((new_data = (char *)malloc(this->sb_length)) == nullptr) { - return false; - } + bool take_ownership(); - memcpy(new_data, this->sb_data, this->sb_length); - this->sb_data = new_data; - LIST_REMOVE(this, sb_link); - this->sb_owner = nullptr; - } - return true; - }; + void disown(); - void disown() { - if (this->sb_owner == nullptr) { - if (this->sb_data != nullptr) { - free(this->sb_data); - } - } else { - LIST_REMOVE(this, sb_link); - } - this->sb_owner = nullptr; - this->sb_data = nullptr; - this->sb_length = 0; - }; - - LIST_ENTRY(shared_buffer_ref) sb_link; private: void copy_ref(const shared_buffer_ref &other) { if (other.sb_data == nullptr) { @@ -165,36 +138,31 @@ private: class shared_buffer { public: - shared_buffer() { - LIST_INIT(&this->sb_refs); - }; - ~shared_buffer() { this->invalidate_refs(); } void add_ref(shared_buffer_ref &ref) { - LIST_INSERT_HEAD(&this->sb_refs, &ref, sb_link); + this->sb_refs.push_back(&ref); }; bool invalidate_refs() { - shared_buffer_ref *ref; bool retval = true; - for (ref = LIST_FIRST(&this->sb_refs); - ref != nullptr; - ref = LIST_FIRST(&this->sb_refs)) { - retval = retval && ref->take_ownership(); + while (!this->sb_refs.empty()) { + auto iter = this->sb_refs.begin(); + + retval = retval && (*iter)->take_ownership(); } return retval; }; - LIST_HEAD(shared_buffer_head, shared_buffer_ref) sb_refs; + std::vector sb_refs; }; struct tmp_shared_buffer { - tmp_shared_buffer(const char *str, size_t len = -1) { + explicit tmp_shared_buffer(const char *str, size_t len = -1) { if (len == (size_t)-1) { len = strlen(str); }