From 1ff81c8101089e9fa26323fac424608a6e96b9c7 Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Sun, 21 Jul 2019 20:55:36 +0200 Subject: [PATCH] * Renamed and improved shared memory management functions. --- telemetry.cpp | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/telemetry.cpp b/telemetry.cpp index 39db1be..52d7ec1 100644 --- a/telemetry.cpp +++ b/telemetry.cpp @@ -52,38 +52,41 @@ bool print_header = true; scs_timestamp_t last_timestamp = static_cast(-1); /** - * @brief Combined telemetry data. + * @brief Combined telemetry data and shared memory management. */ #include "telemetry.h" struct telemetry_state_t *telemetry; -/** - * @brief Function writting message to the game internal log. - */ - static int shmid = -1; -bool init_log(void) +bool init_shm(void) { - if (shmid > 0) { - return true; - } - shmid = shmget (SHM_KEY, sizeof (struct telemetry_state_t), IPC_CREAT | IPC_EXCL | 0600); - telemetry = static_cast(shmat (shmid, NULL, 0)); - return true; + if ( 0 < shmid ) + return true; + shmid = shmget(SHM_KEY, sizeof *telemetry, IPC_CREAT | IPC_EXCL | 0600); + if ( 0 > shmid ) + return false; + void *shmhnd; + if ( (void *)-1 == (shmhnd = shmat(shmid, NULL, 0)) ) + return false; + telemetry = static_cast(shmhnd); + return true; } -void finish_log(void) +void release_shm(void) { - if (shmid < 0) { - return; - } - shmdt (telemetry); - shmctl (shmid, IPC_RMID, NULL); - shmid = -1; + if ( 0 < shmid) { + shmdt(telemetry); + shmctl(shmid, IPC_RMID, NULL); + shmid = -1; + } } +/** + * @brief Function writting message to the game internal log. + */ + void log_print(const char *const text, ...) { } @@ -336,7 +339,7 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in } const scs_telemetry_init_params_v100_t *const version_params = static_cast(params); - if (! init_log()) { + if (! init_shm()) { version_params->common.log(SCS_LOG_TYPE_error, "Unable to initialize the log file"); return SCS_RESULT_generic_error; } @@ -443,7 +446,7 @@ SCSAPI_VOID scs_telemetry_shutdown(void) // Any cleanup needed. The registrations will be removed automatically // so there is no need to do that manually. - finish_log(); + release_shm(); } // Cleanup @@ -456,7 +459,7 @@ BOOL APIENTRY DllMain( ) { if (reason_for_call == DLL_PROCESS_DETACH) { - finish_log(); + release_shm(); } return TRUE; } @@ -465,6 +468,6 @@ BOOL APIENTRY DllMain( #ifdef __linux__ void __attribute__ ((destructor)) unload(void) { - finish_log(); + release_shm(); } #endif -- 2.30.2