From 6c804046b36cdb413a78e2cb3225f9980c5842dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20=27Flameeyes=27=20Petten=C3=B2?= Date: Wed, 7 May 2008 19:53:17 +0200 Subject: Optimise input_vcd: don't use a static-sized filelist array, and use asprintf where appropriate. Instead of creating a 100-elements filelist array, with each element containing a 256 bytes string, make it dynamic, create it when get_autoplay_list is called, free it if it was present before, and use asprintf() for creating the entries. This not only reduces the complexity of the code, but also reduces the size of the input class object (which in turn reduce memory usage), but also avoids memory leaks that the previous code certainly had. The total memory used even with a full list of files should also be drastically reduced. Also, use asprintf while filling the mrls data, rather than allocating a 1 KiB array on the stack, and use it as temporary. --- src/input/input_vcd.c | 52 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/input/input_vcd.c b/src/input/input_vcd.c index 760037e93..90f6fcc70 100644 --- a/src/input/input_vcd.c +++ b/src/input/input_vcd.c @@ -84,7 +84,7 @@ typedef struct { const char *device; - char *filelist[100]; + char **filelist; int mrls_allocated_entries; xine_mrl_t **mrls; @@ -924,6 +924,18 @@ static const char *vcd_class_get_identifier (input_class_t *this_gen) { return "vcdo"; } +static void vcd_filelist_dispose(vcd_input_class_t *this) { + if ( this->filelist == NULL ) return; + + char **entry = this->filelist; + + while(*(entry)) { + free(*(entry++)); + } + + free(this->filelist); +} + static void vcd_class_dispose (input_class_t *this_gen) { vcd_input_class_t *this = (vcd_input_class_t *) this_gen; @@ -932,9 +944,7 @@ static void vcd_class_dispose (input_class_t *this_gen) { config->unregister_callback(config, "media.vcd.device"); - for (i = 0; i < 100; i++) - free (this->filelist[i]); - + vcd_filelist_dispose(this); free (this->mrls); free (this); } @@ -982,11 +992,6 @@ static xine_mrl_t **vcd_class_get_dir (input_class_t *this_gen, const char *file /* printf ("%d tracks\n", this->total_tracks); */ for (i=1; itotal_tracks; i++) { /* FIXME: check if track 0 contains valid data */ - char mrl[1024]; - - memset(&mrl, 0, sizeof (mrl)); - sprintf(mrl, "vcdo:/%d",i); - if((i-1) >= this->mrls_allocated_entries) { ++this->mrls_allocated_entries; /* note: 1 extra pointer for terminating NULL */ @@ -997,11 +1002,9 @@ static xine_mrl_t **vcd_class_get_dir (input_class_t *this_gen, const char *file memset(this->mrls[(i-1)], 0, sizeof(xine_mrl_t)); } - this->mrls[i-1]->origin = NULL; - free(this->mrls[(i-1)]->mrl); - this->mrls[i-1]->mrl = strdup(mrl); - this->mrls[i-1]->link = NULL; - this->mrls[i-1]->type = (0 | mrl_vcd); + asprintf(&(this->mrls[i-1]->mrl), "vcdo:/%d", i); + + this->mrls[i-1]->type = mrl_vcd; /* hack */ this->mrls[i-1]->size = vcd_plugin_get_length ((input_plugin_t *) this); @@ -1048,20 +1051,15 @@ static char ** vcd_class_get_autoplay_list (input_class_t *this_gen, int *num_fi fd = -1; *num_files = this->total_tracks - 1; - - /* printf ("%d tracks\n", this->total_tracks); */ - - for (i = 1; i < this->total_tracks; i++) { /* FIXME: check if track 0 contains valid data */ - if(this->filelist[i - 1] == NULL) - this->filelist[i - 1] = (char *) realloc(this->filelist[i - 1], sizeof(char *) * 256); + vcd_filelist_dispose(this); + this->filelist = calloc(this->total_tracks+1, sizeof(char*)); - sprintf (this->filelist[i - 1], "vcdo:/%d",i); - /* printf ("list[%d] : %d %s\n", i, this->filelist[i-1], this->filelist[i-1]); */ - } + /* FIXME: check if track 0 contains valid data */ + for (i = 1; i < this->total_tracks; i++) + asprintf(&this->filelist[i-1], "vcdo:/%d", i); - this->filelist[i - 1] = (char *) realloc(this->filelist[i-1], sizeof(char *)); - this->filelist[i - 1] = NULL; + /* printf ("%d tracks\n", this->total_tracks); */ return this->filelist; } @@ -1093,10 +1091,6 @@ static void *init_class (xine_t *xine, void *data) { this->mrls = calloc(1, sizeof(xine_mrl_t*)); this->mrls_allocated_entries = 0; - for (i = 0; i < 100; i++) { - this->filelist[i] = calloc(256, sizeof(char)); - } - return this; } -- cgit v1.2.3