Exemplo n.º 1
0
ERL_NIF_TERM
encode_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    jiffy_st* st = (jiffy_st*) enif_priv_data(env);
    Encoder* e;

    ERL_NIF_TERM opts;
    ERL_NIF_TERM val;
    ERL_NIF_TERM tmp_argv[3];

    if(argc != 2) {
        return enif_make_badarg(env);
    }

    e = enc_new(env);
    if(e == NULL) {
        return make_error(st, env, "internal_error");
    }

    tmp_argv[0] = enif_make_resource(env, e);
    tmp_argv[1] = enif_make_list(env, 1, argv[0]);
    tmp_argv[2] = enif_make_list(env, 0);

    enif_release_resource(e);

    opts = argv[1];
    if(!enif_is_list(env, opts)) {
        return enif_make_badarg(env);
    }

    while(enif_get_list_cell(env, opts, &val, &opts)) {
        if(enif_compare(val, e->atoms->atom_uescape) == 0) {
            e->uescape = 1;
        } else if(enif_compare(val, e->atoms->atom_pretty) == 0) {
            e->pretty = 1;
        } else if(enif_compare(val, e->atoms->atom_use_nil) == 0) {
            e->use_nil = 1;
        } else if(enif_compare(val, e->atoms->atom_force_utf8) == 0) {
            // Ignore, handled in Erlang
        } else if(get_bytes_per_iter(env, val, &(e->bytes_per_iter))) {
            continue;
        } else {
            return enif_make_badarg(env);
        }
    }

    return encode_iter(env, 3, tmp_argv);
}
Exemplo n.º 2
0
ERL_NIF_TERM nif_thread_cast(ErlNifEnv* env, void (*f)(nif_thread_arg*), int a, ...)
{
	va_list ap;
	int i;

	nif_thread_arg* args = (nif_thread_arg*)enif_alloc(a * sizeof(nif_thread_arg));

	va_start(ap, a);
	for (i = 0; i < a; i++)
		args[i] = va_arg(ap, void*);
	va_end(ap);

	nif_thread_message* msg = nif_thread_message_alloc(f, args, NULL);

	return nif_thread_send((nif_thread_state*)enif_priv_data(env), msg);
}
Exemplo n.º 3
0
static ERL_NIF_TERM
nif_scheduler_join(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    state_ptr state = (state_ptr) enif_priv_data(env);

    if(state->initilised == 0 ) 
    {
        return enif_make_tuple2(env, 
            enif_make_atom(env, "state_error"), 
            enif_make_atom(env, "scheduler_not_inited"));
    }
    
    SchedulerDriverStatus status = scheduler_join( state->scheduler_state );

    return get_return_value_from_status(env, status);
}
Exemplo n.º 4
0
static ERL_NIF_TERM
nif_scheduler_reconcileTasks(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    unsigned int length ;

    state_ptr state = (state_ptr) enif_priv_data(env);
    
    if(state->initilised == 0 ) 
    {
        return enif_make_tuple2(env, 
            enif_make_atom(env, "state_error"), 
            enif_make_atom(env, "scheduler_not_inited"));
    }

    if(!enif_is_list(env, argv[0])) 
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "task_status_array"));
    };

    if(!enif_get_list_length(env, argv[0], &length))
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "task_status_array"));

    }
    
    ErlNifBinary binary_arr[length];
    if(!inspect_array_of_binary_objects(env, argv[0], &binary_arr ))
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "task_status_array"));
    }   

    BinaryNifArray binaryNifArrayHolder ;
    binaryNifArrayHolder.length = length;
    binaryNifArrayHolder.obj = &binary_arr[0];

    SchedulerDriverStatus status =  scheduler_reconcileTasks( state->scheduler_state, &binaryNifArrayHolder);
    return get_return_value_from_status(env, status);
}
Exemplo n.º 5
0
/**
 * Stop
 */
