+u32
+vl_msg_api_get_msg_index (u8 * name_and_crc)
+{
+ api_main_t *am = vlibapi_get_main ();
+ uword *p;
+
+ if (am->msg_index_by_name_and_crc)
+ {
+ p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
+ if (p)
+ return p[0];
+ }
+ return ~0;
+}
+
+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 ();
+ 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 ();
+ vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
+}
+
+/* Must be nul terminated */
+int
+vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
+{
+ /* copy without nul terminator */
+ u32 len = strlen (buf);
+ if (len > 0)
+ clib_memcpy_fast (str->buf, buf, len);
+ str->length = htonl (len);
+ return len + sizeof (u32);
+}
+
+/* Must NOT be nul terminated */
+int
+vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
+{
+ u32 len = vec_len (vec);
+ clib_memcpy (str->buf, vec, len);
+ str->length = htonl (len);
+ return len + sizeof (u32);
+}
+
+u32
+vl_api_string_len (vl_api_string_t * astr)
+{
+ return clib_net_to_host_u32 (astr->length);
+}
+
+u8 *
+vl_api_format_string (u8 * s, va_list * args)
+{
+ vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
+ vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
+ return s;
+}
+
+/*
+ * Returns a new vector. Remember to free it after use.
+ * NOT nul terminated.
+ */
+u8 *
+vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
+{
+ u8 *v = 0;
+
+ if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
+ return format (0, "insane astr->length %u%c",
+ clib_net_to_host_u32 (astr->length), 0);
+ vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
+ return v;
+}
+
+/*
+ * Returns a new vector. Remember to free it after use.
+ * Nul terminated.
+ */
+char *
+vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
+{
+ char *v = 0;
+ if (clib_net_to_host_u32 (astr->length) > 0)
+ {
+ vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
+ vec_add1 (v, 0);
+ }
+ return v;
+}
+
+void
+vl_api_set_elog_main (elog_main_t * m)
+{
+ api_main_t *am = vlibapi_get_main ();
+ am->elog_main = m;
+}
+
+int
+vl_api_set_elog_trace_api_messages (int enable)
+{
+ int rv;
+ api_main_t *am = vlibapi_get_main ();
+
+ rv = am->elog_trace_api_messages;
+ am->elog_trace_api_messages = enable;
+ return rv;
+}
+
+int
+vl_api_get_elog_trace_api_messages (void)
+{
+ api_main_t *am = vlibapi_get_main ();
+
+ return am->elog_trace_api_messages;
+}
+