Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ BraceWrapping:

AllowShortFunctionsOnASingleLine: None
AllowShortBlocksOnASingleLine: false
SeparateDefinitionBlocks: Always

SpaceAfterCStyleCast: true
SpaceBeforeParens: ControlStatements
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ option(DEDX_BUILD_BINARY_TABLE "Build the binary data tables (requires a working
if(DEDX_BUILD_BINARY_TABLE)
add_subdirectory(buildbins)
endif()
enable_testing()
add_subdirectory(examples)
add_subdirectory(libdedx)

enable_testing()
add_subdirectory(tests)

# add uninstall target according to CMAKE FAQ:
Expand Down
45 changes: 45 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Contributing to libdedx

## C coding style

### Variable declarations

Declare all variables at the top of their enclosing block, before any statements.
This is consistent with the Linux kernel coding style and makes the variable
inventory of a function visible before reading the logic.

```c
/* good */
int foo(int x) {
const char *path;
int result;
FILE *fp;

path = get_path();
fp = fopen(path, "r");
...
}

/* bad — declaration mixed into statements */
int foo(int x) {
const char *path = get_path();
FILE *fp = fopen(path, "r");
do_something();
int result = 0; /* not allowed */
...
}
```

Declarations at the top of an inner block (e.g. inside an `if` or `for`) are
fine when the variable is genuinely local to that scope.

## Thread safety

libdedx is currently **not thread-safe**. There is no synchronization around
the static path cache in `_dedx_get_data_path()`, nor around workspace mutation
in `dedx_load_config()` / `_dedx_load_data()`. Do not share a `dedx_workspace`
across threads without external locking.

The intended fix is to replace the `done` flag in `_dedx_get_data_path()` with
C11 `call_once()`, and audit the rest of the library for shared mutable state.
This is tracked as a known issue.
1 change: 1 addition & 0 deletions Windows/dedx.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef DEDX_H_INCLUDED
#define DEDX_H_INCLUDED

// #include "dedx_workspace.h"
/* available data tables */
enum {
Expand Down
7 changes: 7 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ add_executable(dedx_list dedx_list.c)
add_executable(dedx_bethe dedx_bethe.c)
add_executable(dedx_use_wrappers dedx_use_wrappers.c)
add_executable(dedx_get_sample_csv dedx_get_sample_csv.c)
add_executable(dedx_csda dedx_csda.c)

target_link_libraries (dedx_example dedx)
target_link_libraries (getdedx dedx)
target_link_libraries (dedx_list dedx)
target_link_libraries (dedx_bethe dedx)
target_link_libraries (dedx_use_wrappers dedx)
target_link_libraries (dedx_get_sample_csv dedx)
target_link_libraries (dedx_csda dedx)

# Visual Studio does not need or want you to explicitly request linking the math library.
# You must avoid adding it as a link library when building for Windows.
Expand All @@ -22,11 +24,16 @@ IF (NOT WIN32)
target_link_libraries (dedx_bethe m)
target_link_libraries (dedx_use_wrappers m)
target_link_libraries (dedx_get_sample_csv m)
target_link_libraries (dedx_csda m)
ENDIF()

# some of the examples need data/ folder in the same location as generated executable
add_custom_command(TARGET dedx_example PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/libdedx/data $<TARGET_FILE_DIR:dedx_example>/data)

foreach(example dedx_example dedx_bethe dedx_list dedx_use_wrappers dedx_get_sample_csv dedx_csda)
add_test(NAME ${example} COMMAND ${example})
endforeach()

install (TARGETS getdedx DESTINATION bin)
46 changes: 39 additions & 7 deletions examples/dedx_csda.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
#include <dedx.h>
#include <dedx_tools.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
int err = 0;
int program = 2; // PSTAR
int ion = 1; // Proton
int target = 276; // Water
double A = 1; // Nucleid number of ion
float energy = 100; // Energy per nucleos in MeV
float csda = dedx_get_csda(program, ion, A, target, energy, &err);
printf("Calculated csda %6.3Ecm^2/g\n", csda);
float energy = 100.0f; /* MeV/u */
double csda;
dedx_workspace *ws;
dedx_config *cfg;

ws = dedx_allocate_workspace(1, &err);
if (err != DEDX_OK) {
fprintf(stderr, "failed to allocate workspace: err=%d\n", err);
return 1;
}

cfg = (dedx_config *) calloc(1, sizeof(dedx_config));
cfg->program = DEDX_PSTAR;
cfg->ion = DEDX_PROTON;
cfg->ion_a = 1; /* nucleon number — required by dedx_get_csda */
cfg->target = DEDX_WATER;

dedx_load_config(ws, cfg, &err);
if (err != DEDX_OK) {
fprintf(stderr, "failed to load config: err=%d\n", err);
dedx_free_config(cfg, &err);
dedx_free_workspace(ws, &err);
return 1;
}

csda = dedx_get_csda(ws, cfg, energy, &err);
if (err != DEDX_OK) {
fprintf(stderr, "dedx_get_csda failed: err=%d\n", err);
dedx_free_config(cfg, &err);
dedx_free_workspace(ws, &err);
return 1;
}

printf("CSDA range at %g MeV/u: %6.3E cm^2/g\n", energy, csda);

dedx_free_config(cfg, &err);
dedx_free_workspace(ws, &err);
return 0;
}
1 change: 1 addition & 0 deletions examples/dedx_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* gcc -g -lm dedx_get.c -o dedx -Wall -ldedx*/

int main(int argc, char *argv[]) {
Expand Down
2 changes: 1 addition & 1 deletion examples/dedx_get_sample_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ int main() {
fclose(file);

return 0;
}
}
24 changes: 24 additions & 0 deletions examples/scripting/invoke_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ int get_data_type(data_item *item);
char *get_variable_name(char *params, int *offset);

void add_function(char *cmd, int (*func_p)(char *params));

int set_output(FILE *out) {
fp_out = out;
return 0;
}

int write_line(char *str) {
fprintf(fp_out, "%s", str);
return 0;
Expand All @@ -66,6 +68,7 @@ int get_stp(data_item *config, data_item *energy, float *stp) {

return 0;
}

data_item *create_data_item(char *name) {
data_item *curr = get_data_item(name);
if (curr != NULL) {
Expand All @@ -78,6 +81,7 @@ data_item *create_data_item(char *name) {
data_head = curr;
return curr;
}

data_item *get_data_item(char *name) {
data_item *curr = data_head;
while (curr != NULL) {
Expand All @@ -88,9 +92,11 @@ data_item *get_data_item(char *name) {
}
return NULL;
}

int get_data_type(data_item *item) {
return item->type;
}

char *get_variable_name(char *params, int *offset) {
char *name = (char *) calloc(20, sizeof(char));
int i = 0;
Expand All @@ -103,6 +109,7 @@ char *get_variable_name(char *params, int *offset) {
*offset = i + 1;
return name;
}

int build_functions() {
add_function("CONFIG", &config);
add_function("CLONE", &clone);
Expand All @@ -111,6 +118,7 @@ int build_functions() {
add_function("PRINT", &print);
return 0;
}

int invoke_function(char *cmd, char *params) {
func_item *curr = func_head;
do {
Expand All @@ -122,13 +130,15 @@ int invoke_function(char *cmd, char *params) {
} while (curr != NULL);
return -1;
}

void add_function(char *cmd, int (*func_p)(char *params)) {
func_item *curr = (func_item *) malloc(sizeof(func_item));
strcpy(curr->cmd, cmd);
curr->func_p = func_p;
curr->next = func_head;
func_head = curr;
}

int clone(char *params) {
char a[20];
char b[20];
Expand Down Expand Up @@ -172,13 +182,15 @@ int clone(char *params) {
}
return 0;
}

int clone_config(data_item *a, data_item *b) {
memcpy(a->config, b->config, sizeof(dedx_config));
dedx_config *a_conf = a->config;
dedx_config *b_conf = b->config;
clone_dedx_config(a_conf, b_conf);
return 0;
}

int clone_dedx_config(dedx_config *a_conf, dedx_config *b_conf) {
memcpy(a_conf, b_conf, sizeof(dedx_config));
int length = b_conf->elements_length;
Expand All @@ -200,12 +212,14 @@ int clone_dedx_config(dedx_config *a_conf, dedx_config *b_conf) {
}
return 0;
}

int clone_energy(data_item *a, data_item *b) {
a->energy = malloc(sizeof(float) * b->e_length);
b->e_length = a->e_length;
memcpy(a->energy, b->energy, sizeof(float) * b->e_length);
return 0;
}

int set(char *params) {
int i = 0;
int offset = 0;
Expand Down Expand Up @@ -233,6 +247,7 @@ int set(char *params) {
}
return 0;
}

int energy(char *params) {
int offset = 0;
char *name = get_variable_name(params, &offset);
Expand All @@ -245,6 +260,7 @@ int energy(char *params) {
set_energy(&(item->energy), &(item->e_length), &params[offset]);
return 0;
}

int set_energy(float **energy, int *n, char *params) {
int i;
int j = 0;
Expand Down Expand Up @@ -274,6 +290,7 @@ int set_energy(float **energy, int *n, char *params) {
*n = items;
return 0;
}

int print(char *params) {
int offset = 0;
int i;
Expand Down Expand Up @@ -318,6 +335,7 @@ int print(char *params) {
write_line(text);
return 0;
}

int replace_signs(char *str) {
int i = 0;
int j = 0;
Expand All @@ -343,6 +361,7 @@ int replace_signs(char *str) {
}
return 0;
}

int print_list(char *text, int argv, char **args) {
int i, j, k;

Expand Down Expand Up @@ -389,6 +408,7 @@ int print_list(char *text, int argv, char **args) {
print_values(text, argv, items);
return 0;
}

int print_values(char *text, int items_n, data_item ***items) {
int i, j, k;
int dim = 0;
Expand Down Expand Up @@ -439,6 +459,7 @@ int print_values(char *text, int items_n, data_item ***items) {
}
return 0;
}

int config(char *params) {
int offset = 0;
char *name;
Expand All @@ -457,6 +478,7 @@ int config(char *params) {
}
return 0;
}

int set_config_value(dedx_config *config, char *name, char *value) {
if (strcmp("prog", name) == 0) {
config->prog = atoi(value);
Expand All @@ -473,6 +495,7 @@ int set_config_value(dedx_config *config, char *name, char *value) {
}
return 0;
}

int set_config_list(dedx_config *config, char *name, char *list) {
char params[10][20];
int i = 0;
Expand Down Expand Up @@ -516,6 +539,7 @@ int set_config_list(dedx_config *config, char *name, char *list) {
config->elements_length = items;
return 0;
}

int set_config(dedx_config *config, char *params) {
int i = 1;
int j = 0;
Expand Down
4 changes: 4 additions & 0 deletions examples/scripting/invoke_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
#define INVOKE_FUNC_DEF
#include <dedx.h>
#include <stdio.h>

enum { DEDX_SCRIPT_NONE = 0, DEDX_SCRIPT_CONFIG, DEDX_SCRIPT_ENERGY };

struct func_struct {
char cmd[20];
int (*func_p)(char *params);
struct func_struct *next;
};

struct data_struct {
char name[20];
dedx_config *config;
Expand All @@ -18,6 +21,7 @@ struct data_struct {
int type;
struct data_struct *next;
};

int build_functions();
int invoke_function(char *cmd, char *params);
int set_output(FILE *output);
Expand Down
2 changes: 2 additions & 0 deletions examples/scripting/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int main(int argv, char *argc[]) {
fclose(out);
return 0;
}

int parser(FILE *in, FILE *out) {
build_functions();
set_output(out);
Expand All @@ -59,6 +60,7 @@ int parser(FILE *in, FILE *out) {
}
return 0;
}

/*Remove Comments, lineshift, tabs, white spaces, */
int clean(FILE *in, FILE *out) {
char line[read_c];
Expand Down
Loading
Loading