static ERL_NIF_TERM leo_mcerl_nif_stop(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  lcu_cache* cache;
  ErlNifResourceType* pert;

  if (argc < 1) {
    return enif_make_badarg(env);
  }

  pert = (ErlNifResourceType*)enif_priv_data(env);

  if (!enif_get_resource(env, argv[0], pert, (void**)&cache)) {
    return enif_make_badarg(env);
  }

  lcu_cache_destroy(cache);
  return atom_ok;
}
Exemplo n.º 6
0
ERL_NIF_TERM nif_thread_call(ErlNifEnv* env, ERL_NIF_TERM (*f)(ErlNifEnv*, nif_thread_arg*), int a, ...)
{
	va_list ap;
	int i;

	nif_thread_arg* args = (nif_thread_arg*)enif_alloc(a * sizeof(nif_thread_arg));

	va_start(ap, a);
	for (i = 0; i < a; i++)
		args[i] = va_arg(ap, void*);
	va_end(ap);

	ErlNifPid* pid = (ErlNifPid*)enif_alloc(sizeof(ErlNifPid));
	nif_thread_message* msg = nif_thread_message_alloc((void*)f, args, enif_self(env, pid));

	return nif_thread_send((nif_thread_state*)enif_priv_data(env), msg);
}
Exemplo n.º 7
0
static ERL_NIF_TERM mutate_solution(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]){

    // struct timespec start, end;
    // long diff;
    // clock_gettime(CLOCK_MONOTONIC, &start);

    ERL_NIF_TERM term;
    ErlNifResourceType* sol_type;
    Solution *sol, *mut_sol;
    // unsigned int i;
    double range, rate;

    CHECK(env, argc == 3)

    sol_type = (ErlNifResourceType*) enif_priv_data(env);
    CHECK(env, sol_type)

    CHECK(env, enif_get_resource(env, argv[0], sol_type, (void**) &sol))
    CHECK(env, enif_get_double(env, argv[1], &range))
    CHECK(env, enif_get_double(env, argv[2], &rate))

    mut_sol = (Solution*) enif_alloc_resource(sol_type, sizeof(Solution));
    CHECK(env, mut_sol)
    
    term = enif_make_resource(env, mut_sol);
    CHECK(env,term)
    enif_release_resource(mut_sol);

    mut_sol->len = sol->len;
    mut_sol->genotype = (double*) malloc(sizeof(double)*sol->len);

    mutate(sol, mut_sol, range, rate);

    #ifdef DEBUG
    print_solution(sol, "Genotype");
    print_solution(mut_sol, "MutatedGenotype");
    #endif

    // clock_gettime(CLOCK_MONOTONIC, &end);
    // diff = CLOCKS_PER_SEC * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
    // printf("mut=%llu\n", (long long unsigned int) diff);

    return term;

}
Exemplo n.º 8
0
/**
 * Retrieve total of objects
 */
static ERL_NIF_TERM leo_mcerl_nif_items(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  lcu_cache* cache;
  ErlNifResourceType* pert;
  ErlNifUInt64 len;

  if (argc < 1) {
    return enif_make_badarg(env);
  }

  pert = (ErlNifResourceType*)enif_priv_data(env);

  if (!enif_get_resource(env, argv[0], pert, (void**)&cache)) {
    return enif_make_badarg(env);
  }

  len = (ErlNifUInt64)lcu_cache_item_size(cache);
  return enif_make_tuple2(env, atom_ok, enif_make_uint64(env, len));
}
Exemplo n.º 9
0
    static ERL_NIF_TERM
