# HG changeset patch # User Matti Hamalainen # Date 1578253607 -7200 # Node ID 8d914417227d6a990506715ecab950cf660929ae # Parent 41fe5ec247260b9263910b5e2a2364501c60acf5 Avoid allocating one extra STIL node. Does not make much of a difference. diff -r 41fe5ec24726 -r 8d914417227d sidlib.c --- a/sidlib.c Sun Jan 05 21:45:40 2020 +0200 +++ b/sidlib.c Sun Jan 05 21:46:47 2020 +0200 @@ -594,17 +594,19 @@ } -static int sidlib_stildb_entry_realloc(SIDLibSTILNode *node, const int nsubTunes) +static int sidlib_stildb_entry_realloc(SIDLibSTILNode *node, const int nsubTune, const BOOL alloc) { if (node == NULL) return THERR_NULLPTR; - // Re-allocate subTune structure if needed - if (nsubTunes > node->nsubTunes) + // Re-allocate subtunes pointers structure if needed + if (nsubTune > node->nsubTunes) { size_t clearIndex, clearLength; - node->subTunes = (SIDLibSTILSubNode **) th_realloc(node->subTunes, (nsubTunes + 1) * sizeof(SIDLibSTILSubNode **)); + node->subTunes = (SIDLibSTILSubNode **) th_realloc( + node->subTunes, (nsubTune + 1) * sizeof(SIDLibSTILSubNode **)); + if (node->subTunes == NULL) return THERR_MALLOC; @@ -612,24 +614,24 @@ if (node->nsubTunes == 0) { clearIndex = 0; - clearLength = nsubTunes + 1; + clearLength = nsubTune + 1; } else { clearIndex = node->nsubTunes + 1; - clearLength = nsubTunes - clearIndex + 1; + clearLength = nsubTune - clearIndex + 1; } memset(&(node->subTunes[clearIndex]), 0, clearLength * sizeof(SIDLibSTILSubNode **)); - node->nsubTunes = nsubTunes; + node->nsubTunes = nsubTune; } - // Allocate memory for subTune - if (node->subTunes[nsubTunes] == NULL) + // Allocate memory for the specified subtune + if (alloc && node->subTunes[nsubTune] == NULL) { - node->subTunes[nsubTunes] = (SIDLibSTILSubNode *) th_malloc0(sizeof(SIDLibSTILSubNode)); - if (node->subTunes[nsubTunes] == NULL) + node->subTunes[nsubTune] = (SIDLibSTILSubNode *) th_malloc0(sizeof(SIDLibSTILSubNode)); + if (node->subTunes[nsubTune] == NULL) return THERR_MALLOC; } @@ -673,7 +675,7 @@ if ((node->filename = th_strdup(filename)) == NULL) return THERR_MALLOC; - if ((res = sidlib_stildb_entry_realloc(node, 1)) != THERR_OK) + if ((res = sidlib_stildb_entry_realloc(node, 1, FALSE)) != THERR_OK) { sidlib_stildb_node_free(node); return res; @@ -1033,7 +1035,7 @@ char *data; // Supported field, add it - if ((ret = sidlib_stildb_entry_realloc(entry, subtune)) != THERR_OK) + if ((ret = sidlib_stildb_entry_realloc(entry, subtune, TRUE)) != THERR_OK) goto out; if ((data = th_strdup(tmpStr)) == NULL)