Browse code

core/xavp: Consolidate duplicate code. No changes to public interfaces.

Alex Hermann authored on 25/03/2011 11:17:46
Showing 1 changed files
... ...
@@ -37,6 +37,11 @@ static sr_xavp_t *_xavp_list_head = 0;
37 37
 /*! Pointer to XAVP current list */
38 38
 static sr_xavp_t **_xavp_list_crt = &_xavp_list_head;
39 39
 
40
+/*! Helper functions */
41
+static sr_xavp_t *xavp_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t **prv);
42
+static int xavp_rm_internal(str *name, sr_xavp_t **head, int idx);
43
+
44
+
40 45
 void xavp_shm_free(void *p)
41 46
 {
42 47
 	shm_free(p);
... ...
@@ -74,13 +79,15 @@ void xavp_free_unsafe(sr_xavp_t *xa)
74 74
 	shm_free_unsafe(xa);
75 75
 }
76 76
 
77
-sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
77
+static sr_xavp_t *xavp_new_value(str *name, sr_xval_t *val)
78 78
 {
79
-	sr_xavp_t *avp=0;
79
+	sr_xavp_t *avp;
80 80
 	int size;
81
+	unsigned int id;
81 82
 
82 83
 	if(name==NULL || name->s==NULL || val==NULL)
83 84
 		return NULL;
85
+	id = get_hash1_raw(name->s, name->len);
84 86
 
85 87
 	size = sizeof(sr_xavp_t) + name->len + 1;
86 88
 	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)
89 89
 	if(avp==NULL)
90 90
 		return NULL;
91 91
 	memset(avp, 0, size);
92
-	avp->id = get_hash1_raw(name->s, name->len);
92
+	avp->id = id;
93 93
 	avp->name.s = (char*)avp + sizeof(sr_xavp_t);
94 94
 	memcpy(avp->name.s, name->s, name->len);
95 95
 	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)
102 102
 		avp->val.v.s.s[val->v.s.len] = '\0';
103 103
 		avp->val.v.s.len = val->v.s.len;
104 104
 	}
