diff options
author | Jorge Boncompte [DTI2] <jorge@dti2.net> | 2012-05-07 16:53:14 +0000 |
---|---|---|
committer | David Lamparter <equinox@opensourcerouting.org> | 2012-05-22 20:50:14 +0200 |
commit | 22714f99c4ffeb4d1bade7ad6374adeba0e06e4c (patch) | |
tree | 43b6e720bc05ad1bc5748d8d8a08c725a7fe6a90 /lib/thread.c | |
parent | 64018324d5e5071eea3b3f72f939d91dc7aef029 (diff) |
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>
Diffstat (limited to 'lib/thread.c')
-rw-r--r-- | lib/thread.c | 37 |
1 files changed, 11 insertions, 26 deletions
diff --git a/lib/thread.c b/lib/thread.c index 3740147e..86d0ff8c 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -235,7 +235,7 @@ cpu_record_hash_alloc (struct cpu_thread_history *a) 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 @@ cpu_record_hash_free (void *a) { struct cpu_thread_history *hist = a; - XFREE (MTYPE_THREAD_FUNCNAME, hist->funcname); XFREE (MTYPE_THREAD_STATS, hist); } @@ -303,7 +302,7 @@ cpu_record_print(struct vty *vty, thread_type filter) 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 @@ thread_list_free (struct thread_master *m, struct thread_list *list) 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 @@ thread_timer_remain_second (struct thread *thread) } /* 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 @@ strip_funcname (const char *funcname) tmp = *e; *e = '\0'; - ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b); + strcpy (dest, b); *e = tmp; - - return ret; } /* Get new thread. */ @@ -669,12 +664,7 @@ thread_get (struct thread_master *m, u_char type, { 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_get (struct thread_master *m, u_char type, thread->func = func; thread->arg = arg; - thread->funcname = strip_funcname(funcname); + strip_funcname (thread->funcname, funcname); return thread; } @@ -953,7 +943,6 @@ thread_run (struct thread_master *m, struct thread *thread, { *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 @@ thread_call (struct thread *thread) 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 @@ thread_call (struct thread *thread) realtime/1000, cputime/1000); } #endif /* CONSUMED_TIME_CHECK */ - - XFREE (MTYPE_THREAD_FUNCNAME, thread->funcname); } /* Execute thread */ @@ -1249,10 +1236,8 @@ funcname_thread_execute (struct thread_master *m, 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; } |