Browse code

core: str list - added function to insert a block string in list

Daniel-Constantin Mierla authored on 29/06/2021 06:55:32
Showing 2 changed files
... ...
@@ -13,13 +13,13 @@
13 13
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
  * GNU General Public License for more details.
15 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 
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 18
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 19
  */
20 20
 
21 21
 /**
22
- * @file 
22
+ * @file
23 23
  * @brief Kamailio core :: Simple str type list and helper functions
24 24
  * @ingroup core
25 25
  * Module: @ref core
... ...
@@ -32,7 +32,7 @@
32 32
 
33 33
 /**
34 34
  * @brief Add a new allocated list element to an existing list
35
- * 
35
+ *
36 36
  * Add a new allocated list element to an existing list, the allocation is done
37 37
  * from the private memory pool
38 38
  * @param s input character
... ...
@@ -43,18 +43,45 @@
43 43
  */
44 44
 struct str_list *append_str_list(char *s, int len, struct str_list **last, int *total)
45 45
 {
46
-	struct str_list *new;
47
-	new = pkg_malloc(sizeof(struct str_list));
48
-	if (!new) {
46
+	struct str_list *nv;
47
+	nv = pkg_malloc(sizeof(struct str_list));
48
+	if (!nv) {
49 49
 		PKG_MEM_ERROR;
50 50
 		return 0;
51 51
 	}
52
-	new->s.s = s;
53
-	new->s.len = len;
54
-	new->next = 0;
52
+	nv->s.s = s;
53
+	nv->s.len = len;
54
+	nv->next = 0;
55 55
 
56
-	(*last)->next = new;
57
-	*last = new;
56
+	(*last)->next = nv;
57
+	*last = nv;
58 58
 	*total += len;
59
-	return new;
59
+	return nv;
60
+}
61
+
62
+/**
63
+ * @brief Add a new allocated list element with cloned block value to an existing list
64
+ *
65
+ * Add a new allocated list element with cloned value in block to an existing list,
66
+ * the allocation is done from the private memory pool
67
+ * @param head existing list
68
+ * @param s input character
69
+ * @param len length of input character
70
+ * @return extended list
71
+ */
72
+str_list_t *str_list_block_add(str_list_t **head, char *s, int len)
73
+{
74
+	str_list_t *nv;
75
+	nv = pkg_mallocxz(sizeof(str_list_t) + (len+1)*sizeof(char));
76
+	if (!nv) {
77
+		PKG_MEM_ERROR;
78
+		return 0;
79
+	}
80
+	nv->s.s = (char*)nv + sizeof(str_list_t);
81
+	memcpy(nv->s.s, s, len);
82
+	nv->s.len = len;
83
+	nv->next = *head;
84
+	*head = nv;
85
+
86
+	return nv;
60 87
 }
... ...
@@ -51,4 +51,16 @@ typedef struct str_list {
51 51
  */
52 52
 struct str_list *append_str_list(char *s, int len, struct str_list **last, int *total);
53 53
 
54
+/**
55
+ * @brief Add a new allocated list element with cloned block value to an existing list
56
+ *
57
+ * Add a new allocated list element with cloned value in block to an existing list,
58
+ * the allocation is done from the private memory pool
59
+ * @param head existing list
60
+ * @param s input character
61
+ * @param len length of input character
62
+ * @return extended list
63
+ */
64
+str_list_t *str_list_block_add(str_list_t **head, char *s, int len);
65
+
54 66
 #endif