Permalink
Browse files

Merge pull request #2 from joecaswell/master

Use native uuid_create on BSD systems
  • Loading branch information...
2 parents e58cff8 + ab55648 commit b3c5c2a52bb3f510d168b32e64f6fbc6a3c6a0e6 @kellymclaughlin committed Aug 24, 2012
Showing with 86 additions and 19 deletions.
  1. +59 −0 c_src/druid.c
  2. BIN rebar
  3. +0 −18 rebar.config
  4. +25 −0 rebar.config.script
  5. +2 −1 src/druuid.erl
View
59 c_src/druid.c
@@ -21,7 +21,19 @@
*********************************************************************/
#include "erl_nif.h"
+
+#if (defined(__unix__) || defined(unix)) && !defined(USG)
+#include <sys/param.h>
+#endif
+
+#if (defined(BSD) && (BSD >= 199306))
+#include <uuid.h>
+#include <string.h>
+#define uuid_rc_t uint32_t
+#define USE_BSD_UUID
+#else
#include "uuid.h"
+#endif
static ErlNifResourceType* druuid_RESOURCE;
@@ -30,6 +42,7 @@ static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
+
static ERL_NIF_TERM error_tuple(ErlNifEnv* env,
ERL_NIF_TERM reason,
uuid_rc_t rc);
@@ -47,7 +60,52 @@ static ErlNifFunc nif_funcs[] =
{"v4", 0, druuid_uuid_v4},
{"v4_str", 0, druuid_uuid_v4_str}
};
+#ifdef USE_BSD_UUID
+static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
+ const ERL_NIF_TERM argv[])
+{
+ struct uuid uuid;
+ uint32_t status;
+
+ uuid_create(&uuid, &status);
+ if (status != uuid_s_ok) {
+ return error_tuple(env, ATOM_UUID_CREATE_FAILED, status);
+ }
+
+ ERL_NIF_TERM value_bin;
+ unsigned char* value = enif_make_new_binary(env, sizeof(uuid), &value_bin);
+ memcpy(value, &uuid, sizeof(uuid));
+
+ return value_bin;
+
+}
+
+static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
+ const ERL_NIF_TERM argv[])
+{
+
+ uuid_t uuid;
+ uint32_t status;
+ char *str;
+
+ uuid_create(&uuid, &status);
+ if (status != uuid_s_ok) {
+ return error_tuple(env, ATOM_UUID_CREATE_FAILED, status);
+ }
+ uuid_to_string(&uuid, &str, &status);
+ if (status != uuid_s_ok) {
+ return error_tuple(env, ATOM_UUID_EXPORT_FAILED, status);
+ }
+
+ ERL_NIF_TERM value_bin;
+ unsigned char* value = enif_make_new_binary(env, strlen(str), &value_bin);
+ memcpy(value, str, strlen(str));
+
+ return value_bin;
+
+}
+#else
static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[])
{
@@ -121,6 +179,7 @@ static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
return value_bin;
}
+#endif
static void druuid_resource_cleanup(ErlNifEnv* env, void* arg)
{
View
BIN rebar
Binary file not shown.
View
18 rebar.config
@@ -1,18 +0,0 @@
-{so_name, "druuid.so"}.
-
-{port_sources, ["c_src/*.c"]}.
-
-{port_envs, [
- %% Make sure to set -fPIC when compiling ossp-uuid
- {"CFLAGS", "$CFLAGS -fPIC"},
- {"DRV_CFLAGS", "$DRV_CFLAGS -Werror -I c_src/uuid-1.6.2"},
- {"DRV_LDFLAGS", "$DRV_LDFLAGS c_src/uuid-1.6.2/.libs/libuuid.a"}
- ]}.
-
-%% {pre_hooks, [{compile, "make -C c_src/uuid-1.6.2"},
-%% {clean, "make -C c_src/uuid-1.6.2 clean"}]}.
-
-{pre_hooks, [{compile, "c_src/build_deps.sh"}]}.
-
-{post_hooks, [{clean, "c_src/build_deps.sh clean"}]}.
-
View
25 rebar.config.script
@@ -0,0 +1,25 @@
+case rebar_utils:is_arch("bsd") of
+ true ->
+ [
+ {port_specs, [
+ {"priv/druuid.so", ["c_src/*.c"]}
+ ]},
+ {port_env, [
+ {"CFLAGS", "$CFLAGS -fPIC"},
+ {"DRV_CFLAGS", "$DRV_CFLAGS -Werror"}
+ ]}
+ ];
+_ ->
+ [
+ {port_specs, [
+ {"priv/druuid.so", ["c_src/*.c"]}
+ ]},
+ {port_env, [
+ {"CFLAGS", "$CFLAGS -fPIC"},
+ {"DRV_CFLAGS", "$DRV_CFLAGS -Werror -I c_src/uuid-1.6.2"},
+ {"DRV_LDFLAGS", "$DRV_LDFLAGS c_src/uuid-1.6.2/.libs/libuuid.a"}
+ ]},
+ {pre_hooks, [{compile, "c_src/build_deps.sh"}]},
+ {post_hooks, [{clean, "c_src/build_deps.sh clean"}]}
+]
+end.
View
3 src/druuid.erl
@@ -72,6 +72,7 @@ v4_str() ->
-ifdef(TEST).
basic_test() ->
- ?assertEqual(36, size(v4())).
+ ?assertEqual(16, size(v4())),
+ ?assertEqual(36, size(v4_str())).
-endif.

0 comments on commit b3c5c2a

Please sign in to comment.