From 9d3ba4e95167edd59a941acf22e62cf1b4fc4050 Mon Sep 17 00:00:00 2001 From: horwitz Date: Thu, 5 Jan 2006 01:01:45 +0000 Subject: [PATCH] remove more mallocs and frees git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@61 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- navit/src/block.c | 5 ++--- navit/src/file.c | 11 +++++----- .../gtk_drawing_area/graphics_gtk_drawing_area.c | 2 +- navit/src/popup.c | 25 ++++++++++------------ navit/src/street.c | 3 +-- 5 files changed, 20 insertions(+), 26 deletions(-) diff --git a/navit/src/block.c b/navit/src/block.c index 8363749..8081e92 100644 --- a/navit/src/block.c +++ b/navit/src/block.c @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include "file.h" #include "block.h" #include "data.h" @@ -116,9 +116,8 @@ block_file_private_setup(struct file *file) char *str; struct file_private *file_priv; - file_priv=malloc(sizeof(*file_priv)); + file_priv=g_malloc0(sizeof(*file_priv)); file->private=file_priv; - memset(file_priv, 0, sizeof(*file_priv)); p=file->begin+0x0c; while (*p) { diff --git a/navit/src/file.c b/navit/src/file.c index eb85ea1..cdff873 100644 --- a/navit/src/file.c +++ b/navit/src/file.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -15,19 +14,18 @@ struct file * file_create(char *name) { struct stat stat; - struct file *file=malloc(sizeof(*file)+strlen(name)+1); + struct file *file= g_new0(struct file,1); if (! file) return file; file->fd=open(name, O_RDONLY); if (file->fd < 0) { - free(file); + g_free(file); return NULL; } fstat(file->fd, &stat); file->size=stat.st_size; - file->name=(char *)file+sizeof(*file); - strcpy(file->name, name); + file->name = g_strdup(name); file->begin=mmap(NULL, file->size, PROT_READ|PROT_WRITE, MAP_PRIVATE, file->fd, 0); g_assert(file->begin != NULL); if (file->begin == (void *)0xffffffff) { @@ -129,7 +127,8 @@ file_destroy(struct file *f) { close(f->fd); munmap(f->begin, f->size); - free(f); + g_free(f->name); + g_free(f); } int diff --git a/navit/src/graphics/gtk_drawing_area/graphics_gtk_drawing_area.c b/navit/src/graphics/gtk_drawing_area/graphics_gtk_drawing_area.c index 5402ee5..903148c 100644 --- a/navit/src/graphics/gtk_drawing_area/graphics_gtk_drawing_area.c +++ b/navit/src/graphics/gtk_drawing_area/graphics_gtk_drawing_area.c @@ -195,7 +195,7 @@ display_text_render_shadow(struct text_glyph *g) unsigned char *p, *pm=g->pixmap; GdkImage *ret; - shadow=malloc(str*(g->h+2)); + shadow=malloc(str*(g->h+2)); /* do not use g_malloc() here */ memset(shadow, 0, str*(g->h+2)); for (y = 0 ; y < h ; y++) { p=shadow+str*y; diff --git a/navit/src/popup.c b/navit/src/popup.c index 5c3b8bf..bf6fea0 100644 --- a/navit/src/popup.c +++ b/navit/src/popup.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -61,19 +60,20 @@ param_to_menu_new(char *name,struct param_list *plist, int c, int iso) ret=popup_item_new_text(NULL,name,1); last=NULL; for (i = 0 ; i < c ; i++) { - char name_buffer[strlen(plist[i].name)+strlen(plist[i].value)+2]; - char *text=name_buffer; + char *name_buffer = g_strjoin(":",plist[i].name, plist[i].value,NULL); + char *text = name_buffer; - sprintf(name_buffer,"%s:%s", plist[i].name, plist[i].value); if (iso) { text=g_convert(name_buffer,-1,"utf-8","iso8859-1",NULL,NULL,NULL); if (! text) { printf("problem converting '%s'\n", name_buffer); } } - curr=popup_item_new_text(&last, text, i); + curr=popup_item_new_text(&last,text,i); if (iso) - free(text); + g_free(text); + g_free(name_buffer); + } ret->submenu=last; return ret; @@ -194,17 +194,14 @@ popup_display_list_default(struct display_list *d, struct popup_item **popup_lis if (d->type == 4) desc="Point"; seg=(struct segment *)(d->data); if (seg) { - if (d->label && strlen(d->label)) { - item_text=malloc(strlen(desc)+strlen(d->label)+2); - strcpy(item_text, desc); - strcat(item_text," "); - strcat(item_text, d->label); - } else { - item_text=desc; - } + if (d->label && strlen(d->label)) + item_text=g_strjoin(" ",desc,d->label,NULL); + else + item_text=g_strdup(desc); text=g_convert(item_text,-1,"utf-8","iso8859-1",NULL,NULL,NULL); curr_item=popup_item_new_text(popup_list,text,1); g_free(text); + g_free(item_text); curr_item->submenu=param_to_menu_new("File", plist, file_get_param(seg->blk_inf.file, plist, 100), 1); submenu=curr_item->submenu; diff --git a/navit/src/street.c b/navit/src/street.c index b38e31d..0560135 100644 --- a/navit/src/street.c +++ b/navit/src/street.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include "container.h" @@ -349,7 +348,7 @@ street_coord_get(struct block_info *blk_inf, struct street_str *str) if (debug) printf("p=%p points=%d\n",p_sav, points); p=p_sav; - ret=malloc(sizeof(struct street_coord)+points*sizeof(struct coord)); + ret=g_malloc(sizeof(struct street_coord)+points*sizeof(struct coord)); ret->count=points; c=ret->c; while (points) { -- 2.7.4