Ejemplo n.º 1
0
static ERL_NIF_TERM bool_to_term(ErlNifEnv *env, int value)
{
    return enif_make_atom(env, value?"true":"false");
}
Ejemplo n.º 2
0
static ERL_NIF_TERM alloc_resource_without_make(ErlNifEnv *env, int UNUSED, const ERL_NIF_TERM *UNUSED)
{
    enif_alloc(42);             /* This won't be collected. */
    return enif_make_atom(env, "ok");
}
Ejemplo n.º 3
0
static ERL_NIF_TERM
mmap_ready(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  return enif_make_atom(env, "true");
}
Ejemplo n.º 4
0
static ERL_NIF_TERM gdal_nif_get_meta(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    gdal_img_handle* handle;
    if (enif_get_resource(env, argv[0], gdal_img_RESOURCE, (void**)&handle)) {
        GDALDatasetH in_ds = handle->in_ds;
        if (in_ds != NULL) {
            ERL_NIF_TERM terms[8];
            int idx = 0;

            terms[idx++] = enif_make_tuple2(env,
                                enif_make_atom(env, "description"),
                                enif_make_string(env, GDALGetDescription(in_ds), ERL_NIF_LATIN1));

            GDALDriverH hDriver = GDALGetDatasetDriver(in_ds);
            char buf[256];
            sprintf(buf, "%s/%s", 
                    GDALGetDriverShortName(hDriver), GDALGetDriverLongName(hDriver));
            terms[idx++] = enif_make_tuple2(env, 
                                enif_make_atom(env, "driver"),
                                enif_make_string(env, buf, ERL_NIF_LATIN1));

            terms[idx++] = enif_make_tuple2(env, 
                                enif_make_atom(env, "rasterSize"), 
                                enif_make_tuple2(env, 
                                    enif_make_int(env, GDALGetRasterXSize(in_ds)), 
                                    enif_make_int(env, GDALGetRasterYSize(in_ds))));

            terms[idx++] = enif_make_tuple2(env, 
                                    enif_make_atom(env, "rasterCount"),
                                    enif_make_int(env, GDALGetRasterCount(in_ds)));

            double adfGeoTransform[6];
            if( GDALGetGeoTransform( in_ds, adfGeoTransform ) == CE_None ) {
                terms[idx++] = enif_make_tuple2(env,
                                    enif_make_atom(env, "origin"),
                                    enif_make_tuple2(env,
                                        enif_make_double(env, adfGeoTransform[0]), 
                                        enif_make_double(env, adfGeoTransform[3])));

                terms[idx++] = enif_make_tuple2(env,
                                    enif_make_atom(env, "pixelSize"), 
                                    enif_make_tuple2(env, 
                                        enif_make_double(env, adfGeoTransform[1]), 
                                        enif_make_double(env, adfGeoTransform[5])));
            }

            if (GDALGetProjectionRef(in_ds) != NULL) {
                terms[idx++] = enif_make_tuple2(env,
                                    enif_make_atom(env, "projection"), 
                                    enif_make_string(env, 
                                        GDALGetProjectionRef(in_ds), ERL_NIF_LATIN1));
}

            char** fileList = GDALGetFileList(in_ds);
            if (fileList != NULL) {
                ERL_NIF_TERM fileTerms[16];
                int fileIdx = 0;
                char** files = fileList;

                do {
                    fileTerms[ fileIdx++ ] = enif_make_string(env, *files, ERL_NIF_LATIN1);
                } while(*(++files)) ;
                CSLDestroy(fileList);

                terms[idx++] = enif_make_tuple2(env,
                                    enif_make_atom(env, "fileList"),
                                    enif_make_list_from_array(env, fileTerms, fileIdx));
            }

            return enif_make_list_from_array(env, terms, idx);
        }
        else {
            return ATOM_NOT_OPEN;
        }
    }
    else {
        return enif_make_badarg(env);
    }
}
Ejemplo n.º 5
0
static void init_atoms(ErlNifEnv* env)
{
    a_ok                         = enif_make_atom(env, "ok");
    a_error                      = enif_make_atom(env, "error");
    a_match                      = enif_make_atom(env, "match");
    a_nomatch                    = enif_make_atom(env, "nomatch");
    a_capture                    = enif_make_atom(env, "capture");
    a_global                     = enif_make_atom(env, "global");
    a_offset                     = enif_make_atom(env, "offset");
    a_all                        = enif_make_atom(env, "all");
    a_all_but_first              = enif_make_atom(env, "all_but_first");
    a_first                      = enif_make_atom(env, "first");
    a_none                       = enif_make_atom(env, "none");
    a_index                      = enif_make_atom(env, "index");
    a_binary                     = enif_make_atom(env, "binary");
    a_caseless                   = enif_make_atom(env, "caseless");
    a_max_mem                    = enif_make_atom(env, "max_mem");
    a_err_alloc_binary           = enif_make_atom(env, "alloc_binary");
    a_err_enif_alloc             = enif_make_atom(env, "enif_alloc");
    a_err_get_atom               = enif_make_atom(env, "enif_get_atom");
    a_err_get_string             = enif_make_atom(env, "enif_get_string");
    a_re2_NoError                = enif_make_atom(env, "no_error");
    a_re2_ErrorInternal          = enif_make_atom(env, "internal");
    a_re2_ErrorBadEscape         = enif_make_atom(env, "bad_escape");
    a_re2_ErrorBadCharClass      = enif_make_atom(env, "bad_char_class");
    a_re2_ErrorBadCharRange      = enif_make_atom(env, "bad_char_range");
    a_re2_ErrorMissingBracket    = enif_make_atom(env, "missing_bracket");
    a_re2_ErrorMissingParen      = enif_make_atom(env, "missing_paren");
    a_re2_ErrorTrailingBackslash = enif_make_atom(env, "trailing_backslash");
    a_re2_ErrorRepeatArgument    = enif_make_atom(env, "repeat_argument");
    a_re2_ErrorRepeatSize        = enif_make_atom(env, "repeat_size");
    a_re2_ErrorRepeatOp          = enif_make_atom(env, "repeat_op");
    a_re2_ErrorBadPerlOp         = enif_make_atom(env, "bad_perl_op");
    a_re2_ErrorBadUTF8           = enif_make_atom(env, "bad_utf8");
    a_re2_ErrorBadNamedCapture   = enif_make_atom(env, "bad_named_capture");
    a_re2_ErrorPatternTooLarge   = enif_make_atom(env, "pattern_too_large");
}
Ejemplo n.º 6
0
static ERL_NIF_TERM
nif_scheduler_launchTasks(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    unsigned int length ;
    ErlNifBinary offerId_binary;
    ErlNifBinary filters_binary;

    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], &offerId_binary)) 
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "offer_id"));
    }
    
    if(!enif_is_list(env, argv[1])) 
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "task_info_array"));
    };

    if(!enif_get_list_length(env, argv[1], &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_info_array"));

    }
    
    ErlNifBinary task_info_binary_arr[length];

    if(!inspect_array_of_binary_objects(env, argv[1], &task_info_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_info_array"));
    }   

    if (!enif_inspect_binary(env, argv[2], &filters_binary)) 
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "filters"));
    }

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

    SchedulerDriverStatus status = scheduler_launchTasks(state->scheduler_state, &offerId_binary, &binaryNifArrayHolder, &filters_binary);
    return get_return_value_from_status(env, status);
}
Ejemplo n.º 7
0
static int load(ErlNifEnv *env, void** priv_data, ERL_NIF_TERM prim_file_pid)
{
    ErlNifResourceTypeInit callbacks;

    if(!enif_get_local_pid(env, prim_file_pid, &erts_prim_file_pid)) {
        ASSERT(!"bad pid passed to prim_file_nif");
    }

    am_close = enif_make_atom(env, "close");

    am_ok = enif_make_atom(env, "ok");
    am_error = enif_make_atom(env, "error");
    am_continue = enif_make_atom(env, "continue");

    am_read = enif_make_atom(env, "read");
    am_write = enif_make_atom(env, "write");
    am_exclusive = enif_make_atom(env, "exclusive");
    am_append = enif_make_atom(env, "append");
    am_sync = enif_make_atom(env, "sync");
    am_skip_type_check = enif_make_atom(env, "skip_type_check");

    am_read_write = enif_make_atom(env, "read_write");
    am_none = enif_make_atom(env, "none");

    am_normal = enif_make_atom(env, "normal");
    am_random = enif_make_atom(env, "random");
    am_sequential = enif_make_atom(env, "sequential");
    am_will_need = enif_make_atom(env, "will_need");
    am_dont_need = enif_make_atom(env, "dont_need");
    am_no_reuse = enif_make_atom(env, "no_reuse");

    am_device = enif_make_atom(env, "device");
    am_directory = enif_make_atom(env, "directory");
    am_regular = enif_make_atom(env, "regular");
    am_symlink = enif_make_atom(env, "symlink");
    am_other = enif_make_atom(env, "other");

    am_file_info = enif_make_atom(env, "file_info");

    am_bof = enif_make_atom(env, "bof");
    am_cur = enif_make_atom(env, "cur");
    am_eof = enif_make_atom(env, "eof");

    callbacks.down = owner_death_callback;
    callbacks.dtor = NULL;
    callbacks.stop = NULL;

    efile_resource_type = enif_open_resource_type_x(env, "efile", &callbacks,
        ERL_NIF_RT_CREATE, NULL);

    *priv_data = NULL;

    return 0;
}
Ejemplo n.º 8
0
static ERL_NIF_TERM type_test(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int i;
    int sint;
    unsigned uint;
    long slong;
    unsigned long ulong;
    ErlNifSInt64 sint64;
    ErlNifUInt64 uint64;
    double d;
    ERL_NIF_TERM atom, ref1, ref2;

    sint = INT_MIN;
    do {
	if (!test_int(env,sint)) {
	    goto error;
	}
	sint += ~sint / 3 + 1;
    } while (sint < 0);
    sint = INT_MAX;
    do {
	if (!test_int(env,sint)) {
	    goto error;
	}
	sint -= sint / 3 + 1;
    } while (sint >= 0);

    slong = LONG_MIN;
    do {
	if (!test_long(env,slong)) {
	    goto error;
	}
	slong += ~slong / 3 + 1;
    } while (slong < 0);
    slong = LONG_MAX;
    do {
	if (!test_long(env,slong)) {
	    goto error;
	}
	slong -= slong / 3 + 1;
    } while (slong >= 0);

    sint64 = ((ErlNifSInt64)1 << 63); /* INT64_MIN */
    do {
	if (!test_int64(env,sint64)) {
	    goto error;
	}
	sint64 += ~sint64 / 3 + 1;
    } while (sint64 < 0);
    sint64 = ((ErlNifUInt64)1 << 63) - 1; /* INT64_MAX */
    do {
	if (!test_int64(env,sint64)) {
	    goto error;
	}
	sint64 -= sint64 / 3 + 1;
    } while (sint64 >= 0);

    uint = UINT_MAX;
    for (;;) {
	if (!test_uint(env,uint)) {
	    goto error;
	}
	if (uint == 0) break;
	uint -= uint / 3 + 1;
    }
    ulong = ULONG_MAX;
    for (;;) {
	if (!test_ulong(env,ulong)) {
	    goto error;
	}
	if (ulong == 0) break;
	ulong -= ulong / 3 + 1;
    }    
    uint64 = (ErlNifUInt64)-1; /* UINT64_MAX */
    for (;;) {
	if (!test_uint64(env,uint64)) {
	    goto error;
	}
	if (uint64 == 0) break;
	uint64 -= uint64 / 3 + 1;
    }    

    if (MAX_SMALL < INT_MAX) { /* 32-bit */
	for (i=-10 ; i <= 10; i++) {
	    if (!test_int(env,MAX_SMALL+i)) {
		goto error;
	    }
	}
	for (i=-10 ; i <= 10; i++) {
	    if (!test_int(env,MIN_SMALL+i)) {
		goto error;
	    }
	}
	for (i=-10 ; i <= 10; i++) {
	    if (!test_uint(env,MAX_SMALL+i)) {
		goto error;
	    }
	}
    }
    assert((MAX_SMALL < INT_MAX) == (MIN_SMALL > INT_MIN));

    for (i=-10 ; i < 10; i++) {
	if (!test_long(env,MAX_SMALL+i) || !test_ulong(env,MAX_SMALL+i) ||
	    !test_long(env,MIN_SMALL+i) ||
	    !test_int64(env,MAX_SMALL+i) || !test_uint64(env,MAX_SMALL+i) ||
	    !test_int64(env,MIN_SMALL+i)) {
	    goto error;
	}
	if (MAX_SMALL < INT_MAX) {
	    if (!test_int(env,MAX_SMALL+i) || !test_uint(env,MAX_SMALL+i) ||
		!test_int(env,MIN_SMALL+i)) {
		goto error;
	    }
	}
    }
    for (d=3.141592e-100 ; d < 1e100 ; d *= 9.97) {
	if (!test_double(env,d) || !test_double(env,-d)) {
	    goto error;
	}	
    }

    if (!enif_make_existing_atom(env,"nif_SUITE", &atom, ERL_NIF_LATIN1)
	|| !enif_is_identical(atom,enif_make_atom(env,"nif_SUITE"))) {
	fprintf(stderr, "nif_SUITE not an atom?\r\n");
	goto error;
    }
    for (i=2; i; i--) {
	if (enif_make_existing_atom(env,"nif_SUITE_pink_unicorn", &atom, ERL_NIF_LATIN1)) {
	    fprintf(stderr, "pink unicorn exist?\r\n");
	    goto error;
	}
    }
    ref1 = enif_make_ref(env);
    ref2 = enif_make_ref(env);
    if (!enif_is_ref(env,ref1) || !enif_is_ref(env,ref2) 
	|| enif_is_identical(ref1,ref2) || enif_compare(ref1,ref2)==0) {
	fprintf(stderr, "strange refs?\r\n");
	goto error;
    }
    return enif_make_atom(env,"ok");

error:
    return enif_make_atom(env,"error");
}
Ejemplo n.º 9
0
static ERL_NIF_TERM make_term_atom(struct make_term_info* mti, int n)
{
    return enif_make_atom(mti->dst_env, "make_term_n");    
} 
Ejemplo n.º 10
0
/**
 * Pass an error to Erlang code.
 * Error as a string will be converted to an atom.
 */
