/* * Bind a variable to a prepared statement */ static ERL_NIF_TERM esqlite_bind(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { esqlite_statement *stmt; esqlite_command *cmd = NULL; ErlNifPid pid; if(argc != 4) return enif_make_badarg(env); if(!enif_get_resource(env, argv[0], esqlite_statement_type, (void **) &stmt)) return enif_make_badarg(env); if(!enif_is_ref(env, argv[1])) return make_error_tuple(env, "invalid_ref"); if(!enif_get_local_pid(env, argv[2], &pid)) return make_error_tuple(env, "invalid_pid"); cmd = command_create(); if(!cmd) return make_error_tuple(env, "command_create_failed"); cmd->type = cmd_bind; cmd->ref = enif_make_copy(cmd->env, argv[1]); cmd->pid = pid; cmd->stmt = stmt->statement; cmd->arg = enif_make_copy(cmd->env, argv[3]); if(!stmt->connection) return make_error_tuple(env, "no_connection"); if(!stmt->connection->commands) return make_error_tuple(env, "no_command_queue"); return push_command(env, stmt->connection, cmd); }
/* * Prepare the sql statement */ static ERL_NIF_TERM esqlite_prepare(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { esqlite_connection *conn; esqlite_command *cmd = NULL; ErlNifPid pid; if(argc != 4) return enif_make_badarg(env); if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &conn)) return enif_make_badarg(env); if(!enif_is_ref(env, argv[1])) return make_error_tuple(env, "invalid_ref"); if(!enif_get_local_pid(env, argv[2], &pid)) return make_error_tuple(env, "invalid_pid"); cmd = command_create(); if(!cmd) return make_error_tuple(env, "command_create_failed"); cmd->type = cmd_prepare; cmd->ref = enif_make_copy(cmd->env, argv[1]); cmd->pid = pid; cmd->arg = enif_make_copy(cmd->env, argv[3]); return push_command(env, conn, cmd); }
task_t* init_task(task_type_t type, ERL_NIF_TERM ref, ErlNifPid pid, ERL_NIF_TERM orig_term) { ERL_NIF_TERM term; task_t *task; task = init_empty_task(type); task->pid = pid; task->env = enif_alloc_env(); if (task->env == NULL) { cleanup_task(&task); goto done; } term = enif_make_copy(task->env, orig_term); if (!enif_inspect_binary(task->env, term, &task->data)) { cleanup_task(&task); goto done; } task->ref = enif_make_copy(task->env, ref); done: return task; }
/* * Open the database */ static ERL_NIF_TERM esqlite_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { esqlite_connection *db; esqlite_command *cmd = NULL; ErlNifPid pid; if(argc != 4) return enif_make_badarg(env); if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &db)) return enif_make_badarg(env); if(!enif_is_ref(env, argv[1])) return make_error_tuple(env, "invalid_ref"); if(!enif_get_local_pid(env, argv[2], &pid)) return make_error_tuple(env, "invalid_pid"); /* Note, no check is made for the type of the argument */ cmd = command_create(); if(!cmd) return make_error_tuple(env, "command_create_failed"); cmd->type = cmd_open; cmd->ref = enif_make_copy(cmd->env, argv[1]); cmd->pid = pid; cmd->arg = enif_make_copy(cmd->env, argv[3]); return push_command(env, db, cmd); }
static void generate_keypair_from_seed(brine_task_s *task) { ErlNifEnv *env = task->env; ERL_NIF_TERM result; brine_keypair_s *keypair = (brine_keypair_s *) enif_alloc_resource(brine_keypair_resource, sizeof(brine_keypair_s)); ErlNifBinary seed; if (!keypair) { result = BRINE_ERROR_NO_MEMORY; } else { if (!enif_inspect_binary(env, task->options.generate.seed, &seed)) { result = BRINE_ATOM_ERROR; } else { if (!brine_init_keypair_from_seed(keypair, seed.data, seed.size)) { result = BRINE_ATOM_ERROR; } else { result = enif_make_tuple2(env, enif_make_copy(env, BRINE_ATOM_OK), make_keypair_record(env, keypair)); } } enif_release_resource(keypair); } enif_send(NULL, &task->owner, task->env, enif_make_tuple2(env, task->ref, result)); }
static int prefix_cb(void *data, const unsigned char *k, uint32_t k_len, void *val) { callback_data *cb_data = data; art_elem_struct *elem = val; ErlNifBinary key, value; enif_alloc_binary(k_len - 1, &key); memcpy(key.data, k, k_len - 1); enif_alloc_binary(elem->size, &value); memcpy(value.data, elem->data, elem->size); ErlNifEnv *msg_env = enif_alloc_env(); if(msg_env == NULL) return mk_error(cb_data->env, "env_alloc_error");; ERL_NIF_TERM caller_ref = enif_make_copy(msg_env, cb_data->caller_ref); ERL_NIF_TERM res = enif_make_tuple2(msg_env, caller_ref, enif_make_tuple2(msg_env, enif_make_binary(msg_env, &key), enif_make_binary(msg_env, &value))); if(!enif_send(cb_data->env, &cb_data->pid, msg_env, res)) { enif_free(msg_env); return -1; } enif_free(msg_env); return 0; }
static ERL_NIF_TERM send_blob_thread(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { union { void* vp; struct make_term_info* p; }mti; ERL_NIF_TERM copy; if (!enif_get_resource(env, argv[0], msgenv_resource_type, &mti.vp) || !enif_get_local_pid(env,argv[1], &mti.p->to_pid)) { return enif_make_badarg(env); } copy = enif_make_copy(env, mti.p->blob); mti.p->send_it = enif_is_identical(argv[2],atom_join); if (enif_thread_create("nif_SUITE:send_from_thread", &mti.p->tid, threaded_sender, mti.p, NULL) != 0) { return enif_make_badarg(env); } if (enif_is_identical(argv[2],atom_join)) { int err = enif_thread_join(mti.p->tid, NULL); assert(err == 0); return enif_make_tuple3(env, atom_ok, enif_make_int(env, mti.p->send_res), copy); } else { enif_keep_resource(mti.vp); return enif_make_tuple2(env, atom_ok, copy); } }
static ERL_NIF_TERM elua_newstate_async(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { msg_t *msg; ErlNifPid pid; if(argc != 2) { return enif_make_badarg(env); } // ref if(!enif_is_ref(env, argv[0])){ return make_error_tuple(env, "invalid_ref"); } // dest pid if(!enif_get_local_pid(env, argv[1], &pid)) { return make_error_tuple(env, "invalid_pid"); } msg = msg_create(); if(!msg) { return make_error_tuple(env, "command_create_failed"); } msg->type = msg_newstate; msg->ref = enif_make_copy(msg->env, argv[0]); msg->pid = pid; msg->hold_env = env; msg->res=NULL; return push_command(env, NULL, msg); }
/* OGRFeatureDefnH OGR_L_GetLayerDefn(OGRLayerH hLayer) {ok, DataSource} = lgeo_ogr:open("test/polygon.shp"), {ok, Layer} = lgeo_ogr:ds_get_layer(DataSource, 0), {ok, FeatureDefn} = lgeo_ogr:l_get_layer_defn(Layer). */ static ERL_NIF_TERM l_get_layer_defn(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { EnvLayer_t **layer; ERL_NIF_TERM eterm; if (argc != 1) { return enif_make_badarg(env); } if(!enif_get_resource(env, argv[0], OGR_L_RESOURCE, (void**)&layer)) { return enif_make_badarg(env); } OGRFeatureDefnH feat_defn = OGR_L_GetLayerDefn((**layer).obj); EnvFeatureDefn_t **feature_defn = \ enif_alloc_resource(OGR_FD_RESOURCE, sizeof(EnvFeatureDefn_t*)); ErlNifEnv *feature_defn_env = enif_alloc_env(); *feature_defn = (EnvFeatureDefn_t*) enif_alloc(sizeof(EnvFeatureDefn_t)); (**feature_defn).env = feature_defn_env; (**feature_defn).obj = feat_defn; // Save copy of layer so is not garbage collected enif_make_copy(feature_defn_env, argv[0]); eterm = enif_make_resource(env, feature_defn); enif_release_resource(feature_defn); return enif_make_tuple2(env, enif_make_atom(env, "ok"), eterm); }
/* OGRGeometryH OGR_F_GetGeometryRef(OGRFeatureH hFeat) {ok, DataSource} = lgeo_ogr:open("test/polygon.shp"), {ok, Layer} = lgeo_ogr:ds_get_layer(DataSource, 0), {ok, Feature} = lgeo_ogr:l_get_feature(Layer, 0), {ok, Geometry} = lgeo_ogr:f_get_geometry_ref(Feature). */ static ERL_NIF_TERM f_get_geometry_ref(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { EnvFeature_t **feature; ERL_NIF_TERM eterm; if (argc != 1) { return enif_make_badarg(env); } if(!enif_get_resource(env, argv[0], OGR_F_RESOURCE, (void**)&feature)) { return enif_make_badarg(env); } OGRGeometryH geom = OGR_F_GetGeometryRef((**feature).obj); if(geom == NULL) { return enif_make_atom(env, "undefined"); } EnvGeometry_t **geometry = \ enif_alloc_resource(OGR_G_RESOURCE, sizeof(EnvGeometry_t*)); ErlNifEnv *geometry_env = enif_alloc_env(); *geometry = (EnvGeometry_t*) enif_alloc(sizeof(EnvGeometry_t)); (**geometry).env = geometry_env; (**geometry).obj = geom; // Save copy of feature so is not garbage collected enif_make_copy(geometry_env, argv[0]); eterm = enif_make_resource(env, geometry); enif_release_resource(geometry); return enif_make_tuple2(env, enif_make_atom(env, "ok"), eterm); }
void ewpcap_error(EWPCAP_STATE *ep, char *msg) { int rv = 0; if (ep->p == NULL) return; /* {ewpcap_error, Ref, Error} */ rv = enif_send( NULL, &ep->pid, ep->env, enif_make_tuple3(ep->env, atom_ewpcap_error, enif_make_copy(ep->env, ep->ref), enif_make_string(ep->env, msg, ERL_NIF_LATIN1) ) ); if (!rv) pcap_breakloop(ep->p); enif_clear_env(ep->env); }
ERL_NIF_TERM iterator_locales(ErlNifEnv* env, int argc, const ERL_NIF_TERM /*argv*/[]) { if (argc != 0) return enif_make_badarg(env); return enif_make_copy(env, available_locales); }
static ERL_NIF_TERM copy_blob(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { union { void* vp; struct make_term_info* p; }mti; if (!enif_get_resource(env, argv[0], msgenv_resource_type, &mti.vp)) { return enif_make_badarg(env); } return enif_make_copy(env, mti.p->blob); }
ERL_NIF_TERM timezone_ids(ErlNifEnv* env, int argc, const ERL_NIF_TERM /*argv*/[]) { if (argc != 0) return enif_make_badarg(env); return enif_make_copy(env, available_timezones); }
static ERL_NIF_TERM make_keypair_record(ErlNifEnv *env, brine_keypair_s *keypair) { return enif_make_tuple4(env, enif_make_copy(env, BRINE_ATOM_KEYPAIR), enif_make_resource(env, (void *) keypair), // private key enif_make_resource_binary(env, (void *) keypair, (void *) keypair->private_key, BRINE_PRIVKEY_SZ), // public key enif_make_resource_binary(env, (void *) keypair, (void *) keypair->public_key, BRINE_PUBKEY_SZ)); }
static ERL_NIF_TERM elua_gencall_async(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { elua_t *res; msg_t *msg; ErlNifPid pid; if(argc != 6) { return enif_make_badarg(env); } // first arg: ref if(!enif_get_resource(env, argv[0], RES_SYNC, (void**) &res)) { return enif_make_badarg(env); } // ref if(!enif_is_ref(env, argv[1])) { return make_error_tuple(env, "invalid_ref"); } // dest pid if(!enif_get_local_pid(env, argv[2], &pid)) { return make_error_tuple(env, "invalid_pid"); } // fourth arg: list of input args if(!enif_is_list(env, argv[5])) { return enif_make_badarg(env); } msg = msg_create(); if(!msg) { return make_error_tuple(env, "command_create_failed"); } msg->type = msg_gencall; msg->ref = enif_make_copy(msg->env, argv[1]); msg->pid = pid; msg->arg1 = enif_make_copy(msg->env, argv[3]); msg->arg2 = enif_make_copy(msg->env, argv[4]); msg->arg3 = enif_make_copy(msg->env, argv[5]); msg->res = res; return push_command(env, res, msg); }
int vm_add_call(vm_ptr vm, ENTERM ref, ENPID pid, ENTERM name, ENTERM args) { job_ptr job = job_create(); if(job == NULL) goto error; job->type = job_call; job->ref = enif_make_copy(job->env, ref); job->pid = pid; job->name = enif_make_copy(job->env, name); job->args = enif_make_copy(job->env, args); if(!queue_push(vm->jobs, job)) goto error; return 1; error: if(job != NULL) job_destroy(job); return 0; }
void do_set(robin_q_handle *handle, ERL_NIF_TERM value) { const ERL_NIF_TERM *elements_tuple; int elements_size; ERL_NIF_TERM elements_term = enif_make_copy(handle->env, value); enif_get_tuple(handle->env, elements_term, &elements_size, &elements_tuple); handle->size = elements_size; handle->elements = elements_tuple; handle->index = 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); }
/* * Multi step to a prepared statement */ static ERL_NIF_TERM esqlite_multi_step(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { esqlite_connection *conn; esqlite_statement *stmt; esqlite_command *cmd = NULL; ErlNifPid pid; int chunk_size = 0; if(argc != 5) return enif_make_badarg(env); if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &conn)) return enif_make_badarg(env); if(!enif_get_resource(env, argv[1], esqlite_statement_type, (void **) &stmt)) return enif_make_badarg(env); if(!enif_get_int(env, argv[2], &chunk_size)) return make_error_tuple(env, "invalid_chunk_size"); if(!enif_is_ref(env, argv[3])) return make_error_tuple(env, "invalid_ref"); if(!enif_get_local_pid(env, argv[4], &pid)) return make_error_tuple(env, "invalid_pid"); if(!stmt->statement) return make_error_tuple(env, "no_prepared_statement"); cmd = command_create(); if(!cmd) return make_error_tuple(env, "command_create_failed"); cmd->type = cmd_multi_step; cmd->ref = enif_make_copy(cmd->env, argv[3]); cmd->pid = pid; cmd->stmt = enif_make_copy(cmd->env, argv[1]); cmd->arg = enif_make_copy(cmd->env, argv[2]); return push_command(env, conn, cmd); }
static ErlCall *CreateCall(ERL_NIF_TERM fun, ERL_NIF_TERM args) { enif_mutex_lock(callsMutex); ErlCall *erlCall = (ErlCall *)malloc(sizeof(ErlCall)); erlCall->id = id++; erlCall->env = enif_alloc_env(); ERL_NIF_TERM msgFun = enif_make_copy(erlCall->env, fun); ERL_NIF_TERM msgArgs = enif_make_copy(erlCall->env, args); ERL_NIF_TERM msgId = enif_make_int(erlCall->env, erlCall->id); erlCall->msg = enif_make_tuple3(erlCall->env, msgId, msgFun, msgArgs); erlCall->cond = enif_cond_create("erlcl_cond"); erlCall->mutex = enif_mutex_create("erlcl_mutex"); erlCall->complete = 0; HASH_ADD_INT(calls, id, erlCall); enif_mutex_unlock(callsMutex); return erlCall; }
static ERL_NIF_TERM dirty_heap_access_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ERL_NIF_TERM res = enif_make_list(env, 0); int i; assert(ERL_NIF_THR_DIRTY_CPU_SCHEDULER == enif_thread_type() || ERL_NIF_THR_DIRTY_IO_SCHEDULER == enif_thread_type()); for (i = 0; i < 1000; i++) res = enif_make_list_cell(env, enif_make_copy(env, argv[0]), res); return res; }
static ERL_NIF_TERM send_term(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifEnv* menv; ErlNifPid pid; int ret; if (!enif_get_local_pid(env, argv[0], &pid)) { return enif_make_badarg(env); } menv = enif_alloc_env(); ret = enif_send(env, &pid, menv, enif_make_copy(menv, argv[1])); enif_free_env(menv); return enif_make_int(env, ret); }
static ErlNifEnv * copy_current_line(ErlNifEnv *env, ecsv_parser_t *parser) { ErlNifEnv *old = parser->env; line_t *line = &parser->current_line; parser->env = env; for (size_t i=0; i < line->pos; i++) { line->fields[i] = enif_make_copy(env, line->fields[i]); } return old; }
static ERL_NIF_TERM send_blob(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { union { void* vp; struct make_term_info* p; }mti; ErlNifPid to; ERL_NIF_TERM copy; int res; if (!enif_get_resource(env, argv[0], msgenv_resource_type, &mti.vp) || !enif_get_local_pid(env, argv[1], &to)) { return enif_make_badarg(env); } copy = enif_make_copy(env, mti.p->blob); res = enif_send(env, &to, mti.p->dst_env, mti.p->blob); return enif_make_tuple3(env, atom_ok, enif_make_int(env,res), copy); }
static nif_term_t salt_enqueue_req(nif_heap_t *hp, struct salt_pcb *sc, nif_pid_t pid, nif_term_t ref, uint_t type, uint_t aux) { struct salt_msg *sm; const char *err; /* Prepare async request for worker thread. */ sm = enif_alloc(sizeof(*sm)); if (sm == NULL) return (BADARG); sm->msg_heap = enif_alloc_env(); assert(sm->msg_heap != NULL); sm->msg_next = NULL; sm->msg_from = pid; /* struct copy */ sm->msg_mref = enif_make_copy(sm->msg_heap, ref); sm->msg_type = type; sm->msg_aux = aux; /* Enqueue request checking for failure scenarios. */ enif_mutex_lock(sc->sc_lock); if (sc->sc_req_npend >= 128) { err = "congested"; goto fail; } if (sc->sc_exit_flag) { /* XXX This should not even be possible, no? */ err = "exiting"; goto fail; } *sc->sc_req_lastp = sm; sc->sc_req_lastp = &sm->msg_next; sc->sc_req_npend += 1; enif_cond_signal(sc->sc_cond); enif_mutex_unlock(sc->sc_lock); return (enif_make_atom(hp, "enqueued")); /* Failure treatment. */ fail: enif_mutex_unlock(sc->sc_lock); enif_free_env(sm->msg_heap); enif_free(sm); return (enif_make_atom(hp, err)); }
static ERL_NIF_TERM ErlangCall(ErlNifEnv *env, ERL_NIF_TERM fun, ERL_NIF_TERM args) { ErlCall *erlCall = CreateCall(fun, args); enif_mutex_lock(erlCall->mutex); enif_send(env, &server, erlCall->env, erlCall->msg); while(!erlCall->complete) { enif_cond_wait(erlCall->cond, erlCall->mutex); } enif_mutex_unlock(erlCall->mutex); ERL_NIF_TERM result = enif_make_copy(env, erlCall->result); DestroyCall(erlCall); return result; }
int vm_send(vm_ptr vm, ENTERM data) { job_ptr job = job_create(); if(job == NULL) goto error; job->type = job_response; job->args = enif_make_copy(job->env, data); if(!queue_send(vm->jobs, job)) goto error; return 1; error: if(job != NULL) job_destroy(job); return 0; }
static ERL_NIF_TERM elibart_prefix_search(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { art_tree* t; ErlNifBinary key; callback_data cb_data; // extract arguments atr_tree, key if (argc != 4) return enif_make_badarg(env); if(!enif_get_resource(env, argv[0], elibart_RESOURCE, (void**) &t)) return enif_make_badarg(env); if (!enif_inspect_binary(env, argv[1], &key)) return enif_make_badarg(env); cb_data.env = env; if(!enif_is_pid(env, argv[3])) return mk_error(env, "not_a_pid"); if(!enif_get_local_pid(env, argv[3], &cb_data.pid)) return mk_error(env, "not_a_local_pid"); cb_data.caller_ref = argv[2]; // TODO this should be a worker thread since it's a long opearation (?) if (art_iter_prefix(t, key.data, key.size, prefix_cb, &cb_data)) return mk_error(env, "art_prefix_search"); ErlNifEnv *msg_env = enif_alloc_env(); if(msg_env == NULL) return mk_error(env, "env_alloc_error");; ERL_NIF_TERM caller_ref = enif_make_copy(msg_env, argv[2]); ERL_NIF_TERM res = enif_make_tuple2(msg_env, caller_ref, mk_atom(msg_env, "ok")); if (!enif_send(env, &cb_data.pid, msg_env, res)) { enif_free(msg_env); return mk_error(env, "art_prefix_search"); } enif_free(msg_env); return mk_atom(env, "ok"); }
static void keypair_to_binary(brine_task_s *task) { ErlNifEnv *env = task->env; brine_keypair_s *keypair = task->options.signature.keys; ErlNifBinary blob; ERL_NIF_TERM result; if (!enif_alloc_binary(BRINE_BLOB_SZ, &blob)) { result = BRINE_ERROR_NO_MEMORY; } else { brine_serialize_keypair(keypair, blob.data, blob.size); result = enif_make_tuple2(env, enif_make_copy(env, BRINE_ATOM_OK), enif_make_binary(env, &blob)); enif_release_binary(&blob); } enif_release_resource((void *) keypair); enif_send(NULL, &task->owner, task->env, enif_make_tuple2(env, task->ref, result)); }