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 79
 	shm_free_unsafe(xa);
75 80
 }
76 81
 
77
-sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
82
+static sr_xavp_t *xavp_new_value(str *name, sr_xval_t *val)
78 83
 {
79
-	sr_xavp_t *avp=0;
84
+	sr_xavp_t *avp;
80 85
 	int size;
86
+	unsigned int id;
81 87
 
82 88
 	if(name==NULL || name->s==NULL || val==NULL)
83 89
 		return NULL;
90
+	id = get_hash1_raw(name->s, name->len);
84 91
 
85 92
 	size = sizeof(sr_xavp_t) + name->len + 1;
86 93
 	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 96
 	if(avp==NULL)
90 97
 		return NULL;
91 98
 	memset(avp, 0, size);
92
-	avp->id = get_hash1_raw(name->s, name->len);
99
+	avp->id = id;
93 100
 	avp->name.s = (char*)avp + sizeof(sr_xavp_t);
94 101
 	memcpy(avp->name.s, name->s, name->len);
95 102
 	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 109
 		avp->val.v.s.s[val->v.s.len] = '\0';
103 110
 		avp->val.v.s.len = val->v.s.len;
104 111
 	}
112
+
113
+	return avp;
114
+}
115
+
116
+sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
117
+{
118
+	sr_xavp_t *avp=0;
119
+
120
+	avp = xavp_new_value(name, val);
121
+	if (avp==NULL)
122
+		return NULL;
123
+
124
+	/* Prepend new value to the list */
105 125
 	if(list) {
106 126
 		avp->next = *list;
107 127
 		*list = avp;
... ...
@@ -115,99 +135,39 @@ sr_xavp_t *xavp_add_value(str *name, sr_xval_t *val, sr_xavp_t **list)
115 135
 
116 136
 sr_xavp_t *xavp_set_value(str *name, int idx, sr_xval_t *val, sr_xavp_t **list)
117 137
 {
118
-	sr_xavp_t *avp=0;
138
+	sr_xavp_t *avp;
139
+	sr_xavp_t *cur;
119 140
 	sr_xavp_t *prv=0;
120
-	sr_xavp_t *tmp=0;
121
-	unsigned int id;
122
-	int size;
123
-	int n=0;
124 141
 
125
-	if(name==NULL || name->s==NULL || val==NULL)
142
+	if(val==NULL)
126 143
 		return NULL;
127 144
 
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)
145
+	/* Find the current value */
146
+	cur = xavp_get_internal(name, list, idx, &prv);
147
+	if(cur==NULL)
146 148
 		return NULL;
147
-	tmp = avp;
148 149
 
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)
150
+	avp = xavp_new_value(name, val);
151
+	if (avp==NULL)
154 152
 		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 153
 
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;
154
+	/* Replace the current value with the new */
155
+	avp->next = cur->next;
156
+	if(prv)
157
+		prv->next = avp;
158
+	else if(list)
159
+		*list = avp;
195 160
 	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
-	}
161
+		*_xavp_list_crt = avp;
204 162
 
205
-	return NULL;
163
+	xavp_free(cur);
164
+
165
+	return avp;
206 166
 }
207 167
 
208
-sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
168
+static sr_xavp_t *xavp_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t **prv)
209 169
 {
210
-	sr_xavp_t *avp=0;
170
+	sr_xavp_t *avp;
211 171
 	unsigned int id;
212 172
 	int n = 0;
213 173
 
... ...
@@ -215,10 +175,10 @@ sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
215 175
 		return NULL;
216 176
 	id = get_hash1_raw(name->s, name->len);
217 177
 	
218
-	if(start)
219
-		avp = *start;
178
+	if(list && *list)
179
+		avp = *list;
220 180
 	else
221
-		avp=*_xavp_list_crt;
181
+		avp = *_xavp_list_crt;
222 182
 	while(avp)
223 183
 	{
224 184
 		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 188
 				return avp;
229 189
 			n++;
230 190
 		}
231
-		avp=avp->next;
191
+		if(prv)
192
+			*prv = avp;
193
+		avp = avp->next;
232 194
 	}
233
-
234 195
 	return NULL;
235 196
 }
236 197
 
198
+sr_xavp_t *xavp_get(str *name, sr_xavp_t *start)
199
+{
200
+	return xavp_get_internal(name, &start, 0, NULL);
201
+}
202
+
203
+sr_xavp_t *xavp_get_by_index(str *name, int idx, sr_xavp_t **start)
204
+{
205
+	return xavp_get_internal(name, start, idx, NULL);
206
+}
237 207
 
