Skip to content

Commit f8e71a5

Browse files
authored
Merge pull request #159 from mutouyun/refactoring-ut
refactor(test): comprehensive unit test refactoring
2 parents c5302d0 + cf5738e commit f8e71a5

30 files changed

+4089
-289
lines changed

include/libipc/buffer.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ class IPC_EXPORT buffer {
1717
buffer();
1818

1919
buffer(void* p, std::size_t s, destructor_t d);
20-
buffer(void* p, std::size_t s, destructor_t d, void* additional);
20+
// mem_to_free: pointer to be passed to destructor (if different from p)
21+
// Use case: when p points into a larger allocated block that needs to be freed
22+
buffer(void* p, std::size_t s, destructor_t d, void* mem_to_free);
2123
buffer(void* p, std::size_t s);
2224

2325
template <std::size_t N>
24-
explicit buffer(byte_t const (& data)[N])
26+
explicit buffer(byte_t (& data)[N])
2527
: buffer(data, sizeof(data)) {
2628
}
27-
explicit buffer(char const & c);
29+
explicit buffer(char & c);
2830

2931
buffer(buffer&& rhs);
3032
~buffer();

include/libipc/shm.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,30 @@ enum : unsigned {
1717

1818
IPC_EXPORT id_t acquire(char const * name, std::size_t size, unsigned mode = create | open);
1919
IPC_EXPORT void * get_mem(id_t id, std::size_t * size);
20+
21+
// Release shared memory resource and clean up disk file if reference count reaches zero.
22+
// This function decrements the reference counter. When the counter reaches zero, it:
23+
// 1. Unmaps the shared memory region
24+
// 2. Removes the backing file from disk (shm_unlink on POSIX)
25+
// 3. Frees the id structure
26+
// After calling this function, the id becomes invalid and must not be used again.
27+
// Returns: The reference count before decrement, or -1 on error.
2028
IPC_EXPORT std::int32_t release(id_t id) noexcept;
29+
30+
// Release shared memory resource and force cleanup of disk file.
31+
// This function calls release(id) internally, then unconditionally attempts to
32+
// remove the backing file. WARNING: Do NOT call this after release(id) on the
33+
// same id, as the id is already freed by release(). Use this function alone,
34+
// not in combination with release().
35+
// Typical use case: Force cleanup when you want to ensure the disk file is removed
36+
// regardless of reference count state.
2137
IPC_EXPORT void remove (id_t id) noexcept;
38+
39+
// Remove shared memory backing file by name.
40+
// This function only removes the disk file and does not affect any active memory
41+
// mappings or id structures. Use this for cleanup of orphaned files or for explicit
42+
// file removal without affecting runtime resources.
43+
// Safe to call at any time, even if shared memory is still in use elsewhere.
2244
IPC_EXPORT void remove (char const * name) noexcept;
2345

2446
IPC_EXPORT std::int32_t get_ref(id_t id);

src/libipc/buffer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ buffer::buffer(void* p, std::size_t s, destructor_t d)
3838
: p_(p_->make(p, s, d, nullptr)) {
3939
}
4040

41-
buffer::buffer(void* p, std::size_t s, destructor_t d, void* additional)
42-
: p_(p_->make(p, s, d, additional)) {
41+
buffer::buffer(void* p, std::size_t s, destructor_t d, void* mem_to_free)
42+
: p_(p_->make(p, s, d, mem_to_free)) {
4343
}
4444

4545
buffer::buffer(void* p, std::size_t s)
4646
: buffer(p, s, nullptr) {
4747
}
4848

49-
buffer::buffer(char const & c)
50-
: buffer(const_cast<char*>(&c), 1) {
49+
buffer::buffer(char & c)
50+
: buffer(&c, 1) {
5151
}
5252

5353
buffer::buffer(buffer&& rhs)

src/libipc/platform/linux/condition.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class condition : public sync::obj_impl<a0_cnd_t> {
2727
return false;
2828
}
2929
} else {
30-
auto ts = detail::make_timespec(tm);
30+
auto ts = linux_::detail::make_timespec(tm);
3131
int eno = A0_SYSERR(a0_cnd_timedwait(native(), static_cast<a0_mtx_t *>(mtx.native()), {ts}));
3232
if (eno != 0) {
3333
if (eno != ETIMEDOUT) {

src/libipc/platform/linux/get_wait_time.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "a0/err_macro.h"
1111

1212
namespace ipc {
13+
namespace linux_ {
1314
namespace detail {
1415

1516
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
@@ -43,4 +44,5 @@ inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
4344
}
4445

4546
} // namespace detail
47+
} // namespace linux_
4648
} // namespace ipc

src/libipc/platform/linux/mutex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class robust_mutex : public sync::obj_impl<a0_mtx_t> {
2525
bool lock(std::uint64_t tm) noexcept {
2626
if (!valid()) return false;
2727
for (;;) {
28-
auto ts = detail::make_timespec(tm);
28+
auto ts = linux_::detail::make_timespec(tm);
2929
int eno = A0_SYSERR(
3030
(tm == invalid_value) ? a0_mtx_lock(native())
3131
: a0_mtx_timedlock(native(), {ts}));
@@ -56,7 +56,7 @@ class robust_mutex : public sync::obj_impl<a0_mtx_t> {
5656

5757
bool try_lock() noexcept(false) {
5858
if (!valid()) return false;
59-
int eno = A0_SYSERR(a0_mtx_timedlock(native(), {detail::make_timespec(0)}));
59+
int eno = A0_SYSERR(a0_mtx_timedlock(native(), {linux_::detail::make_timespec(0)}));
6060
switch (eno) {
6161
case 0:
6262
return true;

src/libipc/platform/posix/condition.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class condition {
115115
}
116116
break;
117117
default: {
118-
auto ts = detail::make_timespec(tm);
118+
auto ts = posix_::detail::make_timespec(tm);
119119
int eno;
120120
if ((eno = ::pthread_cond_timedwait(cond_, static_cast<pthread_mutex_t *>(mtx.native()), &ts)) != 0) {
121121
if (eno != ETIMEDOUT) {

src/libipc/platform/posix/get_wait_time.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "libipc/utility/log.h"
1111

1212
namespace ipc {
13+
namespace posix_ {
1314
namespace detail {
1415

1516
inline bool calc_wait_time(timespec &ts, std::uint64_t tm /*ms*/) noexcept {
@@ -36,4 +37,5 @@ inline timespec make_timespec(std::uint64_t tm /*ms*/) noexcept(false) {
3637
}
3738

3839
} // namespace detail
40+
} // namespace posix_
3941
} // namespace ipc

src/libipc/platform/posix/mutex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class mutex {
196196
bool lock(std::uint64_t tm) noexcept {
197197
if (!valid()) return false;
198198
for (;;) {
199-
auto ts = detail::make_timespec(tm);
199+
auto ts = posix_::detail::make_timespec(tm);
200200
int eno = (tm == invalid_value)
201201
? ::pthread_mutex_lock(mutex_)
202202
: ::pthread_mutex_timedlock(mutex_, &ts);
@@ -230,7 +230,7 @@ class mutex {
230230

231231
bool try_lock() noexcept(false) {
232232
if (!valid()) return false;
233-
auto ts = detail::make_timespec(0);
233+
auto ts = posix_::detail::make_timespec(0);
234234
int eno = ::pthread_mutex_timedlock(mutex_, &ts);
235235
switch (eno) {
236236
case 0:

src/libipc/platform/posix/semaphore_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class semaphore {
8888
return false;
8989
}
9090
} else {
91-
auto ts = detail::make_timespec(tm);
91+
auto ts = posix_::detail::make_timespec(tm);
9292
if (::sem_timedwait(h_, &ts) != 0) {
9393
if (errno != ETIMEDOUT) {
9494
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",

0 commit comments

Comments
 (0)