Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2005:
[Freeciv-Dev] (PR#14427) sizeof cleanups in server/
Home

[Freeciv-Dev] (PR#14427) sizeof cleanups in server/

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#14427) sizeof cleanups in server/
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 22 Oct 2005 12:12:18 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=14427 >

This patch replaces hard-typed sizeofs with variable sizeofs in the server.

I also fixed 2 places where sizeof was erronously used instead of 
ARRAY_SIZE.

-jason

Index: server/gotohand.c
===================================================================
--- server/gotohand.c   (revision 11170)
+++ server/gotohand.c   (working copy)
@@ -121,8 +121,11 @@
 static struct mappos_array *get_empty_array(void)
 {
   struct mappos_array *parray;
-  if (!mappos_arrays[array_count])
-    mappos_arrays[array_count] = fc_malloc(sizeof(struct mappos_array));
+
+  if (!mappos_arrays[array_count]) {
+    mappos_arrays[array_count]
+      = fc_malloc(sizeof(*mappos_arrays[array_count]));
+  }
   parray = mappos_arrays[array_count++];
   parray->first_pos = 0;
   parray->last_pos = -1;
Index: server/generator/height_map.c
===================================================================
--- server/generator/height_map.c       (revision 11170)
+++ server/generator/height_map.c       (working copy)
@@ -72,7 +72,7 @@
 void make_random_hmap(int smooth)
 {
   int i = 0;
-  height_map = fc_malloc(sizeof(int) * MAP_INDEX_SIZE);
+  height_map = fc_malloc(sizeof(*height_map) * MAP_INDEX_SIZE);
 
   INITIALIZE_ARRAY(height_map, MAP_INDEX_SIZE, myrand(1000 * smooth));
 
@@ -183,7 +183,7 @@
   /* edges are avoided more strongly as this increases */
   int avoidedge = (100 - map.landpercent) * step / 100 + step / 3; 
 
-  height_map = fc_malloc(sizeof(int) * MAP_INDEX_SIZE);
+  height_map = fc_malloc(sizeof(*height_map) * MAP_INDEX_SIZE);
 
  /* initialize map */
   INITIALIZE_ARRAY(height_map, MAP_INDEX_SIZE, 0);
Index: server/citytools.c
===================================================================
--- server/citytools.c  (revision 11170)
+++ server/citytools.c  (working copy)
@@ -1681,7 +1681,7 @@
   }
 
   if (!plrtile->city) {
-    pdcity = plrtile->city = fc_malloc(sizeof(struct dumb_city));
+    pdcity = plrtile->city = fc_malloc(sizeof(*pdcity));
     plrtile->city->id = pcity->id;
   }
   if (pdcity->id != pcity->id) {
Index: server/report.c
===================================================================
--- server/report.c     (revision 11170)
+++ server/report.c     (working copy)
@@ -187,7 +187,7 @@
     } /* else the player is dead or barbarian or observer */
   } players_iterate_end;
 
-  qsort(size, j, sizeof(struct player_score_entry), secompare);
+  qsort(size, j, sizeof(size[0]), secompare);
   buffer[0] = '\0';
   for (i = 0; i < j; i++) {
     if (i == 0 || size[i].value < size[i - 1].value) {
@@ -243,8 +243,7 @@
       if (value_of_pcity > size[NUM_BEST_CITIES - 1].value) {
        size[NUM_BEST_CITIES - 1].value = value_of_pcity;
        size[NUM_BEST_CITIES - 1].city = pcity;
-       qsort(size, NUM_BEST_CITIES, sizeof(struct player_score_entry),
-             secompare);
+       qsort(size, NUM_BEST_CITIES, sizeof(size[0]), secompare);
       }
     } city_list_iterate_end;
   } players_iterate_end;
@@ -1083,7 +1082,7 @@
     }
   } players_iterate_end;
 
