本文整理汇总了C++中enif_alloc函数的典型用法代码示例。如果您正苦于以下问题:C++ enif_alloc函数的具体用法?C++ enif_alloc怎么用?C++ enif_alloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了enif_alloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cb_http_args
void* cb_http_args(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
http_args_t* args = (http_args_t*)enif_alloc(sizeof(http_args_t));
unsigned int len;
if (!enif_get_list_length(env, argv[0], &len)) goto error0;
len += 1;
args->path = (char *)enif_alloc(len * sizeof(char));
if (!enif_get_string(env, argv[0], args->path, len, ERL_NIF_LATIN1)) goto error1;
if (!enif_get_list_length(env, argv[1], &len)) goto error1;
len += 1;
args->body = (char *)enif_alloc(len * sizeof(char));
if (!enif_get_string(env, argv[1], args->body, len, ERL_NIF_LATIN1)) goto error2;
if (!enif_get_list_length(env, argv[2], &len)) goto error2;
len += 1;
args->content_type = (char *)enif_alloc(len * sizeof(char));
if (!enif_get_string(env, argv[2], args->content_type, len, ERL_NIF_LATIN1)) goto error3;
if (!enif_get_int(env, argv[3], (int*)&args->method)) goto error3;
if (!enif_get_int(env, argv[4], (int*)&args->type)) goto error3;
return (void*)args;
error3:
enif_free(args->content_type);
error2:
enif_free(args->path);
error1:
enif_free(args->body);
error0:
return NULL;
}
示例2: has_same_mount_point
/* @brief Checks whether two *paths* are on the same mount point; they don't
* have to refer to existing or accessible files/directories. */
static int has_same_mount_point(const efile_path_t *path_a, const efile_path_t *path_b) {
WCHAR *mount_a, *mount_b;
int result = 0;
mount_a = enif_alloc(path_a->size);
mount_b = enif_alloc(path_b->size);
if(mount_a != NULL && mount_b != NULL) {
int length_a, length_b;
length_a = PATH_LENGTH(path_a);
length_b = PATH_LENGTH(path_b);
if(GetVolumePathNameW((WCHAR*)path_a->data, mount_a, length_a)) {
ASSERT(wcslen(mount_a) <= length_a);
if(GetVolumePathNameW((WCHAR*)path_b->data, mount_b, length_b)) {
ASSERT(wcslen(mount_b) <= length_b);
result = !_wcsicmp(mount_a, mount_b);
}
}
}
if(mount_b != NULL) {
enif_free(mount_b);
}
if(mount_a != NULL) {
enif_free(mount_a);
}
return result;
}
示例3: load
static int
load(ErlNifEnv *env, void **priv, ERL_NIF_TERM load_info)
{
PRIVDATA *data = NULL;
int nelem = 0;
if (!enif_get_int(env, load_info, &nelem) || (nelem < 1))
return (-1);
data = (PRIVDATA *)enif_alloc(sizeof(PRIVDATA));
if (data == NULL)
return (-1);
data->data = (PRIVVAL *)enif_alloc(sizeof(PRIVVAL) * nelem);
if (data->data == NULL) {
enif_free(data);
return (-1);
}
(void)memset(data->data, 0, sizeof(PRIVVAL) * nelem);
data->lock = enif_mutex_create("wat_lock");
if (data->lock == NULL)
return (-1);
NELEM(data) = nelem;
*priv = data;
return (0);
}
示例4: on_load
int
on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{
UErrorCode status = U_ZERO_ERROR;
int i, j;
couch_ejson_global_ctx_t *globalCtx;
globalCtx = (couch_ejson_global_ctx_t *) enif_alloc(sizeof(couch_ejson_global_ctx_t));
if (globalCtx == NULL) {
return 1;
}
if (!enif_get_int(env, info, &globalCtx->numCollators)) {
return 2;
}
if (globalCtx->numCollators < 1) {
return 3;
}
globalCtx->collMutex = enif_mutex_create("coll_mutex");
if (globalCtx->collMutex == NULL) {
return 4;
}
globalCtx->collators = (UCollator **) enif_alloc(sizeof(UCollator *) * globalCtx->numCollators);
if (globalCtx->collators == NULL) {
enif_mutex_destroy(globalCtx->collMutex);
return 5;
}
for (i = 0; i < globalCtx->numCollators; i++) {
globalCtx->collators[i] = ucol_open("", &status);
if (U_FAILURE(status)) {
for (j = 0; j < i; j++) {
ucol_close(globalCtx->collators[j]);
}
enif_free(globalCtx->collators);
enif_mutex_destroy(globalCtx->collMutex);
return 5;
}
}
globalCtx->collStackTop = 0;
*priv = globalCtx;
ATOM_TRUE = enif_make_atom(env, "true");
ATOM_FALSE = enif_make_atom(env, "false");
ATOM_NULL = enif_make_atom(env, "null");
ATOM_ERROR = enif_make_atom(env, "error");
return 0;
}
示例5: enif_alloc
static struct buf *init_buf(ErlNifEnv* env, int buf_len)
{
struct buf *rbuf = enif_alloc(sizeof(struct buf));
rbuf->limit = buf_len;
rbuf->len = 0;
rbuf->b = enif_alloc(rbuf->limit);
return rbuf;
}
示例6: get_engine_load_cmd_list
static int get_engine_load_cmd_list(ErlNifEnv* env, const ERL_NIF_TERM term, char **cmds, int i)
{
ERL_NIF_TERM head, tail;
const ERL_NIF_TERM *tmp_tuple;
ErlNifBinary tmpbin;
int arity;
char *tuple1 = NULL, *tuple2 = NULL;
if (enif_is_empty_list(env, term)) {
cmds[i] = NULL;
return 0;
}
if (!enif_get_list_cell(env, term, &head, &tail))
goto err;
if (!enif_get_tuple(env, head, &arity, &tmp_tuple))
goto err;
if (arity != 2)
goto err;
if (!enif_inspect_binary(env, tmp_tuple[0], &tmpbin))
goto err;
if ((tuple1 = enif_alloc(tmpbin.size + 1)) == NULL)
goto err;
(void) memcpy(tuple1, tmpbin.data, tmpbin.size);
tuple1[tmpbin.size] = '\0';
cmds[i] = tuple1;
i++;
if (!enif_inspect_binary(env, tmp_tuple[1], &tmpbin))
goto err;
if (tmpbin.size == 0) {
cmds[i] = NULL;
} else {
if ((tuple2 = enif_alloc(tmpbin.size + 1)) == NULL)
goto err;
(void) memcpy(tuple2, tmpbin.data, tmpbin.size);
tuple2[tmpbin.size] = '\0';
cmds[i] = tuple2;
}
i++;
return get_engine_load_cmd_list(env, tail, cmds, i);
err:
if (tuple1 != NULL) {
i--;
enif_free(tuple1);
}
cmds[i] = NULL;
return -1;
}
示例7: handle_start
static int handle_start(void *ctx, int array)
{
state_t *st = (state_t *)ctx;
container_t *c = enif_alloc(st->env, sizeof(container_t));
/* link and initialize container struct */
c->next = st->c;
st->c = c;
c->count = 0;
c->arraysz = 32; /* initial term buffer size */
c->array = enif_alloc(st->env, c->arraysz);
return 1;
}
示例8: nif_create_main_thread
void* nif_create_main_thread(char* name)
{
nif_thread_state* st = (nif_thread_state*)enif_alloc(sizeof(nif_thread_state));
st->lock = enif_mutex_create("esdl2_lock");
st->cond = enif_cond_create("esdl2_cond");
st->mailbox = (nif_thread_mailbox*)enif_alloc(sizeof(nif_thread_mailbox));
TAILQ_INIT(st->mailbox);
enif_thread_create(name, &(st->tid), nif_main_thread, st, NULL);
return (void*)st;
}
示例9: queue_push
int
queue_push(queue *queue, void *item) {
qitem * entry = (qitem *) enif_alloc(sizeof(qitem));
if (entry == NULL)
return 0;
entry->data = item;
entry->next = NULL;
enif_mutex_lock(queue->lock);
assert(queue->length >= 0 && "Invalid queue size at push");
if (queue->tail != NULL) {
queue->tail->next = entry;
}
queue->tail = entry;
if (queue->head == NULL) {
queue->head = queue->tail;
}
queue->length += 1;
enif_cond_signal(queue->cond);
enif_mutex_unlock(queue->lock);
return 1;
}
示例10: queue_new
queue *
queue_new() {
queue *ret;
ret = (queue *) enif_alloc(sizeof(queue));
if (ret == NULL)
goto error;
ret->lock = NULL;
ret->cond = NULL;
ret->head = NULL;
ret->tail = NULL;
ret->message = NULL;
ret->length = 0;
ret->lock = enif_mutex_create("queue_lock");
if (ret->lock == NULL)
goto error;
ret->cond = enif_cond_create("queue_cond");
if (ret->cond == NULL)
goto error;
return ret;
error:
if (ret->lock != NULL)
enif_mutex_destroy(ret->lock);
if (ret->cond != NULL)
enif_cond_destroy(ret->cond);
if (ret != NULL)
enif_free(ret);
return NULL;
}
示例11: on_load
static int
on_load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info)
{
gmperl_privdata_t *priv;
priv = (gmperl_privdata_t *)enif_alloc(sizeof(gmperl_privdata_t));
priv->gmperl_mpz_rt = NULL;
priv->gmperl_mpq_rt = NULL;
priv->gmperl_mpf_rt = NULL;
*priv_data = priv;
/* Use Erlang NIF memory allocation/deallocation */
mp_set_memory_functions(gmperl_allocate, gmperl_reallocate, gmperl_free);
/* Create resource types */
ErlNifResourceFlags flags = (ErlNifResourceFlags)(ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER);
priv->gmperl_mpz_rt = enif_open_resource_type(env, "gmperl_nifs", "gmperl_mpz_resource", gmperl_mpz_t_dtor, flags, NULL);
priv->gmperl_mpq_rt = enif_open_resource_type(env, "gmperl_nifs", "gmperl_mpq_resource", gmperl_mpq_t_dtor, flags, NULL);
priv->gmperl_mpf_rt = enif_open_resource_type(env, "gmperl_nifs", "gmperl_mpf_resource", gmperl_mpf_t_dtor, flags, NULL);
if (!enif_make_existing_atom(env, "ok", &priv->atom_ok, ERL_NIF_LATIN1)
|| !enif_make_existing_atom(env, "true", &priv->atom_true, ERL_NIF_LATIN1)
|| !enif_make_existing_atom(env, "false", &priv->atom_false, ERL_NIF_LATIN1)) {
return -1;
}
return 0;
}
示例12: float_ref
static ERL_NIF_TERM
float_ref(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
int err;
double helper;
float *value;
value = enif_alloc(sizeof(float));
if (!value) {
goto error;
}
err = enif_get_double(env, argv[0], &helper);
if (!err) {
enif_free(value);
goto error;
}
*value = (float)helper;
return enif_make_tuple2(env,
nifty_make_ptr(env, (ptr_t)value),
enif_make_string(env, "nifty.float *", ERL_NIF_LATIN1));
error:
return enif_make_badarg(env);
}
示例13: queue_push
int
queue_push(queue_t* queue, ErlNifPid* pid)
{
qitem_t* item = (qitem_t*) enif_alloc(sizeof(qitem_t));
if(item == NULL) return 0;
item->pid = pid;
item->next = NULL;
enif_mutex_lock(queue->lock);
if(queue->tail != NULL)
{
queue->tail->next = item;
}
queue->tail = item;
if(queue->head == NULL)
{
queue->head = queue->tail;
}
enif_cond_signal(queue->cond);
enif_mutex_unlock(queue->lock);
return 1;
}
示例14: f_get_geometry
/*
{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(Feature).
*/
static ERL_NIF_TERM
f_get_geometry(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");
}
OGRGeometryH geom_clone = OGR_G_Clone(geom);
EnvGeometry_t **geometry = \
enif_alloc_resource(OGR_G_RESOURCE, sizeof(EnvGeometry_t*));
*geometry = (EnvGeometry_t*) enif_alloc(sizeof(EnvGeometry_t));
(**geometry).env = NULL;
(**geometry).obj = geom_clone;
eterm = enif_make_resource(env, geometry);
enif_release_resource(geometry);
return enif_make_tuple2(env, enif_make_atom(env, "ok"), eterm);
}
示例15: new_parser
static ERL_NIF_TERM new_parser(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
XML_Parser parser;
expat_parser *parser_data = (expat_parser *)enif_alloc(sizeof(expat_parser));
ERL_NIF_TERM parser_resource;
// this is the variant from esl/exml:208d5e17e547b303b310627e3e525b57b2843e83
/*parser = XML_ParserCreate_MM("UTF-8", &ms, "\n");*/
// this makes the tests pass, but according to expat documentation it turns namespace support off
/*parser = XML_ParserCreate_MM("UTF-8", &ms, NULL);*/
// this is a try to preserve namespace support but make the tests pass;
// this is the character defined as a macro in xmlwf sources for namespace-enabled parser
/*XML_Char namespaceSeparator = '\001';*/
/*parser = XML_ParserCreate_MM("UTF-8", &ms, &namespaceSeparator);*/
parser = XML_ParserCreate_MM("UTF-8", &ms, ":");
parser_data->env = env;
parser_data->xmlns = (ERL_NIF_TERM)NULL;
init_parser(parser, parser_data);
XML_Parser *xml_parser = (XML_Parser *)enif_alloc_resource(PARSER_POINTER, sizeof(XML_Parser));
*xml_parser = parser;
parser_resource = enif_make_resource(env, (void *)xml_parser);
enif_release_resource(xml_parser);
return enif_make_tuple(env, 2, OK, parser_resource);
};