Rework some move constructors and assignment operators to not simply swap

This commit is contained in:
Jeremy Rifkin 2025-02-19 21:33:03 -06:00
parent b59a08634d
commit 69875cde19
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
2 changed files with 28 additions and 15 deletions

View File

@ -66,8 +66,12 @@ namespace libdwarf {
} }
~die_object() { ~die_object() {
release();
}
void release() {
if(die) { if(die) {
dwarf_dealloc_die(die); dwarf_dealloc_die(exchange(die, nullptr));
} }
} }
@ -75,16 +79,15 @@ namespace libdwarf {
die_object& operator=(const die_object&) = delete; die_object& operator=(const die_object&) = delete;
die_object(die_object&& other) noexcept : dbg(other.dbg), die(other.die) { // dbg doesn't strictly have to be st to null but it helps ensure attempts to use the die_object after this to
// done for finding mistakes, attempts to use the die_object after this should segfault // segfault. A valid use otherwise would be moved_from.get_sibling() which would get the next CU.
// a valid use otherwise would be moved_from.get_sibling() which would get the next CU die_object(die_object&& other) noexcept
other.dbg = nullptr; : dbg(exchange(other.dbg, nullptr)), die(exchange(other.die, nullptr)) {}
other.die = nullptr;
}
die_object& operator=(die_object&& other) noexcept { die_object& operator=(die_object&& other) noexcept {
std::swap(dbg, other.dbg); release();
std::swap(die, other.die); dbg = exchange(other.dbg, nullptr);
die = exchange(other.die, nullptr);
return *this; return *this;
} }

View File

@ -24,12 +24,16 @@ namespace libdwarf {
} }
} }
~srcfiles() { ~srcfiles() {
release();
}
void release() {
if(dw_srcfiles) { if(dw_srcfiles) {
for(unsigned i = 0; i < dw_filecount; i++) { for(unsigned i = 0; i < dw_filecount; i++) {
dwarf_dealloc(dbg, dw_srcfiles[i], DW_DLA_STRING); dwarf_dealloc(dbg, dw_srcfiles[i], DW_DLA_STRING);
dw_srcfiles[i] = nullptr; dw_srcfiles[i] = nullptr;
} }
dwarf_dealloc(dbg, dw_srcfiles, DW_DLA_LIST); dwarf_dealloc(dbg, dw_srcfiles, DW_DLA_LIST);
dw_srcfiles = nullptr;
} }
} }
srcfiles(const srcfiles&) = delete; srcfiles(const srcfiles&) = delete;
@ -38,9 +42,10 @@ namespace libdwarf {
} }
srcfiles& operator=(const srcfiles&) = delete; srcfiles& operator=(const srcfiles&) = delete;
srcfiles& operator=(srcfiles&& other) { srcfiles& operator=(srcfiles&& other) {
std::swap(dbg, other.dbg); release();
std::swap(dw_srcfiles, other.dw_srcfiles); dbg = exchange(other.dbg, nullptr);
std::swap(dw_filecount, other.dw_filecount); dw_srcfiles = exchange(other.dw_srcfiles, nullptr);
dw_filecount = exchange(other.dw_filecount, 0);
return *this; return *this;
} }
// note: dwarf uses 1-indexing // note: dwarf uses 1-indexing
@ -177,7 +182,11 @@ namespace libdwarf {
std::vector<line_entry>&& line_entries std::vector<line_entry>&& line_entries
) : version(version), line_context(line_context), line_entries(std::move(line_entries)) {} ) : version(version), line_context(line_context), line_entries(std::move(line_entries)) {}
~line_table_info() { ~line_table_info() {
release();
}
void release() {
dwarf_srclines_dealloc_b(line_context); dwarf_srclines_dealloc_b(line_context);
line_context = nullptr;
} }
line_table_info(const line_table_info&) = delete; line_table_info(const line_table_info&) = delete;
line_table_info(line_table_info&& other) { line_table_info(line_table_info&& other) {
@ -185,9 +194,10 @@ namespace libdwarf {
} }
line_table_info& operator=(const line_table_info&) = delete; line_table_info& operator=(const line_table_info&) = delete;
line_table_info& operator=(line_table_info&& other) { line_table_info& operator=(line_table_info&& other) {
std::swap(version, other.version); release();
std::swap(line_context, other.line_context); version = other.version;
std::swap(line_entries, other.line_entries); line_context = exchange(other.line_context, nullptr);
line_entries = std::move(other.line_entries);
return *this; return *this;
} }
}; };