get(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    PRIVDATA *data = NULL;
    int k = 0;
    int nelem = 0;

    data = (PRIVDATA *)enif_priv_data(env);
    nelem = NELEM(data);

    if (!enif_get_int(env, argv[0], &k))
        return enif_make_badarg(env);

    if ( (k < 0) || (k >= nelem))
        return error_tuple(env, "out_of_bounds");

    return enif_make_int(env, VAL(data, k));
}
Exemplo n.º 10
0
static
ERL_NIF_TERM exmagick_crop (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  Image* cropped_image;
  RectangleInfo rect;
  exm_resource_t *resource;

  EXM_INIT;
  ErlNifResourceType *type = (ErlNifResourceType *) enif_priv_data(env);

  if (0 == enif_get_resource(env, argv[0], type, (void **) &resource))
  { EXM_FAIL(ehandler, "invalid handle"); }

  if (resource->image == NULL)
  { EXM_FAIL(ehandler, "image not loaded"); }

  /* build rectangle */
  if (0 == enif_get_long(env, argv[1], &rect.x))
  { EXM_FAIL(ehandler, "x0: bad argument"); }

  if (0 == enif_get_long(env, argv[2], &rect.y))
  { EXM_FAIL(ehandler, "y0: bad argument"); }

  if (0 == enif_get_ulong(env, argv[3], &rect.width))
  { EXM_FAIL(ehandler, "width: bad argument"); }

  if (0 == enif_get_ulong(env, argv[4], &rect.height))
  { EXM_FAIL(ehandler, "height: bad argument"); }

  /* actually crops image */
  cropped_image = CropImage(resource->image, &rect, &resource->e_info);
  if (cropped_image == NULL)
  {
    CatchException(&resource->e_info);
    EXM_FAIL(ehandler, resource->e_info.reason);
  }
  DestroyImage(resource->image);
  resource->image = cropped_image;

  return(enif_make_tuple2(env, enif_make_atom(env, "ok"), argv[0]));

ehandler:
  return(enif_make_tuple2(env, enif_make_atom(env, "error"), exmagick_make_utf8str(env, errmsg)));
}
Exemplo n.º 11
0
static ERL_NIF_TERM
nif_scheduler_sendFrameworkMessage(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]){

    ErlNifBinary executorId_binary;
    ErlNifBinary slaveId_binary;
    char data[MAXBUFLEN];

    state_ptr state = (state_ptr) enif_priv_data(env);
    
    if(state->initilised == 0 ) 
    {
        return enif_make_tuple2(env, 
            enif_make_atom(env, "state_error"), 
            enif_make_atom(env, "scheduler_not_inited"));
    }
    if (!enif_inspect_binary(env, argv[0], &executorId_binary)) 
    {
                return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "executor_id"));
    }
    if (!enif_inspect_binary(env, argv[1], &slaveId_binary)) 
    {
                return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "slave_id"));
    }
    //REVIEW : buffer length
    if(!enif_get_string(env, argv[2], data , MAXBUFLEN, ERL_NIF_LATIN1 ))
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "data"));
    }

    SchedulerDriverStatus status = scheduler_sendFrameworkMessage( state->scheduler_state , 
                                                                        &executorId_binary, 
                                                                        &slaveId_binary, 
                                                                        data);
    return get_return_value_from_status(env, status);
}
Exemplo n.º 12
0
ERL_NIF_TERM exmagick_get_attr (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  char atom[EXM_MAX_ATOM_SIZE];
  exm_resource_t *resource;

  EXM_INIT;
  ErlNifResourceType *type = (ErlNifResourceType *) enif_priv_data(env);

  if (0 == enif_get_resource(env, argv[0], type, (void **) &resource))
  { EXM_FAIL(ehandler, "invalid handle"); }

  if (0 == enif_get_atom(env, argv[1], atom, EXM_MAX_ATOM_SIZE, ERL_NIF_LATIN1))
  { EXM_FAIL(ehandler, "invalid attribute"); }

  if (strcmp("adjoin", atom) == 0)
  {
    if (resource->i_info->adjoin == 0)
    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_atom(env, "false"))); }
    else if (resource->i_info->adjoin == 1)
    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_atom(env, "true"))); }
  }
  if (strcmp("rows", atom) == 0)
  {
    if (resource->image == NULL)
    { EXM_FAIL(ehandler, "image not loaded"); }

    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_long(env, resource->image->rows))); }
  }
  if (strcmp("columns", atom) == 0)
  {
    if (resource->image == NULL)
    { EXM_FAIL(ehandler, "image not loaded"); }

    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_long(env, resource->image->columns))); }
  }
  if (strcmp("density", atom) == 0)
  { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), exmagick_make_utf8str(env, resource->i_info->density))); }
  else if (strcmp("magick", atom) == 0)
  { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), exmagick_make_utf8str(env, resource->image->magick))); }

