#include <rtl_malloc.h>
#include <rtl.h>
#include <pthread.h>


MODULE_AUTHOR("Miguel Masmano Tello <mmasmano@disca.upv.es>");
MODULE_DESCRIPTION("Example of use DIDMA");

MODULE_LICENSE("GPL");

pthread_t thread;

void * start_routine(void *arg)
{
  char *string;
  char hello_world [] = "Hello world";
  rtl_printf("Calling malloc... ");

  // DIDMA malloc
  string = (char *) malloc (sizeof (char) * (strlen (hello_world) + 1));
  
  if ( string == NULL) {
    rtl_printf("WRONG\n");
    return (void *) 0;
  }
  rtl_printf("OK\n");
  strcpy (string, hello_world);
  rtl_printf ("HELLO_WORLD: %s\n", hello_world);
  rtl_printf ("HELLO_WORLD copy: %s\n", string);
  rtl_printf("Calling free... ");
  
  // DIDMA free
  free (string);

  rtl_printf("DONE\n");
  return (void *)0;
}

int init_module(void){
  return pthread_create (&thread, NULL, start_routine, 0);
}

void cleanup_module(void){
  pthread_delete_np (thread);
}
