api: Use vl_msg_push/pop_heap

Type: refactor

Change-Id: I0eb46676fc22ce6825b2d879498df344b5a855e8
Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
diff --git a/src/vat/api_format.c b/src/vat/api_format.c
index e4ef0b4..ff61e9a 100644
--- a/src/vat/api_format.c
+++ b/src/vat/api_format.c
@@ -1100,7 +1100,6 @@
 {
   vat_main_t *vam = &vat_main;
   vat_json_node_t node;
-  api_main_t *am = vlibapi_get_main ();
   void *oldheap;
   u8 *reply;
 
@@ -1109,14 +1108,12 @@
   vat_json_object_add_uint (&node, "reply_in_shmem",
 			    ntohl (mp->reply_in_shmem));
   /* Toss the shared-memory original... */
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
 
   reply = uword_to_pointer (mp->reply_in_shmem, u8 *);
   vec_free (reply);
 
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   vat_json_print (vam->ofp, &node);
   vat_json_free (&node);
@@ -2710,7 +2707,6 @@
   (vl_api_get_node_graph_reply_t * mp)
 {
   vat_main_t *vam = &vat_main;
-  api_main_t *am = vlibapi_get_main ();
   i32 retval = ntohl (mp->retval);
   u8 *pvt_copy, *reply;
   void *oldheap;
@@ -2735,13 +2731,11 @@
   pvt_copy = vec_dup (reply);
 
   /* Toss the shared-memory original... */
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
 
   vec_free (reply);
 
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   if (vam->graph_nodes)
     {
@@ -2773,7 +2767,6 @@
   (vl_api_get_node_graph_reply_t * mp)
 {
   vat_main_t *vam = &vat_main;
-  api_main_t *am = vlibapi_get_main ();
   void *oldheap;
   vat_json_node_t node;
   u8 *reply;
@@ -2786,13 +2779,11 @@
   reply = uword_to_pointer (mp->reply_in_shmem, u8 *);
 
   /* Toss the shared-memory original... */
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
 
   vec_free (reply);
 
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   vat_json_print (vam->ofp, &node);
   vat_json_free (&node);
diff --git a/src/vlibapi/api_common.h b/src/vlibapi/api_common.h
index 4d40104..2105f1b 100644
--- a/src/vlibapi/api_common.h
+++ b/src/vlibapi/api_common.h
@@ -201,7 +201,9 @@
 u16 vl_msg_api_get_msg_ids (const char *name, int n);
 u32 vl_msg_api_get_msg_index (u8 * name_and_crc);
 void *vl_msg_push_heap (void);
+void *vl_msg_push_heap_w_region (svm_region_t * vlib_rp);
 void vl_msg_pop_heap (void *oldheap);
+void vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap);
 
 typedef clib_error_t *(vl_msg_api_init_function_t) (u32 client_index);
 
diff --git a/src/vlibapi/api_shared.c b/src/vlibapi/api_shared.c
index a553a5b..aba853d 100644
--- a/src/vlibapi/api_shared.c
+++ b/src/vlibapi/api_shared.c
@@ -1064,19 +1064,31 @@
 }
 
 void *
+vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
+{
+  pthread_mutex_lock (&vlib_rp->mutex);
+  return svm_push_data_heap (vlib_rp);
+}
+
+void *
 vl_msg_push_heap (void)
 {
   api_main_t *am = vlibapi_get_main ();
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  return svm_push_data_heap (am->vlib_rp);
+  return vl_msg_push_heap_w_region (am->vlib_rp);
+}
+
+void
+vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
+{
+  svm_pop_heap (oldheap);
+  pthread_mutex_unlock (&vlib_rp->mutex);
 }
 
 void
 vl_msg_pop_heap (void *oldheap)
 {
   api_main_t *am = vlibapi_get_main ();
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
 }
 
 int
diff --git a/src/vlibmemory/memory_api.c b/src/vlibmemory/memory_api.c
index 8c633e2..b5f3fae 100644
--- a/src/vlibmemory/memory_api.c
+++ b/src/vlibmemory/memory_api.c
@@ -121,31 +121,27 @@
 {
   vl_api_registration_t **regpp;
   vl_api_registration_t *regp;
-  svm_region_t *svm;
   void *oldheap;
   api_main_t *am = vlibapi_get_main ();
 
   ASSERT (vlib_get_thread_index () == 0);
   pool_get (am->vl_clients, regpp);
 
-  svm = am->vlib_rp;
 
-  pthread_mutex_lock (&svm->mutex);
-  oldheap = svm_push_data_heap (svm);
+  oldheap = vl_msg_push_heap ();
   *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
 
   regp = *regpp;
   clib_memset (regp, 0, sizeof (*regp));
   regp->registration_type = REGISTRATION_TYPE_SHMEM;
   regp->vl_api_registration_pool_index = regpp - am->vl_clients;
-  regp->vlib_rp = svm;
+  regp->vlib_rp = am->vlib_rp;
   regp->shmem_hdr = am->shmem_hdr;
 
   regp->vl_input_queue = q;
   regp->name = format (0, "%s%c", name, 0);
 
-  pthread_mutex_unlock (&svm->mutex);
-  svm_pop_heap (oldheap);
+  vl_msg_pop_heap (oldheap);
   return vl_msg_api_handle_from_index_and_epoch
     (regp->vl_api_registration_pool_index,
      am->shmem_hdr->application_restarts);
@@ -160,7 +156,6 @@
   vl_api_registration_t **regpp;
   vl_api_registration_t *regp;
   vl_api_memclnt_create_reply_t *rp;
-  svm_region_t *svm;
   svm_queue_t *q;
   int rv = 0;
   void *oldheap;
@@ -195,17 +190,14 @@
 
   pool_get (am->vl_clients, regpp);
 
-  svm = am->vlib_rp;
-
-  pthread_mutex_lock (&svm->mutex);
-  oldheap = svm_push_data_heap (svm);
+  oldheap = vl_msg_push_heap ();
   *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
 
   regp = *regpp;
   clib_memset (regp, 0, sizeof (*regp));
   regp->registration_type = REGISTRATION_TYPE_SHMEM;
   regp->vl_api_registration_pool_index = regpp - am->vl_clients;
-  regp->vlib_rp = svm;
+  regp->vlib_rp = am->vlib_rp;
   regp->shmem_hdr = am->shmem_hdr;
   regp->clib_file_index = am->shmem_hdr->clib_file_index;
 
@@ -224,8 +216,7 @@
   else
     msg_table = am->serialized_message_table_in_shmem;
 
-  pthread_mutex_unlock (&svm->mutex);
-  svm_pop_heap (oldheap);
+  vl_msg_pop_heap (oldheap);
 
   rp = vl_msg_api_alloc (sizeof (*rp));
   rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
@@ -266,7 +257,6 @@
   vl_api_registration_t **regpp;
   vl_api_registration_t *regp;
   vl_api_memclnt_delete_reply_t *rp;
-  svm_region_t *svm;
   void *oldheap;
   api_main_t *am = vlibapi_get_main ();
   u32 handle, client_index, epoch;
@@ -294,7 +284,6 @@
     {
       int i;
       regp = *regpp;
-      svm = am->vlib_rp;
       int private_registration = 0;
 
       /* Send reply unless client asked us to do the cleanup */
@@ -327,11 +316,11 @@
       for (i = 0; i < vec_len (am->vlib_private_rps); i++)
 	{
 	  /* Is this a pairwise / private API segment? */
-	  if (am->vlib_private_rps[i] == svm)
+	  if (am->vlib_private_rps[i] == am->vlib_rp)
 	    {
 	      /* Note: account for the memfd header page */
-	      uword virtual_base = svm->virtual_base - MMAP_PAGESIZE;
-	      uword virtual_size = svm->virtual_size + MMAP_PAGESIZE;
+	      uword virtual_base = am->vlib_rp->virtual_base - MMAP_PAGESIZE;
+	      uword virtual_size = am->vlib_rp->virtual_size + MMAP_PAGESIZE;
 
 	      /*
 	       * Kill the registration pool element before we make
@@ -355,16 +344,14 @@
 	{
 	  pool_put_index (am->vl_clients,
 			  regp->vl_api_registration_pool_index);
-	  pthread_mutex_lock (&svm->mutex);
-	  oldheap = svm_push_data_heap (svm);
+	  oldheap = vl_msg_push_heap ();
 	  if (mp->do_cleanup)
 	    svm_queue_free (regp->vl_input_queue);
 	  vec_free (regp->name);
 	  /* Poison the old registration */
 	  clib_memset (regp, 0xF1, sizeof (*regp));
 	  clib_mem_free (regp);
-	  pthread_mutex_unlock (&svm->mutex);
-	  svm_pop_heap (oldheap);
+	  vl_msg_pop_heap (oldheap);
 	  /*
 	   * These messages must be freed manually, since they're set up
 	   * as "bounce" messages. In the private_registration == 1 case,
@@ -617,7 +604,6 @@
   if (PREDICT_FALSE (vec_len (dead_indices) > 0))
     {
       int i;
-      svm_region_t *svm;
       void *oldheap;
 
       /* Allow the application to clean up its registrations */
@@ -634,9 +620,7 @@
 	    }
 	}
 
-      svm = am->vlib_rp;
-      pthread_mutex_lock (&svm->mutex);
-      oldheap = svm_push_data_heap (svm);
+      oldheap = vl_msg_push_heap ();
 
       for (i = 0; i < vec_len (dead_indices); i++)
 	{
@@ -644,7 +628,7 @@
 	  if (regpp)
 	    {
 	      /* Is this a pairwise SVM segment? */
-	      if ((*regpp)->vlib_rp != svm)
+	      if ((*regpp)->vlib_rp != am->vlib_rp)
 		{
 		  int i;
 		  svm_region_t *dead_rp = (*regpp)->vlib_rp;
@@ -661,7 +645,7 @@
 		      }
 		  svm_pop_heap (oldheap);
 		  clib_warning ("private rp %llx AWOL", dead_rp);
-		  oldheap = svm_push_data_heap (svm);
+		  oldheap = svm_push_data_heap (am->vlib_rp);
 
 		found:
 		  /* Kill it, accounting for the memfd header page */
@@ -670,7 +654,7 @@
 		    clib_unix_warning ("munmap");
 		  /* Reset the queue-length-address cache */
 		  vec_reset_length (vl_api_queue_cursizes);
-		  oldheap = svm_push_data_heap (svm);
+		  oldheap = svm_push_data_heap (am->vlib_rp);
 		}
 	      else
 		{
@@ -686,14 +670,13 @@
 	      svm_pop_heap (oldheap);
 	      clib_warning ("Duplicate free, client index %d",
 			    regpp - am->vl_clients);
-	      oldheap = svm_push_data_heap (svm);
+	      oldheap = svm_push_data_heap (am->vlib_rp);
 	    }
 	}
 
       svm_client_scan_this_region_nolock (am->vlib_rp);
 
-      pthread_mutex_unlock (&svm->mutex);
-      svm_pop_heap (oldheap);
+      vl_msg_pop_heap (oldheap);
       for (i = 0; i < vec_len (dead_indices); i++)
 	pool_put_index (am->vl_clients, dead_indices[i]);
     }
@@ -776,7 +759,6 @@
 			       u32 reg_index)
 {
   api_main_t *am = vlibapi_get_main ();
-
   return void_mem_api_handle_msg_i (am, am->vlib_private_rps[reg_index], vm,
 				    node, 1 /* is_private */ );
 }
diff --git a/src/vlibmemory/memory_client.c b/src/vlibmemory/memory_client.c
index b509063..bd89526 100644
--- a/src/vlibmemory/memory_client.c
+++ b/src/vlibmemory/memory_client.c
@@ -160,7 +160,6 @@
 int
 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
 {
-  svm_region_t *svm;
   vl_api_memclnt_create_t *mp;
   vl_api_memclnt_create_reply_t *rp;
   svm_queue_t *vl_input_queue;
@@ -181,7 +180,6 @@
       return -1;
     }
 
-  svm = am->vlib_rp;
   shmem_hdr = am->shmem_hdr;
 
   if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
@@ -193,12 +191,10 @@
   CLIB_MEM_UNPOISON (shmem_hdr, sizeof (*shmem_hdr));
   VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
 
-  pthread_mutex_lock (&svm->mutex);
-  oldheap = svm_push_data_heap (svm);
+  oldheap = vl_msg_push_heap ();
   vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
 					     getpid ());
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&svm->mutex);
+  vl_msg_pop_heap (oldheap);
 
   am->my_client_index = ~0;
   am->my_registration = 0;
