mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2026-01-01 03:12:13 +08:00
Header changes (include/libipc/buffer.h):
- Rename: additional → mem_to_free (better semantic name)
- Add documentation comments explaining the parameter's purpose
- Clarifies that mem_to_free is passed to destructor instead of p
- Use case: when data pointer is offset into a larger allocation
Implementation changes (src/libipc/buffer.cpp):
- Update parameter name in constructor implementation
- No logic changes, just naming improvement
Test changes (test/test_buffer.cpp):
- Fix TEST_F(BufferTest, ConstructorWithMemToFree)
- Previous test caused crash: passed stack variable address to destructor
- New test correctly demonstrates the parameter's purpose:
* Allocate 100-byte block
* Use offset portion (bytes 25-75) as data
* Destructor receives original block pointer for proper cleanup
- Prevents double-free and invalid free errors
Semantic explanation:
buffer(data_ptr, size, destructor, mem_to_free)
On destruction:
destructor(mem_to_free ? mem_to_free : data_ptr, size)
This allows:
char* block = new char[100];
char* data = block + 25;
buffer buf(data, 50, my_free, block); // Frees 'block', not 'data'
88 lines
1.7 KiB
C++
Executable File
88 lines
1.7 KiB
C++
Executable File
#include "libipc/buffer.h"
|
|
#include "libipc/utility/pimpl.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace ipc {
|
|
|
|
bool operator==(buffer const & b1, buffer const & b2) {
|
|
return (b1.size() == b2.size()) && (std::memcmp(b1.data(), b2.data(), b1.size()) == 0);
|
|
}
|
|
|
|
bool operator!=(buffer const & b1, buffer const & b2) {
|
|
return !(b1 == b2);
|
|
}
|
|
|
|
class buffer::buffer_ : public pimpl<buffer_> {
|
|
public:
|
|
void* p_;
|
|
std::size_t s_;
|
|
void* a_;
|
|
buffer::destructor_t d_;
|
|
|
|
buffer_(void* p, std::size_t s, buffer::destructor_t d, void* a)
|
|
: p_(p), s_(s), a_(a), d_(d) {
|
|
}
|
|
|
|
~buffer_() {
|
|
if (d_ == nullptr) return;
|
|
d_((a_ == nullptr) ? p_ : a_, s_);
|
|
}
|
|
};
|
|
|
|
buffer::buffer()
|
|
: buffer(nullptr, 0, nullptr, nullptr) {
|
|
}
|
|
|
|
buffer::buffer(void* p, std::size_t s, destructor_t d)
|
|
: p_(p_->make(p, s, d, nullptr)) {
|
|
}
|
|
|
|
buffer::buffer(void* p, std::size_t s, destructor_t d, void* mem_to_free)
|
|
: p_(p_->make(p, s, d, mem_to_free)) {
|
|
}
|
|
|
|
buffer::buffer(void* p, std::size_t s)
|
|
: buffer(p, s, nullptr) {
|
|
}
|
|
|
|
buffer::buffer(char & c)
|
|
: buffer(&c, 1) {
|
|
}
|
|
|
|
buffer::buffer(buffer&& rhs)
|
|
: buffer() {
|
|
swap(rhs);
|
|
}
|
|
|
|
buffer::~buffer() {
|
|
p_->clear();
|
|
}
|
|
|
|
void buffer::swap(buffer& rhs) {
|
|
std::swap(p_, rhs.p_);
|
|
}
|
|
|
|
buffer& buffer::operator=(buffer rhs) {
|
|
swap(rhs);
|
|
return *this;
|
|
}
|
|
|
|
bool buffer::empty() const noexcept {
|
|
return (impl(p_)->p_ == nullptr) || (impl(p_)->s_ == 0);
|
|
}
|
|
|
|
void* buffer::data() noexcept {
|
|
return impl(p_)->p_;
|
|
}
|
|
|
|
void const * buffer::data() const noexcept {
|
|
return impl(p_)->p_;
|
|
}
|
|
|
|
std::size_t buffer::size() const noexcept {
|
|
return impl(p_)->s_;
|
|
}
|
|
|
|
} // namespace ipc
|