- convert the dundi precache list to use the list macros

- change an instance of malloc+memset to ast_calloc


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@23808 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2006-05-01 00:33:24 +00:00
parent c305fb082d
commit 7ce2726c09

View File

@@ -154,7 +154,7 @@ struct dundi_hint_metadata {
}; };
struct dundi_precache_queue { struct dundi_precache_queue {
struct dundi_precache_queue *next; AST_LIST_ENTRY(dundi_precache_queue) list;
char *context; char *context;
time_t expiration; time_t expiration;
char number[0]; char number[0];
@@ -259,14 +259,11 @@ struct dundi_peer {
}; };
AST_LIST_HEAD_STATIC(peers, dundi_peer); AST_LIST_HEAD_STATIC(peers, dundi_peer);
AST_LIST_HEAD_STATIC(pcq, dundi_precache_queue);
AST_LIST_HEAD_NOLOCK_STATIC(mappings, dundi_mapping); AST_LIST_HEAD_NOLOCK_STATIC(mappings, dundi_mapping);
AST_LIST_HEAD_NOLOCK_STATIC(requests, dundi_request); AST_LIST_HEAD_NOLOCK_STATIC(requests, dundi_request);
AST_LIST_HEAD_NOLOCK_STATIC(alltrans, dundi_transaction); AST_LIST_HEAD_NOLOCK_STATIC(alltrans, dundi_transaction);
static struct dundi_precache_queue *pcq;
AST_MUTEX_DEFINE_STATIC(pclock);
static int dundi_xmit(struct dundi_packet *pack); static int dundi_xmit(struct dundi_packet *pack);
static void dundi_debug_output(const char *data) static void dundi_debug_output(const char *data)
@@ -2099,30 +2096,31 @@ static void *process_precache(void *ign)
char context[256]; char context[256];
char number[256]; char number[256];
int run; int run;
for (;;) { for (;;) {
time(&now); time(&now);
run = 0; run = 0;
ast_mutex_lock(&pclock); AST_LIST_LOCK(&pcq);
if (pcq) { if ((qe = AST_LIST_FIRST(&pcq))) {
if (!pcq->expiration) { if (!qe->expiration) {
/* Gone... Remove... */ /* Gone... Remove... */
qe = pcq; AST_LIST_REMOVE_HEAD(&pcq, list);
pcq = pcq->next;
free(qe); free(qe);
} else if (pcq->expiration < now) { } else if (qe->expiration < now) {
/* Process this entry */ /* Process this entry */
pcq->expiration = 0; qe->expiration = 0;
ast_copy_string(context, pcq->context, sizeof(context)); ast_copy_string(context, qe->context, sizeof(context));
ast_copy_string(number, pcq->number, sizeof(number)); ast_copy_string(number, qe->number, sizeof(number));
run = 1; run = 1;
} }
} }
ast_mutex_unlock(&pclock); AST_LIST_UNLOCK(&pcq);
if (run) { if (run) {
dundi_precache(context, number); dundi_precache(context, number);
} else } else
sleep(1); sleep(1);
} }
return NULL; return NULL;
} }
@@ -2606,9 +2604,9 @@ static int dundi_show_precache(int fd, int argc, char *argv[])
if (argc != 3) if (argc != 3)
return RESULT_SHOWUSAGE; return RESULT_SHOWUSAGE;
time(&now); time(&now);
ast_mutex_lock(&pclock);
ast_cli(fd, FORMAT2, "Number", "Context", "Expiration"); ast_cli(fd, FORMAT2, "Number", "Context", "Expiration");
for (qe = pcq;qe;qe = qe->next) { AST_LIST_LOCK(&pcq);
AST_LIST_TRAVERSE(&pcq, qe, list) {
s = qe->expiration - now; s = qe->expiration - now;
h = s / 3600; h = s / 3600;
s = s % 3600; s = s % 3600;
@@ -2616,7 +2614,8 @@ static int dundi_show_precache(int fd, int argc, char *argv[])
s = s % 60; s = s % 60;
ast_cli(fd, FORMAT, qe->number, qe->context, h,m,s); ast_cli(fd, FORMAT, qe->number, qe->context, h,m,s);
} }
ast_mutex_unlock(&pclock); AST_LIST_UNLOCK(&pcq);
return RESULT_SUCCESS; return RESULT_SUCCESS;
#undef FORMAT #undef FORMAT
#undef FORMAT2 #undef FORMAT2
@@ -3553,45 +3552,37 @@ int dundi_lookup(struct dundi_result *result, int maxret, struct ast_channel *ch
static void reschedule_precache(const char *number, const char *context, int expiration) static void reschedule_precache(const char *number, const char *context, int expiration)
{ {
int len; int len;
struct dundi_precache_queue *qe, *prev=NULL; struct dundi_precache_queue *qe, *prev;
ast_mutex_lock(&pclock);
qe = pcq; AST_LIST_LOCK(&pcq);
while(qe) { AST_LIST_TRAVERSE_SAFE_BEGIN(&pcq, qe, list) {
if (!strcmp(number, qe->number) && !strcasecmp(context, qe->context)) { if (!strcmp(number, qe->number) && !strcasecmp(context, qe->context)) {
if (prev) AST_LIST_REMOVE_CURRENT(&pcq, list);
prev->next = qe->next;
else
pcq = qe->next;
qe->next = NULL;
break; break;
} }
prev = qe; }
qe = qe->next; AST_LIST_TRAVERSE_SAFE_END
};
if (!qe) { if (!qe) {
len = sizeof(struct dundi_precache_queue); len = sizeof(*qe);
len += strlen(number) + 1; len += strlen(number) + 1;
len += strlen(context) + 1; len += strlen(context) + 1;
qe = malloc(len); if (!(qe = ast_calloc(1, len))) {
if (qe) { AST_LIST_UNLOCK(&pcq);
memset(qe, 0, len); return;
strcpy(qe->number, number);
qe->context = qe->number + strlen(number) + 1;
strcpy(qe->context, context);
} }
strcpy(qe->number, number);
qe->context = qe->number + strlen(number) + 1;
strcpy(qe->context, context);
} }
time(&qe->expiration); time(&qe->expiration);
qe->expiration += expiration; qe->expiration += expiration;
prev = pcq; if ((prev = AST_LIST_FIRST(&pcq))) {
if (prev) { while (AST_LIST_NEXT(prev, list) && ((AST_LIST_NEXT(prev, list))->expiration <= qe->expiration))
while(prev->next && (prev->next->expiration <= qe->expiration)) prev = AST_LIST_NEXT(prev, list);
prev = prev->next; AST_LIST_INSERT_AFTER(&pcq, prev, qe, list);
qe->next = prev->next;
prev->next = qe;
} else } else
pcq = qe; AST_LIST_INSERT_HEAD(&pcq, qe, list);
ast_mutex_unlock(&pclock); AST_LIST_UNLOCK(&pcq);
} }
static void dundi_precache_full(void) static void dundi_precache_full(void)