-  qsort(size, j, sizeof(struct player_score_entry), secompare);
+  qsort(size, j, sizeof(size[0]), secompare);
 
   packet.nscores = j;
   for (i = 0; i < j; i++) {
Index: server/ruleset.c
===================================================================
--- server/ruleset.c    (revision 11170)
+++ server/ruleset.c    (working copy)
@@ -1792,7 +1792,7 @@
     struct ruler_title *title;
 
     g->num_ruler_titles = 1;
-    g->ruler_titles = fc_calloc(1, sizeof(struct ruler_title));
+    g->ruler_titles = fc_calloc(1, sizeof(*g->ruler_titles));
     title = &(g->ruler_titles[0]);
 
     title->nation = DEFAULT_TITLE;
@@ -1928,7 +1928,7 @@
    * all the name data.  The array is NULL-terminated by
    * having a NULL name at the end.
    */
-  city_names = fc_calloc(dim + 1, sizeof(struct city_name));
+  city_names = fc_calloc(dim + 1, sizeof(*city_names));
   city_names[dim].name = NULL;
 
   /*
Index: server/airgoto.c
===================================================================
--- server/airgoto.c    (revision 11170)
+++ server/airgoto.c    (working copy)
@@ -112,7 +112,7 @@
     /* If refuels.alloc_size was zero (on the first call), 
      * then refuels.points is NULL and realloc will actually malloc */
     refuels.points = fc_realloc(refuels.points, 
-                                refuels.alloc_size * sizeof(struct refuel));
+                                refuels.alloc_size * sizeof(*refuels.points));
     /* This memory, because refuels is static, is never freed.
      * It is just reused. */  
   }
Index: server/gamehand.c
===================================================================
--- server/gamehand.c   (revision 11170)
+++ server/gamehand.c   (working copy)
@@ -51,7 +51,7 @@
     "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   int i;
 
-  for (i = 0; i < sizeof(game.id) - 1; i++) {
+  for (i = 0; i < ARRAY_SIZE(game.id) - 1; i++) {
     game.id[i] = chars[myrand(sizeof(chars) - 1)];
   }
   game.id[i] = '\0';
Index: server/maphand.c
===================================================================
--- server/maphand.c    (revision 11170)
+++ server/maphand.c    (working copy)
@@ -1082,7 +1082,7 @@
       /* Set and send new city info */
       if (from_tile->city) {
        if (!dest_tile->city) {
-         dest_tile->city = fc_malloc(sizeof(struct dumb_city));
+         dest_tile->city = fc_malloc(sizeof(*dest_tile->city));
        }
        *dest_tile->city = *from_tile->city;
        send_city_info_at_tile(pdest, pdest->connections, NULL, ptile);
Index: server/gamelog.c
===================================================================
--- server/gamelog.c    (revision 11170)
+++ server/gamelog.c    (working copy)
@@ -639,7 +639,7 @@
   } players_iterate_end;
 
   buffer[0] = '\0';
-  qsort(rank, count, sizeof(struct player_score_entry), secompare1);
+  qsort(rank, count, sizeof(rank[0]), secompare1);
 
   for (i = 0; i < count; i++) {
     cat_snprintf(buffer, len, "<plr><no>%d</no><r>%d</r><s>%d</s></plr>",
Index: server/diplhand.c
===================================================================
--- server/diplhand.c   (revision 11170)
+++ server/diplhand.c   (working copy)
@@ -675,7 +675,7 @@
   if (could_meet_with_player(pplayer, pother)) {
     struct Treaty *ptreaty;
 
-    ptreaty = fc_malloc(sizeof(struct Treaty));
+    ptreaty = fc_malloc(sizeof(*ptreaty));
     init_treaty(ptreaty, pplayer, pother);
     treaty_list_prepend(treaties, ptreaty);
 
Index: server/savegame.c
===================================================================
--- server/savegame.c   (revision 11170)
+++ server/savegame.c   (working copy)
@@ -2343,7 +2343,7 @@
        nat_y = secfile_lookup_int(file, "player%d.dc%d.y", plrno, j);
        ptile = native_pos_to_tile(nat_x, nat_y);
 
-       pdcity = fc_malloc(sizeof(struct dumb_city));
+       pdcity = fc_malloc(sizeof(*pdcity));
        pdcity->id = secfile_lookup_int(file, "player%d.dc%d.id", plrno, j);
        sz_strlcpy(pdcity->name, secfile_lookup_str(file, "player%d.dc%d.name", 
plrno, j));
        pdcity->size = secfile_lookup_int(file, "player%d.dc%d.size", plrno, j);
@@ -2825,7 +2825,7 @@
       }
     }
     citymap_buf[j]='\0';
-    assert(j < sizeof(citymap_buf));
+    assert(j < ARRAY_SIZE(citymap_buf));
     secfile_insert_str(file, citymap_buf, "player%d.c%d.workers", plrno, i);
 
     secfile_insert_bool(file, pcity->production.is_unit, 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#14427) sizeof cleanups in server/, Jason Short <=