From 87d0eddc2c529b9539a7e52f1cf282c93c5120fe Mon Sep 17 00:00:00 2001
From: maaroufi <moaad.maaroufi@eurecom.fr>
Date: Mon, 14 Aug 2023 13:33:27 +0200
Subject: [PATCH] Modified the code to solve the heap Allocation issue: Removed
 Dynamic allocation based on the FAPI specifications

---
 nfapi/oai_integration/nfapi_vnf.c             | 27 +---------
 .../nfapi/public_inc/nfapi_nr_interface_scf.h |  6 +--
 nfapi/open-nFAPI/nfapi/src/nfapi_p7.c         | 48 ++++++-----------
 openair1/PHY/NR_TRANSPORT/pucch_rx.c          | 54 ++++++++-----------
 openair1/SCHED_NR_UE/fapi_nr_ue_l1.c          | 20 ++-----
 .../LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c     | 16 +++---
 6 files changed, 56 insertions(+), 115 deletions(-)

diff --git a/nfapi/oai_integration/nfapi_vnf.c b/nfapi/oai_integration/nfapi_vnf.c
index 3bf9932d65..0c9be9e2ac 100644
--- a/nfapi/oai_integration/nfapi_vnf.c
+++ b/nfapi/oai_integration/nfapi_vnf.c
@@ -780,21 +780,7 @@ int phy_nr_uci_indication(nfapi_nr_uci_indication_t *ind)
         case NFAPI_NR_UCI_FORMAT_0_1_PDU_TYPE: {
           nfapi_nr_uci_pucch_pdu_format_0_1_t *uci_ind_pdu = &uci_ind->uci_list[i].pucch_pdu_format_0_1;
           nfapi_nr_uci_pucch_pdu_format_0_1_t *ind_pdu = &ind->uci_list[i].pucch_pdu_format_0_1;
-          if (ind_pdu->sr) {
-            uci_ind_pdu->sr = CALLOC(1, sizeof(*uci_ind_pdu->sr));
-            AssertFatal(uci_ind_pdu->sr != NULL, "Memory not allocated for uci_ind_pdu->harq in phy_nr_uci_indication.");
-            *uci_ind_pdu->sr = *ind_pdu->sr;
-          }
-          if (ind_pdu->harq) {
-            uci_ind_pdu->harq = CALLOC(1, sizeof(*uci_ind_pdu->harq));
-            AssertFatal(uci_ind_pdu->harq != NULL, "Memory not allocated for uci_ind_pdu->harq in phy_nr_uci_indication.");
-            *uci_ind_pdu->harq = *ind_pdu->harq;
-
-            uci_ind_pdu->harq->harq_list = CALLOC(uci_ind_pdu->harq->num_harq, sizeof(*uci_ind_pdu->harq->harq_list));
-            AssertFatal(uci_ind_pdu->harq->harq_list != NULL, "Memory not allocated for uci_ind_pdu->harq->harq_list in phy_nr_uci_indication.");
-            for (int j = 0; j < uci_ind_pdu->harq->num_harq; j++)
-                uci_ind_pdu->harq->harq_list[j].harq_value = ind_pdu->harq->harq_list[j].harq_value;
-          }
+          
           break;
         }
 
