summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2025-05-18 23:21:40 +0200
committerCarl Hetherington <cth@carlh.net>2025-05-28 00:33:55 +0200
commit92acfdb520ae9fe0ea429d422955f3bdb4d133e2 (patch)
treefd3c726c6b2544719502a7bb65a9664ef16be983
parentd98e8d11f3dce34b483ed167b6f9b0a812c4cf6f (diff)
Cleanup: extract {de,}init_shm to their own file and remove the containing class.
-rw-r--r--src/lib/grok/messenger.cc17
-rw-r--r--src/lib/grok/messenger.h70
-rw-r--r--src/lib/grok/shm.cc97
-rw-r--r--src/lib/grok/shm.h33
-rw-r--r--src/lib/wscript2
5 files changed, 140 insertions, 79 deletions
diff --git a/src/lib/grok/messenger.cc b/src/lib/grok/messenger.cc
index 2d3b53816..ddb4e5fd9 100644
--- a/src/lib/grok/messenger.cc
+++ b/src/lib/grok/messenger.cc
@@ -19,6 +19,7 @@
*/
+#include "shm.h"
#include "messenger.h"
@@ -130,7 +131,7 @@ Messenger::outbound_thread()
int shm_fd = 0;
char* send_buffer = nullptr;
- if (!SharedMemoryManager::init_shm(_outbound_message_buf, messageBufferLen, &shm_fd, &send_buffer)) {
+ if (!init_shm(_outbound_message_buf, messageBufferLen, &shm_fd, &send_buffer)) {
return;
}
@@ -150,7 +151,7 @@ Messenger::outbound_thread()
_outbound_synch->post(SYNCH_SENT);
}
- SharedMemoryManager::deinit_shm(_outbound_message_buf, messageBufferLen, shm_fd, &send_buffer);
+ ::deinit_shm(_outbound_message_buf, messageBufferLen, shm_fd, &send_buffer);
}
@@ -160,7 +161,7 @@ Messenger::inbound_thread()
int shm_fd = 0;
char* receive_buffer = nullptr;
- if (!SharedMemoryManager::init_shm(_inbound_message_buf, messageBufferLen, &shm_fd, &receive_buffer)) {
+ if (!init_shm(_inbound_message_buf, messageBufferLen, &shm_fd, &receive_buffer)) {
return;
}
@@ -174,7 +175,7 @@ Messenger::inbound_thread()
_receive_queue.push(message);
}
- SharedMemoryManager::deinit_shm(_inbound_message_buf, messageBufferLen, shm_fd, &receive_buffer);
+ ::deinit_shm(_inbound_message_buf, messageBufferLen, shm_fd, &receive_buffer);
}
@@ -198,7 +199,7 @@ Messenger::initBuffers()
{
bool rc = true;
if (_uncompressed_frame_size) {
- rc = rc && SharedMemoryManager::init_shm(
+ rc = rc && init_shm(
grokUncompressedBuf,
_uncompressed_frame_size * _num_frames,
&_uncompressed_fd, &_uncompressed_buffer
@@ -206,7 +207,7 @@ Messenger::initBuffers()
}
if (_compressed_frame_size) {
- rc = rc && SharedMemoryManager::init_shm(
+ rc = rc && init_shm(
grokCompressedBuf,
_compressed_frame_size * _num_frames,
&_compressed_fd, &_compressed_buffer
@@ -220,13 +221,13 @@ Messenger::initBuffers()
bool
Messenger::deinit_shm()
{
- bool rc = SharedMemoryManager::deinit_shm(
+ bool rc = ::deinit_shm(
grokUncompressedBuf,
_uncompressed_frame_size * _num_frames,
_uncompressed_fd, &_uncompressed_buffer
);
- rc = rc && SharedMemoryManager::deinit_shm(
+ rc = rc && ::deinit_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 8f110926e..f3b6f915a 100644
--- a/src/lib/grok/messenger.h
+++ b/src/lib/grok/messenger.h
@@ -142,76 +142,6 @@ private:
};
-struct SharedMemoryManager
-{
- 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) {
- getMessengerLogger()->error("Error opening shared memory: %s", strerror(errno));
- return false;
- }
-
- int rc = ftruncate(*shm_fd, sizeof(char) * len);
-
- if (rc) {
- getMessengerLogger()->error("Error truncating shared memory: %s", strerror(errno));
- if (close(*shm_fd)) {
- getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno));
- }
- rc = shm_unlink(name.c_str());
- 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) {
- getMessengerLogger()->error("Error mapping shared memory: %s", strerror(errno));
- if (close(*shm_fd)) {
- getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno));
- }
- rc = shm_unlink(name.c_str());
- 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) {
- return true;
- }
-
- int rc = munmap(*buffer, len);
- *buffer = nullptr;
-
- if (rc) {
- getMessengerLogger()->error("Error unmapping shared memory %s: %s", name.c_str(), strerror(errno));
- }
-
- rc = close(shm_fd);
- shm_fd = 0;
-
- if (rc) {
- getMessengerLogger()->error("Error closing shared memory %s: %s", name.c_str(), strerror(errno));
- }
-
- rc = shm_unlink(name.c_str());
-
- if (rc && errno != ENOENT) {
- getMessengerLogger()->error("Error unlinking shared memory %s : %s", name.c_str(), strerror(errno));
- }
-
- return true;
- }
-};
template<typename Data>
class MessengerBlockingQueue
diff --git a/src/lib/grok/shm.cc b/src/lib/grok/shm.cc
new file mode 100644
index 000000000..91fe28903
--- /dev/null
+++ b/src/lib/grok/shm.cc
@@ -0,0 +1,97 @@
+/*
+ Copyright (C) 2023 Grok Image Compression Inc.
+
+ This file is part of DCP-o-matic.
+
+ DCP-o-matic is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ DCP-o-matic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "messenger.h"
+#include "shm.h"
+
+
+bool
+grk_plugin::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) {
+ getMessengerLogger()->error("Error opening shared memory: %s", strerror(errno));
+ return false;
+ }
+
+ int rc = ftruncate(*shm_fd, sizeof(char) * len);
+
+ if (rc) {
+ getMessengerLogger()->error("Error truncating shared memory: %s", strerror(errno));
+ if (close(*shm_fd)) {
+ getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno));
+ }
+ rc = shm_unlink(name.c_str());
+ 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) {
+ getMessengerLogger()->error("Error mapping shared memory: %s", strerror(errno));
+ if (close(*shm_fd)) {
+ getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno));
+ }
+ rc = shm_unlink(name.c_str());
+ if (rc && errno != ENOENT) {
+ getMessengerLogger()->error("Error unlinking shared memory: %s", strerror(errno));
+ }
+ }
+
+ return *buffer != nullptr;
+}
+
+
+bool
+grk_plugin::deinit_shm(const std::string &name, size_t len, int &shm_fd, char** buffer)
+{
+ if (!*buffer || !shm_fd) {
+ return true;
+ }
+
+ int rc = munmap(*buffer, len);
+ *buffer = nullptr;
+
+ if (rc) {
+ getMessengerLogger()->error("Error unmapping shared memory %s: %s", name.c_str(), strerror(errno));
+ }
+
+ rc = close(shm_fd);
+ shm_fd = 0;
+
+ if (rc) {
+ getMessengerLogger()->error("Error closing shared memory %s: %s", name.c_str(), strerror(errno));
+ }
+
+ rc = shm_unlink(name.c_str());
+
+ if (rc && errno != ENOENT) {
+ getMessengerLogger()->error("Error unlinking shared memory %s : %s", name.c_str(), strerror(errno));
+ }
+
+ return true;
+}
+
+
diff --git a/src/lib/grok/shm.h b/src/lib/grok/shm.h
new file mode 100644
index 000000000..f9fa5f93d
--- /dev/null
+++ b/src/lib/grok/shm.h
@@ -0,0 +1,33 @@
+/*
+ Copyright (C) 2023 Grok Image Compression Inc.
+
+ This file is part of DCP-o-matic.
+
+ DCP-o-matic is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ DCP-o-matic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include <string>
+
+
+namespace grk_plugin {
+
+
+bool init_shm(std::string const& name, size_t len, int* shm_fd, char** buffer);
+bool deinit_shm(std::string const& name, size_t len, int &shm_fd, char** buffer);
+
+
+}
+
diff --git a/src/lib/wscript b/src/lib/wscript
index fb7fe980b..8c206fcad 100644
--- a/src/lib/wscript
+++ b/src/lib/wscript
@@ -268,7 +268,7 @@ def build(bld):
obj.uselib += ' POLKIT'
if bld.env.ENABLE_GROK:
- obj.source += ' grok_j2k_encoder_thread.cc grok/messenger.cc grok/scheduled_frames.cc grok/synch.cc grok/util.cc'
+ obj.source += ' grok_j2k_encoder_thread.cc grok/messenger.cc grok/scheduled_frames.cc grok/shm.cc grok/synch.cc grok/util.cc'
if bld.env.TARGET_WINDOWS_64 or bld.env.TARGET_WINDOWS_32:
obj.uselib += ' WINSOCK2 DBGHELP SHLWAPI MSWSOCK BOOST_LOCALE SETUPAPI OLE32 UUID'