From f6a336ce890492bcccdf09397d3201f9e1b468e4 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 23 Jan 2013 14:48:43 -0500 Subject: [PATCH] prefer objects on stack instead of heap when possible --- daemon/aux.c | 6 ++---- daemon/aux.h | 2 +- daemon/call.c | 32 ++++++++++++++------------------ 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/daemon/aux.c b/daemon/aux.c index fd97d04e8..5890e9d3a 100644 --- a/daemon/aux.c +++ b/daemon/aux.c @@ -40,8 +40,7 @@ GList *g_list_link(GList *list, GList *el) { } -GQueue *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; @@ -49,7 +48,6 @@ GQueue *pcre_multi_match(pcre *re, pcre_extra *ree, const char *s, unsigned int unsigned int i; void *ins; - q = g_queue_new(); el = malloc(sizeof(*el) * num); for (start = 0, len = strlen(s); pcre_exec(re, ree, s + start, len - start, 0, 0, ovec, G_N_ELEMENTS(ovec)) > 0; start += ovec[1]) { @@ -69,7 +67,7 @@ GQueue *pcre_multi_match(pcre *re, pcre_extra *ree, const char *s, unsigned int free(el); - return q; + return q ? q->length : 0; } diff --git a/daemon/aux.h b/daemon/aux.h index dc7124503..4ac21254a 100644 --- a/daemon/aux.h +++ b/daemon/aux.h @@ -46,7 +46,7 @@ typedef int (*parse_func)(char **, void **, void *); GList *g_list_link(GList *, GList *); -GQueue *pcre_multi_match(pcre *, pcre_extra *, const char *, unsigned int, parse_func, void *); +int pcre_multi_match(pcre *, pcre_extra *, const char *, unsigned int, parse_func, void *, GQueue *); void strmove(char **, char **); void strdupfree(char **, const char *); diff --git a/daemon/call.c b/daemon/call.c index 9049edd70..0c57aaf95 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -444,11 +444,9 @@ static int info_parse_func(char **a, void **ret, void *p) { static void info_parse(const char *s, struct call *c) { - GQueue *q; struct callmaster *m = c->callmaster; - q = pcre_multi_match(m->info_re, m->info_ree, s, 2, info_parse_func, c); - g_queue_free(q); + pcre_multi_match(m->info_re, m->info_ree, s, 2, info_parse_func, c, NULL); } @@ -483,22 +481,20 @@ fail: } -static GQueue *streams_parse(const char *s, struct callmaster *m) { +static void streams_parse(const char *s, struct callmaster *m, GQueue *q) { int i; i = 0; - return pcre_multi_match(m->streams_re, m->streams_ree, s, 3, streams_parse_func, &i); + pcre_multi_match(m->streams_re, m->streams_ree, s, 3, streams_parse_func, &i, q); } static void streams_free(GQueue *q) { struct stream *s; - while (q->head) { - s = g_queue_pop_head(q); - free(s->mediatype); + while ((s = g_queue_pop_head(q))) { + if (s->mediatype) + free(s->mediatype); g_slice_free1(sizeof(*s), s); } - - g_queue_free(q); } @@ -1744,7 +1740,7 @@ fail: char *call_request(const char **out, struct callmaster *m) { struct call *c; - GQueue *s; + GQueue s = G_QUEUE_INIT; int num; char *ret; @@ -1753,9 +1749,9 @@ char *call_request(const char **out, struct callmaster *m) { c->calling_agent = (out[RE_TCP_RL_AGENT] && *out[RE_TCP_RL_AGENT]) ? call_strdup(c, out[RE_TCP_RL_AGENT]) : "UNKNOWN"; info_parse(out[RE_TCP_RL_INFO], c); - s = streams_parse(out[RE_TCP_RL_STREAMS], m); - num = call_streams(c, s, g_hash_table_lookup(c->infohash, "fromtag"), 0); - streams_free(s); + streams_parse(out[RE_TCP_RL_STREAMS], m, &s); + num = call_streams(c, &s, g_hash_table_lookup(c->infohash, "fromtag"), 0); + streams_free(&s); ret = streams_print(c->callstreams, abs(num), (num >= 0) ? 0 : 1, NULL, 0); mutex_unlock(&c->lock); @@ -1769,7 +1765,7 @@ char *call_request(const char **out, struct callmaster *m) { char *call_lookup(const char **out, struct callmaster *m) { struct call *c; - GQueue *s; + GQueue s = G_QUEUE_INIT; int num; char *ret; @@ -1787,9 +1783,9 @@ char *call_lookup(const char **out, struct callmaster *m) { c->called_agent = (out[RE_TCP_RL_AGENT] && *out[RE_TCP_RL_AGENT]) ? call_strdup(c, out[RE_TCP_RL_AGENT]) : "UNKNOWN"; info_parse(out[RE_TCP_RL_INFO], c); - s = streams_parse(out[RE_TCP_RL_STREAMS], m); - num = call_streams(c, s, g_hash_table_lookup(c->infohash, "totag"), 1); - streams_free(s); + streams_parse(out[RE_TCP_RL_STREAMS], m, &s); + num = call_streams(c, &s, g_hash_table_lookup(c->infohash, "totag"), 1); + streams_free(&s); ret = streams_print(c->callstreams, abs(num), (num >= 0) ? 1 : 0, NULL, 0); mutex_unlock(&c->lock);