ehandler:
  return(enif_make_tuple2(env, enif_make_atom(env, "error"), exmagick_make_utf8str(env, errmsg)));
}
Exemplo n.º 13
0
// c_soc:op_alloc(ParentKey, Tuple, Arity) => ok
static ERL_NIF_TERM op_alloc(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  soc::State* st = (soc::State*) enif_priv_data(env);

  int                 parentKey;
  int                 parentKeyInt;
  ERL_NIF_TERM*       tuple;
  ERL_NIF_TERM*       tupleInt;
  int                 tupleArity;
  int                 tupleArityInt;
  int                 arity;
  int                 arityInt;

  if (enif_get_int(env, argv[0], &parentKey) == 0) {
    printf("[op_alloc] error: failed to get parentKey\n");
    return -1;
  }

  parentKeyInt = enif_make_copy(st->GetEnv(), parentKey);
  
  if (enif_get_tuple(env, argv[1], &tupleArity, &tuple) == 0) {
    printf("[op_alloc] error: failed to get tuple\n");
    return -1;
  }

  (*tupleInt) = enif_make_copy(st->GetEnv(), (*tuple));
  tupleArityInt = enif_make_copy(st->GetEnv(), tupleArity);

  if (enif_get_int(env, argv[2], &arity) == 0) {
    printf("[op_alloc] error: failed to initialize arity\n");
    return -1;
  } 

  arityInt = enif_make_copy(st->GetEnv(), arity);

  printf("operation start\n");
  soc::Operation* op = new soc::Operation(parentKeyInt, tupleInt, tupleArityInt, arityInt);
  int currentIndex = st->Insert(op);
  int currentIndexExt = enif_make_copy(env, currentIndex);
  printf("operation index: %d end\n", currentIndexExt);

  return enif_make_int(env, currentIndexExt);
}
Exemplo n.º 14
0
int
enc_init(Encoder* e, ErlNifEnv* env, ERL_NIF_TERM opts, ErlNifBinary* bin)
{
    ERL_NIF_TERM val;

    e->env = env;
    e->atoms = enif_priv_data(env);
    e->uescape = 0;
    e->pretty = 0;
    e->shiftcnt = 0;
    e->count = 0;

    if(!enif_is_list(env, opts)) {
        return 0;
    }

    while(enif_get_list_cell(env, opts, &val, &opts)) {
        if(enif_compare(val, e->atoms->atom_uescape) == 0) {
            e->uescape = 1;
        } else if(enif_compare(val, e->atoms->atom_pretty) == 0) {
            e->pretty = 1;
        } else if(enif_compare(val, e->atoms->atom_force_utf8) == 0) {
            // Ignore, handled in Erlang
        } else {
            return 0;
        }
    }

    e->iolen = 0;
    e->iolist = enif_make_list(env, 0);
    e->curr = bin;
    if(!enif_alloc_binary(BIN_INC_SIZE, e->curr)) {
        return 0;
    }

    memset(e->curr->data, 0, e->curr->size);

    e->p = (char*) e->curr->data;
    e->u = (unsigned char*) e->curr->data;
    e->i = 0;

    return 1;
}
Exemplo n.º 15
0
static
ERL_NIF_TERM exmagick_set_attr (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  ErlNifBinary utf8;
  char atom[EXM_MAX_ATOM_SIZE];
  exm_resource_t *resource;

  EXM_INIT;
  ErlNifResourceType *type = (ErlNifResourceType *) enif_priv_data(env);

  if (0 == enif_get_resource(env, argv[0], type, (void **) &resource))
  { EXM_FAIL(ehandler, "invalid handle"); }

  if (0 == enif_get_atom(env, argv[1], atom, EXM_MAX_ATOM_SIZE, ERL_NIF_LATIN1))
  { EXM_FAIL(ehandler, "argv[1]: bad argument"); }

  if (strcmp("adjoin", atom) == 0)
  {
    if (0 == exmagick_get_boolean_u(env, argv[2], &resource->i_info->adjoin))
    { EXM_FAIL(ehandler, "argv[2]: bad argument"); }
  }
  if (strcmp("magick", atom) == 0)
  {
    if (0 == exmagick_get_utf8str(env, argv[2], &utf8))
    { EXM_FAIL(ehandler, "argv[2]: bad argument"); }
    exmagick_utf8strcpy(resource->image->magick, &utf8, MaxTextExtent);
  }
  if (strcmp("density", atom) == 0)
  {
    if (0 == exmagick_get_utf8str(env, argv[2], &utf8))
    { EXM_FAIL(ehandler, "argv[2]: bad argument"); }
    MagickFree(resource->i_info->density);
    resource->i_info->density=exmagick_utf8strdup(&utf8);
    if (resource->i_info->density == NULL)
    { EXM_FAIL(ehandler, "could not set density"); }
  }

  return(enif_make_tuple2(env, enif_make_atom(env, "ok"), argv[0]));

ehandler:
  return(enif_make_tuple2(env, enif_make_atom(env, "error"), exmagick_make_utf8str(env, errmsg)));
}
Exemplo n.º 16
0
static ERL_NIF_TERM make_blob(ErlNifEnv* caller_env, ErlNifEnv* dst_env,
			      ERL_NIF_TERM other_term)
{
    PrivData* priv = (PrivData*) enif_priv_data(caller_env);
    ERL_NIF_TERM term, list;
    int n = 0;
    struct make_term_info mti;
    mti.caller_env = caller_env;
    mti.dst_env = dst_env;
    mti.reuse_push = 0;
    mti.reuse_pull = 0;
    mti.resource_type = priv->rt_arr[0].t;
    mti.other_term = other_term;

    list = enif_make_list(dst_env, 0); 
    while (make_term_n(&mti, n++, &term)) {
	list = enif_make_list_cell(dst_env, term, list);
    }
    return list;
}
Exemplo n.º 17
0
/**
 * Retrieve an object from LRU-Storage
 */