@@ -256,11 +252,9 @@
   void *oldheap;
   api_main_t *am = vlibapi_get_main ();
 
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
   svm_queue_free (am->vl_input_queue);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
-  svm_pop_heap (oldheap);
+  vl_msg_pop_heap (oldheap);
 
   am->my_client_index = ~0;
   am->my_registration = 0;
diff --git a/src/vlibmemory/memory_shared.c b/src/vlibmemory/memory_shared.c
index 0b232ad..1716f27 100644
--- a/src/vlibmemory/memory_shared.c
+++ b/src/vlibmemory/memory_shared.c
@@ -163,15 +163,13 @@
    */
   am->ring_misses++;
 
-  pthread_mutex_lock (&vlib_rp->mutex);
-  oldheap = svm_push_data_heap (vlib_rp);
+  oldheap = vl_msg_push_heap_w_region (vlib_rp);
   if (may_return_null)
     {
       rv = clib_mem_alloc_or_null (nbytes);
       if (PREDICT_FALSE (rv == 0))
 	{
-	  svm_pop_heap (oldheap);
-	  pthread_mutex_unlock (&vlib_rp->mutex);
+	  vl_msg_pop_heap_w_region (vlib_rp, oldheap);
 	  return 0;
 	}
     }
@@ -180,8 +178,7 @@
 
   rv->q = 0;
   rv->gc_mark_timestamp = 0;
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&vlib_rp->mutex);
+  vl_msg_pop_heap_w_region (vlib_rp, oldheap);
 
 out:
 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