ERL_NIF_TERM make_error(ErlNifEnv* env, const char* code) {
    return build_error(env,
        enif_make_atom(env, code));
}
Ejemplo n.º 11
0
static ERL_NIF_TERM nif_close(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
    closelog();
    return enif_make_atom(env, "ok");
}
Ejemplo n.º 12
0
/* Define an interface for errors. */
ERL_NIF_TERM build_error(ErlNifEnv* env, ERL_NIF_TERM body) {
    return enif_make_tuple2(env, enif_make_atom(env, "i18n_error"), body);
}
Ejemplo n.º 13
0
/**
 * Alloc atoms
 */
int i18n_atom_load(ErlNifEnv *env, void ** /*priv_data*/, 
    ERL_NIF_TERM /*load_info*/)
{
    ATOM_TRUE     = enif_make_atom(env, "true");
    ATOM_FALSE    = enif_make_atom(env, "false");

    ATOM_EQUAL    = enif_make_atom(env, "equal");
    ATOM_GREATER  = enif_make_atom(env, "greater");
    ATOM_LESS     = enif_make_atom(env, "less");

    ATOM_OK       = enif_make_atom(env, "ok");

    ATOM_COUNT    = enif_make_atom(env, "count");
    ATOM_RESOURCE = enif_make_atom(env, "resource");
    ATOM_SEARCH   = enif_make_atom(env, "search");

#if U_IS_BIG_ENDIAN
    ATOM_ENDIAN = enif_make_atom(env, "big");
#else
    ATOM_ENDIAN = enif_make_atom(env, "little");
#endif

    ATOM_ICU_VERSION = enif_make_atom(env, U_ICU_VERSION);
    ATOM_UNICODE_VERSION = enif_make_atom(env, U_UNICODE_VERSION);

    res_error_term = make_error(env, "resource_error");

    return 0;
}
Ejemplo n.º 14
0
/* the async job thread that handles opening/closing of doors */
void *
job_thread(void *arg)
{
	struct zdoor_handle *zhandle;
	int cont = 1;
	int res;

	/* first init the handle */
	zhandle = zdoor_handle_init();

	enif_mutex_lock(gbl.jlock);

	while (cont) {
		struct job *j;
		while (!gbl.jlist)
			enif_cond_wait(gbl.jcond, gbl.jlock);

		j = gbl.jlist;
		while (j) {
			gbl.jlist = j->next;
			enif_mutex_unlock(gbl.jlock);

			if (j->action == ACT_OPEN) {
				enif_rwlock_rwlock(gbl.dlock);
				j->door->next = NULL;
				if (gbl.dlist != NULL)
					j->door->next = gbl.dlist;
				gbl.dlist = j->door;
				enif_rwlock_rwunlock(gbl.dlock);

				res = zdoor_open(zhandle, j->door->zonename, j->door->service, j->door, zdoor_cb);

				ErlNifEnv *env = enif_alloc_env();
				ERL_NIF_TERM ret = enif_make_atom(env, "ok");
				switch (res) {
					case ZDOOR_ERROR:
						ret = enif_make_atom(env, "error");
						break;
					case ZDOOR_NOT_GLOBAL_ZONE:
						ret = enif_make_atom(env, "not_global");
						break;
					case ZDOOR_ZONE_NOT_RUNNING:
						ret = enif_make_atom(env, "not_running");
						break;
					case ZDOOR_ZONE_FORBIDDEN:
						ret = enif_make_atom(env, "eperm");
						break;
					case ZDOOR_ARGS_ERROR:
						ret = enif_make_atom(env, "badarg");
						break;
					case ZDOOR_OUT_OF_MEMORY:
						ret = enif_make_atom(env, "enomem");
						break;
				}
				enif_send(NULL, &j->owner, env,
					enif_make_tuple3(env,
						enif_make_atom(env, "zdoor_job"),
						enif_make_atom(env, "open"),
						ret));
				enif_free_env(env);
			} else if (j->action == ACT_CLOSE) {
				enif_rwlock_rwlock(gbl.dlock);
				enif_rwlock_rwlock(j->door->rlock);

				if (j->door->rlist) {
					enif_rwlock_rwunlock(j->door->rlock);
					enif_rwlock_rwunlock(gbl.dlock);

					ErlNifEnv *env = enif_alloc_env();
					enif_send(NULL, &j->owner, env,
						enif_make_tuple3(env,
							enif_make_atom(env, "zdoor_job"),
							enif_make_atom(env, "close"),
							enif_make_atom(env, "busy")));
					enif_free_env(env);
				} else {
					struct door *d = gbl.dlist;
					if (d == j->door) {
						gbl.dlist = j->door->next;
					} else {
						for (; d; d = d->next) {
							if (d->next == j->door) break;
						}
						if (d)
							d->next = j->door->next;
					}
					enif_rwlock_rwunlock(gbl.dlock);

					zdoor_close(zhandle, j->door->zonename, j->door->service);
					door_free(j->door);

					ErlNifEnv *env = enif_alloc_env();
					enif_send(NULL, &j->owner, env,
						enif_make_tuple3(env,
							enif_make_atom(env, "zdoor_job"),
							enif_make_atom(env, "close"),
							enif_make_atom(env, "ok")));
					enif_free_env(env);
				}
			} else if (j->action == ACT_QUIT) {
				cont = 0;
			}

			enif_free(j);

			enif_mutex_lock(gbl.jlock);
			j = gbl.jlist;
		}
	}

	enif_mutex_unlock(gbl.jlock);

	zdoor_handle_destroy(zhandle);

	return NULL;
}
Ejemplo n.º 15
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);
}
Ejemplo n.º 16
0
static ERL_NIF_TERM
mpegts_decode(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  ErlNifBinary bin;
  Mpegts *ts;
  int eof = 0;
  if(argc != 2) return enif_make_badarg(env);

  char eof_str[10];


  if(!enif_inspect_binary(env, argv[0], &bin)) {
    if(!enif_get_atom(env, argv[0], eof_str, sizeof(eof_str), ERL_NIF_LATIN1)) return enif_make_badarg(env);
    if(!strcmp(eof_str, "eof")) eof = 1;
  }

  if(eof) return enif_make_tuple3(env, enif_make_atom(env, "ok"), argv[1], enif_make_list(env, 0));


  if(!enif_get_resource(env, argv[1], MpegtsResource, (void **)&ts)) {
    return enif_make_badarg(env); 
  }


  // fprintf(stderr, "Decode %d bytes, %d in buffer, %d going to leave \r\n", (int)bin.size, ts->last_ts_size, (bin.size - ts->last_ts_size) % 188);

  uint8_t *pkt = bin.data;
  uint32_t size = bin.size;
  if(ts->last_ts_size > 0) {
    if(ts->last_ts_size + size < TS_BUFFERED) {
      memcpy(ts->last_ts + ts->last_ts_size, pkt, size);
      ts->last_ts_size += bin.size;
      size = 0;
    } else {
      uint32_t size1 = TS_BUFFERED - ts->last_ts_size;
      memcpy(ts->last_ts + ts->last_ts_size, pkt, size1);
      pkt += size1;
      size -= size1;
      ts->last_ts_size += size1;
      size1 = 0;
      while(size1 <= ts->last_ts_size - 188) {
        handle_ts(ts, ts->last_ts+size1);
        size1 += 188;
      }
      ts->last_ts_size = 0;
    }
  }


  while(size >= TS_BUFFERED) {
    while(size >= TS_BUFFERED && (pkt[0] != 0x47 || pkt[188] != 0x47 || pkt[2*188] != 0x47)) {
      pkt++;
      size--;
    }
    if(size >= TS_BUFFERED) {
      handle_ts(ts, pkt);
      pkt+=188;
      size-=188;
    }
  }

  if(size > 0) {
    assert(size <= TS_BUFFERED);
    ts->last_ts_size = size;
    memcpy(ts->last_ts, pkt, ts->last_ts_size);
  }
  return enif_make_tuple3(env, enif_make_atom(env, "ok"), argv[1], enif_make_list(env, 0));
}
Ejemplo n.º 17
0
static ERL_NIF_TERM
nif_scheduler_requestResources(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, "request_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, "request_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, "request_array"));
    }

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

    SchedulerDriverStatus status =  scheduler_requestResources( state->scheduler_state, &binaryNifArrayHolder);
    return get_return_value_from_status(env, status);
}
Ejemplo n.º 18
0
// takes the first term off of the lua stack and return it as an
// erlang term in the state 'env'.
static int terminator_toerl_core(lua_State* lua, ERL_NIF_TERM *result, ErlNifEnv* env, ErlNifResourceType* resource_type)
{
	const int top = lua_gettop(lua);

	switch(lua_type(lua, top))
	{
		case LUA_TNIL:
		{
			*result = enif_make_atom(env, "nil");
			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TNUMBER:
		{
			const lua_Number number = luaL_checknumber(lua, top);

			if(is_int64(number))
			{
				*result = enif_make_int64(env, (int64_t)number);
			}
			else
			{
				*result = enif_make_double(env, (double)number);
			}

			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TBOOLEAN:
		{
			const int truefalse = lua_toboolean(lua, top);
			*result = truefalse ? enif_make_atom(env, "true") : enif_make_atom(env, "false");
			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TSTRING:
		{
			// get the lua string
			size_t string_len;
			const char * lua_string = lua_tolstring (lua, top, &string_len);

			// make space in erlang for it
			ErlNifBinary binary;
			if(enif_alloc_binary(string_len, &binary))
			{
				// clean it
				memset(binary.data, 0, binary.size);

				// copy it over
				memcpy(binary.data, lua_string, string_len);

				*result = enif_make_binary(env, &binary);
			}
			else
			{
				luaL_error (lua, "could not convert lua string");
			}
			lua_pop( lua, 1);

			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TTABLE:
		{
			size_t table_size = 0;

			int is_array = lua_is_array(lua);

			// table is at the top of the stack
			lua_pushnil(lua);  // nil as the first key
			while (lua_next(lua, top) != 0) {
				++table_size;
				lua_pop(lua, 1);
			}

			// make sure we can grow the stack
			luaL_checkstack(lua, 2, ERROR_STACK_MESSAGE);

			ERL_NIF_TERM *new_table = (ERL_NIF_TERM*) node_alloc(table_size * sizeof(ERL_NIF_TERM));
			ERL_NIF_TERM *next_cell = new_table;

			// table is at the top of the stack
			lua_pushnil(lua);  // nil as the first key
			while (lua_next(lua, top) != 0) 
			{
				// uses 'key' (at index -2) and 'value' (at index -1)
				if(is_array)
				{
					// remove 'value', keeps 'key' for next iteration
					ERL_NIF_TERM value;
					terminator_toerl_core(lua, &value, env, resource_type);
					*next_cell = value;
				}
				else
				{					
					// remove 'value', keeps 'key' for next iteration
					ERL_NIF_TERM tuple_value;
					terminator_toerl_core(lua, &tuple_value, env, resource_type);

					ERL_NIF_TERM tuple_key;
					lua_pushvalue( lua, -1);
					terminator_toerl_core(lua, &tuple_key, env, resource_type);

					*next_cell = enif_make_tuple2(env, tuple_key, tuple_value);
				}				

				next_cell++;
			}

			if(NULL != new_table)
			{
				*result = enif_make_list_from_array(env, new_table, table_size);
				node_free(new_table);
			}

			lua_pop( lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TUSERDATA:
		{
			// add metatable to stack
			if(lua_getmetatable (lua, top))
			{

				// put the pid metatable onto the stack
				// compare the two metatables
				luaL_getmetatable(lua, TYPE_ERL_PID);
				if(lua_compare(lua, -1, -2, LUA_OPEQ))
				{
					const ErlNifPid* userdata = (const ErlNifPid*) lua_touserdata(lua, top);
					*result = enif_make_pid(env, userdata);
					lua_pop(lua, 3);
					assert(lua_gettop(lua) == top-1);
					return 1;
				}
				// pop the pid metatable
				lua_pop(lua, 1);			
				
				// push the ref metatable
				luaL_getmetatable(lua, TYPE_ERL_REF);
				if(lua_compare(lua, -1, -2, LUA_OPEQ))
				{
					erlref_ptr erlref = (erlref_ptr) lua_touserdata(lua, top);
					*result = enif_make_copy( env, erlref->reference );
					lua_pop(lua, 3);
					assert(lua_gettop(lua) == top-1);
					return 1;
				}
				lua_pop(lua, 1);

				// pop the ref metatable
				luaL_getmetatable(lua, TYPE_LUA_ADDRESS);
				if(lua_compare(lua, -1, -2, LUA_OPEQ))
				{
					mailbox_address_ptr address = (mailbox_address_ptr) lua_touserdata(lua, top);

					assert(NULL != address);
					state_work_ptr state_work = address->state_work;
					assert(NULL != state_work);
					void* resource;
					(*result) = state_make_resource( env, &resource, resource_type, state_work, WEAK_REF);
					assert(NULL != resource);
					lua_pop(lua, 3);

					assert(lua_gettop(lua) == top-1);
					return 1;
				}

				// pop the metatable
				lua_pop(lua, 1);
			}

			lua_pop( lua, 2);
			*result = enif_make_atom(env, "unknown_lua_userdata");
			
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TLIGHTUSERDATA:
		{
			*result = enif_make_atom(env, "lua_lightuserdata_notsupported");
			lua_pop(lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TTHREAD:
		{
			*result = enif_make_atom(env, "lua_thread_notsupported");
			lua_pop(lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
		case LUA_TFUNCTION:
		{
			*result = enif_make_atom(env, "lua_function_notsupported");
			lua_pop(lua, 1);
			assert(lua_gettop(lua) == top-1);
			return 1;
			break;
		}
	}
	assert(lua_gettop(lua) == top-1);
	return 0;
}
Ejemplo n.º 19
0
static ERL_NIF_TERM
nif_scheduler_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary frameworkInfo_binary;
    ErlNifBinary credentials_binary;
    char masterUrl[MAXBUFLEN];

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

    ErlNifPid* pid = (ErlNifPid*) enif_alloc(sizeof(ErlNifPid));

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

    if (!enif_inspect_binary(env, argv[1], &frameworkInfo_binary)) 
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "framework_info"));
    }

    if(!enif_get_string(env, argv[2], masterUrl , 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, "master_info"));
    }

    if(argc == 4 )
    {
        if(!enif_inspect_binary(env,argv[3], &credentials_binary))
        {           
            return enif_make_tuple3(env, 
                        enif_make_atom(env, "argument_error"), 
                        enif_make_atom(env, "invalid_or_corrupted_parameter"),
                        enif_make_atom(env, "credential"));
            
        }
        state->scheduler_state = scheduler_init(pid, &frameworkInfo_binary, masterUrl, 1, &credentials_binary);
    }
    else
    {
        state->scheduler_state = scheduler_init(pid, &frameworkInfo_binary, masterUrl, 0, &credentials_binary);
    }
    state->initilised = 1;
    return enif_make_atom(env, "ok");
}
Ejemplo n.º 20
0
static void init_atoms(ErlNifEnv* env)
{
    a_ok                         = enif_make_atom(env, "ok");
    a_error                      = enif_make_atom(env, "error");
}
Ejemplo n.º 21
0
static ERL_NIF_TERM posix_error_to_tuple(ErlNifEnv *env, posix_errno_t posix_errno) {
    ERL_NIF_TERM error = enif_make_atom(env, erl_errno_id(posix_errno));
    return enif_make_tuple2(env, am_error, error);
}
Ejemplo n.º 22
0
static ERL_NIF_TERM return_lcb_error(ErlNifEnv* env, int const value){
    switch (value) {
        case LIBCOUCHBASE_SUCCESS:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "success"));
        case LIBCOUCHBASE_AUTH_CONTINUE:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "auth_continue"));
        case LIBCOUCHBASE_AUTH_ERROR:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "auth_error"));
        case LIBCOUCHBASE_DELTA_BADVAL:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "delta_badval"));
        case LIBCOUCHBASE_E2BIG:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "e2big"));
        case LIBCOUCHBASE_EBUSY:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "ebusy"));
        case LIBCOUCHBASE_EINTERNAL:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "einternal"));
        case LIBCOUCHBASE_EINVAL:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "einval"));
        case LIBCOUCHBASE_ENOMEM:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "enomem"));
        case LIBCOUCHBASE_ERANGE:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "erange"));
        case LIBCOUCHBASE_ERROR:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "error"));
        case LIBCOUCHBASE_ETMPFAIL:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "etmpfail"));
        case LIBCOUCHBASE_KEY_EEXISTS:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "key_eexists"));
        case LIBCOUCHBASE_KEY_ENOENT:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "key_enoent"));
        case LIBCOUCHBASE_LIBEVENT_ERROR:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "libevent_error"));
        case LIBCOUCHBASE_NETWORK_ERROR:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "network_error"));
        case LIBCOUCHBASE_NOT_MY_VBUCKET:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "not_my_vbucket"));
        case LIBCOUCHBASE_NOT_STORED:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "not_stored"));
        case LIBCOUCHBASE_NOT_SUPPORTED:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "not_supported"));
        case LIBCOUCHBASE_UNKNOWN_COMMAND:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "unknown_command"));
        case LIBCOUCHBASE_UNKNOWN_HOST:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "unknown_host"));
        case LIBCOUCHBASE_PROTOCOL_ERROR:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "protocol_error"));
        case LIBCOUCHBASE_ETIMEDOUT:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "etimedout"));
        case LIBCOUCHBASE_CONNECT_ERROR:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "connect_error"));
        case LIBCOUCHBASE_BUCKET_ENOENT:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "bucket_enoent"));
        case LIBCOUCHBASE_CLIENT_ENOMEM:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "client_enomem"));
        default:
            return enif_make_tuple2(env, a_error, enif_make_atom(env, "unknown_error"));            
    }
}
Ejemplo n.º 23
0
    static ERL_NIF_TERM