105
+
106
+	return avp;
107
+}
108
+
109
+sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
110
+{
111
+	sr_xavp_t *avp=0;
112
+
113
+	avp = xavp_new_value(name, val);
114
+	if (avp==NULL)
115
+		return NULL;
116
+
117
+	/* Prepend new value to the list */
105 118
 	if(list) {
106 119
 		avp->next = *list;
107 120
 		*list = avp;
... ...
@@ -115,99 +135,39 @@ sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
115 115
 
116 116
 sr_xavp_t *xavp_set_value(str *name, int idx, sr_xval_t *val, sr_xavp_t **list)
117 117
 {
118
-	sr_xavp_t *avp=0;
118
+	sr_xavp_t *avp;
119
+	sr_xavp_t *cur;
119 120
 	sr_xavp_t *prv=0;
120
-	sr_xavp_t *tmp=0;
121
-	unsigned int id;
122
-	int size;
123
-	int n=0;
124 121
 
125
-	if(name==NULL || name->s==NULL || val==NULL)
122
+	if(val==NULL)
126 123
 		return NULL;
127 124
 
128
-	id = get_hash1_raw(name->s, name->len);
129
-	if(list)
130
-		avp = *list;
131
-	else
132
-		avp=*_xavp_list_crt;
133
-	while(avp)
134
-	{
135
-		if(avp->id==id && avp->name.len==name->len
136
-				&& strncmp(avp->name.s, name->s, name->len)==0)
137
-		{
138
-			if(idx==n)
139
-				break;
140
-			n++;
141
-		}
142
-		prv = avp;
143
-		avp=avp->next;
144
-	}
145
-	if(avp==NULL)
125
+	/* Find the current value */
126
+	cur = xavp_get_internal(name, list, idx, &prv);
127
+	if(cur==NULL)
146 128
 		return NULL;
147
-	tmp = avp;
148 129
 
149
-	size = sizeof(sr_xavp_t) + name->len + 1;
150
-	if(val->type == SR_XTYPE_STR)
151
-		size += val->v.s.len + 1;
152
-	avp = (sr_xavp_t*)shm_malloc(size);
153
-	if(avp==NULL)
130
+	avp = xavp_new_value(name, val);
131
+	if (avp==NULL)
154 132
 		return NULL;
155
-	memset(avp, 0, size);
156
-	avp->id = get_hash1_raw(name->s, name->len);
157
-	avp->name.s = (char*)avp + sizeof(sr_xavp_t);
158
-	memcpy(avp->name.s, name->s, name->len);
159
-	avp->name.s[name->len] = '\0';
160
-	avp->name.len = name->len;
161
-	memcpy(&avp->val, val, sizeof(sr_xval_t));
162
-	if(val->type == SR_XTYPE_STR)
163
-	{
164
-		avp->val.v.s.s = avp->name.s + avp->name.len + 1;
165
-		memcpy(avp->val.v.s.s, val->v.s.s, val->v.s.len);
166
-		avp->val.v.s.s[val->v.s.len] = '\0';
167
-		avp->val.v.s.len = val->v.s.len;
168
-	}
169
-	avp->next = tmp->next;
170
-	if(prv) {
171
-		prv->next = avp;
172
-	} else {
173
-		if(list) {
174
-			*list = avp;
175
-		} else {
176
-			*_xavp_list_crt = avp;
177
-		}
178
-	}
179
-	xavp_free(tmp);
180
-
181
-	return avp;
182
-}
183
-
184
-sr_xavp_t *xavp_get(str *name, sr_xavp_t *start)
185
-{
186
-	sr_xavp_t *avp=0;
187
-	unsigned int id;
188 133
 
189
-	if(name==NULL || name->s==NULL)
190
-		return NULL;
191
-	id = get_hash1_raw(name->s, name->len);
192
-	
193
-	if(start)
194
-		avp = start;
134
+	/* Replace the current value with the new */
135
+	avp->next = cur->next;
136
+	if(prv)
137
+		prv->next = avp;
138
+	else if(list)
139
+		*list = avp;
195 140
 	else
196
-		avp=*_xavp_list_crt;
197
-	while(avp)
198
-	{
199
-		if(avp->id==id && avp->name.len==name->len
200
-				&& strncmp(avp->name.s, name->s, name->len)==0)
201
-			return avp;
202
-		avp=avp->next;
203
-	}
141
+		*_xavp_list_crt = avp;
204 142
 
205
-	return NULL;
143
+	xavp_free(cur);
144
+
145
+	return avp;
206 146
 }
207 147
 
208
-sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
148
+static sr_xavp_t *xavp_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t **prv)
209 149
 {
210
-	sr_xavp_t *avp=0;
150
+	sr_xavp_t *avp;
211 151
 	unsigned int id;
212 152
 	int n = 0;
213 153
 
... ...
@@ -215,10 +175,10 @@ sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
215 215
 		return NULL;
216 216
 	id = get_hash1_raw(name->s, name->len);
217 217
 	
218
-	if(start)
219
-		avp = *start;
218
+	if(list && *list)
219
+		avp = *list;
220 220
 	else
221
-		avp=*_xavp_list_crt;
221
+		avp = *_xavp_list_crt;
222 222
 	while(avp)
223 223
 	{
224 224
 		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)
228 228
 				return avp;
229 229
 			n++;
230 230
 		}
231
-		avp=avp->next;
231
+		if(prv)
232
+			*prv = avp;
233
+		avp = avp->next;
232 234
 	}
233
-
234 235
 	return NULL;
235 236
 }
236 237
 
238
+sr_xavp_t *xavp_get(str *name, sr_xavp_t *start)
239
+{
240
+	return xavp_get_internal(name, &start, 0, NULL);
241
+}
242
+
243
+sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
244
+{
245
+	return xavp_get_internal(name, start, idx, NULL);
246
+}
237 247
 
238 248
 sr_xavp_t *xavp_get_next(sr_xavp_t *start)
