Do a bit of code cleanup.

- convert handling of IE PLTYPEs to switch statements
 - add braces to various small blocks
 - remove a bit of trailing whitespace
 - remove a couple of unnecessary ast_strdupa() uses


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@191848 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2009-05-02 21:15:18 +00:00
parent a3af213506
commit a398f9e0ac

View File

@@ -339,60 +339,95 @@ enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type typ
ie_type = va_arg(ap, enum ast_event_type)) ie_type = va_arg(ap, enum ast_event_type))
{ {
struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value)); struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
int insert = 1;
memset(ie_value, 0, sizeof(*ie_value)); memset(ie_value, 0, sizeof(*ie_value));
ie_value->ie_type = ie_type; ie_value->ie_type = ie_type;
ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype); ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_UINT) switch (ie_value->ie_pltype) {
case AST_EVENT_IE_PLTYPE_UINT:
ie_value->payload.uint = va_arg(ap, uint32_t); ie_value->payload.uint = va_arg(ap, uint32_t);
else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_STR) break;
ie_value->payload.str = ast_strdupa(va_arg(ap, const char *)); case AST_EVENT_IE_PLTYPE_STR:
else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) { ie_value->payload.str = va_arg(ap, const char *);
break;
case AST_EVENT_IE_PLTYPE_RAW:
{
void *data = va_arg(ap, void *); void *data = va_arg(ap, void *);
size_t datalen = va_arg(ap, size_t); size_t datalen = va_arg(ap, size_t);
ie_value->payload.raw = alloca(datalen); ie_value->payload.raw = alloca(datalen);
memcpy(ie_value->payload.raw, data, datalen); memcpy(ie_value->payload.raw, data, datalen);
ie_value->raw_datalen = datalen; ie_value->raw_datalen = datalen;
break;
}
case AST_EVENT_IE_PLTYPE_UNKNOWN:
insert = 0;
case AST_EVENT_IE_PLTYPE_EXISTS:
break;
}
if (insert) {
AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
} }
AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
} }
va_end(ap); va_end(ap);
AST_RWDLLIST_RDLOCK(&ast_event_subs[type]); AST_RWDLLIST_RDLOCK(&ast_event_subs[type]);
AST_RWDLLIST_TRAVERSE(&ast_event_subs[type], sub, entry) { AST_RWDLLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) { AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
int break_out = 0;
AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) { AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
if (sub_ie_val->ie_type == ie_val->ie_type) if (sub_ie_val->ie_type == ie_val->ie_type) {
break; break;
}
} }
if (!sub_ie_val) { if (!sub_ie_val) {
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) /* This subscriber doesn't care about this IE, so consider
break; * it matched. */
continue; continue;
} }
/* The subscriber doesn't actually care what the value is */
if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) switch (ie_val->ie_pltype) {
continue; case AST_EVENT_IE_PLTYPE_UINT:
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT && break_out = (ie_val->payload.uint != sub_ie_val->payload.uint);
ie_val->payload.uint != sub_ie_val->payload.uint)
break; break;
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR && case AST_EVENT_IE_PLTYPE_STR:
strcmp(ie_val->payload.str, sub_ie_val->payload.str)) break_out = strcmp(ie_val->payload.str, sub_ie_val->payload.str);
break; break;
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW && case AST_EVENT_IE_PLTYPE_RAW:
memcmp(ie_val->payload.raw, sub_ie_val->payload.raw, ie_val->raw_datalen)) break_out = memcmp(ie_val->payload.raw,
sub_ie_val->payload.raw, ie_val->raw_datalen);
break; break;
case AST_EVENT_IE_PLTYPE_EXISTS:
/* The subscriber doesn't actually care what the value is */
break_out = 1;
break;
case AST_EVENT_IE_PLTYPE_UNKNOWN:
break;
}
if (break_out) {
break;
}
} }
if (!ie_val)
if (!ie_val) {
/* Everything matched */
break; break;
}
} }
AST_RWDLLIST_UNLOCK(&ast_event_subs[type]); AST_RWDLLIST_UNLOCK(&ast_event_subs[type]);
if (sub) /* All parameters were matched */ if (sub) {
/* All parameters were matched */
return AST_EVENT_SUB_EXISTS; return AST_EVENT_SUB_EXISTS;
}
AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]); AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
if (!AST_DLLIST_EMPTY(&ast_event_subs[AST_EVENT_ALL])) if (!AST_DLLIST_EMPTY(&ast_event_subs[AST_EVENT_ALL])) {
res = AST_EVENT_SUB_EXISTS; res = AST_EVENT_SUB_EXISTS;
}
AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]); AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
return res; return res;
@@ -529,13 +564,15 @@ void ast_event_report_subs(const struct ast_event_sub *event_sub)
AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]); AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) { AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
if (event_sub == sub) if (event_sub == sub) {
continue; continue;
}
event = gen_sub_event(sub); event = gen_sub_event(sub);
if (!event) if (!event) {
continue; continue;
}
event_sub->cb(event, event_sub->userdata); event_sub->cb(event, event_sub->userdata);
@@ -544,7 +581,7 @@ void ast_event_report_subs(const struct ast_event_sub *event_sub)
AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]); AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
} }
struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type, struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
ast_event_cb_t cb, void *userdata) ast_event_cb_t cb, void *userdata)
{ {
struct ast_event_sub *sub; struct ast_event_sub *sub;
@@ -554,8 +591,9 @@ struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
return NULL; return NULL;
} }
if (!(sub = ast_calloc(1, sizeof(*sub)))) if (!(sub = ast_calloc(1, sizeof(*sub)))) {
return NULL; return NULL;
}
sub->type = type; sub->type = type;
sub->cb = cb; sub->cb = cb;
@@ -570,11 +608,13 @@ int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
{ {
struct ast_event_ie_val *ie_val; struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) {
return -1; return -1;
}
if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
return -1; return -1;
}
ie_val->ie_type = ie_type; ie_val->ie_type = ie_type;
ie_val->payload.uint = unsigned_int; ie_val->payload.uint = unsigned_int;
@@ -590,11 +630,13 @@ int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
{ {
struct ast_event_ie_val *ie_val; struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) {
return -1; return -1;
}
if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
return -1; return -1;
}
ie_val->ie_type = ie_type; ie_val->ie_type = ie_type;
ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS; ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
@@ -604,16 +646,18 @@ int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
return 0; return 0;
} }
int ast_event_sub_append_ie_str(struct ast_event_sub *sub, int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
enum ast_event_ie_type ie_type, const char *str) enum ast_event_ie_type ie_type, const char *str)
{ {
struct ast_event_ie_val *ie_val; struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) {
return -1; return -1;
}
if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
return -1; return -1;
}
ie_val->ie_type = ie_type; ie_val->ie_type = ie_type;
ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR; ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
@@ -630,16 +674,18 @@ int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
return 0; return 0;
} }
int ast_event_sub_append_ie_raw(struct ast_event_sub *sub, int ast_event_sub_append_ie_raw(struct ast_event_sub *sub,
enum ast_event_ie_type ie_type, void *data, size_t raw_datalen) enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
{ {
struct ast_event_ie_val *ie_val; struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX) {
return -1; return -1;
}
if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
return -1; return -1;
}
ie_val->ie_type = ie_type; ie_val->ie_type = ie_type;
ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW; ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
@@ -666,8 +712,9 @@ int ast_event_sub_activate(struct ast_event_sub *sub)
event = gen_sub_event(sub); event = gen_sub_event(sub);
if (event) if (event) {
ast_event_queue(event); ast_event_queue(event);
}
} }
AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]); AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
@@ -677,15 +724,16 @@ int ast_event_sub_activate(struct ast_event_sub *sub)
return 0; return 0;
} }
struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb, struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
void *userdata, ...) void *userdata, ...)
{ {
va_list ap; va_list ap;
enum ast_event_ie_type ie_type; enum ast_event_ie_type ie_type;
struct ast_event_sub *sub; struct ast_event_sub *sub;
if (!(sub = ast_event_subscribe_new(type, cb, userdata))) if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
return NULL; return NULL;
}
va_start(ap, userdata); va_start(ap, userdata);
for (ie_type = va_arg(ap, enum ast_event_type); for (ie_type = va_arg(ap, enum ast_event_type);
@@ -734,8 +782,9 @@ void ast_event_sub_destroy(struct ast_event_sub *sub)
{ {
struct ast_event_ie_val *ie_val; struct ast_event_ie_val *ie_val;
while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
ast_event_ie_val_destroy(ie_val); ast_event_ie_val_destroy(ie_val);
}
ast_free(sub); ast_free(sub);
} }
@@ -751,14 +800,15 @@ struct ast_event_sub *ast_event_unsubscribe(struct ast_event_sub *sub)
if (ast_event_check_subscriber(AST_EVENT_UNSUB, if (ast_event_check_subscriber(AST_EVENT_UNSUB,
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type, AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) { AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
event = ast_event_new(AST_EVENT_UNSUB, event = ast_event_new(AST_EVENT_UNSUB,
AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid, AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type, AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
AST_EVENT_IE_END); AST_EVENT_IE_END);
if (event) if (event) {
ast_event_queue(event); ast_event_queue(event);
}
} }
ast_event_sub_destroy(sub); ast_event_sub_destroy(sub);
@@ -771,7 +821,6 @@ void ast_event_iterator_init(struct ast_event_iterator *iterator, const struct a
iterator->event_len = ntohs(event->event_len); iterator->event_len = ntohs(event->event_len);
iterator->event = event; iterator->event = event;
iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) ); iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
return;
} }
int ast_event_iterator_next(struct ast_event_iterator *iterator) int ast_event_iterator_next(struct ast_event_iterator *iterator)
@@ -842,8 +891,9 @@ const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_i
int res = 0; int res = 0;
for (ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) { for (ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
if (ast_event_iterator_get_ie_type(&iterator) == ie_type) if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
return ast_event_iterator_get_ie_raw(&iterator); return ast_event_iterator_get_ie_raw(&iterator);
}
} }
return NULL; return NULL;
@@ -881,8 +931,9 @@ int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_
event_len = ntohs((*event)->event_len); event_len = ntohs((*event)->event_len);
extra_len = sizeof(*ie) + data_len; extra_len = sizeof(*ie) + data_len;
if (!(*event = ast_realloc(*event, event_len + extra_len))) if (!(*event = ast_realloc(*event, event_len + extra_len))) {
return -1; return -1;
}
ie = (struct ast_event_ie *) ( ((char *) *event) + event_len ); ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
ie->ie_type = htons(ie_type); ie->ie_type = htons(ie_type);
@@ -915,40 +966,70 @@ struct ast_event *ast_event_new(enum ast_event_type type, ...)
ie_type = va_arg(ap, enum ast_event_type)) ie_type = va_arg(ap, enum ast_event_type))
{ {
struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value)); struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
int insert = 1;
memset(ie_value, 0, sizeof(*ie_value)); memset(ie_value, 0, sizeof(*ie_value));
ie_value->ie_type = ie_type; ie_value->ie_type = ie_type;
ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype); ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_UINT) switch (ie_value->ie_pltype) {
case AST_EVENT_IE_PLTYPE_UINT:
ie_value->payload.uint = va_arg(ap, uint32_t); ie_value->payload.uint = va_arg(ap, uint32_t);
else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_STR) break;
ie_value->payload.str = ast_strdupa(va_arg(ap, const char *)); case AST_EVENT_IE_PLTYPE_STR:
else if (ie_value->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) { ie_value->payload.str = va_arg(ap, const char *);
break;
case AST_EVENT_IE_PLTYPE_RAW:
{
void *data = va_arg(ap, void *); void *data = va_arg(ap, void *);
size_t datalen = va_arg(ap, size_t); size_t datalen = va_arg(ap, size_t);
ie_value->payload.raw = alloca(datalen); ie_value->payload.raw = alloca(datalen);
memcpy(ie_value->payload.raw, data, datalen); memcpy(ie_value->payload.raw, data, datalen);
ie_value->raw_datalen = datalen; ie_value->raw_datalen = datalen;
break;
}
case AST_EVENT_IE_PLTYPE_UNKNOWN:
insert = 0;
break;
case AST_EVENT_IE_PLTYPE_EXISTS:
break;
}
if (insert) {
AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
} }
AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
} }
va_end(ap); va_end(ap);
if (!(event = ast_calloc(1, sizeof(*event)))) if (!(event = ast_calloc(1, sizeof(*event)))) {
return NULL; return NULL;
}
event->type = htons(type); event->type = htons(type);
event->event_len = htons(sizeof(*event)); event->event_len = htons(sizeof(*event));
AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) { AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) switch (ie_val->ie_pltype) {
case AST_EVENT_IE_PLTYPE_STR:
ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str); ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW)
ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
if (!event)
break; break;
case AST_EVENT_IE_PLTYPE_UINT:
ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
break;
case AST_EVENT_IE_PLTYPE_RAW:
ast_event_append_ie_raw(&event, ie_val->ie_type,
ie_val->payload.raw, ie_val->raw_datalen);
break;
case AST_EVENT_IE_PLTYPE_EXISTS:
ast_log(LOG_WARNING, "PLTYPE_EXISTS unsupported in event_new\n");
break;
case AST_EVENT_IE_PLTYPE_UNKNOWN:
ast_log(LOG_WARNING, "PLTYPE_UNKNOWN passed as an IE type "
"for a new event\n");
break;
}
if (!event) {
break;
}
} }
if (!ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) { if (!ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {