ms: Hold a reference during ms_alloc

Make the caller hold a reference to the MS object just allocated, so
that it hs to explicitly unref it and, in turn, if no new references
were added during its use, trigger release of the MS object.
This is useful to avoid leaking MS object if it was allocated and then
no TBF is attached to it because allocation of TBF failed.

Related: OS#6002
Change-Id: I2088a7ddd76fe9157b6626ef96ae4315e88779ea
diff --git a/tests/alloc/AllocTest.cpp b/tests/alloc/AllocTest.cpp
index 065a257..8a67152 100644
--- a/tests/alloc/AllocTest.cpp
+++ b/tests/alloc/AllocTest.cpp
@@ -135,8 +135,9 @@
 	 * least this part is working okay.
 	 */
 	for (i = 0; i < (int)ARRAY_SIZE(tbfs); ++i) {
-		ms = ms_alloc(bts);
+		ms = ms_alloc(bts, __func__);
 		tbfs[i] = tbf_alloc(bts, ms, dir, -1, 0);
+		ms_unref(ms, __func__);
 		if (tbfs[i] == NULL)
 			break;
 
@@ -155,7 +156,7 @@
 		if (tbfs[i])
 			tbf_free(tbfs[i]);
 
-	ms = ms_alloc(bts);
+	ms = ms_alloc(bts, NULL);
 	tbfs[0] = tbf_alloc(bts, ms, dir, -1, 0);
 	OSMO_ASSERT(tbfs[0]);
 	tbf_free(tbfs[0]);
@@ -221,13 +222,15 @@
 
 	enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
 
-	ms = ms_alloc(bts);
+	ms = ms_alloc(bts, __func__);
 	ms_set_ms_class(ms, ms_class);
 	/* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
 	ms_set_timeout(ms, 0);
 	ul_tbf = ul_tbf_alloc(bts, ms, -1, true);
-	if (!ul_tbf)
+	if (!ul_tbf) {
+		ms_unref(ms, __func__);
 		return false;
+	}
 
 	OSMO_ASSERT(ul_tbf->ms());
 	OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
@@ -236,13 +239,16 @@
 
 	/* assume final ack has not been sent */
 	dl_tbf = dl_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
-	if (!dl_tbf)
+	if (!dl_tbf) {
+		ms_unref(ms, __func__);
 		return false;
+	}
 
 	dump_assignment(dl_tbf, "DL", verbose);
 
 	check_tfi_usage(bts);
 
+	ms_unref(ms, __func__);
 	tbf_free(dl_tbf);
 	tbf_free(ul_tbf);
 	talloc_free(bts);
@@ -264,13 +270,15 @@
 
 	enable_ts_on_bts(bts, ts0, ts1, ts2, ts3, ts4, ts5, ts6, ts7);
 
-	ms = ms_alloc(bts);
+	ms = ms_alloc(bts, __func__);
 	ms_set_ms_class(ms, ms_class);
 	/* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
 	ms_set_timeout(ms, 0);
 	dl_tbf = dl_tbf_alloc(bts, ms, -1, true);
-	if (!dl_tbf)
+	if (!dl_tbf) {
+		ms_unref(ms, __func__);
 		return false;
+	}
 
 	ms_confirm_tlli(ms, 0x23);
 	OSMO_ASSERT(dl_tbf->ms() == ms);
@@ -279,8 +287,10 @@
 	dump_assignment(dl_tbf, "DL", verbose);
 
 	ul_tbf = ul_tbf_alloc(bts, ms, ms_current_trx(ms)->trx_no, false);
-	if (!ul_tbf)
+	if (!ul_tbf) {
+		ms_unref(ms, __func__);
 		return false;
+	}
 
 	ms_update_announced_tlli(ms, 0x23);
 	ul_tbf->m_contention_resolution_done = true;
@@ -293,6 +303,7 @@
 
 	check_tfi_usage(bts);
 
+	ms_unref(ms, __func__);
 	tbf_free(dl_tbf);
 	tbf_free(ul_tbf);
 	talloc_free(bts);
@@ -315,13 +326,15 @@
 
 	tfi = bts_tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx_no, -1);
 	OSMO_ASSERT(tfi >= 0);
-	ms = ms_alloc(bts);
+	ms = ms_alloc(bts, __func__);
 	ms_set_ms_class(ms, ms_class);
 	/* Avoid delaying free to avoid tons of to-be-freed ms objects queuing */
 	ms_set_timeout(ms, 0);
 	ul_tbf = ul_tbf_alloc(bts, ms, -1, false);
-	if (!ul_tbf)
+	if (!ul_tbf) {
+		ms_unref(ms, __func__);
 		return false;
+	}
 
 	OSMO_ASSERT(ul_tbf->ms() == ms);
 	OSMO_ASSERT(ms_current_trx(ul_tbf->ms()));
@@ -330,13 +343,16 @@
 
 	/* assume final ack has not been sent */
 	dl_tbf = dl_tbf_alloc(bts, ms, trx_no, false);
-	if (!dl_tbf)
+	if (!dl_tbf) {
+		ms_unref(ms, __func__);
 		return false;
+	}
 
 	dump_assignment(dl_tbf, "DL", true);
 
 	check_tfi_usage(bts);
 
+	ms_unref(ms, __func__);
 	tbf_free(dl_tbf);
 	tbf_free(ul_tbf);
 	talloc_free(bts);
@@ -561,7 +577,7 @@
 
 		ms = bts_get_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
 		if (!ms)
-			ms = ms_alloc(bts);
+			ms = ms_alloc(bts, NULL);
 		ms_set_ms_class(ms, ms_class);
 		ms = alloc_tbfs(bts, ms, mode);
 		if (!ms)
@@ -770,7 +786,7 @@
 	trx->pdch[6].enable();
 	trx->pdch[7].enable();
 
-	ms = ms_alloc(bts);
+	ms = ms_alloc(bts, NULL);
 	ms_set_ms_class(ms, ms_class);
 	ms_set_egprs_ms_class(ms, egprs_ms_class);
 	dl_tbf1 = dl_tbf_alloc(bts, ms, 0, false);
@@ -783,7 +799,7 @@
 	OSMO_ASSERT(numTs1 == 4);
 	printf("TBF1: numTs(%d)\n", numTs1);
 
-	ms = ms_alloc(bts);
+	ms = ms_alloc(bts, NULL);
 	ms_set_ms_class(ms, ms_class);
 	ms_set_egprs_ms_class(ms, egprs_ms_class);
 	dl_tbf2 = dl_tbf_alloc(bts, ms, 0, false);