Skip to content

Commit

Permalink
Make big_string pod
Browse files Browse the repository at this point in the history
`big_string` is not pod which means it needs to be properly
constructed and destroyed. Instead make it POD and destroy it
manually in `string` destructor.
  • Loading branch information
alkis committed Jun 8, 2024
1 parent d44a7dc commit 4c427ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
30 changes: 11 additions & 19 deletions ctl/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,19 @@

namespace ctl {

namespace __ {

big_string::~big_string() /* noexcept */
{
if (n) {
if (n >= c)
__builtin_trap();
if (p[n])
__builtin_trap();
}
if (c && !p)
__builtin_trap();
free(p);
}

} // namespace __

string::~string() /* noexcept */
string::~string() noexcept
{
if (isbig()) {
big()->~big_string();
auto* b = big();
if (b->n) {
if (b->n >= b->c)
__builtin_trap();
if (b->p[b->n])
__builtin_trap();
}
if (b->c && !b->p)
__builtin_trap();
free(b->p);
}
}

Expand Down
4 changes: 1 addition & 3 deletions ctl/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ struct big_string
size_t c : sizeof(size_t) * 8 - 1;
size_t big : 1 /* = 1 */;
#endif

~big_string() /* noexcept */;
};

} // namespace __
Expand All @@ -51,7 +49,7 @@ class string
using const_iterator = const char*;
static constexpr size_t npos = -1;

~string() /* noexcept */;
~string() noexcept;
string(const string_view) noexcept;
string(const char*) noexcept;
string(const string&) noexcept;
Expand Down

0 comments on commit 4c427ea

Please sign in to comment.