error_tuple(ErlNifEnv *env, int errnum)
{
    return enif_make_tuple2(env, atom_error,
            enif_make_atom(env, erl_errno_id(errnum)));
}
Ejemplo n.º 24
0
ERL_NIF_TERM nif_test_from_file(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {

  char tweets_buffer[MAX_LIST_BUFFER_LEN];
  memset(tweets_buffer, 0, MAX_LIST_BUFFER_LEN);

  int out_length = 0;
  if (argc != 1) {
#ifndef SENTIMENT_DEBUG
     return enif_make_atom(env, "error"); 
#else
     return enif_make_atom(env, "error_invalid_argc"); 
#endif
  } else {
    char file_name_str[MAX_NAME_LEN];
    memset(file_name_str, 0, MAX_NAME_LEN);
    ErlNifBinary file_name;

    if (enif_inspect_binary(env, argv[0], &file_name)) {
      memcpy(file_name_str, file_name.data, file_name.size);
      file_name_str[file_name.size] = '\0';
#ifdef ERLANG_R14B02 
      enif_release_binary(&file_name);
#else
      enif_release_binary(env, &file_name);
#endif
    }   else {
#ifdef ERLANG_R14B02 
      enif_release_binary(&file_name);
#else
      enif_release_binary(env, &file_name);
#endif
#ifndef SENTIMENT_DEBUG
      return enif_make_atom(env, "error");
#else
      return enif_make_atom(env, "error_file_name");
#endif
    }

    if (GetTestTweetsFromFile(file_name_str, MAX_LIST_BUFFER_LEN, tweets_buffer, &out_length) < 0) {
#ifndef SENTIMENT_DEBUG
      return enif_make_atom(env, "error");
#else
      return enif_make_atom(env, "error_get_test_tweets_for_user");
#endif
    }
  }

  if (0 == out_length || out_length > MAX_LIST_BUFFER_LEN) {
#ifndef SENTIMENT_DEBUG
    return enif_make_atom(env, "error");
#else
    return enif_make_atom(env, "error_out_length");
#endif
  }

  ErlNifBinary tweet;
  char *tweet_start = tweets_buffer;
  char *tweet_end = strstr(tweet_start, "|");
  int ret_val = 0;
  unsigned int i = 0;
  unsigned int tweet_len = 0;
  ERL_NIF_TERM arg_array[1]; 
  ERL_NIF_TERM tuple6;
  ERL_NIF_TERM tuple6_list = enif_make_list(env, 0);
  unsigned int error_count = 0;

  while (tweet_start && tweet_end && *tweet_end != '\0') {

    tweet_end = strstr(tweet_start, "|");
    if (!tweet_end)
      break;
    *tweet_end = '\0';
    tweet_len = tweet_end - tweet_start;

    if (tweet_len <= 0 || tweet_len >= MAX_BUFFER_LEN) {
#ifndef SENTIMENT_DEBUG
      return enif_make_atom(env, "error");
#else
      return enif_make_atom(env, "error_out_length");
#endif
    }

#ifdef ERLANG_R14B02
    ret_val = enif_alloc_binary(tweet_len, &tweet);
#else
    ret_val = enif_alloc_binary(env, tweet_len, &tweet);
#endif

    if (ret_val < 0) {
#ifndef SENTIMENT_DEBUG
      return enif_make_atom(env, "error");
#else
      return enif_make_atom(env, "error_tweet_len");
#endif
    }
    for (i=0; i<tweet_len; i++) {
      tweet.data[i] = *(tweet_start + i);
    }

    arg_array[0] = enif_make_binary(env, &tweet);
    tuple6 = nif_get_sentiment(env, 1, arg_array);
    if (enif_is_atom(env, tuple6)) {
      error_count++;
    } else {
      tuple6_list = enif_make_list_cell(env, tuple6, tuple6_list);
    }
    *tweet_end = '|';
    tweet_start = tweet_end + 1;
  }
  tweet_start = NULL;
  tweet_end = NULL;

  return tuple6_list;
}
Ejemplo n.º 25
0
static ERL_NIF_TERM lib_loaded(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    return enif_make_atom(env, "true");
}
Ejemplo n.º 26
0
ERL_NIF_TERM nif_get_sentiment(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {

  if (argc != 1) {
#ifndef SENTIMENT_DEBUG
    return enif_make_atom(env, "error");
#else
    return enif_make_atom(env, "error_invalid_argc");
#endif
  }

  ErlNifBinary text;
  unsigned char text_str[MAX_BUFFER_LEN];
  unsigned int text_buffer_len = MAX_BUFFER_LEN;
  memset(text_str, 0, MAX_BUFFER_LEN);

  bool success = enif_inspect_binary(env, argv[0], &text);
  int text_len = text.size;
  if (success && text_len > 1 && text_len < MAX_BUFFER_LEN) {
    memcpy(text_str, text.data, text_len);
    text_str[text_len] = '\0';
#ifdef ERLANG_R14B02 
    enif_release_binary(&text);
#else
    enif_release_binary(env, &text);
#endif
  } else {
#ifdef ERLANG_R14B02 
    enif_release_binary(&text);
#else
    enif_release_binary(env, &text);
#endif
#ifndef SENTIMENT_DEBUG
    return enif_make_atom(env, "error");
#else
    return enif_make_atom(env, "error_invalid_tweet_len");
#endif
  }

  char safe_status[10];
  memset(safe_status, 0, 10);
  char script[4];
  memset(script, 0, 4);
  int sentiment_valence = 0;

  int ret_value = 0;
  if ((ret_value = GetSentiment((unsigned char *) text_str, text_buffer_len, text_len,
                                (char *) safe_status, 10,
                                (char *) script, 4,
                                &sentiment_valence)) < 0) {
#ifndef SENTIMENT_DEBUG
    return enif_make_atom(env, "error");
#else
    return enif_make_atom(env, "error_GetSentiment");
#endif
  }

  unsigned int len = 0;
  unsigned int i = 0;
  int ret_val = 0;
  ErlNifBinary safe_status_bin;
  ErlNifBinary script_bin;
  ERL_NIF_TERM safe_status_term; 
  ERL_NIF_TERM script_term; 

  len = strlen(safe_status);
  if (len < 4 || len > 6) {
    strcpy(safe_status, "ERROR");
    len = 5;
  }
#ifdef ERLANG_R14B02 
  ret_val = enif_alloc_binary(len, &safe_status_bin);
#else
  ret_val = enif_alloc_binary(env, len, &safe_status_bin);
#endif
  if (ret_val < 0) {
#ifndef SENTIMENT_DEBUG
    return enif_make_atom(env, "error");
#else
    return enif_make_atom(env, "error_safe_status_bin_alloc");
#endif
  }
  for (i=0; i<len; i++) {
    safe_status_bin.data[i] = *(safe_status + i);
  }
  safe_status_term = enif_make_binary(env, &safe_status_bin);

  len = strlen(script);
  if (len != 2 && len != 3) {
    strcpy(script, "00");
    len = 2;
  }
#ifdef ERLANG_R14B02 
  ret_val = enif_alloc_binary(len, &script_bin);
#else
  ret_val = enif_alloc_binary(env, len, &script_bin);
#endif
  if (ret_val < 0) {
#ifndef SENTIMENT_DEBUG
    return enif_make_atom(env, "error");
#else
    return enif_make_atom(env, "error_script_bin_alloc");
#endif
  }
  for (i=0; i<len; i++) {
    script_bin.data[i] = *(script + i);
  }
  script_term = enif_make_binary(env, &script_bin);

  ERL_NIF_TERM sentiment_term;
  sentiment_term = enif_make_int(env, sentiment_valence);

  return enif_make_tuple3(env, safe_status_term, script_term, sentiment_term);

}
Ejemplo n.º 27
0
static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
{
  atom_xmlelement = enif_make_atom(env, "xmlel");
  atom_xmlcdata = enif_make_atom(env, "xmlcdata");
  return 0;
}
Ejemplo n.º 28
0
static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
{
    atom_true = enif_make_atom(env,"true");
    atom_false = enif_make_atom(env,"false");
    atom_error = enif_make_atom(env,"error");
    atom_not_available = enif_make_atom(env,"not_available");
    atom_badarg = enif_make_atom(env,"badarg");
    atom_ok = enif_make_atom(env,"ok");

    atom_trace = enif_make_atom(env,"trace");
    atom_seq_trace = enif_make_atom(env,"seq_trace");
    atom_remove = enif_make_atom(env,"remove");
    atom_discard = enif_make_atom(env,"discard");

#ifdef HAVE_USE_LTTNG

    /* gc */

    atom_gc_minor_start = enif_make_atom(env,"gc_minor_start");
    atom_gc_minor_end   = enif_make_atom(env,"gc_minor_end");
    atom_gc_major_start = enif_make_atom(env,"gc_major_start");
    atom_gc_major_end   = enif_make_atom(env,"gc_major_end");

    atom_old_heap_block_size = enif_make_atom(env,"old_heap_block_size"); 
    atom_heap_block_size     = enif_make_atom(env,"heap_block_size");

    /* process 'proc' */

    atom_spawn = enif_make_atom(env,"spawn");
    atom_exit = enif_make_atom(env,"exit");
    atom_register = enif_make_atom(env,"register");
    atom_unregister = enif_make_atom(env,"unregister");
    atom_link = enif_make_atom(env,"link");
    atom_unlink = enif_make_atom(env,"unlink");
    atom_getting_unlinked = enif_make_atom(env,"getting_unlinked");
    atom_getting_linked = enif_make_atom(env,"getting_linked");

    /* process 'running' and 'exiting' */

    atom_in = enif_make_atom(env,"in");
    atom_out = enif_make_atom(env,"out");
    atom_in_exiting = enif_make_atom(env,"in_exiting");
    atom_out_exiting = enif_make_atom(env,"out_exiting");
    atom_out_exited = enif_make_atom(env,"out_exited");

    /* process messages 'send' and 'receive' */

    atom_send = enif_make_atom(env,"send");
    atom_receive = enif_make_atom(env,"receive");
    atom_send_to_non_existing_process = enif_make_atom(env,"send_to_non_existing_process");

    /* ports 'ports' */

    atom_open = enif_make_atom(env,"open");
    atom_closed = enif_make_atom(env,"closed");

    /* 'call' */

    atom_call = enif_make_atom(env,"call");
    atom_return_from = enif_make_atom(env,"return_from");
    atom_return_to = enif_make_atom(env,"return_to");
    atom_exception_from = enif_make_atom(env,"exception_from");
#endif

    return 0;
}
Ejemplo n.º 29
0
static ERL_NIF_TERM
mmap_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  Mmap *desc;
  ERL_NIF_TERM f;
  ErlNifBinary bin_path;
  char path[PATH_SIZE];
  int fd;
  struct stat st;
  int debug = 0;
  ERL_NIF_TERM opt, opts;
  
  
  if (!enif_inspect_binary(env, argv[0], &bin_path)) {
    return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "Should pass binary filename", ERL_NIF_LATIN1));
  }
  if (!enif_is_list(env, argv[1])) {
    return enif_make_badarg(env);
  }
  
  bzero(path, PATH_SIZE);
  strncpy(path, (const char *)bin_path.data, bin_path.size >= PATH_SIZE ? PATH_SIZE - 1 : bin_path.size);
  
  opts = argv[1];
  while(enif_get_list_cell(env, opts, &opt, &opts)) {
    if(!enif_compare(opt, enif_make_atom(env, "debug"))) {
      debug = 1;
    }
  }
  
  
  fd = open(path, O_RDONLY);
  if(fd == -1) {
    if(errno == ENOENT) return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_atom(env, "enoent"));
    return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, strerror(errno), ERL_NIF_LATIN1));
  }
  if(fstat(fd, &st)) {
    close(fd);
    return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, strerror(errno), ERL_NIF_LATIN1));
  }
  
  if (debug) fprintf(stderr, "Opened file %s %ld\r\n", path, (ssize_t)st.st_size);
  
  desc = (Mmap *)enif_alloc_resource(MmapResource, sizeof(Mmap));
  if(!desc) {
    close(fd);
    return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "Couldn't allocate mmap resource", ERL_NIF_LATIN1));
  }
  desc->fd = fd;
  desc->size = st.st_size;
  if (debug) fprintf(stderr, "Mmaping file: %p\r\n", desc);
  desc->ptr = mmap(NULL, desc->size, PROT_READ, MAP_FILE | MAP_PRIVATE, desc->fd, 0);
  close(fd);
  if(desc->ptr == MAP_FAILED) {
    enif_release_resource(desc);
    return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "Couldn't mmap", ERL_NIF_LATIN1));
  }
  if (debug) fprintf(stderr, "Mmaped file to %p\r\n", desc->ptr);
  f = enif_make_resource_binary(env, (void *)desc, desc->ptr, desc->size);
  enif_release_resource(desc);
  desc->debug = debug;
  
  return enif_make_tuple2(env, enif_make_atom(env, "ok"), f);
}
Ejemplo n.º 30
0
static ERL_NIF_TERM enum_to_atom(ErlNifEnv *env, int value, const char*table[])
{
    return enif_make_atom(env, table[value]);
}