commit 0bc140c0bb5981c91a8e32da2d117a6f9efe03ef
parent 59ea6be440b0a0631ac65bde0df5657d632206ad
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 29 Oct 2020 10:53:56 +0100
Implement the device API
Diffstat:
| M | README.md | | | 7 | ++++--- |
| A | cmake/CMakeLists.txt | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | doc/atrck | | | 37 | +++++++++++++++++++++++++++++++++++++ |
| D | doc/atrkc | | | 37 | ------------------------------------- |
| A | src/atrck.c | | | 105 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/atrck.h | | | 69 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/atrck_c.h | | | 34 | ++++++++++++++++++++++++++++++++++ |
| A | src/atrck_log.c | | | 125 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/atrck_log.h | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 files changed, 534 insertions(+), 40 deletions(-)
diff --git a/README.md b/README.md
@@ -1,6 +1,7 @@
-# AsToRia: K correlation
+# AsToRia: Correlated K
-This C library loads the radiative properties of a gas mixture.
+This C library loads the radiative properties of a gas mixture saved wrt the
+AtrCK fileformat.
## How to build
@@ -20,7 +21,7 @@ for further informations on CMake.
## License
-Copyright (C) 2020 Centre National de la Recherche Scientifique (CNRS). AtrKC
+Copyright (C) 2020 Centre National de la Recherche Scientifique (CNRS). AtrCK
is free software released under the GPL v3+ license: GNU GPL version 3 or
later. You are welcome to redistribute it under certain conditions; refer to
the COPYING file for details.
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -0,0 +1,92 @@
+# Copyright (C) 2020 CNRS
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+cmake_minimum_required(VERSION 2.8)
+project(atrck C)
+enable_testing()
+
+set(ATRCK_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
+option(NO_TEST "Do not build tests" OFF)
+
+################################################################################
+# Check dependencies
+################################################################################
+find_package(RCMake 0.4 REQUIRED)
+find_package(RSys 0.10 REQUIRED)
+
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
+include(rcmake)
+include(rcmake_runtime)
+
+include_directories(${RSys_INCLUDE_DIR})
+
+################################################################################
+# Configure and define targets
+################################################################################
+set(VERSION_MAJOR 0)
+set(VERSION_MINOR 0)
+set(VERSION_PATCH 0)
+set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
+
+set(ATRCK_FILES_SRC
+ atrck.c
+ atrck_log.c)
+set(ATRCK_FILES_INC
+ atrck_c.h
+ atrck_log.h)
+set(ATRCK_FILES_INC_API
+ atrck.h)
+
+set(ATRCK_FILES_DOC COPYING README.md)
+
+# Prepend each file in the `ATRCK_FILES_<SRC|INC>' list by `ATRCK_SOURCE_DIR'
+rcmake_prepend_path(ATRCK_FILES_SRC ${ATRCK_SOURCE_DIR})
+rcmake_prepend_path(ATRCK_FILES_INC ${ATRCK_SOURCE_DIR})
+rcmake_prepend_path(ATRCK_FILES_INC_API ${ATRCK_SOURCE_DIR})
+rcmake_prepend_path(ATRCK_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
+
+add_library(atrck SHARED ${ATRCK_FILES_SRC} ${ATRCK_FILES_INC} ${ATRCK_FILES_INC_API})
+target_link_libraries(atrck RSys)
+
+set_target_properties(atrck PROPERTIES
+ DEFINE_SYMBOL ATRCK_SHARED_BUILD
+ VERSION ${VERSION}
+ SOVERSION ${VERSION_MAJOR})
+
+rcmake_setup_devel(atrck AtrCK ${VERSION} astoria/atrck_version.h)
+
+################################################################################
+# Add tests
+################################################################################
+if(NOT NO_TEST)
+ # function(new_test _name)
+ # add_executable(${_name} ${ATRCK_SOURCE_DIR}/${_name}.c)
+ # target_link_libraries(${_name} atrck RSys ${ARGN})
+ # add_test(${_name} ${_name})
+ # endfunction()
+ #
+ # new_test(test_atrck)
+endif()
+
+################################################################################
+# Define output & install directories
+################################################################################
+install(TARGETS atrck
+ ARCHIVE DESTINATION bin
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION bin)
+install(FILES ${ATRCK_FILES_INC_API} DESTINATION include/astoria)
+install(FILES ${ATRCK_FILES_DOC} DESTINATION share/doc/atrck)
+
diff --git a/doc/atrck b/doc/atrck
@@ -0,0 +1,37 @@
+<atrck> ::= <pagesize> <#bands> <#nodes>
+ <bands-list>
+ <padding>
+ <meshes-list>
+
+<pagesize> ::= INT64
+<#bands> ::= INT64
+<#nodes> ::= INT64
+<padding> ::= [ BYTE ... ] #ensure alignment on <pagesize>
+
+---
+
+<bands-list> ::= <band> [ <band> ... ]
+<band> ::= <band-id> <band-center> <band-size> <#quad-pts>
+ <quad-pts-list>
+
+<band-id> ::= INT64
+<band-center> ::= DOUBLE
+<band-size> ::= DOUBLE
+<#quad-pts> ::= INT64
+
+---
+
+<quad-pts-list> ::= <quad-pt> [ <quad-pt> ... ]
+<quad-pt> ::= <quad-abscissa> <quad-weight>
+
+<quad-abscissa> ::= DOUBLE
+<quad-weight> ::= DOUBLE
+
+---
+
+<meshes-list> ::= <ka-list>
+ <padding>
+ [ <ka-list> ... ]
+<ka-list> ::= <ka> [ <ka> ... ]
+<ka> ::= DOUBLE
+
diff --git a/doc/atrkc b/doc/atrkc
@@ -1,37 +0,0 @@
-<atrkc> ::= <pagesize> <#bands> <#nodes>
- <bands-list>
- <padding>
- <meshes-list>
-
-<pagesize> ::= INT64
-<#bands> ::= INT64
-<#nodes> ::= INT64
-<padding> ::= [ BYTE ... ] #ensure alignment on <pagesize>
-
----
-
-<bands-list> ::= <band> [ <band> ... ]
-<band> ::= <band-id> <band-center> <band-size> <#quad-pts>
- <quad-pts-list>
-
-<band-id> ::= INT64
-<band-center> ::= DOUBLE
-<band-size> ::= DOUBLE
-<#quad-pts> ::= INT64
-
----
-
-<quad-pts-list> ::= <quad-pt> [ <quad-pt> ... ]
-<quad-pt> ::= <quad-abscissa> <quad-weight>
-
-<quad-abscissa> ::= DOUBLE
-<quad-weight> ::= DOUBLE
-
----
-
-<meshes-list> ::= <k-list>
- <padding>
- [ <k-list> ... ]
-<k-list> ::= <k> [ <k> ... ]
-<k> ::= DOUBLE
-
diff --git a/src/atrck.c b/src/atrck.c
@@ -0,0 +1,105 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "atrck.h"
+#include "atrck_c.h"
+#include "atrck_log.h"
+
+#include <unistd.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+release_atrck(ref_T* ref)
+{
+ struct atrck* atrck;
+ ASSERT(ref);
+ atrck = CONTAINER_OF(ref, struct atrck, ref);
+ if(atrck->logger == &atrck->logger__) logger_release(&atrck->logger__);
+ MEM_RM(atrck->allocator, atrck);
+}
+
+/*******************************************************************************
+ * Exported functions
+ ******************************************************************************/
+res_T
+atrck_create
+ (struct logger* logger, /* NULL <=> use default logger */
+ struct mem_allocator* mem_allocator, /* NULL <=> use default allocator */
+ const int verbose, /* Verbosity level */
+ struct atrck** out_atrck)
+{
+ struct atrck* atrck = NULL;
+ struct mem_allocator* allocator = NULL;
+ res_T res = RES_OK;
+
+ if(!out_atrck) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
+ atrck = MEM_CALLOC(allocator, 1, sizeof(*atrck));
+ if(!atrck) {
+ if(verbose) {
+ #define ERR_STR "Could not allocate the AtrKC device.\n"
+ if(logger) {
+ logger_print(logger, LOG_ERROR, ERR_STR);
+ } else {
+ fprintf(stderr, MSG_ERROR_PREFIX ERR_STR);
+ }
+ #undef ERR_STR
+ }
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ ref_init(&atrck->ref);
+ atrck->allocator = allocator;
+ atrck->verbose = verbose;
+ atrck->pagesize_os = (size_t)sysconf(_SC_PAGESIZE);
+ if(logger) {
+ atrck->logger = logger;
+ } else {
+ setup_log_default(atrck);
+ }
+
+exit:
+ if(out_atrck) *out_atrck = atrck;
+ return res;
+error:
+ if(atrck) {
+ ATRCK(ref_put(atrck));
+ atrck = NULL;
+ }
+ goto exit;
+}
+
+res_T
+atrck_ref_get(struct atrck* atrck)
+{
+ if(!atrck) return RES_BAD_ARG;
+ ref_get(&atrck->ref);
+ return RES_OK;
+}
+
+res_T
+atrck_ref_put(struct atrck* atrck)
+{
+ if(!atrck) return RES_BAD_ARG;
+ ref_put(&atrck->ref, release_atrck);
+ return RES_OK;
+}
+
diff --git a/src/atrck.h b/src/atrck.h
@@ -0,0 +1,69 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef ATRCK_H
+#define ATRCK_H
+
+#include <rsys/rsys.h>
+
+/* Library symbol management */
+#if defined(ATRCK_SHARED_BUILD) /* Build shared library */
+ #define ATRCK_API extern EXPORT_SYM
+#elif defined(ATRCK_STATIC) /* Use/build static library */
+ #define ATRCK_API extern LOCAL_SYM
+#else /* Use shared library */
+ #define ATRCK_API extern IMPORT_SYM
+#endif
+
+/* Helper macro that asserts if the invocation of the atrck function `Func'
+ * returns an error. One should use this macro on sth function calls for
+ * which no explicit error checking is performed */
+#ifndef NDEBUG
+ #define ATRCK(Func) ASSERT(atrck_ ## Func == RES_OK)
+#else
+ #define ATRCK(Func) atrck_ ## Func
+#endif
+
+/* Forward declaration of external data types */
+struct logger;
+struct mem_allocator;
+
+/* Forward declaration of opaque data types */
+struct atrck;
+
+BEGIN_DECLS
+
+/*******************************************************************************
+ * AtrKC API
+ ******************************************************************************/
+ATRCK_API res_T
+atrck_create
+ (struct logger* logger, /* NULL <=> use default logger */
+ struct mem_allocator* allocator, /* NULL <=> use default allocator */
+ const int verbose, /* Verbosity level */
+ struct atrck** atrck);
+
+ATRCK_API res_T
+atrck_ref_get
+ (struct atrck* atrck);
+
+ATRCK_API res_T
+atrck_ref_put
+ (struct atrck* atrck);
+
+END_DECLS
+
+#endif /* ATRCK_H */
+
diff --git a/src/atrck_c.h b/src/atrck_c.h
@@ -0,0 +1,34 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef ATRCK_C_H
+#define ATRCK_C_H
+
+#include <rsys/logger.h>
+#include <rsys/ref_count.h>
+
+struct mem_allocator;
+
+struct atrck {
+ size_t pagesize_os;
+
+ struct mem_allocator* allocator;
+ struct logger* logger;
+ struct logger logger__; /* Default logger */
+ int verbose;
+ ref_T ref;
+};
+
+#endif /* ATRCK_C_H */
diff --git a/src/atrck_log.c b/src/atrck_log.c
@@ -0,0 +1,125 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "atrck_c.h"
+#include "atrck_log.h"
+
+#include <rsys/cstr.h>
+#include <rsys/logger.h>
+
+#include <stdarg.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static INLINE void
+log_msg
+ (const struct atrck* atrck,
+ const enum log_type stream,
+ const char* msg,
+ va_list vargs)
+{
+ ASSERT(atrck && msg);
+ if(atrck->verbose) {
+ res_T res; (void)res;
+ res = logger_vprint(atrck->logger, stream, msg, vargs);
+ ASSERT(res == RES_OK);
+ }
+}
+
+static void
+print_info(const char* msg, void* ctx)
+{
+ (void)ctx;
+ fprintf(stderr, MSG_INFO_PREFIX"%s", msg);
+}
+
+static void
+print_err(const char* msg, void* ctx)
+{
+ (void)ctx;
+ fprintf(stderr, MSG_ERROR_PREFIX"%s", msg);
+}
+
+static void
+print_warn(const char* msg, void* ctx)
+{
+ (void)ctx;
+ fprintf(stderr, MSG_WARNING_PREFIX"%s", msg);
+}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+res_T
+setup_log_default(struct atrck* atrck)
+{
+ res_T res = RES_OK;
+ ASSERT(atrck);
+
+ res = logger_init(atrck->allocator, &atrck->logger__);
+ if(res != RES_OK) {
+ if(atrck->verbose) {
+ fprintf(stderr,
+ MSG_ERROR_PREFIX
+ "Could not setup the AtrCK default logger -- %s.\n",
+ res_to_cstr(res));
+ }
+ goto error;
+ }
+ logger_set_stream(&atrck->logger__, LOG_OUTPUT, print_info, NULL);
+ logger_set_stream(&atrck->logger__, LOG_ERROR, print_err, NULL);
+ logger_set_stream(&atrck->logger__, LOG_WARNING, print_warn, NULL);
+ atrck->logger = &atrck->logger__;
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
+void
+log_info(const struct atrck* atrck, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(atrck && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(atrck, LOG_OUTPUT, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_err(const struct atrck* atrck, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(atrck && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(atrck, LOG_ERROR, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_warn(const struct atrck* atrck, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(atrck && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(atrck, LOG_WARNING, msg, vargs_list);
+ va_end(vargs_list);
+}
+
diff --git a/src/atrck_log.h b/src/atrck_log.h
@@ -0,0 +1,68 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef ATRCK_LOG_H
+#define ATRCK_LOG_H
+
+#include <rsys/rsys.h>
+
+#define MSG_INFO_PREFIX "AtrCK:\x1b[1m\x1b[32minfo\x1b[0m: "
+#define MSG_ERROR_PREFIX "AtrCK:\x1b[1m\x1b[31merror\x1b[0m: "
+#define MSG_WARNING_PREFIX "AtrCK:\x1b[1m\x1b[33mwarning\x1b[0m: "
+
+struct atrck;
+struct logger;
+
+extern LOCAL_SYM res_T
+setup_log_default
+ (struct atrck* atrck);
+
+/* Conditionally log a message on the LOG_OUTPUT stream of the atrck logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_info
+ (const struct atrck* atrck,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_ERROR stream of the atrck logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_err
+ (const struct atrck* atrck,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_WARNING stream of the atrck logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_warn
+ (const struct atrck* atrck,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+#endif /* ATRCK_LOG_H */