Browse code

core: move redundant and identical implementation of a str_list type to core

- move redundant str_list and new_str implementations from tm and tmx to core
- rename new_str to a more descriptive append_str_list, no interface changes
- fix callers in tm and tmx
- add doxygen documentation

Henning Westerholt authored on 05/07/2011 21:38:13
Showing 4 changed files
... ...
@@ -29,6 +29,7 @@
29 29
 #include "../../socket_info.h"
30 30
 #include "../../ut.h"
31 31
 #include "../../parser/parse_from.h"
32
+#include "../../str_list.h"
32 33
 #include "ut.h"
33 34
 #include "dlg.h"
34 35
 #include "uac.h"
... ...
@@ -50,13 +51,6 @@
50 50
 
51 51
 
52 52
 
53
-struct str_list {
54
-	str s;
55
-	struct str_list *next;
56
-};
57
-
58
-
59
-
60 53
 /** make sure the rpc user created the msg properly.
61 54
  * Make sure that the FIFO user created the message
62 55
  * correctly and fill some extra parameters in function
... ...
@@ -151,25 +145,6 @@ err:
151 151
 
152 152
 
153 153
 
154
-static inline struct str_list *new_str(char *s, int len,
155
-										struct str_list **last, int *total)
156
-{
157
-	struct str_list *new;
158
-	new = pkg_malloc(sizeof(struct str_list));
159
-	if (!new) {
160
-		LOG(L_ERR, "new_str: Not enough mem\n");
161
-		return 0;
162
-	}
163
-	new->s.s = s;
164
-	new->s.len = len;
165
-	new->next = 0;
166
-
167
-	(*last)->next = new;
168
-	*last = new;
169
-	*total += len;
170
-	return new;
171
-}
172
-
173 154
 
174 155
 
175 156
 /** construct a "header block" from a header list.
... ...
@@ -208,7 +183,7 @@ static char *get_hfblock(str *uri, struct hdr_field *hf, int proto,
208 208
 		while(p) {
209 209
 			d = q_memchr(needle, SUBST_CHAR, p);
210 210
 			if (!d || d + 1 >= needle + p) { /* nothing to substitute */
211
-				if (!new_str(begin, p, &last, &total_len)) goto error;
211
+				if (!append_str_list(begin, p, &last, &total_len)) goto error;
212 212
 				break;
