@@ -647,37 +647,43 @@ char *DurationString(uint64_t duration) {
647647 return s ;
648648} // End of DurationString
649649
650- void InitStringlist (stringlist_t * list , uint32_t capacity ) {
651- list -> list = NULL ;
652- list -> num_strings = 0 ;
653- list -> capacity = capacity ;
654-
655- } // End of InitStringlist
656-
657- void InsertString (stringlist_t * list , char * string ) {
658- if (!list -> list ) {
659- // default if not yet initialised
660- if (list -> capacity == 0 ) list -> capacity = 8 ;
661- list -> num_strings = 0 ;
662- list -> list = (char * * )malloc (list -> capacity * sizeof (char * ));
663- if (!list -> list ) {
664- LogError ("malloc() error in %s line %d: %s" , __FILE__ , __LINE__ , strerror (errno ));
665- exit (250 );
666- }
650+ stringlist_t * NewStringlist (stringlist_t * list , uint32_t capacity ) {
651+ stringlist_t * sl = calloc (1 , sizeof (stringlist_t ));
652+ if (!sl ) {
653+ LogError ("calloc() error in %s line %d: %s" , __FILE__ , __LINE__ , strerror (errno ));
654+ return NULL ;
667655 }
668- list -> list [list -> num_strings ++ ] = string ? strdup (string ) : NULL ;
656+ return sl ;
657+ } // End of NewStringlist
669658
670- // if all slots used, double capacity
671- if (list -> num_strings == list -> capacity ) {
672- list -> capacity += list -> capacity ;
673- list -> list = (char * * )realloc (list -> list , list -> capacity * sizeof (char * ));
674- if (!list -> list ) {
659+ void InsertString ( stringlist_t * sl , const char * s ) {
660+ if (sl -> num_strings == sl -> capacity ) {
661+ sl -> capacity = sl -> capacity ? sl -> capacity * 2 : 16 ;
662+ sl -> list = (char * * )realloc (sl -> list , sl -> capacity * sizeof (char * ));
663+ if (!sl -> list ) {
675664 LogError ("realloc() error in %s line %d: %s" , __FILE__ , __LINE__ , strerror (errno ));
676- exit (250 );
665+ exit (EXIT_FAILURE );
677666 }
678667 }
679668
680- } // End of InsertString
669+ if (s ) {
670+ sl -> list [sl -> num_strings ++ ] = strdup (s );
671+ } else {
672+ /* allow explicit NULL sentinel */
673+ sl -> list [sl -> num_strings ++ ] = NULL ;
674+ }
675+ } // // End of InsertString
676+
677+ void ClearStringList (stringlist_t * sl ) {
678+ if (sl -> list ) free (sl -> list );
679+ memset (sl , 0 , sizeof (stringlist_t ));
680+ } // End of ClearStringList
681+
682+ void FreeStringList (stringlist_t * sl ) {
683+ if (sl == NULL ) return ;
684+ ClearStringList (sl );
685+ free (sl );
686+ } // End of ClearStringList
681687
682688void format_number (uint64_t num , numStr s , int plain , int fixed_width ) {
683689 double f = num ;
0 commit comments