lib: do not allocate/free thread funcnames
This avoids memory heap fragmentation and imposses less load on the
system memory allocator.
* thread.h: FUNCNAME_LEN defined to 64 (ISO C99 says max 63)
Signed-off-by: Jorge Boncompte [DTI2] <jorge@dti2.net>
[changed FUNCNAME_LEN to a less arbitrary value]
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/lib/memtypes.c b/lib/memtypes.c
index 1723490..bbf9692 100644
--- a/lib/memtypes.c
+++ b/lib/memtypes.c
@@ -21,7 +21,6 @@
{ MTYPE_THREAD, "Thread" },
{ MTYPE_THREAD_MASTER, "Thread master" },
{ MTYPE_THREAD_STATS, "Thread stats" },
- { MTYPE_THREAD_FUNCNAME, "Thread function name" },
{ MTYPE_VTY, "VTY" },
{ MTYPE_VTY_OUT_BUF, "VTY output buffer" },
{ MTYPE_VTY_HIST, "VTY history" },
diff --git a/lib/thread.c b/lib/thread.c
index 3740147..86d0ff8 100644
--- a/lib/thread.c
+++ b/lib/thread.c
@@ -235,7 +235,7 @@
struct cpu_thread_history *new;
new = XCALLOC (MTYPE_THREAD_STATS, sizeof (struct cpu_thread_history));
new->func = a->func;
- new->funcname = XSTRDUP(MTYPE_THREAD_FUNCNAME, a->funcname);
+ strcpy(new->funcname, a->funcname);
return new;
}
@@ -244,7 +244,6 @@
{
struct cpu_thread_history *hist = a;
- XFREE (MTYPE_THREAD_FUNCNAME, hist->funcname);
XFREE (MTYPE_THREAD_STATS, hist);
}
@@ -303,7 +302,7 @@
void *args[3] = {&tmp, vty, &filter};
memset(&tmp, 0, sizeof tmp);
- tmp.funcname = (char *)"TOTAL";
+ strcpy(tmp.funcname, "TOTAL");
tmp.types = filter;
#ifdef HAVE_RUSAGE
@@ -577,8 +576,6 @@
for (t = list->head; t; t = next)
{
next = t->next;
- if (t->funcname)
- XFREE (MTYPE_THREAD_FUNCNAME, t->funcname);
XFREE (MTYPE_THREAD, t);
list->count--;
m->alloc--;
@@ -636,11 +633,11 @@
}
/* Trim blankspace and "()"s */
-static char *
-strip_funcname (const char *funcname)
+void
+strip_funcname (char *dest, const char *funcname)
{
- char buff[100];
- char tmp, *ret, *e, *b = buff;
+ char buff[FUNCNAME_LEN];
+ char tmp, *e, *b = buff;
strncpy(buff, funcname, sizeof(buff));
buff[ sizeof(buff) -1] = '\0';
@@ -656,10 +653,8 @@
tmp = *e;
*e = '\0';
- ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b);
+ strcpy (dest, b);
*e = tmp;
-
- return ret;
}
/* Get new thread. */
@@ -669,12 +664,7 @@
{
struct thread *thread = thread_trim_head (&m->unuse);
- if (thread)
- {
- if (thread->funcname)
- XFREE(MTYPE_THREAD_FUNCNAME, thread->funcname);
- }
- else
+ if (! thread)
{
thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
m->alloc++;
@@ -685,7 +675,7 @@
thread->func = func;
thread->arg = arg;
- thread->funcname = strip_funcname(funcname);
+ strip_funcname (thread->funcname, funcname);
return thread;
}
@@ -953,7 +943,6 @@
{
*fetch = *thread;
thread->type = THREAD_UNUSED;
- thread->funcname = NULL; /* thread_call will free fetch's copied pointer */
thread_add_unuse (m, thread);
return fetch;
}
@@ -1187,7 +1176,7 @@
struct cpu_thread_history tmp;
tmp.func = thread->func;
- tmp.funcname = thread->funcname;
+ strcpy(tmp.funcname, thread->funcname);
thread->hist = hash_get (cpu_record, &tmp,
(void * (*) (void *))cpu_record_hash_alloc);
@@ -1227,8 +1216,6 @@
realtime/1000, cputime/1000);
}
#endif /* CONSUMED_TIME_CHECK */
-
- XFREE (MTYPE_THREAD_FUNCNAME, thread->funcname);
}
/* Execute thread */
@@ -1249,10 +1236,8 @@
dummy.func = func;
dummy.arg = arg;
dummy.u.val = val;
- dummy.funcname = strip_funcname (funcname);
+ strip_funcname (dummy.funcname, funcname);
thread_call (&dummy);
- XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname);
-
return NULL;
}
diff --git a/lib/thread.h b/lib/thread.h
index 56f4d07..67902cf 100644
--- a/lib/thread.h
+++ b/lib/thread.h
@@ -62,6 +62,9 @@
typedef unsigned char thread_type;
+/* ISO C99 maximum function name length is 63 */
+#define FUNCNAME_LEN 64
+
/* Thread itself. */
struct thread
{
@@ -79,13 +82,12 @@
} u;
struct timeval real;
struct cpu_thread_history *hist; /* cache pointer to cpu_history */
- char* funcname;
+ char funcname[FUNCNAME_LEN];
};
struct cpu_thread_history
{
int (*func)(struct thread *);
- char *funcname;
unsigned int total_calls;
struct time_stats
{
@@ -95,6 +97,7 @@
struct time_stats cpu;
#endif
thread_type types;
+ char funcname[FUNCNAME_LEN];
};
/* Clocks supported by Quagga */