From de37ac046c32deb9091522a656275a062dd805c9 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Sun, 8 Feb 2015 16:26:07 -0500 Subject: [PATCH] use TLS buffer array for temporary strings --- daemon/aux.c | 19 ++++++++++++++++++- daemon/aux.h | 22 ++++++++++++++++++++++ daemon/call.c | 25 ++++++++++++------------- daemon/cli.c | 9 ++------- daemon/control_ng.c | 4 +--- daemon/stun.c | 5 +---- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/daemon/aux.c b/daemon/aux.c index 8856f7e76..ad34fc449 100644 --- a/daemon/aux.c +++ b/daemon/aux.c @@ -21,6 +21,9 @@ struct detach_thread { void (*func)(void *); void *data; }; +struct thread_buf { + char buf[THREAD_BUF_SIZE]; +}; static mutex_t threads_lists_lock = MUTEX_STATIC_INIT; @@ -28,6 +31,9 @@ static GList *threads_to_join; static GList *threads_running; static cond_t threads_cond = COND_STATIC_INIT; +static struct thread_buf __thread t_bufs[NUM_THREAD_BUFS]; +static int __thread t_buf_idx; + GList *g_list_link(GList *list, GList *el) { @@ -39,7 +45,9 @@ GList *g_list_link(GList *list, GList *el) { } -int pcre_multi_match(pcre *re, pcre_extra *ree, const char *s, unsigned int num, parse_func f, void *p, GQueue *q) { +int pcre_multi_match(pcre *re, pcre_extra *ree, const char *s, unsigned int num, parse_func f, + void *p, GQueue *q) +{ unsigned int start, len; int ovec[60]; int *ov; @@ -202,3 +210,12 @@ int in6_addr_eq(const void *a, const void *b) { const struct in6_addr *A = a, *B = b; return !memcmp(A, B, sizeof(*A)); } + +char *get_thread_buf(void) { + char *ret; + ret = t_bufs[t_buf_idx].buf; + t_buf_idx++; + if (t_buf_idx >= G_N_ELEMENTS(t_bufs)) + t_buf_idx = 0; + return ret; +} diff --git a/daemon/aux.h b/daemon/aux.h index 21bb0498b..96e7df160 100644 --- a/daemon/aux.h +++ b/daemon/aux.h @@ -50,6 +50,9 @@ #define UINT64F "%" G_GUINT64_FORMAT +#define THREAD_BUF_SIZE 64 +#define NUM_THREAD_BUFS 8 + @@ -59,6 +62,7 @@ GList *g_list_link(GList *, GList *); int pcre_multi_match(pcre *, pcre_extra *, const char *, unsigned int, parse_func, void *, GQueue *); INLINE void strmove(char **, char **); INLINE void strdupfree(char **, const char *); +char *get_thread_buf(void); #if !GLIB_CHECK_VERSION(2,14,0) @@ -156,6 +160,12 @@ INLINE void smart_ntop(char *o, const struct in6_addr *a, size_t len) { *o = '\0'; } +INLINE char *smart_ntop_buf(const struct in6_addr *a) { + char *buf = get_thread_buf(); + smart_ntop(buf, a, THREAD_BUF_SIZE); + return buf; +} + INLINE char *smart_ntop_p(char *o, const struct in6_addr *a, size_t len) { int l; @@ -178,6 +188,12 @@ INLINE char *smart_ntop_p(char *o, const struct in6_addr *a, size_t len) { } } +INLINE char *smart_ntop_p_buf(const struct in6_addr *a) { + char *buf = get_thread_buf(); + smart_ntop_p(buf, a, THREAD_BUF_SIZE); + return buf; +} + INLINE void smart_ntop_port(char *o, const struct sockaddr_in6 *a, size_t len) { char *e; @@ -189,6 +205,12 @@ INLINE void smart_ntop_port(char *o, const struct sockaddr_in6 *a, size_t len) { sprintf(e, ":%hu", ntohs(a->sin6_port)); } +INLINE char *smart_ntop_port_buf(const struct sockaddr_in6 *a) { + char *buf = get_thread_buf(); + smart_ntop_port(buf, a, THREAD_BUF_SIZE); + return buf; +} + INLINE int smart_pton(int af, char *src, void *dst) { char *p; int ret; diff --git a/daemon/call.c b/daemon/call.c index 8c9beb260..6492a0abb 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -599,14 +599,14 @@ static int stream_packet(struct stream_fd *sfd, str *s, struct sockaddr_in6 *fsi struct call *call; struct callmaster *cm; /*unsigned char cc;*/ - char addr[64]; + char *addr; struct endpoint endpoint; rewrite_func rwf_in, rwf_out; struct interface_address *loc_addr; call = sfd->call; cm = call->callmaster; - smart_ntop_port(addr, fsin, sizeof(addr)); + addr = smart_ntop_port_buf(fsin); rwlock_lock_r(&call->master_lock); @@ -787,15 +787,15 @@ update_addr: loc_addr = g_atomic_pointer_get(&media->local_address); if (dst && memcmp(dst, &loc_addr->addr, sizeof(*dst))) { struct interface_address *ifa; - char ifa_buf[64]; - smart_ntop(ifa_buf, dst, sizeof(ifa_buf)); ifa = get_interface_from_address(media->interface, dst); if (!ifa) { - ilog(LOG_ERROR, "No matching local interface for destination address %s found", ifa_buf); + ilog(LOG_ERROR, "No matching local interface for destination address %s found", + smart_ntop_buf(dst)); goto drop; } if (g_atomic_pointer_compare_and_exchange(&media->local_address, loc_addr, ifa)) { - ilog(LOG_INFO, "Switching local interface to %s", ifa_buf); + ilog(LOG_INFO, "Switching local interface to %s", + smart_ntop_buf(dst)); update = 1; } } @@ -1219,7 +1219,7 @@ void kill_calls_timer(GSList *list, struct callmaster *m) { struct call_monologue *cm; const char *url, *url_prefix, *url_suffix; struct xmlrpc_helper *xh = NULL; - char addr[64], url_buf[128]; + char url_buf[128]; if (!list) return; @@ -1250,9 +1250,9 @@ void kill_calls_timer(GSList *list, struct callmaster *m) { rwlock_lock_r(&ca->master_lock); if (url_prefix) { - smart_ntop_p(addr, &ca->created_from_addr.sin6_addr, sizeof(addr)); snprintf(url_buf, sizeof(url_buf), "%s%s%s", - url_prefix, addr, url_suffix); + url_prefix, smart_ntop_p_buf(&ca->created_from_addr.sin6_addr), + url_suffix); } else snprintf(url_buf, sizeof(url_buf), "%s", url_suffix); @@ -2414,7 +2414,6 @@ void call_destroy(struct call *c) { struct call_monologue *ml; struct call_media *md; GList *k, *o; - char buf[64]; struct timeval tim_result_duration; static const int CDRBUFLENGTH = 4096*2; char reasonbuf[16]; memset(&reasonbuf,0,16); @@ -2484,7 +2483,7 @@ void call_destroy(struct call *c) { if (PS_ISSET(ps, FALLBACK_RTCP)) continue; - smart_ntop_p(buf, &ps->endpoint.ip46, sizeof(buf)); + char *addr = smart_ntop_p_buf(&ps->endpoint.ip46); if (_log_facility_cdr) { const char* protocol = (!PS_ISSET(ps, RTP) && PS_ISSET(ps, RTCP)) ? "rtcp" : "rtp"; @@ -2496,7 +2495,7 @@ void call_destroy(struct call *c) { "ml%i_midx%u_%s_relayed_bytes=%llu, " "ml%i_midx%u_%s_relayed_errors=%llu, " "ml%i_midx%u_%s_last_packet=%llu, ", - cdrlinecnt, md->index, protocol, buf, + cdrlinecnt, md->index, protocol, addr, cdrlinecnt, md->index, protocol, ps->endpoint.port, cdrlinecnt, md->index, protocol, (unsigned int) (ps->sfd ? ps->sfd->fd.localport : 0), cdrlinecnt, md->index, protocol, (unsigned long long) ps->stats.packets, @@ -2509,7 +2508,7 @@ void call_destroy(struct call *c) { "%llu p, %llu b, %llu e, %llu last_packet", md->index, (unsigned int) (ps->sfd ? ps->sfd->fd.localport : 0), - buf, ps->endpoint.port, + addr, ps->endpoint.port, (!PS_ISSET(ps, RTP) && PS_ISSET(ps, RTCP)) ? " (RTCP)" : "", (unsigned long long) ps->stats.packets, (unsigned long long) ps->stats.bytes, diff --git a/daemon/cli.c b/daemon/cli.c index dfa0c6744..e6e4300ab 100644 --- a/daemon/cli.c +++ b/daemon/cli.c @@ -70,11 +70,9 @@ static void cli_incoming_list_totals(char* buffer, int len, struct callmaster* m } for (GList *l = list; l; l = l->next) { struct control_ng_stats* cur = l->data; - char buf[128]; memset(&buf,0,128); - smart_ntop_p(buf, &(cur->proxy), sizeof(buf)); printlen = snprintf(replybuffer,(outbufend-replybuffer), " %10s | %10u | %10u | %10u | %10u | %10u | %10u | %10u \n", - buf, + smart_ntop_p_buf(&cur->proxy), cur->offer, cur->answer, cur->delete, @@ -98,7 +96,6 @@ static void cli_incoming_list_callid(char* buffer, int len, struct callmaster* m struct packet_stream *ps; GSList *l; GList *k, *o; - char buf[64]; int printlen=0; char tagtypebuf[16]; memset(&tagtypebuf,0,16); struct timeval tim_result_duration; memset(&tim_result_duration,0,sizeof(struct timeval)); @@ -150,13 +147,11 @@ static void cli_incoming_list_callid(char* buffer, int len, struct callmaster* m if (PS_ISSET(ps, FALLBACK_RTCP)) continue; - smart_ntop_p(buf, &ps->endpoint.ip46, sizeof(buf)); - printlen = snprintf(replybuffer,(outbufend-replybuffer), "------ Media #%u, port %5u <> %15s:%-5hu%s, " "%llu p, %llu b, %llu e, %llu last_packet\n", md->index, (unsigned int) (ps->sfd ? ps->sfd->fd.localport : 0), - buf, ps->endpoint.port, + smart_ntop_p_buf(&ps->endpoint.ip46), ps->endpoint.port, (!PS_ISSET(ps, RTP) && PS_ISSET(ps, RTCP)) ? " (RTCP)" : "", (unsigned long long) ps->stats.packets, (unsigned long long) ps->stats.bytes, diff --git a/daemon/control_ng.c b/daemon/control_ng.c index 911479bea..dcee3c4f5 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -65,9 +65,7 @@ struct control_ng_stats* get_control_ng_stats(struct control_ng* c, const struct if (!cur) { cur = g_slice_alloc0(sizeof(struct control_ng_stats)); cur->proxy = *addr; - char buf[128]; memset(&buf,0,128); - smart_ntop_p(buf, addr, sizeof(buf)); - ilog(LOG_DEBUG,"Adding a proxy for control ng stats:%s",buf); + ilog(LOG_DEBUG,"Adding a proxy for control ng stats:%s", smart_ntop_p_buf(addr)); g_hash_table_insert(m->cngs_hash, &cur->proxy, cur); } mutex_unlock(&m->cngs_lock); diff --git a/daemon/stun.c b/daemon/stun.c index 11e1bcb8a..d0826f95c 100644 --- a/daemon/stun.c +++ b/daemon/stun.c @@ -416,7 +416,7 @@ INLINE int u_int16_t_arr_len(u_int16_t *arr) { #define SLF " from %s" -#define SLP addr +#define SLP smart_ntop_port_buf(sin) /* return values: * 0 = stun packet processed successfully * -1 = stun packet not processed, processing should continue as non-stun packet @@ -429,9 +429,6 @@ int stun(str *b, struct packet_stream *ps, struct sockaddr_in6 *sin) { struct stun_attrs attrs; u_int16_t unknowns[UNKNOWNS_COUNT]; const char *err; - char addr[64]; - - smart_ntop_port(addr, sin, sizeof(addr)); msglen = ntohs(req->msg_len); err = "message-length mismatch";