summaryrefslogtreecommitdiffstats
path: root/src/vlibmemory/memory_api.c
diff options
context:
space:
mode:
authorDave Barach <dave@barachs.net>2019-11-27 11:42:13 -0500
committerFlorin Coras <florin.coras@gmail.com>2019-12-10 01:13:05 +0000
commit39d69112fcec114fde34955ceb41555221d3ba11 (patch)
tree156f21a0e66bf596b2c1e5184d82c80d99ea0a79 /src/vlibmemory/memory_api.c
parent6b3f25caff262b536a5449ac5461dd041c92a655 (diff)
api: multiple connections per process
Type: feature Signed-off-by: Dave Barach <dave@barachs.net> Change-Id: I2272521d6e69edcd385ef684af6dd4eea5eaa953
Diffstat (limited to 'src/vlibmemory/memory_api.c')
-rw-r--r--src/vlibmemory/memory_api.c34
1 files changed, 17 insertions, 17 deletions
diff --git a/src/vlibmemory/memory_api.c b/src/vlibmemory/memory_api.c
index b5c97e9acb7..8c633e20223 100644
--- a/src/vlibmemory/memory_api.c
+++ b/src/vlibmemory/memory_api.c
@@ -64,7 +64,7 @@ static void
memclnt_queue_callback (vlib_main_t * vm)
{
int i;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
1 + vec_len (am->vlib_private_rps)))
@@ -123,7 +123,7 @@ vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
vl_api_registration_t *regp;
svm_region_t *svm;
void *oldheap;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
ASSERT (vlib_get_thread_index () == 0);
pool_get (am->vl_clients, regpp);
@@ -164,7 +164,7 @@ vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
svm_queue_t *q;
int rv = 0;
void *oldheap;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
u8 *msg_table;
/*
@@ -246,7 +246,7 @@ vl_api_call_reaper_functions (u32 client_index)
clib_error_t *error = 0;
_vl_msg_api_function_list_elt_t *i;
- i = api_main.reaper_function_registrations;
+ i = vlibapi_get_main ()->reaper_function_registrations;
while (i)
{
error = i->f (client_index);
@@ -268,7 +268,7 @@ vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
vl_api_memclnt_delete_reply_t *rp;
svm_region_t *svm;
void *oldheap;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
u32 handle, client_index, epoch;
handle = mp->index;
@@ -411,7 +411,7 @@ vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
api_main_t *am;
vl_shmem_hdr_t *shmem_hdr;
- am = &api_main;
+ am = vlibapi_get_main ();
shmem_hdr = am->shmem_hdr;
rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
@@ -439,7 +439,7 @@ int
vl_mem_api_init (const char *region_name)
{
int rv;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
vl_msg_api_msg_config_t cfg;
vl_msg_api_msg_config_t *c = &cfg;
vl_shmem_hdr_t *shm;
@@ -488,7 +488,7 @@ vl_mem_api_init (const char *region_name)
clib_error_t *
map_api_segment_init (vlib_main_t * vm)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
int rv;
if ((rv = vl_mem_api_init (am->region_name)) < 0)
@@ -503,8 +503,8 @@ static void
send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
{
vl_api_memclnt_keepalive_t *mp;
- api_main_t *am = &api_main;
svm_queue_t *q;
+ api_main_t *am = vlibapi_get_main ();
q = regp->vl_input_queue;
@@ -722,7 +722,7 @@ void_mem_api_handle_msg_i (api_main_t * am, svm_region_t * vlib_rp,
int
vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
return void_mem_api_handle_msg_i (am, am->vlib_rp, vm, node,
0 /* is_private */ );
}
@@ -730,7 +730,7 @@ vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
int
vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
int i;
uword *tmp, mp;
@@ -775,7 +775,7 @@ int
vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
u32 reg_index)
{
- api_main_t *am = &api_main;
+ 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 */ );
@@ -786,7 +786,7 @@ vl_mem_api_client_index_to_registration (u32 handle)
{
vl_api_registration_t **regpp;
vl_api_registration_t *regp;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
vl_shmem_hdr_t *shmem_hdr;
u32 index;
@@ -814,7 +814,7 @@ svm_queue_t *
vl_api_client_index_to_input_queue (u32 index)
{
vl_api_registration_t *regp;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
/* Special case: vlib trying to send itself a message */
if (index == (u32) ~ 0)
@@ -884,7 +884,7 @@ vl_api_ring_command (vlib_main_t * vm,
{
int i;
vl_shmem_hdr_t *shmem_hdr;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
/* First, dump the primary region rings.. */
@@ -950,7 +950,7 @@ VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
clib_error_t *
vlibmemory_init (vlib_main_t * vm)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
svm_map_region_args_t _a, *a = &_a;
u8 *remove_path1, *remove_path2;
void vlibsocket_reference (void);
@@ -999,7 +999,7 @@ vlibmemory_init (vlib_main_t * vm)
void
vl_set_memory_region_name (const char *name)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
am->region_name = name;
}
or: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vppinfra/valloc.h>

/** @file
    @brief Simple first-fit virtual space allocator
*/

/** Add a chunk of memory to a virtual allocation arena
    @param vam - clib_valloc_main_t * pointer to the allocation arena
    @param template - clib_valloc_chunk_t * pointer to a template chunk which
    describes the virtual address range to add

    @note only the baseva and size member of the template chunk are significant
    It's perfectly OK for the new chunk to be discontinuous with previous
    chunks, the chunk fusion algorithm won't merge them.
 */

void
clib_valloc_add_chunk (clib_valloc_main_t * vam,
		       clib_valloc_chunk_t * template)
{
  clib_valloc_chunk_t *ch, *new_ch;
  u32 index;

  ASSERT (vam->flags & CLIB_VALLOC_INITIALIZED);

  clib_spinlock_lock_if_init (&vam->lock);

  /* Add at the beginning, or at the end... */
  index = vam->first_index;

  /*
   * Make sure we're not trying to add an overlapping chunk..
   * It's worth checking, because someone will eventually do that.
   */
  if (CLIB_DEBUG > 0 && index != ~0)
    {
      while (index != ~0)
	{
	  ch = pool_elt_at_index (vam->chunks, index);
	  ASSERT (template->baseva < ch->baseva || template->baseva >=
		  (ch->baseva + ch->size));
	  ASSERT (template->baseva + template->size < ch->baseva ||
		  template->baseva + template->size >=
		  (ch->baseva + ch->size));
	  index = ch->next;
	}
      index = vam->first_index;
    }

  if (index != ~0)
    ch = pool_elt_at_index (vam->chunks, index);

  if (index == ~0 || template->baseva < ch->baseva)
    {
      pool_get (vam->chunks, new_ch);
      clib_memset (new_ch, 0, sizeof (*new_ch));

      if (index != ~0)
	{
	  ch = pool_elt_at_index (vam->chunks, index);

	  new_ch->next = index;
	  new_ch->prev = ~0;
	  ch->prev = new_ch - vam->chunks;
	}
      else
	{
	  new_ch->next = new_ch->prev = ~0;
	}

      new_ch->baseva = template->baseva;
      new_ch->size = template->size;

      vam->first_index = new_ch - vam->chunks;

      hash_set (vam->chunk_index_by_baseva, new_ch->baseva, vam->first_index);
    }
  else
    {
      /* Walk to the end of the chunk chain */
      while (index != ~0)
	{
	  ch = pool_elt_at_index (vam->chunks, index);
	  index = ch->next;
	}
      /* we want the last chunk index */
      index = ch - vam->chunks;

      pool_get (vam->chunks, new_ch);
      clib_memset (new_ch, 0, sizeof (*new_ch));

      ch = pool_elt_at_index (vam->chunks, index);

      new_ch->next = ~0;
      new_ch->prev = index;
      ch->next = new_ch - vam->chunks;

      new_ch->baseva = template->baseva;
      new_ch->size = template->size;

      hash_set (vam->chunk_index_by_baseva, new_ch->baseva,
		new_ch - vam->chunks);
    }

  clib_spinlock_unlock_if_init (&vam->lock);
}

/** Initialize a virtual memory allocation arena
    @param vam - clib_valloc_main_t * pointer to the arena to initialize
    @param template - clib_valloc_chunk_t * pointer to a template chunk which
    describes the initial virtual address range
*/
__clib_export void
clib_valloc_init (clib_valloc_main_t * vam, clib_valloc_chunk_t * template,
		  int need_lock)
{
  ASSERT (template && template->baseva && template->size);
  clib_memset (vam, 0, sizeof (*vam));
  if (need_lock)
    clib_spinlock_init (&vam->lock);

  vam->chunk_index_by_baseva = hash_create (0, sizeof (uword));
  vam->first_index = ~0;
  vam->flags |= CLIB_VALLOC_INITIALIZED;

  clib_valloc_add_chunk (vam, template);
}

/** Allocate virtual space
    @param vam - clib_valloc_main_t * pointer to the allocation arena
    @param size - u64 number of bytes to allocate
    @os_out_of_memory_on_failure - 1=> panic on allocation failure
    @return uword allocated space, 0=> failure
*/
__clib_export uword
clib_valloc_alloc (clib_valloc_main_t * vam, uword size,
		   int os_out_of_memory_on_failure)
{
  clib_valloc_chunk_t *ch, *new_ch, *next_ch;
  u32 index;

  clib_spinlock_lock_if_init (&vam->lock);

  index = vam->first_index;

  while (index != ~0)
    {
      ch = pool_elt_at_index (vam->chunks, index);
      /* If the chunk is free... */
      if ((ch->flags & CLIB_VALLOC_BUSY) == 0)
	{
	  /* Too small? */
	  if (ch->size < size)
	    goto next_chunk;
	  /* Exact match? */
	  if (ch->size == size)
	    {
	      ch->flags |= CLIB_VALLOC_BUSY;
	      clib_spinlock_unlock_if_init (&vam->lock);
	      return ch->baseva;
	    }
	  /*
	   * The current free chunk is larger than necessary, split the block.
	   */
	  pool_get (vam->chunks, new_ch);
	  /* ch might have just moved */
	  ch = pool_elt_at_index (vam->chunks, index);
	  clib_memset (new_ch, 0, sizeof (*new_ch));
	  new_ch->next = new_ch->prev = ~0;
	  new_ch->baseva = ch->baseva + size;
	  new_ch->size = ch->size - size;
	  ch->size = size;

	  /* Insert into doubly-linked list */
	  new_ch->next = ch->next;
	  new_ch->prev = ch - vam->chunks;

	  if (ch->next != ~0)
	    {
	      next_ch = pool_elt_at_index (vam->chunks, ch->next);
	      next_ch->prev = new_ch - vam->chunks;
	    }
	  ch->next = new_ch - vam->chunks;

	  hash_set (vam->chunk_index_by_baseva, new_ch->baseva,
		    new_ch - vam->chunks);

	  ch->flags |= CLIB_VALLOC_BUSY;
	  clib_spinlock_unlock_if_init (&vam->lock);
	  return ch->baseva;
	}

    next_chunk:
      index = ch->next;
    }
  clib_spinlock_unlock_if_init (&vam->lock);

  if (os_out_of_memory_on_failure)
    os_out_of_memory ();

  return 0;
}


/** Free virtual space
    @param vam - clib_valloc_main_t * pointer to the allocation arena
    @param baseva - uword base virtual address of the returned space
    @return uword - size of the freed virtual chunk
    @note the size is returned since we know it / in case the caller
    doesn't memorize chunk sizes
*/
__clib_export uword
clib_valloc_free (clib_valloc_main_t * vam, uword baseva)
{
  clib_valloc_chunk_t *ch, *prev_ch, *next_ch, *n2_ch;
  u32 index;
  uword return_size = 0;
  uword *p;

  clib_spinlock_lock_if_init (&vam->lock);

  p = hash_get (vam->chunk_index_by_baseva, baseva);

  /* Check even in production images */
  if (p == 0)
    os_panic ();

  index = p[0];

  ch = pool_elt_at_index (vam->chunks, index);

  return_size = ch->size;

  ASSERT (ch->baseva == baseva);
  ASSERT ((ch->flags & CLIB_VALLOC_BUSY) != 0);

  ch->flags &= ~CLIB_VALLOC_BUSY;

  /* combine with previous entry? */
  if (ch->prev != ~0)
    {
      prev_ch = pool_elt_at_index (vam->chunks, ch->prev);
      /*
       * Previous item must be free as well, and
       * tangent to this block.
       */
      if ((prev_ch->flags & CLIB_VALLOC_BUSY) == 0
	  && ((prev_ch->baseva + prev_ch->size) == ch->baseva))
	{
	  hash_unset (vam->chunk_index_by_baseva, baseva);
	  prev_ch->size += ch->size;
	  prev_ch->next = ch->next;
	  if (ch->next != ~0)
	    {
	      next_ch = pool_elt_at_index (vam->chunks, ch->next);
	      next_ch->prev = ch->prev;
	    }
	  ASSERT (ch - vam->chunks != vam->first_index);
	  clib_memset (ch, 0xfe, sizeof (*ch));
	  pool_put (vam->chunks, ch);
	  /* See about combining with next elt */
	  ch = prev_ch;
	}
    }

  /* Combine with next entry? */
  if (ch->next != ~0)
    {
      next_ch = pool_elt_at_index (vam->chunks, ch->next);

      if ((next_ch->flags & CLIB_VALLOC_BUSY) == 0
	  && ((ch->baseva + ch->size) == next_ch->baseva))
	{
	  hash_unset (vam->chunk_index_by_baseva, next_ch->baseva);
	  ch->size += next_ch->size;
	  ch->next = next_ch->next;
	  if (ch->next != ~0)
	    {
	      n2_ch = pool_elt_at_index (vam->chunks, next_ch->next);
	      n2_ch->prev = ch - vam->chunks;
	    }
	  ASSERT (next_ch - vam->chunks != vam->first_index);
	  clib_memset (next_ch, 0xfe, sizeof (*ch));
	  pool_put (vam->chunks, next_ch);
	}
    }

  clib_spinlock_unlock_if_init (&vam->lock);
  return return_size;
}

/** format a virtual allocation arena (varargs)
    @param vam - clib_valloc_main_t pointer to the arena to format
    @param verbose - int - verbosity level
    @return u8 vector
*/
u8 *
format_valloc (u8 * s, va_list * va)
{
  clib_valloc_main_t *vam = va_arg (*va, clib_valloc_main_t *);
  int verbose = va_arg (*va, int);
  clib_valloc_chunk_t *ch;
  u32 index;
  uword *p;

  clib_spinlock_lock_if_init (&vam->lock);

  s = format (s, "%d chunks, first index %d\n",
	      pool_elts (vam->chunks), vam->first_index);

  if (verbose)
    {
      index = vam->first_index;
      while (index != ~0)
	{
	  ch = pool_elt_at_index (vam->chunks, index);

	  s = format (s, "[%d] base %llx size %llx (%lld) prev %d %s\n",
		      index, ch->baseva, ch->size, ch->size, ch->prev,
		      (ch->flags & CLIB_VALLOC_BUSY) ? "busy" : "free");

	  p = hash_get (vam->chunk_index_by_baseva, ch->baseva);
	  if (p == 0)
	    {
	      s = format (s, "   BUG: baseva not in hash table!\n");
	    }
	  else if (p[0] != index)
	    {
	      s = format (s, "   BUG: baseva in hash table %d not %d!\n",
			  p[0], index);
	    }
	  index = ch->next;
	}
    }

  clib_spinlock_unlock_if_init (&vam->lock);

  return s;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */