Module: sip-router
Branch: alexh/master
Commit: 95ab8fc302b16b08db38978950e368c1b3bf1269
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=95ab8fc…
Author: Alex Hermann <alex(a)speakup.nl>
Committer: Alex Hermann <alex(a)speakup.nl>
Date: Fri Mar 25 12:17:46 2011 +0100
core/xavp: Consolidate duplicate code. No changes to public interfaces.
---
xavp.c | 241 +++++++++++++++++++++++++---------------------------------------
1 files changed, 93 insertions(+), 148 deletions(-)
diff --git a/xavp.c b/xavp.c
index aac6d21..265729e 100644
--- a/xavp.c
+++ b/xavp.c
@@ -37,6 +37,11 @@ static sr_xavp_t *_xavp_list_head = 0;
/*! Pointer to XAVP current list */
static sr_xavp_t **_xavp_list_crt = &_xavp_list_head;
+/*! Helper functions */
+static sr_xavp_t *xavp_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t
**prv);
+static int xavp_rm_internal(str *name, sr_xavp_t **head, int idx);
+
+
void xavp_shm_free(void *p)
{
shm_free(p);
@@ -74,13 +79,15 @@ void xavp_free_unsafe(sr_xavp_t *xa)
shm_free_unsafe(xa);
}
-sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
+static sr_xavp_t *xavp_new_value(str *name, sr_xval_t *val)
{
- sr_xavp_t *avp=0;
+ sr_xavp_t *avp;
int size;
+ unsigned int id;
if(name==NULL || name->s==NULL || val==NULL)
return NULL;
+ id = get_hash1_raw(name->s, name->len);
size = sizeof(sr_xavp_t) + name->len + 1;
if(val->type == SR_XTYPE_STR)
@@ -89,7 +96,7 @@ sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
if(avp==NULL)
return NULL;
memset(avp, 0, size);
- avp->id = get_hash1_raw(name->s, name->len);
+ avp->id = id;
avp->name.s = (char*)avp + sizeof(sr_xavp_t);
memcpy(avp->name.s, name->s, name->len);
avp->name.s[name->len] = '\0';
@@ -102,6 +109,19 @@ sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t
**list)
avp->val.v.s.s[val->v.s.len] = '\0';
avp->val.v.s.len = val->v.s.len;
}
+
+ return avp;
+}
+
+sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
+{
+ sr_xavp_t *avp=0;
+
+ avp = xavp_new_value(name, val);
+ if (avp==NULL)
+ return NULL;
+
+ /* Prepend new value to the list */
if(list) {
avp->next = *list;
*list = avp;
@@ -115,99 +135,39 @@ sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t
**list)
sr_xavp_t *xavp_set_value(str *name, int idx, sr_xval_t *val, sr_xavp_t **list)
{
- sr_xavp_t *avp=0;
+ sr_xavp_t *avp;
+ sr_xavp_t *cur;
sr_xavp_t *prv=0;
- sr_xavp_t *tmp=0;
- unsigned int id;
- int size;
- int n=0;
- if(name==NULL || name->s==NULL || val==NULL)
+ if(val==NULL)
return NULL;
- id = get_hash1_raw(name->s, name->len);
- if(list)
- avp = *list;
- else
- avp=*_xavp_list_crt;
- while(avp)
- {
- if(avp->id==id && avp->name.len==name->len
- && strncmp(avp->name.s, name->s, name->len)==0)
- {
- if(idx==n)
- break;
- n++;
- }
- prv = avp;
- avp=avp->next;
- }
- if(avp==NULL)
+ /* Find the current value */
+ cur = xavp_get_internal(name, list, idx, &prv);
+ if(cur==NULL)
return NULL;
- tmp = avp;
- size = sizeof(sr_xavp_t) + name->len + 1;
- if(val->type == SR_XTYPE_STR)
- size += val->v.s.len + 1;
- avp = (sr_xavp_t*)shm_malloc(size);
- if(avp==NULL)
+ avp = xavp_new_value(name, val);
+ if (avp==NULL)
return NULL;
- memset(avp, 0, size);
- avp->id = get_hash1_raw(name->s, name->len);
- avp->name.s = (char*)avp + sizeof(sr_xavp_t);
- memcpy(avp->name.s, name->s, name->len);
- avp->name.s[name->len] = '\0';
- avp->name.len = name->len;
- memcpy(&avp->val, val, sizeof(sr_xval_t));
- if(val->type == SR_XTYPE_STR)
- {
- avp->val.v.s.s = avp->name.s + avp->name.len + 1;
- memcpy(avp->val.v.s.s, val->v.s.s, val->v.s.len);
- avp->val.v.s.s[val->v.s.len] = '\0';
- avp->val.v.s.len = val->v.s.len;
- }
- avp->next = tmp->next;
- if(prv) {
- prv->next = avp;
- } else {
- if(list) {
- *list = avp;
- } else {
- *_xavp_list_crt = avp;
- }
- }
- xavp_free(tmp);
-
- return avp;
-}
-
-sr_xavp_t *xavp_get(str *name, sr_xavp_t *start)
-{
- sr_xavp_t *avp=0;
- unsigned int id;
- if(name==NULL || name->s==NULL)
- return NULL;
- id = get_hash1_raw(name->s, name->len);
-
- if(start)
- avp = start;
+ /* Replace the current value with the new */
+ avp->next = cur->next;
+ if(prv)
+ prv->next = avp;
+ else if(list)
+ *list = avp;
else
- avp=*_xavp_list_crt;
- while(avp)
- {
- if(avp->id==id && avp->name.len==name->len
- && strncmp(avp->name.s, name->s, name->len)==0)
- return avp;
- avp=avp->next;
- }
+ *_xavp_list_crt = avp;
- return NULL;
+ xavp_free(cur);
+
+ return avp;
}
-sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
+static sr_xavp_t *xavp_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t
**prv)
{
- sr_xavp_t *avp=0;
+ sr_xavp_t *avp;
unsigned int id;
int n = 0;
@@ -215,10 +175,10 @@ sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
return NULL;
id = get_hash1_raw(name->s, name->len);
- if(start)
- avp = *start;
+ if(list && *list)
+ avp = *list;
else
- avp=*_xavp_list_crt;
+ avp = *_xavp_list_crt;
while(avp)
{
if(avp->id==id && avp->name.len==name->len
@@ -228,16 +188,26 @@ sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
return avp;
n++;
}
- avp=avp->next;
+ if(prv)
+ *prv = avp;
+ avp = avp->next;
}
-
return NULL;
}
+sr_xavp_t *xavp_get(str *name, sr_xavp_t *start)
+{
+ return xavp_get_internal(name, &start, 0, NULL);
+}
+
+sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
+{
+ return xavp_get_internal(name, start, idx, NULL);
+}
sr_xavp_t *xavp_get_next(sr_xavp_t *start)
{
- sr_xavp_t *avp=0;
+ sr_xavp_t *avp;
if(start==NULL)
return NULL;
@@ -257,7 +227,7 @@ sr_xavp_t *xavp_get_next(sr_xavp_t *start)
int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
{
- sr_xavp_t *avp=0;
+ sr_xavp_t *avp;
sr_xavp_t *prv=0;
if(head!=NULL)
@@ -271,11 +241,10 @@ int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
{
if(prv)
prv->next=avp->next;
+ else if(head!=NULL)
+ *head = avp->next;
else
- if(head!=NULL)
- *head = avp->next;
- else
- *_xavp_list_crt = avp->next;
+ *_xavp_list_crt = avp->next;
xavp_free(avp);
return 1;
}
@@ -284,14 +253,19 @@ int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
return 0;
}
-
-int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
+/* Remove xavps
+ * idx: <0 remove all xavps with the same name
+ * >=0 remove only the specified index xavp
+ * Returns number of xavps that were deleted
+ */
+static int xavp_rm_internal(str *name, sr_xavp_t **head, int idx)
{
- sr_xavp_t *avp=0;
- sr_xavp_t *foo=0;
+ sr_xavp_t *avp;
+ sr_xavp_t *foo;
sr_xavp_t *prv=0;
- unsigned int id = 0;
+ unsigned int id;
int n=0;
+ int count=0;
if(name==NULL || name->s==NULL)
return 0;
@@ -300,7 +274,7 @@ int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
if(head!=NULL)
avp = *head;
else
- avp=*_xavp_list_crt;
+ avp = *_xavp_list_crt;
while(avp)
{
foo = avp;
@@ -308,72 +282,43 @@ int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
if(foo->id==id && foo->name.len==name->len
&& strncmp(foo->name.s, name->s, name->len)==0)
{
- if(prv!=NULL)
- prv->next=foo->next;
- else
- if(head!=NULL)
+ if(idx<0 || idx==n)
+ {
+ if(prv!=NULL)
+ prv->next=foo->next;
+ else if(head!=NULL)
*head = foo->next;
else
*_xavp_list_crt = foo->next;
- xavp_free(foo);
+ xavp_free(foo);
+ if(idx>=0)
+ return 1;
+ count++;
+ }
n++;
- if(all==0)
- return n;
} else {
prv = foo;
}
}
- return n;
+ return count;
}
-int xavp_rm_by_index(str *name, int idx, sr_xavp_t **head)
+int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
{
- sr_xavp_t *avp=0;
- sr_xavp_t *foo=0;
- sr_xavp_t *prv=0;
- unsigned int id = 0;
- int n=0;
+ return xavp_rm_internal(name, head, -1*all);
+}
- if(name==NULL || name->s==NULL)
- return 0;
- if(idx<0)
+int xavp_rm_by_index(str *name, int idx, sr_xavp_t **head)
+{
+ if (idx<0)
return 0;
-
- id = get_hash1_raw(name->s, name->len);
- if(head!=NULL)
- avp = *head;
- else
- avp=*_xavp_list_crt;
- while(avp)
- {
- foo = avp;
- avp=avp->next;
- if(foo->id==id && foo->name.len==name->len
- && strncmp(foo->name.s, name->s, name->len)==0)
- {
- if(idx==n)
- {
- if(prv!=NULL)
- prv->next=foo->next;
- else
- if(head!=NULL)
- *head = foo->next;
- else
- *_xavp_list_crt = foo->next;
- xavp_free(foo);
- return 1;
- }
- n++;
- }
- prv = foo;
- }
- return 0;
+ return xavp_rm_internal(name, head, idx);
}
int xavp_count(str *name, sr_xavp_t **start)
{
- sr_xavp_t *avp=0;
+ sr_xavp_t *avp;
unsigned int id;
int n = 0;