예제 #1
0
파일: heap_test.c 프로젝트: hit9/C-Snip
void case_heap_clear() {
    struct heap *heap = heap(heap_cmp);
    int a = 1;
    assert(heap_push(heap, (void *)&a) == HEAP_OK);
    assert(heap_len(heap) == 1);
    assert(heap_cap(heap) == 1);
    heap_clear(heap);
    assert(heap_len(heap) == 0);
    heap_free(heap);
}
예제 #2
0
파일: heap_test.c 프로젝트: hit9/C-Snip
void case_heap_len() {
    struct heap *heap = heap(heap_cmp);
    assert(heap_len(heap) == 0);
    int a = 1, b = 2, c = 3;
    assert(heap_push(heap, (void *)&a) == HEAP_OK);
    assert(heap_push(heap, (void *)&b) == HEAP_OK);
    assert(heap_push(heap, (void *)&c) == HEAP_OK);
    assert(heap_len(heap) == 3);
    heap_free(heap);
}
예제 #3
0
파일: heap_test.c 프로젝트: hit9/C-Snip
void case_heap_pop() {
    struct heap *heap = heap(heap_cmp);
    int a = 3, b = 1, c = 2, d = 4;
    assert(heap_push(heap, (void *)&a) == HEAP_OK);
    assert(heap_push(heap, (void *)&b) == HEAP_OK);
    assert(heap_push(heap, (void *)&c) == HEAP_OK);
    assert(heap_push(heap, (void *)&d) == HEAP_OK);
    assert(heap_len(heap) == 4);
    assert(*(int *)heap_pop(heap) == 1);
    assert(*(int *)heap_pop(heap) == 2);
    assert(*(int *)heap_pop(heap) == 3);
    assert(*(int *)heap_pop(heap) == 4);
    assert(heap_len(heap) == 0);
    heap_free(heap);
}
예제 #4
0
void main_loop(void)
{
        int n;
        struct timer *timer;
        int delay;

        while (num_pollfds > 1 || heap_len(timers) > 1
               || pollfds[0].events & POLLOUT) {
                if (heap_empty(timers)) {
                        timer = NULL;
                        delay = -1;
                } else {
                        timer = heap_peek(timers);
                        delay = (timer->time - get_now()) * 1000;
                }
                
                if (timer && delay <= 0)
                        n = 0;
                else
                        n = poll(pollfds, num_pollfds, delay);

                if (!n) {
                        timer = heap_extract_min(timers);
                        timer->func(timer->data);
                        free(timer);
                        goto cont;
                }

                for (int i = 0; i < num_pollfds && n; i++) {
                        if (pollfds[i].revents) {
                                event_handlers[i]->func(event_handlers[i]->data);
                                n--;

                                /* We may have just deleted this id.
                                 * Try it again in case it's a new
                                 * one. */
                                i--;
                        }
                }

        cont:
                maybe_dequeue();                
        }
}
예제 #5
0
파일: heap_test.c 프로젝트: hit9/C-Snip
void case_heap_del() {
    struct heap *heap = heap(heap_cmp);
    int a = 3, b = 2, c = 5, d = 7, e = 4, f = 6, g = 1, h = 4;
    assert(heap_push(heap, (void *)&a) == HEAP_OK);
    assert(heap_push(heap, (void *)&b) == HEAP_OK);
    assert(heap_push(heap, (void *)&c) == HEAP_OK);
    assert(heap_push(heap, (void *)&d) == HEAP_OK);
    assert(heap_push(heap, (void *)&e) == HEAP_OK);
    assert(heap_push(heap, (void *)&f) == HEAP_OK);
    assert(heap_push(heap, (void *)&g) == HEAP_OK);
    assert(4 == *(int *)heap_del(heap, 4)); /* this assert means nothing */
    assert(heap_len(heap) == 6);
    assert(1 == *(int *)heap_pop(heap));
    assert(2 == *(int *)heap_pop(heap));
    assert(3 == *(int *)heap_pop(heap));
    assert(heap_push(heap, (void *)&h) == HEAP_OK);
    assert(4 == *(int *)heap_pop(heap));
    assert(5 == *(int *)heap_pop(heap));
    assert(6 == *(int *)heap_pop(heap));
    assert(7 == *(int *)heap_pop(heap));
    heap_free(heap);
}
예제 #6
0
파일: test_heap.c 프로젝트: saumzy/clib
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;
}