Hi all,
Both internal state variables, lclptr and gmtptr are allocated once but
never freed so I was wondering if this little diff which avoids the
unnecessary dynamic allocations would have any use ?
Thanks in advance.
Regards.
Index: localtime.c
===================================================================
RCS file: /cvs/src/lib/libc/time/localtime.c,v
retrieving revision 1.52
diff -u -p -r1.52 localtime.c
--- localtime.c 7 Apr 2015 01:47:04 -0000 1.52
+++ localtime.c 7 Sep 2015 19:58:34 -0000
@@ -168,8 +168,10 @@ static int tzload(const char * name, st
static int tzparse(const char * name, struct state * sp,
int lastditch);
-static struct state * lclptr;
-static struct state * gmtptr;
+static struct state lclmem;
+static struct state gmtmem;
+static struct state * lclptr = &lclmem;
+static struct state * gmtptr = &gmtmem;
#ifndef TZ_STRLEN_MAX
@@ -1093,13 +1095,6 @@ tzsetwall_basic(void)
return;
lcl_is_set = -1;
- if (lclptr == NULL) {
- lclptr = calloc(1, sizeof *lclptr);
- if (lclptr == NULL) {
- settzname(); /* all we can do */
- return;
- }
- }
if (tzload(NULL, lclptr, TRUE) != 0)
gmtload(lclptr);
settzname();
@@ -1137,13 +1132,6 @@ tzset_basic(void)
if (lcl_is_set)
strlcpy(lcl_TZname, name, sizeof lcl_TZname);
- if (lclptr == NULL) {
- lclptr = calloc(1, sizeof *lclptr);
- if (lclptr == NULL) {
- settzname(); /* all we can do */
- return;
- }
- }
if (*name == '\0') {
/*
** User wants it fast rather than right.
@@ -1309,9 +1297,7 @@ gmtsub(const time_t *timep, long offset,
_THREAD_PRIVATE_MUTEX_LOCK(gmt);
if (!gmt_is_set) {
gmt_is_set = TRUE;
- gmtptr = (struct state *) calloc(1, sizeof *gmtptr);
- if (gmtptr != NULL)
- gmtload(gmtptr);
+ gmtload(gmtptr);
}
_THREAD_PRIVATE_MUTEX_UNLOCK(gmt);
result = timesub(timep, offset, gmtptr, tmp);
@@ -1323,12 +1309,8 @@ gmtsub(const time_t *timep, long offset,
*/
if (offset != 0)
tmp->TM_ZONE = wildabbr;
- else {
- if (gmtptr == NULL)
- tmp->TM_ZONE = (char *)gmt;
- else
- tmp->TM_ZONE = gmtptr->chars;
- }
+ else
+ tmp->TM_ZONE = gmtptr->chars;
#endif /* defined TM_ZONE */
return result;
}