238 208
 sr_xavp_t *xavp_get_next(sr_xavp_t *start)
239 209
 {
240
-	sr_xavp_t *avp=0;
210
+	sr_xavp_t *avp;
241 211
 
242 212
 	if(start==NULL)
243 213
 		return NULL;
... ...
@@ -257,7 +227,7 @@ sr_xavp_t *xavp_get_next(sr_xavp_t *start)
257 227
 
258 228
 int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
259 229
 {
260
-	sr_xavp_t *avp=0;
230
+	sr_xavp_t *avp;
261 231
 	sr_xavp_t *prv=0;
262 232
 
263 233
 	if(head!=NULL)
... ...
@@ -271,11 +241,10 @@ int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
271 241
 		{
272 242
 			if(prv)
273 243
 				prv->next=avp->next;
244
+			else if(head!=NULL)
245
+				*head = avp->next;
274 246
 			else
275
-				if(head!=NULL)
276
-					*head = avp->next;
277
-				else
278
-					*_xavp_list_crt = avp->next;
247
+				*_xavp_list_crt = avp->next;
279 248
 			xavp_free(avp);
280 249
 			return 1;
281 250
 		}
... ...
@@ -284,14 +253,19 @@ int xavp_rm(sr_xavp_t *xa, sr_xavp_t **head)
284 253
 	return 0;
285 254
 }
286 255
 
287
-
288
-int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
256
+/* Remove xavps
257
+ * idx: <0 remove all xavps with the same name
258
+ *      >=0 remove only the specified index xavp
259
+ * Returns number of xavps that were deleted
260
+ */
261
+static int xavp_rm_internal(str *name, sr_xavp_t **head, int idx)
289 262
 {
290
-	sr_xavp_t *avp=0;
291
-	sr_xavp_t *foo=0;
263
+	sr_xavp_t *avp;
264
+	sr_xavp_t *foo;
292 265
 	sr_xavp_t *prv=0;
293
-	unsigned int id = 0;
266
+	unsigned int id;
294 267
 	int n=0;
268
+	int count=0;
295 269
 
296 270
 	if(name==NULL || name->s==NULL)
297 271
 		return 0;
... ...
@@ -300,7 +274,7 @@ int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
300 274
 	if(head!=NULL)
301 275
 		avp = *head;
302 276
 	else
303
-		avp=*_xavp_list_crt;
277
+		avp = *_xavp_list_crt;
304 278
 	while(avp)
305 279
 	{
306 280
 		foo = avp;
... ...
@@ -308,72 +282,43 @@ int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
308 282
 		if(foo->id==id && foo->name.len==name->len
309 283
 				&& strncmp(foo->name.s, name->s, name->len)==0)
310 284
 		{
311
-			if(prv!=NULL)
312
-				prv->next=foo->next;
313
-			else
314
-				if(head!=NULL)
285
+			if(idx<0 || idx==n)
286
+			{
287
+				if(prv!=NULL)
288
+					prv->next=foo->next;
289
+				else if(head!=NULL)
315 290
 					*head = foo->next;
316 291
 				else
317 292
 					*_xavp_list_crt = foo->next;
318
-			xavp_free(foo);
293
+				xavp_free(foo);
294
+				if(idx>=0)
295
+					return 1;
296
+				count++;
297
+			}
319 298
 			n++;
320
-			if(all==0)
321
-				return n;
322 299
 		} else {
323 300
 			prv = foo;
324 301
 		}
325 302
 	}
326
-	return n;
303
+	return count;
327 304
 }
328 305
 
329
-int xavp_rm_by_index(str *name, int idx, sr_xavp_t **head)
306
+int xavp_rm_by_name(str *name, int all, sr_xavp_t **head)
330 307
 {
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;
308
+	return xavp_rm_internal(name, head, -1*all);
309
+}
336 310
 
337
-	if(name==NULL || name->s==NULL)
338
-		return 0;
339
-	if(idx<0)
311
+int xavp_rm_by_index(str *name, int idx, sr_xavp_t **head)
312
+{
313
+	if (idx<0)
340 314
 		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;
315
+	return xavp_rm_internal(name, head, idx);
371 316
 }
372 317
 
373 318
 
374 319
 int xavp_count(str *name, sr_xavp_t **start)
375 320
 {
376
-	sr_xavp_t *avp=0;
321
+	sr_xavp_t *avp;
377 322
 	unsigned int id;
378 323
 	int n = 0;
379 324