213 213
 			} else {
214 214
 				frag_len = d - begin;
... ...
@@ -216,7 +191,7 @@ static char *get_hfblock(str *uri, struct hdr_field *hf, int proto,
216 216
 				switch(*d) {
217 217
 				case SUBST_CHAR: /* double SUBST_CHAR: IP */
218 218
 					     /* string before substitute */
219
-					if (!new_str(begin, frag_len, &last, &total_len))
219
+					if (!append_str_list(begin, frag_len, &last, &total_len))
220 220
 						goto error;
221 221
 					     /* substitute */
222 222
 					if (!sock_name) {
... ...
@@ -234,12 +209,12 @@ static char *get_hfblock(str *uri, struct hdr_field *hf, int proto,
234 234
 						sock_name = &di.send_sock->address_str;
235 235
 						portname = &di.send_sock->port_no_str;
236 236
 					}
237
-					if (!new_str(sock_name->s, sock_name->len, &last,
237
+					if (!append_str_list(sock_name->s, sock_name->len, &last,
238 238
 									&total_len))
239 239
 						goto error;
240 240
 					/* inefficient - FIXME --andrei*/
241
-					if (!new_str(":", 1, &last, &total_len)) goto error;
242
-					if (!new_str(portname->s, portname->len, &last,
241
+					if (!append_str_list(":", 1, &last, &total_len)) goto error;
242
+					if (!append_str_list(portname->s, portname->len, &last,
243 243
 								&total_len)) goto error;
244 244
 					/* keep going ... */
245 245
 					begin = needle = d + 1;
... ...
@@ -38,13 +38,9 @@
38 38
 #include "../../parser/parse_from.h"
39 39
 #include "../../modules/tm/ut.h"
40 40
 #include "../../lib/kmi/mi.h"
41
+#include "../../str_list.h"
41 42
 #include "tmx_mod.h"
42 43
 
43
-/*! simple string list */
44
-struct str_list {
45
-	str s;
46
-	struct str_list *next;
47
-};
48 44
 
49 45
 /*! Which header fields should be skipped */
50 46
 #define skip_hf(_hf) \
... ...
@@ -160,35 +156,6 @@ static inline struct mi_root* mi_check_msg(struct sip_msg* msg, str* method,
160 160
 	return 0;
161 161
 }
162 162
 
163
-/*!
164
- * \brief Allocate a new str on a str list
165
- *
166
- * Allocate a new str in pkg_mem and attach it to a str list. Update
167
- * the total number of list elements.
168
- * \param s char array
169
- * \param len length of the char array
170
- * \param last last list element
171
- * \param total total number of list elements
172
- * \return pointer to the new list element
173
- */
174
-static inline struct str_list *new_str(char *s, int len, struct str_list **last, int *total)
175
-{
176
-	struct str_list *new;
177
-	new=pkg_malloc(sizeof(struct str_list));
178
-	if (!new) {
179
-		LM_ERR("no more pkg mem\n");
180
-		return 0;
181
-	}
182
-	new->s.s=s;
183
-	new->s.len=len;
184
-	new->next=0;
185
-
186
-	(*last)->next=new;
187
-	*last=new;
188
-	*total+=len;
189
-
190
-	return new;
191
-}
192 163
 
193 164
 /*!
194 165
  * \brief Convert a header field block to char array
... ...
@@ -225,7 +192,7 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
225 225
 		while(hf_avail) {
226 226
 			d=memchr(needle, SUBST_CHAR, hf_avail);
227 227
 			if (!d || d+1>=needle+hf_avail) { /* nothing to substitute */
228
-				new=new_str(begin, hf_avail, &last, &total_len); 
228
+				new=append_str_list(begin, hf_avail, &last, &total_len); 
229 229
 				if (!new) goto error;
230 230
 				break;
231 231
 			} else {
... ...
@@ -234,7 +201,7 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
234 234
 				switch(*d) {
235 235
 					case SUBST_CHAR:	/* double SUBST_CHAR: IP */
236 236
 						/* string before substitute */
237
-						new=new_str(begin, frag_len, &last, &total_len); 
237
+						new=append_str_list(begin, frag_len, &last, &total_len); 
238 238
 						if (!new) goto error;
239 239
 						/* substitute */
240 240
 						if (!sock_name) {
... ...
@@ -248,13 +215,13 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
248 248
 							sock_name=&(*send_sock)->address_str;
249 249
 							portname=&(*send_sock)->port_no_str;
250 250
 						}
251
-						new=new_str(sock_name->s, sock_name->len,
251
+						new=append_str_list(sock_name->s, sock_name->len,
252 252
 								&last, &total_len );
253 253
 						if (!new) goto error;
254 254
 						/* inefficient - FIXME --andrei*/
255
-						new=new_str(":", 1, &last, &total_len);
255
+						new=append_str_list(":", 1, &last, &total_len);
256 256
 						if (!new) goto error;
257
-						new=new_str(portname->s, portname->len,
257
+						new=append_str_list(portname->s, portname->len,
258 258
 								&last, &total_len );
259 259
 						if (!new) goto error;
260 260
 						/* keep going ... */
... ...
@@ -268,7 +235,7 @@ static inline char *get_hfblock( str *uri, struct hdr_field *hf, int *l, struct
268 268
 			} /* possible substitute */
269 269
 		} /* substitution loop */
270 270
 		/* proceed to next header */
271
-		/* new=new_str(CRLF, CRLF_LEN, &last, &total_len );
271
+		/* new=append_str_list(CRLF, CRLF_LEN, &last, &total_len );
272 272
 		if (!new) goto error; */
273 273
 		LM_DBG("one more hf processed\n");
274 274
 	} /* header loop */
275 275
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+/*
1
+ * Copyright (C) 2001-2003 FhG Fokus
2
+ *
3
+ * This file is part of sip-router, a free SIP server.
4
+ *
5
+ * sip-router is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation; either version 2 of the License, or
8
+ * (at your option) any later version
9
+ *
10
+ * sip-router is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License 
16
+ * along with this program; if not, write to the Free Software 
17
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
+ */
19
+
20
+/**
21
+ * @file 
22
+ * @brief Simple str type list and helper functions
23
+ */
24
+
25
+
26
+#include "str.h"
27
+#include "mem/mem.h"
28
+#include "str_list.h"
29
+
30
+
31
+/**
32
+ * @brief Add a new allocated list element to an existing list
33
+ * 
34
+ * Add a new allocated list element to an existing list, the allocation is done
35
+ * from the private memory pool
36
+ * @param s input character
37
+ * @param len length of input character
38
+ * @param last existing list
39
+ * @param total length of total characters in list
40
+ * @return extended list
41
+ */
42
+struct str_list *append_str_list(char *s, int len, struct str_list **last, int *total)
43
+{
44
+	struct str_list *new;
45
+	new = pkg_malloc(sizeof(struct str_list));
46
+	if (!new) {
47
+		PKG_MEM_ERROR;
48
+		return 0;
49
+	}
50
+	new->s.s = s;
51
+	new->s.len = len;
52
+	new->next = 0;
53
+
54
+	(*last)->next = new;
55
+	*last = new;
56
+	*total += len;
57
+	return new;
58
+}
0 59
new file mode 100644
... ...
@@ -0,0 +1,54 @@
0
+/*
1
+ * Copyright (C) 2001-2003 FhG Fokus
2
+ * Copyright (C) 2006 Voice Sistem SRL
3
+ *
4
+ * This file is part of sip-router, a free SIP server.
5
+ *
6
+ * sip-router is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * sip-router is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License 
17
+ * along with this program; if not, write to the Free Software 
18
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
+ */
20
+
21
+/**
22
+ * @file 
23
+ * @brief Simple str type list and helper functions
24
+ */
25
+
26
+
27
+#ifndef str_list_h
28
+#define str_list_h
29
+
30
+
31
+/**
32
+ * @brief Simple str type list
33
+ */
34
+struct str_list {
35
+	str s;
36
+	struct str_list *next;
37
+};
38
+
39
+
40
+/**
41
+ * @brief Add a new allocated list element to an existing list
42
+ * 
43
+ * Add a new allocated list element to an existing list, the allocation is done
44
+ * from the private memory pool
45
+ * @param s input character
46
+ * @param len length of input character
47
+ * @param last existing list
48
+ * @param total length of total characters in list
49
+ * @return extended list
50
+ */
51
+struct str_list *append_str_list(char *s, int len, struct str_list **last, int *total);
52
+
53
+#endif