Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Erlang NIF problem

By: a guest on Aug 5th, 2010  |  syntax: C  |  size: 3.06 KB  |  views: 320  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "erl_nif.h"
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5.    
  6. static int
  7. load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
  8. {
  9.   return 0;
  10. }
  11.  
  12. static int
  13. reload(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
  14. {
  15.   return 0;
  16. }
  17.  
  18. static int
  19. upgrade(ErlNifEnv* env, void** priv, void** old_priv,
  20.         ERL_NIF_TERM load_info)
  21. {
  22.   return 0;
  23. }
  24.  
  25. static void
  26. unload(ErlNifEnv* env, void* priv)
  27. {
  28.   return;
  29. }
  30.  
  31.  
  32. // Function to generate binary
  33. // Right now we are only constructing one binary and returns that.
  34. static ERL_NIF_TERM
  35. generate_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM *argv)
  36. {
  37.  
  38.   ErlNifPid to_pid;
  39.   ErlNifBinary bin;
  40.   int SIZE, i;
  41.  
  42.   //ErlNifEnv *env = enif_alloc_env();
  43.  
  44.  
  45.   enif_get_int(env, argv[0], &SIZE);
  46.   fprintf(stderr, "Value of argv[0]: %d\n", SIZE);
  47.  
  48.  
  49.   enif_get_local_pid(env, argv[1], &to_pid);
  50.  
  51.   // Allocate memory for the binary
  52.   enif_alloc_binary(SIZE, &bin);
  53.  
  54.  
  55.   srand( (unsigned int) time(NULL));
  56.  
  57.   fprintf(stderr, "SIZE = %d\n", SIZE);
  58.  
  59.   // This is just random data right now. We might want to generate something more useful
  60.   for(i = 0; i < SIZE; i++)
  61.     {
  62.       fprintf(stderr, "Iteration %d\n", i);
  63.       bin.data[i] = (i % 26) + 'a';
  64.     }
  65.  
  66.   // Create a enviroment
  67.   ErlNifEnv *tmp_env = enif_alloc_env();
  68.  
  69.   // Make a NIF_TERM out of our binary
  70.   ERL_NIF_TERM msg = enif_make_binary(tmp_env, &bin);
  71.  
  72.   // We send a message to the PID we have in the argumentlist
  73.   if(enif_send(NULL, &to_pid, tmp_env, msg))
  74.     {
  75.       fprintf(stderr, "Sent message");
  76.     } else {
  77.     fprintf(stderr, "Could not send message");
  78.   }
  79.  
  80.  
  81.   return enif_make_string(env, "Hello world", ERL_NIF_LATIN1);
  82.  
  83. }
  84.  
  85.  
  86. static ERL_NIF_TERM
  87. generate_buffer(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
  88. {
  89.   ErlNifTid thread;
  90.   ErlNifThreadOpts *thread_opts;
  91.  
  92.   thread_opts = enif_thread_opts_create("thread_opts");
  93.  
  94.  
  95.  
  96.  
  97.  
  98.   int size, buffers_per_batch;
  99.   ErlNifPid pid;
  100.  
  101.   // Extract the arguments
  102.   if (!enif_get_int(env, argv[0], &size)) {
  103.     fprintf(stderr, "Argument 1 not an int: %d\n", size);
  104.     return enif_make_badarg(env);
  105.   }
  106.  
  107.  
  108.   if (!enif_get_int(env, argv[2], &buffers_per_batch)) {
  109.     fprintf(stderr, "Argument 3 not an int: %d\n", buffers_per_batch);
  110.     return enif_make_badarg(env);
  111.   }
  112.  
  113.   if (!enif_get_local_pid(env, argv[1], &pid)) {
  114.     fprintf(stderr, "Argument 2 not a pid\n");
  115.     return enif_make_badarg(env);
  116.   }
  117.   ERL_NIF_TERM *arguments = (ERL_NIF_TERM *)malloc(sizeof(ERL_NIF_TERM)*5);
  118.  
  119.   arguments[0] = enif_make_int(env, size);
  120.   arguments[1] = enif_make_pid(env, &pid);
  121.   arguments[2] = enif_make_int(env, buffers_per_batch);
  122.  
  123.  
  124.   enif_thread_create("data_generation", &thread, (void *)generate_binary, &arguments, NULL);
  125.  
  126.  
  127.  
  128.   return enif_make_string(env, "Hello World",ERL_NIF_LATIN1);
  129. }
  130.  
  131. static ErlNifFunc mynif_funcs[] =
  132.   {
  133.     {"generate_binary", 3, generate_binary},
  134.     {"generate_buffer", 3, generate_buffer}
  135.   };
  136.  
  137.  
  138. ERL_NIF_INIT(ctr_in_fg_nif,mynif_funcs,NULL,NULL,NULL,NULL);