mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 09:16:46 +08:00
refactor: improve name handling in shm_posix.cpp to match semaphore pattern
Check if name already starts with '/' before adding prefix, consistent with the pattern used in semaphore_impl.h. This avoids duplicate prefix when users provide names in the correct format.
This commit is contained in:
parent
addfe4f5cf
commit
ce0773b3e6
@ -51,7 +51,12 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
|
|||||||
}
|
}
|
||||||
// For portable use, a shared memory object should be identified by name of the form /somename.
|
// For portable use, a shared memory object should be identified by name of the form /somename.
|
||||||
// see: https://man7.org/linux/man-pages/man3/shm_open.3.html
|
// see: https://man7.org/linux/man-pages/man3/shm_open.3.html
|
||||||
ipc::string op_name = ipc::string{"/"} + name;
|
ipc::string op_name;
|
||||||
|
if (name[0] == '/') {
|
||||||
|
op_name = name;
|
||||||
|
} else {
|
||||||
|
op_name = ipc::string{"/"} + name;
|
||||||
|
}
|
||||||
// Open the object for read-write access.
|
// Open the object for read-write access.
|
||||||
int flag = O_RDWR;
|
int flag = O_RDWR;
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
@ -206,7 +211,12 @@ void remove(char const * name) noexcept {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// For portable use, a shared memory object should be identified by name of the form /somename.
|
// For portable use, a shared memory object should be identified by name of the form /somename.
|
||||||
ipc::string op_name = ipc::string{"/"} + name;
|
ipc::string op_name;
|
||||||
|
if (name[0] == '/') {
|
||||||
|
op_name = name;
|
||||||
|
} else {
|
||||||
|
op_name = ipc::string{"/"} + name;
|
||||||
|
}
|
||||||
int unlink_ret = ::shm_unlink(op_name.c_str());
|
int unlink_ret = ::shm_unlink(op_name.c_str());
|
||||||
if (unlink_ret == -1) {
|
if (unlink_ret == -1) {
|
||||||
ipc::error("fail shm_unlink[%d]: %s\n", errno, op_name.c_str());
|
ipc::error("fail shm_unlink[%d]: %s\n", errno, op_name.c_str());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user