239 249
 {
240
-	sr_xavp_t *avp=0;
250
+	sr_xavp_t *avp;
241 251
 
242 252
 	if(start==NULL)
243 253
 		return NULL;
... ...
@@ -257,7 +227,7 @@ sr_xavp_t *xavp_get_next(sr_xavp_t *start)
257 257
 
258 258
 int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
259 259
 {
260
-	sr_xavp_t *avp=0;
260
+	sr_xavp_t *avp;
261 261
 	sr_xavp_t *prv=0;
262 262
 
263 263
 	if(head!=NULL)
... ...
@@ -271,11 +241,10 @@ int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
271 271
 		{
272 272
 			if(prv)
273 273
 				prv->next=avp->next;
274
+			else if(head!=NULL)
275
+				*head = avp->next;
274 276
 			else
275
-				if(head!=NULL)
276
-					*head = avp->next;
277
-				else
278
-					*_xavp_list_crt = avp->next;
277
+				*_xavp_list_crt = avp->next;
279 278
 			xavp_free(avp);
280 279
 			return 1;
281 280
 		}
... ...
@@ -284,14 +253,19 @@ int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
284 284
 	return 0;
285 285
 }
286 286
 
287
-
288
-int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
287
+/* Remove xavps
288
+ * idx: <0 remove all xavps with the same name
289
+ *      >=0 remove only the specified index xavp
290
+ * Returns number of xavps that were deleted
291
+ */
292
+static int xavp_rm_internal(str *name, sr_xavp_t **head, int idx)
289 293
 {
290
-	sr_xavp_t *avp=0;
291
-	sr_xavp_t *foo=0;
294
+	sr_xavp_t *avp;
295
+	sr_xavp_t *foo;
292 296
 	sr_xavp_t *prv=0;
293
-	unsigned int id = 0;
297
+	unsigned int id;
294 298
 	int n=0;
299
+	int count=0;
295 300
 
296 301
 	if(name==NULL || name->s==NULL)
297 302
 		return 0;
... ...
@@ -300,7 +274,7 @@ int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
300 300
 	if(head!=NULL)
301 301
 		avp = *head;
302 302
 	else
303
-		avp=*_xavp_list_crt;
303
+		avp = *_xavp_list_crt;
304 304
 	while(avp)
305 305
 	{
306 306
 		foo = avp;
... ...
@@ -308,72 +282,43 @@ int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
308 308
 		if(foo->id==id && foo->name.len==name->len
309 309
 				&& strncmp(foo->name.s, name->s, name->len)==0)
310 310
 		{
311
-			if(prv!=NULL)
312
-				prv->next=foo->next;
313
-			else
314
-				if(head!=NULL)
311
+			if(idx<0 || idx==n)
312
+			{
313
+				if(prv!=NULL)
314
+					prv->next=foo->next;
315
+				else if(head!=NULL)
315 316
 					*head = foo->next;
316 317
 				else
317 318
 					*_xavp_list_crt = foo->next;
318
-			xavp_free(foo);
319
+				xavp_free(foo);
320
+				if(idx>=0)
321
+					return 1;
322
+				count++;
323
+			}
319 324
 			n++;
320
-			if(all==0)
321
-				return n;
322 325
 		} else {
323 326
 			prv = foo;
324 327
 		}
325 328
 	}
326
-	return n;
329
+	return count;
327 330
 }
328 331
 
329
-int xavp_rm_by_index(str *name, int idx, sr_xavp_t **head)
332
+int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
330 333
 {
331
-	sr_xavp_t *avp=0;
332
-	sr_xavp_t *foo=0;
333
-	sr_xavp_t *prv=0;
334
-	unsigned int id = 0;
335
-	int n=0;
334
+	return xavp_rm_internal(name, head, -1*all);
335
+}
336 336
 
337
-	if(name==NULL || name->s==NULL)
338
-		return 0;
339
-	if(idx<0)
337
+int xavp_rm_by_index(str *name, int idx, sr_xavp_t **head)
338
+{
339
+	if (idx<0)
340 340
 		return 0;
341
-
342
-	id = get_hash1_raw(name->s, name->len);
343
-	if(head!=NULL)
344
-		avp = *head;
345
-	else
346
-		avp=*_xavp_list_crt;
347
-	while(avp)
348
-	{
349
-		foo = avp;
350
-		avp=avp->next;
351
-		if(foo->id==id && foo->name.len==name->len
352
-				&& strncmp(foo->name.s, name->s, name->len)==0)
353
-		{
354
-			if(idx==n)
355
-			{
356
-				if(prv!=NULL)
357
-					prv->next=foo->next;
358
-				else
359
-					if(head!=NULL)
360
-						*head = foo->next;
361
-					else
362
-						*_xavp_list_crt = foo->next;
363
-				xavp_free(foo);
364
-				return 1;
365
-			}
366
-			n++;
367
-		}
368
-		prv = foo;
369
-	}
370
-	return 0;
341
+	return xavp_rm_internal(name, head, idx);
371 342
 }
372 343
 
373 344
 
374 345
 int xavp_count(str *name, sr_xavp_t **start)
375 346
 {
376
-	sr_xavp_t *avp=0;
347
+	sr_xavp_t *avp;
377 348
 	unsigned int id;
378 349
 	int n = 0;
379 350