static ERL_NIF_TERM mark_to_term(ErlNifEnv *env, yaml_mark_t *mark) { if(mark) { return enif_make_tuple3(env, enif_make_ulong(env, mark->index), enif_make_ulong(env, mark->line), enif_make_ulong(env, mark->column)); } else { return enif_make_atom(env, "null"); } }
static ERL_NIF_TERM nif_sendmsg(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { int s = -1; ErlNifBinary msg = {0}; int flags = 0; ssize_t n = 0; if (!enif_get_int(env, argv[0], &s)) return enif_make_badarg(env); if (!enif_inspect_binary(env, argv[1], &msg)) return enif_make_badarg(env); if (!enif_get_int(env, argv[2], &flags)) return enif_make_badarg(env); if (msg.size != sizeof(struct msghdr)) return enif_make_badarg(env); /* Make the binary mutable */ if (!enif_realloc_binary(&msg, msg.size)) return error_tuple(env, ENOMEM); n = sendmsg(s, (const struct msghdr *)msg.data, flags); if (n < 0) return error_tuple(env, errno); return enif_make_tuple3(env, atom_ok, enif_make_ulong(env, n), enif_make_binary(env, &msg)); }
static ERL_NIF_TERM emmap_position(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { mhandle *handle; long position; long relpos; if (argc==3 && enif_get_resource(env, argv[0], MMAP_RESOURCE, (void**)&handle) && enif_get_long(env, argv[2], &relpos) && (argv[1] == ATOM_CUR || argv[1] == ATOM_BOF || argv[1] == ATOM_EOF)) { RW_LOCK; if (argv[1] == ATOM_BOF) { position = 0L + relpos; } else if (argv[1] == ATOM_CUR) { position = handle->position + relpos; } else if (argv[1] == ATOM_EOF) { position = handle->len - relpos; } if (position < 0L || ((unsigned long)position) > handle->len) { RW_UNLOCK; return enif_make_badarg(env); } handle->position = position; RW_UNLOCK; return enif_make_tuple2(env, ATOM_OK, enif_make_ulong(env, position)); } else { return enif_make_badarg(env); } }
void ewpcap_send(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) { EWPCAP_STATE *ep = (EWPCAP_STATE *)user; ErlNifBinary buf = {0}; int rv = 0; /* XXX no way to indicate an error? */ if (ep->p == NULL) return; if (!enif_alloc_binary(h->caplen, &buf)) { pcap_breakloop(ep->p); return; } (void)memcpy(buf.data, bytes, buf.size); /* {ewpcap, Ref, DatalinkType, Time, ActualLength, Packet} */ rv = enif_send( NULL, &ep->pid, ep->env, enif_make_tuple6(ep->env, atom_ewpcap, enif_make_copy(ep->env, ep->ref), enif_make_int(ep->env, ep->datalink), enif_make_tuple3(ep->env, enif_make_ulong(ep->env, abs(h->ts.tv_sec / 1000000)), enif_make_ulong(ep->env, h->ts.tv_sec % 1000000), enif_make_ulong(ep->env, h->ts.tv_usec) ), enif_make_ulong(ep->env, h->len), enif_make_binary(ep->env, &buf) ) ); if (!rv) pcap_breakloop(ep->p); enif_clear_env(ep->env); }
static int test_ulong(ErlNifEnv* env, unsigned long i1) { unsigned long i2 = 0; ERL_NIF_TERM int_term = enif_make_ulong(env, i1); if (!enif_get_ulong(env,int_term, &i2) || i1 != i2) { fprintf(stderr, "test_ulong(%lu) ...FAILED i2=%lu\r\n", i1, i2); return 0; } return 1; }
ERL_NIF_TERM _hh_get_total_count(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { hh_ctx_t* ctx = NULL; ErlNifResourceType* ctx_type = get_hh_ctx_type(env); if (ctx_type != NULL && !enif_get_resource(env, argv[0], ctx_type, (void **)&ctx)) { return enif_make_badarg(env); } if (ctx != NULL) { return enif_make_ulong(env,ctx->data->total_count); } return make_error(env, "bad_hdr_histogram_nif_impl"); }
ERL_NIF_TERM meter_stats(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { meter_handle* handle; if (enif_get_resource(env,argv[0],meter_RESOURCE,(void**)&handle)) { pthread_mutex_lock(&(handle->m)); ERL_NIF_TERM result = enif_make_list4(env, enif_make_tuple2(env,ATOM_COUNT, enif_make_ulong(env, handle->p->count())), enif_make_tuple2(env,ATOM_ONE, enif_make_double(env,handle->p->one())), enif_make_tuple2(env,ATOM_FIVE,enif_make_double(env, handle->p->five())), enif_make_tuple2(env,ATOM_FIFTEEN,enif_make_double(env, handle->p->fifteen()))); pthread_mutex_unlock(&(handle->m)); return result; } else return enif_make_badarg(env); }
static ERL_NIF_TERM mmap_pread(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary bin; ERL_NIF_TERM sub; unsigned long offset, size; if(!enif_inspect_binary(env, argv[0], &bin)) { return enif_make_badarg(env); } if(!enif_get_ulong(env, argv[1], &offset)) { return enif_make_badarg(env); } if(!enif_get_ulong(env, argv[2], &size)) { return enif_make_badarg(env); } if((sub = enif_make_sub_binary(env, argv[0], offset, size))) { return enif_make_tuple2(env, enif_make_atom(env, "ok"), sub); } return enif_make_ulong(env, 42); }
static ERL_NIF_TERM get_priv_data_ptr(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ADD_CALL("get_priv_data_ptr"); return enif_make_ulong(env, (unsigned long)priv_data(env)); }
static ERL_NIF_TERM make_term_ulong(struct make_term_info* mti, int n) { unsigned long ul; fill(&ul, sizeof(ul), n); return enif_make_ulong(mti->dst_env, ul); }