Browse code

core: added generic fparam fixup_free functions

Added generic fparam fixup_free functions that can be used to
clean up after a fparam fixup:

void fparam_free_restore(void** param);
int fixup_free_fparam_all(void** param, int param_no);
int fixup_free_fparam_1(void** param, int param_no);
int fixup_free_fparam_2(void** param, int param_no);

They will free anything that was allocated by the fixup and
restore the parameter pointer to the saved original value.

Andrei Pelinescu-Onciul authored on 05/08/2010 20:47:29
Showing 3 changed files
... ...
@@ -34,6 +34,7 @@
34 34
 
35 35
 #include "mod_fix.h"
36 36
 #include "mem/mem.h"
37
+#include "trim.h"
37 38
 
38 39
 
39 40
 
... ...
@@ -1169,6 +1169,7 @@ error:
1169 1169
 
1170 1170
 /** fparam_t free function.
1171 1171
  *  Frees the "content" of a fparam, but not the fparam itself.
1172
+ *  Note: it doesn't free fp->orig!
1172 1173
  *  Assumes pkg_malloc'ed content.
1173 1174
  *  @param fp -  fparam to be freed
1174 1175
  *
... ...
@@ -1220,10 +1221,25 @@ void fparam_free_contents(fparam_t* fp)
1220 1220
 			}
1221 1221
 			break;
1222 1222
 	}
1223
-	if (fp->orig){
1224
-		pkg_free(fp->orig);
1225
-		fp->orig=0;
1226
-	}
1223
+}
1224
+
1225
+
1226
+
1227
+/** generic free fixup type function for a fixed fparam.
1228
+ * It will free whatever was allocated during the initial fparam fixup
1229
+ * and restore the original param value.
1230
+ */
1231
+void fparam_free_restore(void** param)
1232
+{
1233
+	fparam_t *fp;
1234
+	void *orig;
1235
+	
1236
+	fp = *param;
1237
+	orig = fp->orig;
1238
+	fp->orig = 0;
1239
+	fparam_free_contents(fp);
1240
+	pkg_free(fp);
1241
+	*param = orig;
1227 1242
 }
1228 1243
 
1229 1244
 
... ...
@@ -1574,6 +1590,49 @@ int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param)
1574 1574
 
1575 1575
 
1576 1576
 
1577
+/** generic free fixup function for "pure" fparam type fixups.
1578
+ * @param  param - double pointer to param, as for normal fixup functions.
1579
+ * @param  param_no - parameter number, ignored.
1580
+ * @return 0 on success (always).
1581
+ */
1582
+int fixup_free_fparam_all(void** param, int param_no)
1583
+{
1584
+	fparam_free_restore(param);
1585
+	return 0;
1586
+}
1587
+
1588
+
1589
+
1590
+/** generic free fixup function for "pure"  first parameter fparam type fixups.
1591
+ * @param  param - double pointer to param, as for normal fixup functions.
1592
+ * @param  param_no - parameter number: the function will work only for
1593
+ *                     param_no == 1 (first parameter).
1594
+ * @return 0 on success (always).
1595
+ */
1596
+int fixup_free_fparam_1(void** param, int param_no)
1597
+{
1598
+	if (param_no == 1)
1599
+		fparam_free_restore(param);
1600
+	return 0;
1601
+}
1602
+
1603
+
1604
+
1605
+/** generic free fixup function for "pure"  2nd parameter fparam type fixups.
1606
+ * @param  param - double pointer to param, as for normal fixup functions.
1607
+ * @param  param_no - parameter number: the function will work only for
1608
+ *                     param_no == 2 (2nd parameter).
1609
+ * @return 0 on success (always).
1610
+ */
1611
+int fixup_free_fparam_2(void** param, int param_no)
1612
+{
1613
+	if (param_no == 2)
1614
+		fparam_free_restore(param);
1615
+	return 0;
1616
+}
1617
+
1618
+
1619
+
1577 1620
 /** returns true if a fixup is a fparam_t* one.
1578 1621
  * Used to automatically detect fparam fixups that can be used with non
1579 1622
  * contant RVEs.
... ...
@@ -591,4 +591,13 @@ int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param);
591 591
 
592 592
 int is_fparam_rve_fixup(fixup_function f);
593 593
 
594
+
595
+/** generic free fixup type function for a fixed fparam.
596
+ * It will free whatever was allocated during the initial fparam fixup
597
+ * and restore the original param value.
598
+ */
599
+void fparam_free_restore(void** param);
600
+int fixup_free_fparam_all(void** param, int param_no);
601
+int fixup_free_fparam_1(void** param, int param_no);
602
+int fixup_free_fparam_2(void** param, int param_no);
594 603
 #endif /* sr_module_h */