@@ -834,16 +820,7 @@ int phy_nr_uci_indication(nfapi_nr_uci_indication_t *ind)
       {
           if (uci_ind->uci_list[i].pdu_type == NFAPI_NR_UCI_FORMAT_0_1_PDU_TYPE)
           {
-            if (uci_ind->uci_list[i].pucch_pdu_format_0_1.harq) {
-              free(uci_ind->uci_list[i].pucch_pdu_format_0_1.harq->harq_list);
-              uci_ind->uci_list[i].pucch_pdu_format_0_1.harq->harq_list = NULL;
-              free(uci_ind->uci_list[i].pucch_pdu_format_0_1.harq);
-              uci_ind->uci_list[i].pucch_pdu_format_0_1.harq = NULL;
-            }
-            if (uci_ind->uci_list[i].pucch_pdu_format_0_1.sr) {
-              free(uci_ind->uci_list[i].pucch_pdu_format_0_1.sr);
-              uci_ind->uci_list[i].pucch_pdu_format_0_1.sr = NULL;
-            }
+            
           }
           if (uci_ind->uci_list[i].pdu_type == NFAPI_NR_UCI_FORMAT_2_3_4_PDU_TYPE)
           {
diff --git a/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h b/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h
index 0af6504110..67ee697934 100644
--- a/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h
+++ b/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h
@@ -1580,7 +1580,7 @@ typedef struct
 {
   uint8_t num_harq;
   uint8_t harq_confidence_level;
-  nfapi_nr_harq_t* harq_list;
+  nfapi_nr_harq_t harq_list[2];
 
 } nfapi_nr_harq_pdu_0_1_t;
 
@@ -1628,8 +1628,8 @@ typedef struct
   uint8_t  ul_cqi;
   uint16_t timing_advance;
   uint16_t rssi;
-  nfapi_nr_sr_pdu_0_1_t *sr;//67
-  nfapi_nr_harq_pdu_0_1_t *harq;//68
+  nfapi_nr_sr_pdu_0_1_t  sr;//67
+  nfapi_nr_harq_pdu_0_1_t harq;//68
   
 
 }nfapi_nr_uci_pucch_pdu_format_0_1_t;
diff --git a/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c b/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c
index 6af410bf53..e470883f4d 100644
--- a/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c
+++ b/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c
@@ -3292,20 +3292,20 @@ static uint8_t pack_nr_uci_pucch_0_1(void* tlv, uint8_t **ppWritePackedMsg, uint
 	if (!push16(value->rssi, ppWritePackedMsg, end))
 		return 0;
 	if (value->pduBitmap & 0x01) { //SR
-		if (!push8(value->sr->sr_indication, ppWritePackedMsg, end))
+		if (!push8(value->sr.sr_indication, ppWritePackedMsg, end))
 			return 0;
-		if (!push8(value->sr->sr_confidence_level, ppWritePackedMsg, end))
+		if (!push8(value->sr.sr_confidence_level, ppWritePackedMsg, end))
 			return 0;
 	}
 
 	if (((value->pduBitmap >> 1) & 0x01)) { //HARQ
-		if (!push8(value->harq->num_harq, ppWritePackedMsg, end))
+		if (!push8(value->harq.num_harq, ppWritePackedMsg, end))
 			return 0;
-		if (!push8(value->harq->harq_confidence_level, ppWritePackedMsg, end))
+		if (!push8(value->harq.harq_confidence_level, ppWritePackedMsg, end))
 			return 0;
-		for (int i = 0; i < value->harq->num_harq; i++)
+		for (int i = 0; i < value->harq.num_harq; i++)
 		{
-			if (!push8(value->harq->harq_list[i].harq_value, ppWritePackedMsg, end))
+			if (!push8(value->harq.harq_list[i].harq_value, ppWritePackedMsg, end))
 				return 0;
 		}
 	}
@@ -6075,41 +6075,23 @@ static uint8_t unpack_nr_uci_pucch_0_1(nfapi_nr_uci_pucch_pdu_format_0_1_t *valu
 		 ))
 		  return 0;
 	if (value->pduBitmap & 0x01) { //SR
-		value->sr = nfapi_p7_allocate(sizeof(*value->sr), config);
-		if (value->sr == NULL)
-		{
-			NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s failed to allocate sr pdu\n", __FUNCTION__);
-			return 0;
-		}
-		if(!(pull8(ppReadPackedMsg, &value->sr->sr_indication, end) &&
-	 	 pull8(ppReadPackedMsg, &value->sr->sr_confidence_level, end) 
+		if(!(pull8(ppReadPackedMsg, &value->sr.sr_indication, end) &&
+	 	 pull8(ppReadPackedMsg, &value->sr.sr_confidence_level, end) 
 		 ))
 		  return 0;
 	}
 
 	if (((value->pduBitmap >> 1) & 0x01)) { //HARQ
 
-                value->harq = nfapi_p7_allocate(sizeof(*value->harq), config);
-                if (value->harq == NULL)
-                {
-                        NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s failed to allocate nr_harq pdu\n", __FUNCTION__);
-                        return 0;
-                }
-                if (!(pull8(ppReadPackedMsg, &value->harq->num_harq, end) &&
-                        pull8(ppReadPackedMsg, &value->harq->harq_confidence_level, end)
+                
+                if (!(pull8(ppReadPackedMsg, &value->harq.num_harq, end) &&
+                        pull8(ppReadPackedMsg, &value->harq.harq_confidence_level, end)
                         ))
                         return 0;
-                value->harq->harq_list = NULL;
-                if (value->harq->num_harq > 0) {
-                        value->harq->harq_list = nfapi_p7_allocate(sizeof(*value->harq->harq_list) * value->harq->num_harq, config);
-                        if (value->harq->harq_list == NULL)
-                        {
-                                NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s failed to allocate harq ind pdu list (count:%d)\n",
-                                                __FUNCTION__, value->harq->num_harq);
-                                return 0;
-                        }
-                        for (int i = 0; i < value->harq->num_harq; i++) {
-                                if (!pull8(ppReadPackedMsg, &value->harq->harq_list[i].harq_value, end)) {
+                if (value->harq.num_harq > 0) {
+                        
+                        for (int i = 0; i < value->harq.num_harq; i++) {
+                                if (!pull8(ppReadPackedMsg, &value->harq.harq_list[i].harq_value, end)) {
                                         return 0;
                                 }
                         }
diff --git a/openair1/PHY/NR_TRANSPORT/pucch_rx.c b/openair1/PHY/NR_TRANSPORT/pucch_rx.c
index 02de3a2bbd..58048c5224 100644
--- a/openair1/PHY/NR_TRANSPORT/pucch_rx.c
+++ b/openair1/PHY/NR_TRANSPORT/pucch_rx.c
@@ -146,7 +146,7 @@ static const int16_t idft12_im[12][12] = {
   {0,-20066,-20066,0,20066,20066,0,-20066,-20066,0,20066,20066},
   {0,-11585,-20066,-23170,-20066,-11585,0,11585,20066,23170,20066,11585}
 };
-
+//************************************************************************//
 void nr_decode_pucch0(PHY_VARS_gNB *gNB,
                       int frame,
                       int slot,
@@ -406,31 +406,27 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
   uci_pdu->rssi = 1280 - (10*dB_fixed(32767*32767))-dB_fixed_times10(sigenergy);
 
   if (pucch_pdu->bit_len_harq==0) {
-    uci_pdu->harq = NULL;
-    uci_pdu->sr = calloc(1,sizeof(*uci_pdu->sr));
-    uci_pdu->sr->sr_confidence_level = SNRtimes10 < uci_stats->pucch0_thres;
+    uci_pdu->sr.sr_confidence_level = SNRtimes10 < uci_stats->pucch0_thres;
     uci_stats->pucch0_sr_trials++;
     if (xrtmag_dBtimes10>(10*max_n0+100)) {
-      uci_pdu->sr->sr_indication = 1;
+      uci_pdu->sr.sr_indication = 1;
       uci_stats->pucch0_positive_SR++;
       LOG_D(PHY,"PUCCH0 got positive SR. Cumulative number of positive SR %d\n", uci_stats->pucch0_positive_SR);
     } else {
-      uci_pdu->sr->sr_indication = 0;
+      uci_pdu->sr.sr_indication = 0;
     }
   }
   else if (pucch_pdu->bit_len_harq==1) {
-    uci_pdu->harq = calloc(1,sizeof(*uci_pdu->harq));
-    uci_pdu->harq->num_harq = 1;
-    uci_pdu->harq->harq_confidence_level = no_conf;
-    uci_pdu->harq->harq_list = (nfapi_nr_harq_t*)malloc(sizeof *uci_pdu->harq->harq_list);
-    uci_pdu->harq->harq_list[0].harq_value = !(index&0x01);
+    uci_pdu->harq.num_harq = 1;
+    uci_pdu->harq.harq_confidence_level = no_conf;
+    uci_pdu->harq.harq_list[0].harq_value = !(index&0x01);
     LOG_D(PHY,
           "[DLSCH/PDSCH/PUCCH] %d.%d HARQ %s with confidence level %s xrt_mag %d xrt_mag_next %d n0 %d (%d,%d) pucch0_thres %d, "
           "cqi %d, SNRtimes10 %d, energy %f\n",
           frame,
           slot,
-          uci_pdu->harq->harq_list[0].harq_value == 0 ? "ACK" : "NACK",
-          uci_pdu->harq->harq_confidence_level == 0 ? "good" : "bad",
+          uci_pdu->harq.harq_list[0].harq_value == 0 ? "ACK" : "NACK",
+          uci_pdu->harq.harq_confidence_level == 0 ? "good" : "bad",
           xrtmag_dBtimes10,
           xrtmag_next_dBtimes10,
           max_n0,
@@ -442,10 +438,9 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
           10 * log10((double)sigenergy));
 
     if (pucch_pdu->sr_flag == 1) {
-      uci_pdu->sr = calloc(1,sizeof(*uci_pdu->sr));
-      uci_pdu->sr->sr_indication = (index>1);
-      uci_pdu->sr->sr_confidence_level = no_conf;
-      if(uci_pdu->sr->sr_indication == 1 && uci_pdu->sr->sr_confidence_level == 0) {
+      uci_pdu->sr.sr_indication = (index>1);
+      uci_pdu->sr.sr_confidence_level = no_conf;
+      if(uci_pdu->sr.sr_indication == 1 && uci_pdu->sr.sr_confidence_level == 0) {
         uci_stats->pucch0_positive_SR++;
         LOG_D(PHY,"PUCCH0 got positive SR. Cumulative number of positive SR %d\n", uci_stats->pucch0_positive_SR);
       }
@@ -453,21 +448,19 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
     uci_stats->pucch01_trials++;
   }
   else {
-    uci_pdu->harq = calloc(1,sizeof(*uci_pdu->harq));
-    uci_pdu->harq->num_harq = 2;
-    uci_pdu->harq->harq_confidence_level = no_conf;
-    uci_pdu->harq->harq_list = (nfapi_nr_harq_t*)malloc(2 * sizeof( *uci_pdu->harq->harq_list));
+    uci_pdu->harq.num_harq = 2;
+    uci_pdu->harq.harq_confidence_level = no_conf;
 
-    uci_pdu->harq->harq_list[1].harq_value = !(index&0x01);
-    uci_pdu->harq->harq_list[0].harq_value = !((index>>1)&0x01);
+    uci_pdu->harq.harq_list[1].harq_value = !(index&0x01);
+    uci_pdu->harq.harq_list[0].harq_value = !((index>>1)&0x01);
     LOG_D(PHY,
           "[DLSCH/PDSCH/PUCCH] %d.%d HARQ values (%s, %s) with confidence level %s, xrt_mag %d xrt_mag_next %d n0 %d (%d,%d) "
           "pucch0_thres %d, cqi %d, SNRtimes10 %d\n",
           frame,
           slot,
-          uci_pdu->harq->harq_list[1].harq_value == 0 ? "ACK" : "NACK",
-          uci_pdu->harq->harq_list[0].harq_value == 0 ? "ACK" : "NACK",
-          uci_pdu->harq->harq_confidence_level == 0 ? "good" : "bad",
+          uci_pdu->harq.harq_list[1].harq_value == 0 ? "ACK" : "NACK",
+          uci_pdu->harq.harq_list[0].harq_value == 0 ? "ACK" : "NACK",
+          uci_pdu->harq.harq_confidence_level == 0 ? "good" : "bad",
           xrtmag_dBtimes10,
           xrtmag_next_dBtimes10,
           max_n0,
@@ -477,17 +470,16 @@ void nr_decode_pucch0(PHY_VARS_gNB *gNB,
           cqi,
           SNRtimes10);
     if (pucch_pdu->sr_flag == 1) {
-      uci_pdu->sr = calloc(1,sizeof(*uci_pdu->sr));
-      uci_pdu->sr->sr_indication = (index>3) ? 1 : 0;
-      uci_pdu->sr->sr_confidence_level = no_conf;
-      if(uci_pdu->sr->sr_indication == 1 && uci_pdu->sr->sr_confidence_level == 0) {
+      uci_pdu->sr.sr_indication = (index>3) ? 1 : 0;
+      uci_pdu->sr.sr_confidence_level = no_conf;
+      if(uci_pdu->sr.sr_indication == 1 && uci_pdu->sr.sr_confidence_level == 0) {
         uci_stats->pucch0_positive_SR++;
         LOG_D(PHY,"PUCCH0 got positive SR. Cumulative number of positive SR %d\n", uci_stats->pucch0_positive_SR);
       }
     }
   }
 }
-
+//*****************************************************************//
 void nr_decode_pucch1(c16_t **rxdataF,
 		      pucch_GroupHopping_t pucch_GroupHopping,
                       uint32_t n_id,       // hoppingID higher layer parameter  
diff --git a/openair1/SCHED_NR_UE/fapi_nr_ue_l1.c b/openair1/SCHED_NR_UE/fapi_nr_ue_l1.c
index 33d550e130..14ef545871 100644
--- a/openair1/SCHED_NR_UE/fapi_nr_ue_l1.c
+++ b/openair1/SCHED_NR_UE/fapi_nr_ue_l1.c
@@ -83,15 +83,7 @@ static void free_uci_inds(nfapi_nr_uci_indication_t *uci_ind)
         if (uci_ind->uci_list[k].pdu_type == NFAPI_NR_UCI_FORMAT_0_1_PDU_TYPE)
         {
             nfapi_nr_uci_pucch_pdu_format_0_1_t *pdu_0_1 = &uci_ind->uci_list[k].pucch_pdu_format_0_1;
-            free(pdu_0_1->sr);
-            pdu_0_1->sr = NULL;
-            if (pdu_0_1->harq)
-            {
-                free(pdu_0_1->harq->harq_list);
-                pdu_0_1->harq->harq_list = NULL;
-            }
-            free(pdu_0_1->harq);
-            pdu_0_1->harq = NULL;
+            
         }
         if (uci_ind->uci_list[k].pdu_type == NFAPI_NR_UCI_FORMAT_2_3_4_PDU_TYPE)
         {
@@ -259,10 +251,8 @@ int8_t nr_ue_scheduled_response_stub(nr_scheduled_response_t *scheduled_response
                 if (mac->nr_ue_emul_l1.num_harqs > 0) {
                   int harq_index = 0;
                   pdu_0_1->pduBitmap = 2; // (value->pduBitmap >> 1) & 0x01) == HARQ and (value->pduBitmap) & 0x01) == SR
-                  pdu_0_1->harq = CALLOC(1, sizeof(*pdu_0_1->harq));
-                  pdu_0_1->harq->num_harq = mac->nr_ue_emul_l1.num_harqs;
-                  pdu_0_1->harq->harq_confidence_level = 0;
-                  pdu_0_1->harq->harq_list = CALLOC(pdu_0_1->harq->num_harq, sizeof(*pdu_0_1->harq->harq_list));
+                  pdu_0_1->harq.num_harq = mac->nr_ue_emul_l1.num_harqs;
+                  pdu_0_1->harq.harq_confidence_level = 0;
                   int harq_pid = -1;
                   for (int k = 0; k < NR_MAX_HARQ_PROCESSES; k++) {
                     if (mac->nr_ue_emul_l1.harq[k].active &&
@@ -270,8 +260,8 @@ int8_t nr_ue_scheduled_response_stub(nr_scheduled_response_t *scheduled_response
                         mac->nr_ue_emul_l1.harq[k].active_dl_harq_slot == uci_ind->slot) {
                       mac->nr_ue_emul_l1.harq[k].active = false;
                       harq_pid = k;
-                      AssertFatal(harq_index < pdu_0_1->harq->num_harq, "Invalid harq_index %d\n", harq_index);
-                      pdu_0_1->harq->harq_list[harq_index].harq_value = !mac->dl_harq_info[k].ack;
+                      AssertFatal(harq_index < pdu_0_1->harq.num_harq, "Invalid harq_index %d\n", harq_index);
+                      pdu_0_1->harq.harq_list[harq_index].harq_value = !mac->dl_harq_info[k].ack;
                       harq_index++;
                     }
                   }
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c
index cf6001626d..c21d7b1d9c 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_uci.c
@@ -938,9 +938,9 @@ void handle_nr_uci_pucch_0_1(module_id_t mod_id,
 
   if (((uci_01->pduBitmap >> 1) & 0x01)) {
     // iterate over received harq bits
-    for (int harq_bit = 0; harq_bit < uci_01->harq->num_harq; harq_bit++) {
-      const uint8_t harq_value = uci_01->harq->harq_list[harq_bit].harq_value;
-      const uint8_t harq_confidence = uci_01->harq->harq_confidence_level;
+    for (int harq_bit = 0; harq_bit < uci_01->harq.num_harq; harq_bit++) {
+      const uint8_t harq_value = uci_01->harq.harq_list[harq_bit].harq_value;
+      const uint8_t harq_confidence = uci_01->harq.harq_confidence_level;
       NR_UE_harq_t *harq = find_harq(frame, slot, UE, nrmac->dl_bler.harq_round_max);
       if (!harq) {
         LOG_E(NR_MAC, "Oh no! Could not find a harq in %s!\n", __FUNCTION__);
@@ -955,24 +955,24 @@ void handle_nr_uci_pucch_0_1(module_id_t mod_id,
     }
 
     // tpc (power control) only if we received AckNack
-    if (uci_01->harq->harq_confidence_level==0)
+    if (uci_01->harq.harq_confidence_level==0)
       sched_ctrl->tpc1 = nr_get_tpc(nrmac->pucch_target_snrx10, uci_01->ul_cqi, 30);
     else
       sched_ctrl->tpc1 = 3;
     sched_ctrl->pucch_snrx10 = uci_01->ul_cqi * 5 - 640;
 
-    free(uci_01->harq->harq_list);
-    free(uci_01->harq);
+    
+    
   }
 
   // check scheduling request result, confidence_level == 0 is good
   if (uci_01->pduBitmap & 0x1) {
-    if (uci_01->sr->sr_indication && uci_01->sr->sr_confidence_level == 0 && uci_01->ul_cqi >= 148) {
+    if (uci_01->sr.sr_indication && uci_01->sr.sr_confidence_level == 0 && uci_01->ul_cqi >= 148) {
       // SR detected with SNR >= 10dB
       sched_ctrl->SR |= true;
       LOG_D(NR_MAC, "SR UE %04x ul_cqi %d\n", uci_01->rnti, uci_01->ul_cqi);
     }
-    free(uci_01->sr);
+    
   }
   NR_SCHED_UNLOCK(&nrmac->sched_lock);
 }
-- 
2.26.2