-
Notifications
You must be signed in to change notification settings - Fork 2
/
sum_nif.c
73 lines (57 loc) · 1.7 KB
/
sum_nif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include "erl_nif.h"
#define MAXN 10000
extern int sum(int a, int b);
extern int string_length(char * string);
static ERL_NIF_TERM sum_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
int x, y, ret;
if (!enif_get_int(env, argv[0], &x)) {
return enif_make_badarg(env);
}
if (!enif_get_int(env, argv[1], &y)) {
return enif_make_badarg(env);
}
ret = sum(x,y);
return enif_make_int(env, ret);
}
static ERL_NIF_TERM factorial_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
int x,ret;
if (!enif_get_int(env, argv[0], &x)) {
return enif_make_badarg(env);
}
ret = factorial(x);
return enif_make_int(env, ret);
}
static ERL_NIF_TERM string_length_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
int ret;
char * string;
string = malloc(MAXN*sizeof(char));
if (!enif_get_string(env, argv[0], string, MAXN, ERL_NIF_LATIN1)) {
return enif_make_badarg(env);
}
ret = string_length(string);
free(string);
return enif_make_int(env, ret);
}
static ERL_NIF_TERM binary_length_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary bin;
enif_inspect_binary(env, argv[0], &bin);
return enif_make_int(env, bin.size);
}
static ERL_NIF_TERM to_string_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary bin;
enif_inspect_binary(env, argv[0], &bin);
return enif_make_string(env, bin.data, ERL_NIF_LATIN1);
}
static ErlNifFunc nif_funcs[] = {
{"sum", 2, sum_nif},
{"factorial", 1, factorial_nif},
{"binary_length", 1, binary_length_nif},
{"to_string", 1, to_string_nif},
};
ERL_NIF_INIT(sum, nif_funcs, NULL, NULL, NULL, NULL);