-
Notifications
You must be signed in to change notification settings - Fork 120
Portable float NIF #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
deadtrickster
wants to merge
2
commits into
master
Choose a base branch
from
prometheus_value
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ rebar3.crashdump | |
_* | ||
.eunit | ||
*.o | ||
*.so | ||
*.beam | ||
*.plt | ||
*.swp | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
sudo: false | ||
dist: trusty | ||
language: erlang | ||
install: 'true' | ||
before_script: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Based on c_src.mk from erlang.mk by Loic Hoguin <[email protected]> | ||
|
||
CURDIR := $(shell pwd) | ||
BASEDIR := $(abspath $(CURDIR)/..) | ||
|
||
PROJECT := prometheus_nif | ||
|
||
ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).") | ||
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).") | ||
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).") | ||
|
||
C_SRC_DIR = $(CURDIR) | ||
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so | ||
|
||
# System type and C compiler/flags. | ||
|
||
UNAME_SYS := $(shell uname -s) | ||
ifeq ($(UNAME_SYS), Darwin) | ||
CC ?= cc | ||
CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes | ||
CXXFLAGS ?= -O3 -arch x86_64 -std=c++11 -finline-functions -Wall | ||
LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress | ||
else ifeq ($(UNAME_SYS), FreeBSD) | ||
CC ?= clang | ||
CXX ?= clang++ | ||
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes | ||
CXXFLAGS ?= -O3 -std=c++11 -finline-functions -Wall | ||
else ifeq ($(UNAME_SYS), Linux) | ||
CC ?= gcc | ||
CXX ?= g++ | ||
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes | ||
CXXFLAGS ?= -O3 -std=c++11 -finline-functions -Wall | ||
endif | ||
|
||
CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) | ||
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -std=c++11 | ||
|
||
LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei | ||
|
||
ifeq ($(UNAME_SYS), OpenBSD) | ||
LDLIBS += -lestdc++ | ||
else | ||
LDLIBS += -lstdc++ | ||
endif | ||
|
||
LDFLAGS += -shared | ||
|
||
# Verbosity. | ||
|
||
c_verbose_0 = @echo " C " $(?F); | ||
c_verbose = $(c_verbose_$(V)) | ||
|
||
cpp_verbose_0 = @echo " CPP " $(?F); | ||
cpp_verbose = $(cpp_verbose_$(V)) | ||
|
||
link_verbose_0 = @echo " LD " $(@F); | ||
link_verbose = $(link_verbose_$(V)) | ||
|
||
SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \)) | ||
OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) | ||
|
||
COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c | ||
COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c | ||
|
||
$(C_SRC_OUTPUT): $(OBJECTS) | ||
@mkdir -p $(BASEDIR)/priv/ | ||
$(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT) | ||
|
||
%.o: %.c | ||
$(COMPILE_C) $(OUTPUT_OPTION) $< | ||
|
||
%.o: %.cc | ||
$(COMPILE_CPP) $(OUTPUT_OPTION) $< | ||
|
||
%.o: %.C | ||
$(COMPILE_CPP) $(OUTPUT_OPTION) $< | ||
|
||
%.o: %.cpp | ||
$(COMPILE_CPP) $(OUTPUT_OPTION) $< | ||
|
||
clean: | ||
@rm -f $(C_SRC_OUTPUT) $(OBJECTS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Copyright (c) 2017, Benoit Chesneau <[email protected]>. | ||
* | ||
* This file is part of instrument released under the MIT license. | ||
* See the NOTICE for more information. | ||
*/ | ||
|
||
#ifndef ATOMS_H | ||
#define ATOMS_H | ||
|
||
namespace prometheus { | ||
extern ERL_NIF_TERM ATOM_OK; | ||
extern ERL_NIF_TERM ATOM_ERROR; | ||
extern ERL_NIF_TERM ATOM_EINVAL; | ||
extern ERL_NIF_TERM ATOM_BADARG; | ||
|
||
} // namespace prometheus | ||
|
||
|
||
#endif // ATOMS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* Copyright (c) 2017, Benoit Chesneau <[email protected]>. | ||
* | ||
* This file is part of prometheus released under the MIT license. | ||
* See the NOTICE for more information. | ||
*/ | ||
|
||
#include <syslog.h> | ||
|
||
#include <new> | ||
#include <stdexcept> | ||
|
||
#include "prometheus_nif.h" | ||
|
||
#include "erl_nif.h" | ||
|
||
#ifndef ATOMS_H | ||
#include "atoms.h" | ||
#endif | ||
|
||
#include "value.h" | ||
|
||
|
||
static ErlNifFunc nif_funcs[] = | ||
{ | ||
{"new_value", 0, prometheus::NewValue}, | ||
{"inc_value", 1, prometheus::IncValue}, | ||
{"inc_value", 2, prometheus::IncValue}, | ||
{"dec_value", 1, prometheus::DecValue}, | ||
{"dec_value", 2, prometheus::DecValue}, | ||
{"set_value", 2, prometheus::SetValue}, | ||
{"get_value", 1, prometheus::GetValue} | ||
}; | ||
|
||
namespace prometheus { | ||
|
||
ERL_NIF_TERM ATOM_OK; | ||
ERL_NIF_TERM ATOM_ERROR; | ||
ERL_NIF_TERM ATOM_EINVAL; | ||
ERL_NIF_TERM ATOM_BADARG; | ||
|
||
|
||
ErlNifResourceType *m_Value_RESOURCE; | ||
|
||
void | ||
value_resource_cleanup(ErlNifEnv *env, void *res) | ||
{ | ||
|
||
} | ||
|
||
void | ||
CreateValueType(ErlNifEnv *env) | ||
{ | ||
ErlNifResourceFlags flags = (ErlNifResourceFlags)(ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER); | ||
m_Value_RESOURCE = enif_open_resource_type(env, NULL, "prometheus_Value", value_resource_cleanup, flags, NULL); | ||
return; | ||
} | ||
|
||
ERL_NIF_TERM | ||
NewValue( | ||
ErlNifEnv* env, | ||
int argc, | ||
const ERL_NIF_TERM argv[]) | ||
{ | ||
prometheus::Value * value; | ||
void *alloc_ptr; | ||
alloc_ptr = enif_alloc_resource(m_Value_RESOURCE, sizeof(prometheus::Value)); | ||
value = new(alloc_ptr) prometheus::Value(); | ||
ERL_NIF_TERM result = enif_make_resource(env, value); | ||
enif_release_resource(value); | ||
return enif_make_tuple2(env, ATOM_OK, result); | ||
} | ||
|
||
ERL_NIF_TERM | ||
IncValue( | ||
ErlNifEnv* env, | ||
int argc, | ||
const ERL_NIF_TERM argv[]) | ||
{ | ||
prometheus::Value* value_ptr; | ||
|
||
if(!enif_get_resource(env, argv[0], m_Value_RESOURCE, (void **) &value_ptr)) | ||
return enif_make_badarg(env); | ||
|
||
if(argc > 1) | ||
{ | ||
double v; | ||
if (!enif_get_double(env, argv[1], &v)) | ||
return enif_make_badarg(env); | ||
value_ptr->Increment(v); | ||
} | ||
else | ||
{ | ||
value_ptr->Increment(); | ||
} | ||
|
||
return ATOM_OK; | ||
} | ||
|
||
ERL_NIF_TERM | ||
DecValue( | ||
ErlNifEnv* env, | ||
int argc, | ||
const ERL_NIF_TERM argv[]) | ||
{ | ||
prometheus::Value* value_ptr; | ||
|
||
if(!enif_get_resource(env, argv[0], m_Value_RESOURCE, (void **) &value_ptr)) | ||
return enif_make_badarg(env); | ||
|
||
if(argc > 1) | ||
{ | ||
double v; | ||
if (!enif_get_double(env, argv[1], &v)) | ||
return enif_make_badarg(env); | ||
value_ptr->Decrement(v); | ||
} | ||
else | ||
{ | ||
value_ptr->Decrement(); | ||
} | ||
|
||
return ATOM_OK; | ||
} | ||
|
||
ERL_NIF_TERM | ||
SetValue( | ||
ErlNifEnv* env, | ||
int argc, | ||
const ERL_NIF_TERM argv[]) | ||
{ | ||
prometheus::Value* value_ptr; | ||
|
||
if(!enif_get_resource(env, argv[0], m_Value_RESOURCE, (void **) &value_ptr)) | ||
return enif_make_badarg(env); | ||
|
||
double v; | ||
if (!enif_get_double(env, argv[1], &v)) | ||
return enif_make_badarg(env); | ||
value_ptr->Set(v); | ||
|
||
return ATOM_OK; | ||
} | ||
|
||
ERL_NIF_TERM | ||
GetValue( | ||
ErlNifEnv* env, | ||
int argc, | ||
const ERL_NIF_TERM argv[]) | ||
{ | ||
prometheus::Value* value_ptr; | ||
|
||
if(!enif_get_resource(env, argv[0], m_Value_RESOURCE, (void **) &value_ptr)) | ||
return enif_make_badarg(env); | ||
|
||
double v; | ||
v = value_ptr->GetValue(); | ||
return enif_make_double(env, v); | ||
} | ||
|
||
} // namespace prometheus | ||
|
||
|
||
/*nif initialization */ | ||
|
||
static void on_unload(ErlNifEnv *env, void *priv_data) | ||
{ | ||
} | ||
|
||
|
||
static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) | ||
{ | ||
try | ||
{ | ||
prometheus::CreateValueType(env); | ||
|
||
#define ATOM(Id, Value) { Id = enif_make_atom(env, Value); } | ||
// initialize the atoms | ||
ATOM(prometheus::ATOM_OK, "ok"); | ||
ATOM(prometheus::ATOM_ERROR, "error"); | ||
ATOM(prometheus::ATOM_EINVAL, "einval"); | ||
ATOM(prometheus::ATOM_BADARG, "badarg"); | ||
#undef ATOM | ||
|
||
return 0; | ||
|
||
} | ||
catch(...) | ||
{ | ||
return -1; | ||
} | ||
} | ||
|
||
extern "C" { | ||
ERL_NIF_INIT(prometheus_value, nif_funcs, &on_load, NULL, NULL, &on_unload); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* Copyright (c) 2017, Benoit Chesneau <[email protected]>. | ||
* | ||
* This file is part of instrument released under the MIT license. | ||
* See the NOTICE for more information. | ||
*/ | ||
|
||
|
||
#ifndef INCL_instrument_H | ||
#define INCL_instrument_H | ||
|
||
extern "C" { | ||
#include "erl_nif.h" | ||
|
||
ERL_NIF_TERM instrument_new_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM instrument_inc_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM instrument_dec_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM instrument_set_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM instrument_get_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
} | ||
|
||
namespace prometheus { | ||
|
||
ERL_NIF_TERM NewValue(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM IncValue(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM DecValue(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM SetValue(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
ERL_NIF_TERM GetValue(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
|
||
} // namespace instrument | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* Copyright (c) 2017, Benoit Chesneau <[email protected]>. | ||
* | ||
* This file is part of instrument released under the MIT license. | ||
* See the NOTICE for more information. | ||
*/ | ||
|
||
#include "value.h" | ||
|
||
namespace prometheus { | ||
|
||
Value::Value() : value_{0} {} | ||
|
||
Value::Value(double value) : value_{value} {} | ||
|
||
void Value::Increment() { Increment(1.0); } | ||
void Value::Increment(double value) { | ||
if (value < 0.0) { | ||
return; | ||
} | ||
Change(value); | ||
} | ||
|
||
void Value::Decrement() { Decrement(1.0); } | ||
|
||
void Value::Decrement(double value) { | ||
if (value < 0.0) { | ||
return; | ||
} | ||
Change(-1.0 * value); | ||
} | ||
|
||
void Value::Set(double value) { value_.store(value); } | ||
|
||
void Value::Change(double value) { | ||
auto current = value_.load(); | ||
while (!value_.compare_exchange_weak(current, current + value)) | ||
; | ||
} | ||
|
||
double Value::GetValue() const { return value_; } | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default travis vms are very old (