static ERL_NIF_TERM leo_mcerl_nif_get(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  lcu_cache* cache;
  String key;
  String val;

  ErlNifResourceType* pert;
  ErlNifBinary keybin;
  ErlNifBinary bin;

  if (argc < 2) {
    return enif_make_badarg(env);
  }

  pert = (ErlNifResourceType*)enif_priv_data(env);
  if (!enif_get_resource(env, argv[0], pert, (void**)&cache)) {
    return enif_make_badarg(env);
  }

  if (!enif_inspect_binary(env, argv[1], &keybin)) {
    return enif_make_badarg(env);
  }

  if (keybin.size <= 0) {
    return enif_make_badarg(env);
  }

  key.str = keybin.data;
  key.len = keybin.size;
  lcu_cache_get(cache, key, &val);

  if (val.str == NULL) {
    return atom_not_found;
  }

  if (!enif_alloc_binary(val.len, &bin)) {
    return enif_make_badarg(env);
  }

  memcpy(bin.data, val.str, val.len);
  return enif_make_tuple2(env, atom_ok, enif_make_binary(env, &bin));
}
Exemplo n.º 18
0
static ERL_NIF_TERM
nif_scheduler_stop(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int failover;
    state_ptr state = (state_ptr) enif_priv_data(env);
    
    if(state->initilised == 0 ) 
    {
        return enif_make_tuple2(env, 
            enif_make_atom(env, "state_error"), 
            enif_make_atom(env, "sheduler_not_inited"));
    }
    
    if(!enif_get_int( env, argv[0], &failover))
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "failover"));
    }

    if(failover < 0 || failover > 1)
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "failover"));
    }

    SchedulerDriverStatus status = scheduler_stop( state->scheduler_state, failover );
    
    if(status == 4){ // driver_stopped
        return enif_make_tuple2(env, 
                            enif_make_atom(env, "ok"), 
                            get_atom_from_status(env, status));
    }else{
        return enif_make_tuple2(env, 
                            enif_make_atom(env, "error"), 
                            get_atom_from_status(env, status));
    }
}
Exemplo n.º 19
0
/* 1: password, 2: dictpath */
static ERL_NIF_TERM
nif_check(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    PRIV *priv = NULL;

    ErlNifBinary passwd;
    ErlNifBinary path;

    char *err = NULL;


    priv = (PRIV *)enif_priv_data(env);

    if (!enif_inspect_iolist_as_binary(env, argv[0], &passwd))
        return enif_make_badarg(env);

    if (!enif_inspect_iolist_as_binary(env, argv[1], &path))
        return enif_make_badarg(env);

    /* NULL terminate strings */
    if (!enif_realloc_binary(&passwd, passwd.size+1))
        return atom_enomem;
    if (!enif_realloc_binary(&path, path.size+1))
        return atom_enomem;

    /* passwd.size is now equal to old passwd.size+1 */
    passwd.data[passwd.size-1] = '\0';
    path.data[path.size-1] = '\0';

    enif_mutex_lock(priv->lock);
    err = (char *)FascistCheck((char *)passwd.data, (char *)path.data);
    enif_mutex_unlock(priv->lock);

    (void)memset(passwd.data, '\0', passwd.size);

    enif_release_binary(&passwd);
    enif_release_binary(&path);

    return ( (err == NULL) ? atom_ok : error_tuple(env, err));
}
Exemplo n.º 20
0
static ERL_NIF_TERM create_solution(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]){

    // struct timespec start, end;
    // long diff;
    // clock_gettime(CLOCK_MONOTONIC, &start);

    ERL_NIF_TERM term;
    ErlNifResourceType* sol_type;
    Solution* sol;
    unsigned int len;
    unsigned int i;

    CHECK(env, argc == 1)
    CHECK(env, enif_get_uint(env, argv[0], &len))

    sol_type = (ErlNifResourceType*) enif_priv_data(env);
    CHECK(env, sol_type)

    sol = (Solution*) enif_alloc_resource(sol_type, sizeof(Solution));
    CHECK(env, sol)
    
    term = enif_make_resource(env, sol);
    CHECK(env,term)
    enif_release_resource(sol);

    sol->len = len;
    sol->genotype = (double*) malloc(sizeof(double)*len);
    for (i=0;i<len;i++){
        sol->genotype[i] = randdouble(-50.0, 50.0);
    }

    // clock_gettime(CLOCK_MONOTONIC, &end);

    // diff = CLOCKS_PER_SEC * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
    // printf("create=%llu\n", (long long unsigned int) diff);


    return term;
}
Exemplo n.º 21
0
Decoder*
dec_new(ErlNifEnv* env)
{
    jiffy_st* st = (jiffy_st*) enif_priv_data(env);
    Decoder* d = enif_alloc_resource(st->res_dec, sizeof(Decoder));
    int i;

    if(d == NULL) {
        return NULL;
    }

    d->atoms = st;

    d->bytes_per_red = DEFAULT_BYTES_PER_REDUCTION;
    d->is_partial = 0;
    d->return_maps = 0;
    d->return_trailer = 0;
    d->dedupe_keys = 0;
    d->null_term = d->atoms->atom_null;

    d->p = NULL;
    d->u = NULL;
    d->len = -1;
    d->i = -1;

    d->st_data = (char*) enif_alloc(STACK_SIZE_INC * sizeof(char));
    d->st_size = STACK_SIZE_INC;
    d->st_top = 0;

    for(i = 0; i < d->st_size; i++) {
        d->st_data[i] = st_invalid;
    }

    d->st_data[0] = st_value;
    d->st_top++;

    return d;
}
Exemplo n.º 22
0
static ERL_NIF_TERM alloc_msgenv(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    PrivData* priv = (PrivData*) enif_priv_data(env);
    struct make_term_info* mti;
    ERL_NIF_TERM ret;

    mti = (struct make_term_info*) enif_alloc_resource(msgenv_resource_type,
						       sizeof(*mti));
    mti->caller_env = NULL;
    mti->dst_env = enif_alloc_env();
    mti->reuse_push = 0;
    mti->reuse_pull = 0;
    mti->resource_type = priv->rt_arr[0].t;
    mti->other_term = enif_make_list(mti->dst_env, 0);
    mti->blob = enif_make_list(mti->dst_env, 0);
    mti->mtx = enif_mutex_create("nif_SUITE:mtx");
    mti->cond = enif_cond_create("nif_SUITE:cond");
    mti->send_res = 0xcafebabe;
    mti->n = 0;
    ret = enif_make_resource(env, mti);
    enif_release_resource(mti);
    return ret;
}
Exemplo n.º 23
0
/**
 * Initialize
 */
