Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2003:
[Freeciv-Dev] (PR#6822) some variable-sized arrays
Home

[Freeciv-Dev] (PR#6822) some variable-sized arrays

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#6822) some variable-sized arrays
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 10 Nov 2003 16:56:27 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=6822 >

The attached patch replaces some malloc'd arrays with variable-sized 
stack arrays.  One memory leak is fixed in the process.

This should be faster, but my tests didn't show a measurable difference 
(on systems with inefficient malloc calls this may make an impact - but 
then those systems aren't likely to compile C99 anyway).  It's also 
safer and more legible.

jason

Index: common/unit.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.h,v
retrieving revision 1.103
diff -u -r1.103 unit.h
--- common/unit.h       2003/10/29 08:00:39     1.103
+++ common/unit.h       2003/11/11 00:54:21
@@ -182,7 +182,7 @@
 { \
   int _size = unit_list_size(&unitlist); \
   if (_size > 0) { \
-    int *_ids = fc_malloc(sizeof(int) * _size); \
+    int _ids[_size]; \
     int _i = 0; \
     unit_list_iterate(unitlist, punit) { \
       _ids[_i++] = punit->id; \
@@ -194,7 +194,6 @@
 #define unit_list_iterate_safe_end \
       } \
     } \
-    free(_ids); \
   } \
 }
 
Index: server/gamelog.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gamelog.c,v
retrieving revision 1.30
diff -u -r1.30 gamelog.c
--- server/gamelog.c    2003/10/12 10:36:36     1.30
+++ server/gamelog.c    2003/11/11 00:54:22
@@ -136,10 +136,7 @@
 void gamelog_save(void) {
   int i, count = 0;
   char buffer[4096];
-  struct player_score_entry *size =
-    fc_malloc(sizeof(struct player_score_entry) * game.nplayers);
-  struct player_score_entry *rank =
-    fc_malloc(sizeof(struct player_score_entry) * game.nplayers);
+  struct player_score_entry size[game.nplayers], rank[game.nplayers];
 
   players_iterate(pplayer) {
     if (!is_barbarian(pplayer)) {
@@ -197,7 +194,6 @@
                 size[i].value);
   }
   gamelog(GAMELOG_EOT,buffer);
-  free(size);
   qsort(rank, count, sizeof(struct player_score_entry), secompare1);
   buffer[0] = 0;
   for (i = 0; i < count; i++) {
@@ -217,6 +213,4 @@
                 game.players[rank[i].idx].is_connected, rank[i].value);
   }
   gamelog(GAMELOG_STATUS, buffer);
-
-  free(rank);
 }
Index: server/report.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/report.c,v
retrieving revision 1.47
diff -u -r1.47 report.c
--- server/report.c     2003/10/12 10:36:36     1.47
+++ server/report.c     2003/11/11 00:54:22
@@ -160,8 +160,7 @@
   int i, j = 0, rank = 0;
   char buffer[4096];
   char title[1024];
-  struct player_score_entry *size =
-      fc_malloc(sizeof(struct player_score_entry) * game.nplayers);
+  struct player_score_entry size[game.nplayers];
 
   players_iterate(pplayer) {
     if (pplayer->is_alive && !is_barbarian(pplayer)) {
@@ -199,7 +198,6 @@
                 _("%2d: The %s %s\n"), rank + 1, _(greatness[rank]),
                 get_nation_name_plural(size[i].player->nation));
   }
-  free(size);
   my_snprintf(title, sizeof(title), _(historian_message[which_news]),
     _(historian_name[myrand(ARRAY_SIZE(historian_name))]));
   page_conn_etype(&game.game_connections, _("Historian Publishes!"),
@@ -230,8 +228,7 @@
   const int NUM_BEST_CITIES = 5;
   /* a wonder equals WONDER_FACTOR citizen */
   const int WONDER_FACTOR = 5;
-  struct city_score_entry *size =
-      fc_malloc(sizeof(struct city_score_entry) * NUM_BEST_CITIES);
+  struct city_score_entry size[NUM_BEST_CITIES];
   int i;
   char buffer[4096];
 
@@ -278,7 +275,6 @@
                   PL_("with %d wonder\n", "with %d wonders\n", wonders),
                   wonders);}
   }
-  free(size);
   page_conn(dest, _("Traveler's Report:"),
            _("The Five Greatest Cities in the World!"), buffer);
 }
@@ -1079,8 +1075,7 @@
 {
   int i, j = 0;
   char buffer[4096];
-  struct player_score_entry *size =
-      fc_malloc(sizeof(struct player_score_entry) * game.nplayers);
+  struct player_score_entry size[game.nplayers];
 
   players_iterate(pplayer) {
     if (!is_barbarian(pplayer)) {
@@ -1102,7 +1097,6 @@
                 get_nation_name_plural(size[i].player->nation),
                 size[i].value);
   }
-  free(size);
   page_conn(&game.game_connections,
            _("Progress Scores:"),
            _("The Greatest Civilizations in the world."), buffer);
@@ -1114,8 +1108,7 @@
 void report_final_scores(void)
 {
   int i, j = 0;
-  struct player_score_entry *size =
-      fc_malloc(sizeof(struct player_score_entry) * game.nplayers);
+  struct player_score_entry size[game.nplayers];
   struct packet_endgame_report packet;
 
   players_iterate(pplayer) {
Index: server/savegame.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/savegame.c,v
retrieving revision 1.140
diff -u -r1.140 savegame.c
--- server/savegame.c   2003/10/27 14:25:24     1.140
+++ server/savegame.c   2003/11/11 00:54:23
@@ -72,7 +72,7 @@
 #define SAVE_MAP_DATA(map_x, map_y, nat_x, nat_y, line,                     \
                       GET_XY_CHAR, SECFILE_INSERT_LINE)                     \
 {                                                                           \
-  char *line = fc_malloc(map.xsize + 1);                                    \
+  char line[map.xsize + 1];                                                 \
   int nat_x, nat_y, map_x, map_y;                                           \
                                                                             \
   for (nat_y = 0; nat_y < map.ysize; nat_y++) {                             \
@@ -87,7 +87,6 @@
     line[map.xsize] = '\0';                                                 \
     (SECFILE_INSERT_LINE);                                                  \
   }                                                                         \
-  free(line);                                                               \
 }
 
 /*
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.144
diff -u -r1.144 srv_main.c
--- server/srv_main.c   2003/11/10 20:33:58     1.144
+++ server/srv_main.c   2003/11/11 00:54:23
@@ -1188,14 +1188,13 @@
 **************************************************************************/
 static Nation_Type_id select_random_nation(const char* class)
 {
-  Nation_Type_id* nations, selected;
+  Nation_Type_id nations[num_nations_avail], selected;
   int i, j;
   
   if (class == NULL) {
     return nations_avail[myrand(num_nations_avail)];
   }
   
-  nations = fc_malloc(num_nations_avail * sizeof(*nations));
   for (j = i = 0; i < num_nations_avail; i++) {
     struct nation_type* nation = get_nation_by_idx(nations_avail[i]);
 
@@ -1213,7 +1212,6 @@
     assert(strcmp(get_nation_by_idx(selected)->class, class) == 0);
   }
 
-  free(nations);
   return selected;
 }
 

[Prev in Thread] Current Thread [Next in Thread]