diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-05-18 23:15:21 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-05-28 00:33:55 +0200 |
| commit | d98e8d11f3dce34b483ed167b6f9b0a812c4cf6f (patch) | |
| tree | 024bfd36bd498ff7b1b86654b3577c9bb77884e0 /src/lib/grok | |
| parent | 74c7f7d987c74f7a3d796fef5a3536c8b0df06fb (diff) | |
Cleanup: rename initShm -> init_shm and tidy up.
Diffstat (limited to 'src/lib/grok')
| -rw-r--r-- | src/lib/grok/messenger.cc | 8 | ||||
| -rw-r--r-- | src/lib/grok/messenger.h | 53 |
2 files changed, 36 insertions, 25 deletions
diff --git a/src/lib/grok/messenger.cc b/src/lib/grok/messenger.cc index 942912677..2d3b53816 100644 --- a/src/lib/grok/messenger.cc +++ b/src/lib/grok/messenger.cc @@ -130,7 +130,7 @@ Messenger::outbound_thread() int shm_fd = 0; char* send_buffer = nullptr; - if (!SharedMemoryManager::initShm(_outbound_message_buf, messageBufferLen, &shm_fd, &send_buffer)) { + if (!SharedMemoryManager::init_shm(_outbound_message_buf, messageBufferLen, &shm_fd, &send_buffer)) { return; } @@ -160,7 +160,7 @@ Messenger::inbound_thread() int shm_fd = 0; char* receive_buffer = nullptr; - if (!SharedMemoryManager::initShm(_inbound_message_buf, messageBufferLen, &shm_fd, &receive_buffer)) { + if (!SharedMemoryManager::init_shm(_inbound_message_buf, messageBufferLen, &shm_fd, &receive_buffer)) { return; } @@ -198,7 +198,7 @@ Messenger::initBuffers() { bool rc = true; if (_uncompressed_frame_size) { - rc = rc && SharedMemoryManager::initShm( + rc = rc && SharedMemoryManager::init_shm( grokUncompressedBuf, _uncompressed_frame_size * _num_frames, &_uncompressed_fd, &_uncompressed_buffer @@ -206,7 +206,7 @@ Messenger::initBuffers() } if (_compressed_frame_size) { - rc = rc && SharedMemoryManager::initShm( + rc = rc && SharedMemoryManager::init_shm( grokCompressedBuf, _compressed_frame_size * _num_frames, &_compressed_fd, &_compressed_buffer diff --git a/src/lib/grok/messenger.h b/src/lib/grok/messenger.h index ac8e5e979..8f110926e 100644 --- a/src/lib/grok/messenger.h +++ b/src/lib/grok/messenger.h @@ -144,59 +144,70 @@ private: struct SharedMemoryManager { - static bool initShm(const std::string &name, size_t len, int* shm_fd, char** buffer) + static bool init_shm(std::string const& name, size_t len, int* shm_fd, char** buffer) { *shm_fd = shm_open(name.c_str(), O_CREAT | O_RDWR, 0666); - if(*shm_fd < 0) - { + + if (*shm_fd < 0) { getMessengerLogger()->error("Error opening shared memory: %s", strerror(errno)); return false; } + int rc = ftruncate(*shm_fd, sizeof(char) * len); - if(rc) - { + + if (rc) { getMessengerLogger()->error("Error truncating shared memory: %s", strerror(errno)); - rc = close(*shm_fd); - if(rc) + if (close(*shm_fd)) { getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno)); + } rc = shm_unlink(name.c_str()); - // 2 == No such file or directory - if(rc && errno != 2) + if (rc && errno != ENOENT) { getMessengerLogger()->error("Error unlinking shared memory: %s", strerror(errno)); + } return false; } + *buffer = static_cast<char*>(mmap(0, len, PROT_WRITE, MAP_SHARED, *shm_fd, 0)); - if(!*buffer) - { + + if (!*buffer) { getMessengerLogger()->error("Error mapping shared memory: %s", strerror(errno)); - rc = close(*shm_fd); - if(rc) + if (close(*shm_fd)) { getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno)); + } rc = shm_unlink(name.c_str()); - // 2 == No such file or directory - if(rc && errno != 2) + if (rc && errno != ENOENT) { getMessengerLogger()->error("Error unlinking shared memory: %s", strerror(errno)); + } } return *buffer != nullptr; } + static bool deinit_shm(const std::string &name, size_t len, int &shm_fd, char** buffer) { - if (!*buffer || !shm_fd) + if (!*buffer || !shm_fd) { return true; + } int rc = munmap(*buffer, len); *buffer = nullptr; - if(rc) + + if (rc) { getMessengerLogger()->error("Error unmapping shared memory %s: %s", name.c_str(), strerror(errno)); + } + rc = close(shm_fd); shm_fd = 0; - if(rc) + + if (rc) { getMessengerLogger()->error("Error closing shared memory %s: %s", name.c_str(), strerror(errno)); + } + rc = shm_unlink(name.c_str()); - // 2 == No such file or directory - if(rc && errno != 2) - fprintf(stderr,"Error unlinking shared memory %s : %s\n", name.c_str(), strerror(errno)); + + if (rc && errno != ENOENT) { + getMessengerLogger()->error("Error unlinking shared memory %s : %s", name.c_str(), strerror(errno)); + } return true; } |