static ERL_NIF_TERM leo_mcerl_nif_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  ErlNifUInt64 max_size;
  ERL_NIF_TERM term;
  ErlNifResourceType* pert;
  lcu_cache* cache;

  if (argc < 1) {
    return enif_make_badarg(env);
  }

  if (!enif_get_uint64(env, argv[0], &max_size)) {
    return enif_make_badarg(env);
  }

  pert = (ErlNifResourceType*)enif_priv_data(env);
  cache = enif_alloc_resource(pert, sizeof(lcu_cache));
  lcu_cache_init(cache, auto_eject_on, max_size, 2, 256, 1024 * 1024 * 8);

  term = enif_make_resource(env, cache);
  enif_release_resource(cache);

  return enif_make_tuple2(env, atom_ok, term);
}
Exemplo n.º 24
0
static ERL_NIF_TERM do_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  char deviceName[MAXBUFLEN];
  long latency;
  ERL_NIF_TERM streamTerm;
  PortMidiStream **streamAlloc;
  PmError result;

  ErlNifResourceType* streamType = (ErlNifResourceType*)enif_priv_data(env);
  streamAlloc = (PortMidiStream**)enif_alloc_resource(streamType, sizeof(PortMidiStream*));

  enif_get_string(env, argv[0], deviceName, MAXBUFLEN, ERL_NIF_LATIN1);
  enif_get_long(env, argv[1], &latency);

  if((result = findDevice(streamAlloc, deviceName, OUTPUT, latency)) != pmNoError) {
    ERL_NIF_TERM reason = enif_make_atom(env, makePmErrorAtom(result));
    return enif_make_tuple2(env, enif_make_atom(env, "error"), reason);
  }

  streamTerm = enif_make_resource(env, streamAlloc);
  enif_keep_resource(streamAlloc);

  return enif_make_tuple2(env, enif_make_atom(env, "ok"), streamTerm);
}
Exemplo n.º 25
0
ERL_NIF_TERM
less_json_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    couch_ejson_ctx_t ctx;
    int result;
    ErlNifBinary key1Bin, key2Bin;
    char *keys;

    if (!enif_inspect_iolist_as_binary(env, argv[0], &key1Bin)) {
        return enif_make_badarg(env);
    }
    if (!enif_inspect_iolist_as_binary(env, argv[1], &key2Bin)) {
        return enif_make_badarg(env);
    }

    keys = (char *) enif_alloc(key1Bin.size + key2Bin.size + 2);
    if (keys == NULL) {
        return enif_make_tuple2(env, ATOM_ERROR, enif_make_atom(env, "Failed to allocate memory"));
    }

    memcpy(keys, key1Bin.data, key1Bin.size);
    keys[key1Bin.size] = '\0';
    memcpy(keys + key1Bin.size + 1, key2Bin.data, key2Bin.size);
    keys[key1Bin.size + 1 + key2Bin.size] = '\0';

    ctx.env = env;
    ctx.error = 0;
    ctx.errorMsg = NULL;
    ctx.coll = NULL;
    ctx.globalCtx = (couch_ejson_global_ctx_t *) enif_priv_data(env);

    result = less_json(keys, keys + key1Bin.size + 1, &ctx);
    release_coll(&ctx);
    enif_free(keys);

    return make_return_term(&ctx, result);
}
Exemplo n.º 26
0
// c_soc:op_update(Index, Term) => {ripe, Frame} | ok | error
static ERL_NIF_TERM op_update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  soc::State* st = (soc::State*) enif_priv_data(env);

  int index;
  
  if(enif_get_int(env, argv[0], &index) == 0) {
    printf("[op_alloc] error: failed to initialize index\n");
    return -1;
  }

  printf("update start \n");
  const soc::Operation* op = st->Update(index, &argv[1]);
  soc::Frame* frame = op->GetFrame();
  printf("update end\n");

  if (frame->IsRipe()) {
    printf("frame->IsRipe()\n");
    ERL_NIF_TERM values = enif_make_list_from_array(env, frame->GetValues(), frame->GetLimit());
    return enif_make_tuple2(env, enif_make_atom(env, "ripe"), values);
  } else {
    return enif_make_atom(env, "ok");
  }
}
Exemplo n.º 27
0
// c_soc:op_lookup(Index) => op
static ERL_NIF_TERM op_lookup(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  soc::State* st = (soc::State*) enif_priv_data(env);

  int index;
  
  if (enif_get_int(env, argv[0], &index) == 0) {
    printf("[op_alloc] error: failed to initialize index\n");
    return -1;
  }

  const soc::Operation* op;

  printf("loc / msg lookup index %d start\n", index);
  try {
    op = st->Lookup(index);
  } catch (std::out_of_range exn) {
    printf("Exception: index %d out of range\n", index);
  }

  ERL_NIF_TERM loc = enif_make_tuple2(env, enif_make_int(env, index), 
				      enif_make_int(env, op->GetParentKey()));

  ERL_NIF_TERM ext_msg;
  ERL_NIF_TERM int_msg;
  try {
    ext_msg = enif_make_tuple_from_array(st->GetEnv(), op->GetTuple(), op->TupleArity());
    int_msg = enif_make_copy(env, ext_msg);
  } catch (...) {
    printf("ERROR: ...");
  }

  printf("loc / msg lookup success\n");

  return enif_make_tuple2(env, loc, int_msg);
}
Exemplo n.º 28
0
static
ERL_NIF_TERM exmagick_image_thumb (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  long width, height;
  Image* thumb;
  exm_resource_t *resource;

  EXM_INIT;
  ErlNifResourceType *type = (ErlNifResourceType *) enif_priv_data(env);

  if (0 == enif_get_resource(env, argv[0], type, (void **) &resource))
  { EXM_FAIL(ehandler, "invalid handle"); }

  if (resource->image == NULL)
  { EXM_FAIL(ehandler, "image not loaded"); }

  if (0 == enif_get_long(env, argv[1], &width))
  { EXM_FAIL(ehandler, "width: bad argument"); }

  if (0 == enif_get_long(env, argv[2], &height))
  { EXM_FAIL(ehandler, "height: bad argument"); }

  thumb = ThumbnailImage(resource->image, width, height, &resource->e_info);
  if (thumb == NULL)
  {
    CatchException(&resource->e_info);
    EXM_FAIL(ehandler, resource->e_info.reason);
  }
  DestroyImage(resource->image);
  resource->image = thumb;

  return(enif_make_tuple2(env, enif_make_atom(env, "ok"), argv[0]));

ehandler:
  return(enif_make_tuple2(env, enif_make_atom(env, "error"), exmagick_make_utf8str(env, errmsg)));
}
Exemplo n.º 29
0
/**
 * Insert an object into LRU-Storage
 */
