Esempio n. 1
0
void object_release(Object *x) {
	x->ref_count --;

	if (x->ref_count == 0) {
		if (object_get_owns(x)) {
			heap_dealloc(x->content_heap, x->content);
		}
	}

	// debug purpose
	x->is_a = NULL;
	x->content = NULL;

	heap_dealloc(x->object_heap, x);
}
Esempio n. 2
0
File: error.c Progetto: Venkattk/vpp
/* Reserves given number of error codes for given node. */
void
vlib_register_errors (vlib_main_t * vm,
		      u32 node_index, u32 n_errors, char *error_strings[])
{
  vlib_error_main_t *em = &vm->error_main;
  vlib_node_t *n = vlib_get_node (vm, node_index);
  uword l;

  ASSERT (os_get_cpu_number () == 0);

  /* Free up any previous error strings. */
  if (n->n_errors > 0)
    heap_dealloc (em->error_strings_heap, n->error_heap_handle);

  n->n_errors = n_errors;
  n->error_strings = error_strings;

  if (n_errors == 0)
    return;

  n->error_heap_index =
    heap_alloc (em->error_strings_heap, n_errors, n->error_heap_handle);

  l = vec_len (em->error_strings_heap);

  clib_memcpy (vec_elt_at_index (em->error_strings_heap, n->error_heap_index),
	       error_strings, n_errors * sizeof (error_strings[0]));

  /* Allocate a counter/elog type for each error. */
  vec_validate (em->counters, l - 1);
  vec_validate (vm->error_elog_event_types, l - 1);

  /* Zero counters for re-registrations of errors. */
  if (n->error_heap_index + n_errors <= vec_len (em->counters_last_clear))
    clib_memcpy (em->counters + n->error_heap_index,
		 em->counters_last_clear + n->error_heap_index,
		 n_errors * sizeof (em->counters[0]));
  else
    memset (em->counters + n->error_heap_index,
	    0, n_errors * sizeof (em->counters[0]));

  {
    elog_event_type_t t;
    uword i;

    memset (&t, 0, sizeof (t));
    for (i = 0; i < n_errors; i++)
      {
	t.format = (char *) format (0, "%v %s: %%d",
				    n->name, error_strings[i]);
	vm->error_elog_event_types[n->error_heap_index + i] = t;
      }
  }
}
Esempio n. 3
0
static void ip_del_adjacency2 (ip_lookup_main_t * lm, u32 adj_index, u32 delete_multipath_adjacency)
{
  ip_adjacency_t * adj;
  uword handle;

  ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 1);

  adj = ip_get_adjacency (lm, adj_index);
  handle = adj->heap_handle;

  if (delete_multipath_adjacency)
    ip_multipath_del_adjacency (lm, adj_index);

  ip_poison_adjacencies (adj, adj->n_adj);

  heap_dealloc (lm->adjacency_heap, handle);
}
Esempio n. 4
0
void knnsearch(TREENODE *subroot, VECTOR *vp, int k, NUMBER radius) {
  heap *hp;
  assert(subroot != NULL);
  assert(vp->len != 0);

  hp = heap_alloc(k, vp->len);

  assert(hp != NULL);
  rknnsearch(subroot, vp, hp, radius, 0);

  printf("=========================\n\n");
  printf("k nearest neighbor search: \n");
  printf("target vector: ");
  vecprint(vp);
  heap_print(hp);
  /* Should print heap first */
  heap_dealloc(hp);
}
Esempio n. 5
0
int main (int argc, char * argv[])
{
  word i, j, k, n, seed, check_mask;
  u32 * h = 0;
  uword * objects = 0;
  uword * handles = 0;
  uword objects_used;
  uword align, fixed_size;

  n = 10;
  seed = getpid ();
  check_mask = 0;
  fixed_size = 0;

  if (argc > 1)
    {
      n = atoi (argv[1]);
      verbose = 1;
    }
  if (argc > 2)
    {
      word i = atoi (argv[2]);
      if (i)
	seed = i;
    }
  if (argc > 3)
    check_mask = atoi (argv[3]);

  align = 0;
  if (argc > 4)
    align = 1 << atoi (argv[4]);

  if_verbose   ("testing %wd iterations seed %wd\n", n, seed);

  srandom (seed);

  if (verbose) fformat (stderr, "%U\n", format_clib_mem_usage, /* verbose */ 0);

  vec_resize (objects, 1000);
  memset (objects, ~0, vec_bytes (objects));
  vec_resize (handles, vec_len (objects));

  objects_used = 0;

  if (fixed_size)
    {
      uword max_len = 1024 * 1024;
      void * memory = clib_mem_alloc (max_len * sizeof (h[0]));
      h = heap_create_from_memory (memory, max_len, sizeof (h[0]));
    }

  for (i = 0; i < n; i++)
    {
      while (1)
	{
	  j = random () % vec_len (objects);
	  if (objects[j] != ~0 || i + objects_used < n)
	    break;
	}

      if (objects[j] != ~0)
	{
	  heap_dealloc (h, handles[j]);
	  objects_used--;
	  objects[j] = ~0;
	}
      else
	{
	  u32 * data;
	  uword size;

	  size = 1 + (random () % 100);
	  objects[j] = heap_alloc_aligned (h, size, align, handles[j]);
	  objects_used++;

	  if (align)
	    ASSERT (0 == (objects[j] & (align - 1)));
	  ASSERT (objects[j] < vec_len (h));
	  ASSERT (size <= heap_len (h, handles[j]));

	  /* Set newly allocated object with test data. */
	  if (check_mask & 2)
	    {
	      data = h + objects[j];

	      for (k = 0; k < size; k++)
		data[k] = objects[j] + k;
	    }
	}

      if (check_mask & 1)
	heap_validate (h);

      if (check_mask & 4)
	{
	  /* Duplicate heap at each iteration. */
	  u32 * h1 = heap_dup (h);
	  heap_free (h);
	  h = h1;
	}

      /* Verify that all used objects have correct test data. */
      if (check_mask & 2)
	{
	  for (j = 0; j < vec_len (objects); j++)
	    if (objects[j] != ~0)
	      {
		u32 * data = h + objects[j];
		for (k = 0; k < heap_len (h, handles[j]); k++)
		  ASSERT(data[k] == objects[j] + k);
	      }
	}
    }

  if (verbose) fformat (stderr, "%U\n", format_heap, h, 1);

  {
    u32 * h1 = heap_dup (h);
    if (verbose) fformat (stderr, "%U\n", format_heap, h1, 1);
    heap_free (h1);
  }

  heap_free (h);
  if (verbose) fformat (stderr, "%U\n", format_heap, h, 1);
  ASSERT (objects_used == 0);

  vec_free (objects);
  vec_free (handles);

  if (fixed_size)
    vec_free_h (h, sizeof (heap_header_t));

  if (verbose) fformat (stderr, "%U\n", format_clib_mem_usage, /* verbose */ 0);

  return 0;
}