@@ -296,8 +293,7 @@
       return;
     }
 
-  pthread_mutex_lock (&vlib_rp->mutex);
-  oldheap = svm_push_data_heap (vlib_rp);
+  oldheap = vl_msg_push_heap_w_region (vlib_rp);
 
 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
   {
@@ -308,8 +304,7 @@
 #endif
 
   clib_mem_free (rv);
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&vlib_rp->mutex);
+  vl_msg_pop_heap_w_region (vlib_rp, oldheap);
 }
 
 void
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
index 3369106..0b66cf8 100644
--- a/src/vnet/session/session.c
+++ b/src/vnet/session/session.c
@@ -1333,7 +1333,6 @@
 {
   u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
   ssvm_private_t *eqs = &smm->evt_qs_segment;
-  api_main_t *am = vlibapi_get_main ();
   uword eqs_size = 64 << 20;
   pid_t vpp_pid = getpid ();
   void *oldheap;
@@ -1363,7 +1362,7 @@
   if (smm->evt_qs_use_memfd_seg)
     oldheap = ssvm_push_heap (eqs->sh);
   else
-    oldheap = svm_push_data_heap (am->vlib_rp);
+    oldheap = vl_msg_push_heap ();
 
   for (i = 0; i < vec_len (smm->wrk); i++)
     {
@@ -1388,7 +1387,7 @@
   if (smm->evt_qs_use_memfd_seg)
     ssvm_pop_heap (oldheap);
   else
-    svm_pop_heap (oldheap);
+    vl_msg_pop_heap (oldheap);
 }
 
 ssvm_private_t *
diff --git a/src/vpp/api/api.c b/src/vpp/api/api.c
index c62e7d0..70839b6 100644
--- a/src/vpp/api/api.c
+++ b/src/vpp/api/api.c
@@ -143,22 +143,19 @@
   u8 **shmem_vecp = (u8 **) arg;
   u8 *shmem_vec;
   void *oldheap;
-  api_main_t *am = vlibapi_get_main ();
   u32 offset;
 
   shmem_vec = *shmem_vecp;
 
   offset = vec_len (shmem_vec);
 
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
 
   vec_validate (shmem_vec, offset + buffer_bytes - 1);
 
   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
 
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   *shmem_vecp = shmem_vec;
 }
@@ -170,7 +167,6 @@
   vl_api_cli_reply_t *rp;
   vl_api_registration_t *reg;
   vlib_main_t *vm = vlib_get_main ();
-  api_main_t *am = vlibapi_get_main ();
   unformat_input_t input;
   u8 *shmem_vec = 0;
   void *oldheap;
@@ -187,13 +183,9 @@
 
   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
 
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
-
+  oldheap = vl_msg_push_heap ();
   vec_add1 (shmem_vec, 0);
-
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   rp->reply_in_shmem = (uword) shmem_vec;
 
@@ -439,15 +431,13 @@
 {
   int rv = 0;
   u8 *vector = 0;
-  api_main_t *am = vlibapi_get_main ();
   vlib_main_t *vm = vlib_get_main ();
   void *oldheap;
   vl_api_get_node_graph_reply_t *rmp;
   static vlib_node_t ***node_dups;
   static vlib_main_t **stat_vms;
 
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
 
   /*
    * Keep the number of memcpy ops to a minimum (e.g. 1).
@@ -462,8 +452,7 @@
   vector = vlib_node_serialize (vm, node_dups, vector, 1 /* include nexts */ ,
 				1 /* include stats */ );
 
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
+  vl_msg_pop_heap (oldheap);
 
   /* *INDENT-OFF* */
   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