static ERL_NIF_TERM leo_mcerl_nif_put(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  lcu_cache* cache;
  String key;
  String val;

  ErlNifResourceType* pert;
  ErlNifBinary keybin;
  ErlNifBinary bin;
  bool ret;

  if (argc < 3) {
    return enif_make_badarg(env);
  }

  pert = (ErlNifResourceType*)enif_priv_data(env);

  if (!enif_get_resource(env, argv[0], pert, (void**)&cache)) {
    return enif_make_badarg(env);
  }
  if (!enif_inspect_binary(env, argv[1], &keybin)) {
    return enif_make_badarg(env);
  }
  if (keybin.size <= 0) {
    return enif_make_badarg(env);
  }
  if (!enif_inspect_binary(env, argv[2], &bin)) {
    return enif_make_badarg(env);
  }

  key.str = keybin.data;
  key.len = keybin.size;
  val.str = bin.data;
  val.len = bin.size;
  ret = lcu_cache_put(cache, key, val);
  return ret ? atom_ok : enif_make_tuple2(env, atom_error, atom_oom);
}
Exemplo n.º 30
0
static ERL_NIF_TERM
nif_scheduler_abort(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    state_ptr state = (state_ptr) enif_priv_data(env);
    
    if(state->initilised == 0 ) 
    {
        return enif_make_tuple2(env, 
            enif_make_atom(env, "state_error"), 
            enif_make_atom(env, "scheduler_not_inited"));
    }
    
    SchedulerDriverStatus status = scheduler_abort( state->scheduler_state );
    
    if(status == 3){ // DRIVER_ABORTED
        return enif_make_tuple2(env, 
                            enif_make_atom(env, "ok"), 
                            get_atom_from_status(env, status));
    }else{
        return enif_make_tuple2(env, 
                            enif_make_atom(env, "error"), 
                            get_atom_from_status(env, status));
    }
}