mgcp_ratectr: do not set talloc destructor on library allocated item

The rate counter and stats item groups, which are allocated in
mgcp_ratectr.c are freed using a talloc destructor that is set in the
context of the item we just allocated using the stats / rate counter API
functions. When we do that, we risk overwriting an already existing
talloc destructor. Lets instead implement own free functions and set
those as talloc_destructor from above (trunk and MGCP config)

Change-Id: Ifc5091e9f95cc721e58d1eb2e55b97102c497706
Related: OS#5201
diff --git a/src/libosmo-mgcp/mgcp_trunk.c b/src/libosmo-mgcp/mgcp_trunk.c
index a97ad39..c69c242 100644
--- a/src/libosmo-mgcp/mgcp_trunk.c
+++ b/src/libosmo-mgcp/mgcp_trunk.c
@@ -35,7 +35,17 @@
 	{ 0, NULL }
 };
 
-/*! allocate trunk and add it (if required) to the trunk list.
+/* Free trunk, this function is automatically called by talloc_free when the trunk is freed. It does not free the
+ * endpoints on the trunk, this must be done separately before freeing the trunk. */
+static int trunk_free_talloc_destructor(struct mgcp_trunk *trunk)
+{
+	llist_del(&trunk->entry);
+	mgcp_ratectr_trunk_free(trunk);
+	mgcp_stat_trunk_free(trunk);
+	return 0;
+}
+
+/*! allocate trunk and add it to the trunk list.
  *  (called once at startup by VTY).
  *  \param[in] cfg mgcp configuration.
  *  \param[in] ttype trunk type.
@@ -66,6 +76,7 @@
 
 	mgcp_ratectr_trunk_alloc(trunk);
 	mgcp_stat_trunk_alloc(trunk);
+	talloc_set_destructor(trunk, trunk_free_talloc_destructor);
 
 	return trunk;
 }