diff --git a/src/vpp/api/api_main.c b/src/vpp/api/api_main.c
index 7fcbe70..1d16f78 100644
--- a/src/vpp/api/api_main.c
+++ b/src/vpp/api/api_main.c
@@ -76,7 +76,6 @@
 {
   vl_api_registration_t **regpp;
   vl_api_registration_t *regp;
-  svm_region_t *svm;
   void *oldheap;
   api_main_t *am = vlibapi_get_main ();
 
@@ -85,17 +84,15 @@
 
   pool_get (am->vl_clients, regpp);
 
-  svm = am->vlib_rp;
+  oldheap = vl_msg_push_heap ();
 
-  pthread_mutex_lock (&svm->mutex);
-  oldheap = svm_push_data_heap (svm);
   *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
 
   regp = *regpp;
   clib_memset (regp, 0, sizeof (*regp));
   regp->registration_type = REGISTRATION_TYPE_SHMEM;
   regp->vl_api_registration_pool_index = regpp - am->vl_clients;
-  regp->vlib_rp = svm;
+  regp->vlib_rp = am->vlib_rp;
   regp->shmem_hdr = am->shmem_hdr;
 
   /* Loopback connection */
@@ -104,8 +101,7 @@
   regp->name = format (0, "%s", "vpp-internal");
   vec_add1 (regp->name, 0);
 
-  pthread_mutex_unlock (&svm->mutex);
-  svm_pop_heap (oldheap);
+  vl_msg_pop_heap (oldheap);
 
   vam->my_client_index = vl_msg_api_handle_from_index_and_epoch
     (regp->vl_api_registration_pool_index,