moved creating and freeing API thread data to separate functions
authorHolger Schemel <info@artsoft.org>
Fri, 24 Sep 2021 14:25:27 +0000 (16:25 +0200)
committerHolger Schemel <info@artsoft.org>
Fri, 24 Sep 2021 14:25:27 +0000 (16:25 +0200)
src/files.c
src/screens.c

index fc45f7731b298cf7f3ff9d1d2c496c0ed0e85b15..a38c4122567e1db7b61b23feaa08cf9032b2b107 100644 (file)
@@ -9074,6 +9074,26 @@ struct ApiGetScoreThreadData
   char *score_cache_filename;
 };
 
+static void *CreateThreadData_ApiGetScore(int nr)
+{
+  struct ApiGetScoreThreadData *data =
+    checked_malloc(sizeof(struct ApiGetScoreThreadData));
+  char *score_cache_filename = getScoreCacheFilename(nr);
+
+  data->level_nr = nr;
+  data->score_cache_filename = getStringCopy(score_cache_filename);
+
+  return data;
+}
+
+static void FreeThreadData_ApiGetScore(void *data_raw)
+{
+  struct ApiGetScoreThreadData *data = data_raw;
+
+  checked_free(data->score_cache_filename);
+  checked_free(data);
+}
+
 static void ApiGetScoreExt(struct HttpRequest *request,
                           struct HttpResponse *response,
                           int level_nr,
@@ -9167,20 +9187,14 @@ static int ApiGetScoreThread(void *data_raw)
   ApiGetScore(data->level_nr,
              data->score_cache_filename);
 
-  checked_free(data->score_cache_filename);
-  checked_free(data);
+  FreeThreadData_ApiGetScore(data_raw);
 
   return 0;
 }
 
 static void ApiGetScoreAsThread(int nr)
 {
-  struct ApiGetScoreThreadData *data =
-    checked_malloc(sizeof(struct ApiGetScoreThreadData));
-  char *score_cache_filename = getScoreCacheFilename(nr);
-
-  data->level_nr = nr;
-  data->score_cache_filename = getStringCopy(score_cache_filename);
+  struct ApiGetScoreThreadData *data = CreateThreadData_ApiGetScore(nr);
 
   ExecuteAsThread(ApiGetScoreThread,
                  "ApiGetScore", data,
@@ -9334,6 +9348,30 @@ struct ApiAddScoreThreadData
   struct ScoreEntry score_entry;
 };
 
+static void *CreateThreadData_ApiAddScore(int nr, char *score_tape_filename)
+{
+  struct ApiAddScoreThreadData *data =
+    checked_malloc(sizeof(struct ApiAddScoreThreadData));
+  struct ScoreEntry *score_entry = &scores.entry[scores.last_added];
+
+  if (score_tape_filename == NULL)
+    score_tape_filename = getScoreTapeFilename(score_entry->tape_basename, nr);
+
+  data->level_nr = nr;
+  data->score_entry = *score_entry;
+  data->score_tape_filename = getStringCopy(score_tape_filename);
+
+  return data;
+}
+
+static void FreeThreadData_ApiAddScore(void *data_raw)
+{
+  struct ApiAddScoreThreadData *data = data_raw;
+
+  checked_free(data->score_tape_filename);
+  checked_free(data);
+}
+
 static void ApiAddScoreExt(struct HttpRequest *request,
                           struct HttpResponse *response,
                           int level_nr,
@@ -9465,8 +9503,7 @@ static int ApiAddScoreThread(void *data_raw)
              data->score_tape_filename,
              &data->score_entry);
 
-  checked_free(data->score_tape_filename);
-  checked_free(data);
+  FreeThreadData_ApiAddScore(data_raw);
 
   return 0;
 }
@@ -9474,15 +9511,7 @@ static int ApiAddScoreThread(void *data_raw)
 static void ApiAddScoreAsThread(int nr, char *score_tape_filename)
 {
   struct ApiAddScoreThreadData *data =
-    checked_malloc(sizeof(struct ApiAddScoreThreadData));
-  struct ScoreEntry *score_entry = &scores.entry[scores.last_added];
-
-  if (score_tape_filename == NULL)
-    score_tape_filename = getScoreTapeFilename(score_entry->tape_basename, nr);
-
-  data->level_nr = nr;
-  data->score_entry = *score_entry;
-  data->score_tape_filename = getStringCopy(score_tape_filename);
+    CreateThreadData_ApiAddScore(nr, score_tape_filename);
 
   ExecuteAsThread(ApiAddScoreThread,
                  "ApiAddScore", data,
index 8a5279b71a0122705844c3f1aaef7498052d3bf1..46632bc45b0c39fd4bc0ccd791edde19261b9e74 100644 (file)
@@ -4048,6 +4048,26 @@ struct ApiRenamePlayerThreadData
   char *player_uuid;
 };
 
+static void *CreateThreadData_ApiRenamePlayer(void)
+{
+  struct ApiRenamePlayerThreadData *data =
+    checked_malloc(sizeof(struct ApiRenamePlayerThreadData));
+
+  data->player_name = getStringCopy(setup.player_name);
+  data->player_uuid = getStringCopy(setup.player_uuid);
+
+  return data;
+}
+
+static void FreeThreadData_ApiRenamePlayer(void *data_raw)
+{
+  struct ApiRenamePlayerThreadData *data = data_raw;
+
+  checked_free(data->player_name);
+  checked_free(data->player_uuid);
+  checked_free(data);
+}
+
 static void ApiRenamePlayerExt(struct HttpRequest *request,
                               struct HttpResponse *response,
                               char *player_name_raw,
@@ -4114,9 +4134,7 @@ static int ApiRenamePlayerThread(void *data_raw)
 
   ApiRenamePlayer(data->player_name, data->player_uuid);
 
-  checked_free(data->player_name);
-  checked_free(data->player_uuid);
-  checked_free(data);
+  FreeThreadData_ApiRenamePlayer(data_raw);
 
   return 0;
 }
@@ -4124,10 +4142,7 @@ static int ApiRenamePlayerThread(void *data_raw)
 static void ApiRenamePlayerAsThread(void)
 {
   struct ApiRenamePlayerThreadData *data =
-    checked_malloc(sizeof(struct ApiRenamePlayerThreadData));
-
-  data->player_name = getStringCopy(setup.player_name);
-  data->player_uuid = getStringCopy(setup.player_uuid);
+    CreateThreadData_ApiRenamePlayer();
 
   ExecuteAsThread(ApiRenamePlayerThread,
                  "ApiRenamePlayer", data,