* Renamed and improved shared memory management functions.
authorUrban Wallasch <urban.wallasch@freenet.de>
Sun, 21 Jul 2019 18:55:36 +0000 (20:55 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Sun, 21 Jul 2019 18:55:36 +0000 (20:55 +0200)
telemetry.cpp

index 39db1be0c3f71f40f91c0e2fe2c770c18eee076a..52d7ec16b14a73e3543216c3bcb045ee36f0f50f 100644 (file)
@@ -52,38 +52,41 @@ bool print_header = true;
 scs_timestamp_t last_timestamp = static_cast<scs_timestamp_t>(-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<struct telemetry_state_t *>(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<struct telemetry_state_t *>(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<const scs_telemetry_init_params_v100_t *>(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