Skip to content

Commit

Permalink
buffer::free, memory_holder::reset
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Nov 4, 2024
1 parent 52577de commit 362ad47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
14 changes: 9 additions & 5 deletions include/cista/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ struct buffer final {
std::memcpy(buf_, str, size_);
}

~buffer() {
std::free(buf_);
buf_ = nullptr;
}
~buffer() { free(); }

buffer(buffer const&) = delete;
buffer& operator=(buffer const&) = delete;
Expand All @@ -43,7 +40,7 @@ struct buffer final {
return *this;
}
if (buf_ != nullptr) {
std::free(buf_);
free();
}
buf_ = o.buf_;
size_ = o.size_;
Expand Down Expand Up @@ -74,6 +71,13 @@ struct buffer final {
size_ = 0U;
}

void free() noexcept {
if (buf_ != nullptr) {
std::free(buf_);
reset();
}
}

void* buf_;
std::size_t size_;
};
Expand Down
9 changes: 9 additions & 0 deletions include/cista/memory_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ struct wrapped {
}
explicit wrapped(raw::unique_ptr<T> el) : el_{std::move(el)} {}

void reset() {
el_->~T();
if (std::holds_alternative<buffer>(mem_)) {
std::get<buffer>(mem_) = buffer{};
} else if (std::holds_alternative<byte_buf>(mem_)) {
std::get<byte_buf>(mem_) = byte_buf{};
}
}

friend bool operator==(wrapped const& x, std::nullptr_t) {
return x.el_ == nullptr;
}
Expand Down

0 comments on commit 362ad47

Please sign in to comment.