Browse Source

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 15 years ago
parent
commit
6a40b4bdc0
3 changed files with 73 additions and 4 deletions
  1. 1 0
      mod_fix.c
  2. 63 4
      sr_module.c
  3. 9 0
      sr_module.h

+ 1 - 0
mod_fix.c

@@ -34,6 +34,7 @@
 
 #include "mod_fix.h"
 #include "mem/mem.h"
+#include "trim.h"
 
 
 

+ 63 - 4
sr_module.c

@@ -1169,6 +1169,7 @@ error:
 
 /** fparam_t free function.
  *  Frees the "content" of a fparam, but not the fparam itself.
+ *  Note: it doesn't free fp->orig!
  *  Assumes pkg_malloc'ed content.
  *  @param fp -  fparam to be freed
  *
@@ -1220,10 +1221,25 @@ void fparam_free_contents(fparam_t* fp)
 			}
 			break;
 	}
-	if (fp->orig){
-		pkg_free(fp->orig);
-		fp->orig=0;
-	}
+}
+
+
+
+/** generic free fixup type function for a fixed fparam.
+ * It will free whatever was allocated during the initial fparam fixup
+ * and restore the original param value.
+ */
+void fparam_free_restore(void** param)
+{
+	fparam_t *fp;
+	void *orig;
+	
+	fp = *param;
+	orig = fp->orig;
+	fp->orig = 0;
+	fparam_free_contents(fp);
+	pkg_free(fp);
+	*param = orig;
 }
 
 
@@ -1574,6 +1590,49 @@ int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param)
 
 
 
+/** generic free fixup function for "pure" fparam type fixups.
+ * @param  param - double pointer to param, as for normal fixup functions.
+ * @param  param_no - parameter number, ignored.
+ * @return 0 on success (always).
+ */
+int fixup_free_fparam_all(void** param, int param_no)
+{
+	fparam_free_restore(param);
+	return 0;
+}
+
+
+
+/** generic free fixup function for "pure"  first parameter fparam type fixups.
+ * @param  param - double pointer to param, as for normal fixup functions.
+ * @param  param_no - parameter number: the function will work only for
+ *                     param_no == 1 (first parameter).
+ * @return 0 on success (always).
+ */
+int fixup_free_fparam_1(void** param, int param_no)
+{
+	if (param_no == 1)
+		fparam_free_restore(param);
+	return 0;
+}
+
+
+
+/** generic free fixup function for "pure"  2nd parameter fparam type fixups.
+ * @param  param - double pointer to param, as for normal fixup functions.
+ * @param  param_no - parameter number: the function will work only for
+ *                     param_no == 2 (2nd parameter).
+ * @return 0 on success (always).
+ */
+int fixup_free_fparam_2(void** param, int param_no)
+{
+	if (param_no == 2)
+		fparam_free_restore(param);
+	return 0;
+}
+
+
+
 /** returns true if a fixup is a fparam_t* one.
  * Used to automatically detect fparam fixups that can be used with non
  * contant RVEs.

+ 9 - 0
sr_module.h

@@ -591,4 +591,13 @@ int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param);
 
 int is_fparam_rve_fixup(fixup_function f);
 
+
+/** generic free fixup type function for a fixed fparam.
+ * It will free whatever was allocated during the initial fparam fixup
+ * and restore the original param value.
+ */
+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);
 #endif /* sr_module_h */