Fix:maptool:Made more memory efficient
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 8 Nov 2009 20:50:58 +0000 (20:50 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 8 Nov 2009 20:50:58 +0000 (20:50 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk/navit@2730 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/maptool/ch.c
navit/maptool/maptool.c
navit/maptool/tile.c

index 71f8e34..31116c1 100644 (file)
@@ -96,10 +96,23 @@ road_speed(enum item_type type)
        }
 }
 
+static void
+coord_slice_free(void *data)
+{
+       g_slice_free(struct coord, data);
+}
+
+
 static GHashTable *
 coord_hash_new(void)
 {
-        return g_hash_table_new_full(coord_hash, coord_equal, g_free, NULL);
+        return g_hash_table_new_full(coord_hash, coord_equal, coord_slice_free, NULL);
+}
+
+static void
+item_id_slice_free(void *data)
+{
+       g_slice_free(struct item_id, data);
 }
 
 #define sq(x) ((double)(x)*(x))
@@ -108,7 +121,7 @@ static void
 add_node_to_hash(FILE *idx, GHashTable *hash, struct coord *c, int *nodes)
 {
        if (! g_hash_table_lookup(hash, c)) {
-               struct coord *ct=g_new(struct coord, 1);
+               struct coord *ct=g_slice_new(struct coord);
                *ct=*c;
                fwrite(c, sizeof(*c), 1, idx);
                (*nodes)++;
@@ -117,6 +130,12 @@ add_node_to_hash(FILE *idx, GHashTable *hash, struct coord *c, int *nodes)
 
 }
 
+static void
+edge_hash_slice_free(void *data)
+{
+       g_slice_free(struct edge_hash_item, data);
+}
+
 static guint
 edge_hash_hash(gconstpointer key)
 {
@@ -150,7 +169,7 @@ ch_generate_ddsg(FILE *in, FILE *ref, FILE *idx, FILE *ddsg)
                        edges++;
                }
        }
-       edge_hash=g_hash_table_new_full(edge_hash_hash, edge_hash_equal, g_free, g_free);
+       edge_hash=g_hash_table_new_full(edge_hash_hash, edge_hash_equal, edge_hash_slice_free, item_id_slice_free);
        fseek(in, 0, SEEK_SET);
        fprintf(ddsg,"d\n");
        fprintf(ddsg,"%d %d\n", nodes, edges);
@@ -162,8 +181,8 @@ ch_generate_ddsg(FILE *in, FILE *ref, FILE *idx, FILE *ddsg)
                double l;
                fread(&road_id, sizeof(road_id), 1, ref);
                if (speed) {
-                       struct edge_hash_item *hi=g_new(struct edge_hash_item, 1);
-                       struct item_id *id=g_new(struct item_id, 1);
+                       struct edge_hash_item *hi=g_slice_new(struct edge_hash_item);
+                       struct item_id *id=g_slice_new(struct item_id);
                        *id=road_id;
                        dbg_assert((n1=GPOINTER_TO_INT(g_hash_table_lookup(hash, &c[0]))) != 0);
                        dbg_assert((n2=GPOINTER_TO_INT(g_hash_table_lookup(hash, &c[ccount-1]))) != 0);
@@ -439,11 +458,12 @@ ch_assemble_map(char *map_suffix, char *suffix, struct zip_info *zip_info)
        FILE *ref=tempfile(suffix,"sgr_ref",1);
        struct item_id id;
        int nodeid=0;
-       
+
        create_tile_hash();
+
        th=tile_head_root;
         while (th) {
-               th->zip_data=malloc(th->total_size);
+               th->zip_data=NULL;
                th->process=1;
                 th=th->next;
         }
@@ -452,9 +472,9 @@ ch_assemble_map(char *map_suffix, char *suffix, struct zip_info *zip_info)
        ch_copy_to_tiles(suffix, ch_levels, &info, ref);
        fclose(ref);
        ref=tempfile(suffix,"sgr_ref",0);
-       sgr_nodes_hash=g_hash_table_new_full(NULL, NULL, NULL, g_free);
+       sgr_nodes_hash=g_hash_table_new_full(NULL, NULL, NULL, item_id_slice_free);
        while (fread(&id, sizeof(id), 1, ref)) {
-               struct item_id *id2=g_new(struct item_id, 1);
+               struct item_id *id2=g_slice_new(struct item_id);
                *id2=id;
 #if 0
                dbg(0,"%d is "ITEM_ID_FMT"\n",nodeid,ITEM_ID_ARGS(*id2));
@@ -490,6 +510,7 @@ ch_assemble_map(char *map_suffix, char *suffix, struct zip_info *zip_info)
                } else {
                        fwrite(th->zip_data, th->total_size, 1, zip_info->index);
                }
+               g_free(th->zip_data);
                 th=th->next;
         }
 }
index e0dd1a9..eacd59e 100644 (file)
@@ -168,6 +168,7 @@ int main(int argc, char **argv)
        int i;
        main_init(argv[0]);
        struct zip_info zip_info;
+       int suffix_start=0;
 
        while (1) {
 #if 0
@@ -445,11 +446,11 @@ int main(int argc, char **argv)
                }
                exit(0);
        }
-       for (i = 0 ; i < suffix_count ; i++) {
+       for (i = suffix_start ; i < suffix_count ; i++) {
                suffix=suffixes[i];
                if (start <= 4) {
                        phase=3;
-                       if (i == 0) {
+                       if (i == suffix_start) {
                                memset(&zip_info, 0, sizeof(zip_info));
                        }
                        zipnum=zip_info.zipnum;
@@ -480,7 +481,7 @@ int main(int argc, char **argv)
                if (start <= 5) {
                        phase=4;
                        fprintf(stderr,"PROGRESS: Phase 5: assembling map %s\n",suffix);
-                       if (i == 0) {
+                       if (i == suffix_start) {
                                zip_info.dir_size=0;
                                zip_info.offset=0;
                                zip_info.maxnamelen=14+strlen(suffixes[0]);
index f96a1ae..7c5bd39 100644 (file)
@@ -314,7 +314,8 @@ write_item(char *tile, struct item_bin *ib, FILE *reference)
                        fwrite(&th->zipnum, sizeof(th->zipnum), 1, reference);
                        fwrite(&offset, sizeof(th->total_size_used), 1, reference);
                }
-               memcpy(th->zip_data+th->total_size_used, ib, size);
+               if (th->zip_data)
+                       memcpy(th->zip_data+th->total_size_used, ib, size);
                th->total_size_used+=size;
        } else {
                fprintf(stderr,"no tile hash found for %s\n", tile);