/* freelist.c
 *
 * Copyright (c) 2001
 * Hansjörg Malthaner
 *
 * Permission to use, copy, modify and distribute this software
 * for any purpose is hereby granted without fee, provided that 
 * the above copyright notice appears in all copies and that both
 * that copyright notice and this permission notice appear in 
 * supporting documentation.  
 *
 * I make no representations about the suitability of this software
 * for any purpose.  
 * It is provided "as is" without express or implied warranty.
 */

#include <time.h>
#include <stdlib.h>

#define LOOPS 10000000

struct node {
  struct node *next;
} node;


static struct node * list = 0;

struct node * gimme_node() {
  if(list) {
    struct node *p = list;
    list = list->next;
    return p;
  } else {
    return malloc(sizeof(struct node));
  }
};


void putback_node(struct node *nd) {
  nd->next = list;
  list = nd;
};


int main() {

  clock_t t0, t1, te;
  int i;

  t0 = clock();

  i=0;
  do {
    i++;
  } while(i<LOOPS);


  t1 = clock();

  te = t1-t0;

  printf("Empty test loop took %f seconds\n", ((double)te)/CLOCKS_PER_SEC);



  t0 = clock();

  i=0;
  do {
    struct node *n = malloc(sizeof(struct node));
    free(n);
    i++;
  } while(i<LOOPS);


  t1 = clock();

  printf("Calls took %f seconds\n", ((double)(t1-t0)-te)/CLOCKS_PER_SEC);


  t0 = clock();

  i=0;
  do {
    struct node *n = gimme_node();
    putback_node(n);

    i++;
  } while(i<LOOPS);


  t1 = clock();

  printf("Calls took %f seconds\n", ((double)(t1-t0)-te)/CLOCKS_PER_SEC);
}
