summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrhaas <rhaas@17b73243-c579-4c4c-a9d2-2d5706c11dac>2014-03-15 23:56:14 +0000
committerrhaas <rhaas@17b73243-c579-4c4c-a9d2-2d5706c11dac>2014-03-15 23:56:14 +0000
commit9b0bea0cfb240f57aedca5cbb5fada076f3a6f43 (patch)
treef7128b38d2faf9ef5e0cf7bfc9fddec3b9af8320 /src
parentd842694cd77a0bb7218bf2d2fc65af59fea44a9a (diff)
automatically expand StringLists when required
This works around issues where ActiveThorns tries to activated more thorns than are compiled into the executable. Not change in behaviour if the initial size of the StringList is large enough. git-svn-id: http://svn.cactuscode.org/flesh/trunk@5097 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src')
-rw-r--r--src/main/ActiveThorns.c6
-rw-r--r--src/util/StringList.c74
2 files changed, 76 insertions, 4 deletions
diff --git a/src/main/ActiveThorns.c b/src/main/ActiveThorns.c
index 54b1f455..94b62cc3 100644
--- a/src/main/ActiveThorns.c
+++ b/src/main/ActiveThorns.c
@@ -977,7 +977,8 @@ int CCTKi_ActivateThorns(const char *activethornlist)
new_thorn = this_thorn->activates_thorns[i];
if (! CCTK_IsThornActive(new_thorn))
{
- switch (Util_StringListAdd(new_thorns, new_thorn))
+ const int ierr = Util_StringListAdd(new_thorns, new_thorn);
+ switch (ierr)
{
case 0:
/* Thorn already scheduled for activation */
@@ -988,7 +989,8 @@ int CCTKi_ActivateThorns(const char *activethornlist)
did_add_thorns = 1;
break;
default:
- CCTK_Warn(0, __LINE__, __FILE__, "Cactus", "Internal error");
+ CCTK_VError(__LINE__, __FILE__, "Cactus", "Internal error: %d",
+ ierr);
}
}
}
diff --git a/src/util/StringList.c b/src/util/StringList.c
index 2634e17a..d29e903a 100644
--- a/src/util/StringList.c
+++ b/src/util/StringList.c
@@ -3,12 +3,13 @@
@date Mon May 21 16:55:14 2001
@author Tom Goodale
@desc
- Stuff for fixed-size lists of sorted unique strings.
+ Stuff for lists of sorted unique strings.
@enddesc
@version $Header$
@@*/
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#ifndef TEST_STRINGLIST
#include "cctk.h"
@@ -30,6 +31,7 @@ CCTK_FILEVERSION(util_StringList_c);
/********************************************************************
********************* Local Routine Prototypes *********************
********************************************************************/
+static int Utili_StringListExpand(uStringList *list);
/********************************************************************
********************* Other Routine Prototypes *********************
@@ -56,7 +58,7 @@ CCTK_FILEVERSION(util_StringList_c);
@endhistory
@var size
- @vdesc Size of the list
+ @vdesc Initial size of the list
@vtype int
@vio in
@vcomment
@@ -132,6 +134,10 @@ int Util_StringListAdd(uStringList *list, const char *item)
{
if (list->max_size < list->fill + 1)
{
+ Utili_StringListExpand(list);
+ }
+ if (list->max_size < list->fill + 1)
+ {
retval = -2;
}
else
@@ -153,6 +159,10 @@ int Util_StringListAdd(uStringList *list, const char *item)
{
if (list->max_size < list->fill + 1)
{
+ Utili_StringListExpand(list);
+ }
+ if (list->max_size < list->fill + 1)
+ {
retval = -2;
}
else
@@ -186,6 +196,10 @@ int Util_StringListAdd(uStringList *list, const char *item)
{
if (list->max_size < list->fill + 1)
{
+ Utili_StringListExpand(list);
+ }
+ if (list->max_size < list->fill + 1)
+ {
retval = -2;
}
else
@@ -295,6 +309,62 @@ void Util_StringListDestroy(uStringList *this)
********************* Local Routines *************************
********************************************************************/
+ /*@@
+ @routine Util_StringListExpand
+ @date Sat Mar 15 15:27:08 PDT 2014
+ @author Roland Haas
+ @desc
+ Doubles size of a stringlist.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+ @var this
+ @vdesc The stringlist object
+ @vtype uStringList
+ @vio inout
+ @vcomment
+
+ @endvar
+
+ @returntype int
+ @returndesc
+ 1 if memory could be allocated
+ 0 on failure
+ @endreturndesc
+@@*/
+static int Utili_StringListExpand(uStringList *list)
+{
+ int retval;
+
+ struct iInternalStringList *temp =
+ realloc(list->list,(2*list->max_size+1)*sizeof(struct iInternalStringList));
+ if(temp)
+ {
+ memset(temp+list->max_size+1, 0,
+ list->max_size*sizeof(struct iInternalStringList));
+ /* re-link entries based on their position in the (old) array */
+ for(int i=0; i < list->fill; i++)
+ {
+ temp[i].next = temp[i].next ? temp+(temp[i].next-list->list) : NULL;
+ }
+ list->head = list->head ? temp+(list->head-list->list) : NULL;
+ list->current = list->current ? temp+(list->current-list->list) : NULL;
+ list->list = temp;
+ list->max_size = 2*list->max_size;
+
+ retval = 1;
+ }
+ else
+ {
+ retval = 0;
+ }
+
+ return retval;
+}
+
#ifdef TEST_STRINGLIST
int main(int argc, const char *argv[])