commit 46b3b65aac79e97590fd7a9290dea7912bba5b03
parent c0e7ef42569d34d9b100cd7f6f0a42c8a9aa3837
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 14 Jun 2022 14:34:05 +0200
Implement sars_create and sars_ref_<get|put>
Diffstat:
| A | cmake/CMakeLists.txt | | | 118 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/sars.c | | | 108 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/sars_c.h | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/sars_log.c | | | 125 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/sars_log.h | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 472 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -0,0 +1,118 @@
+# Copyright (C) 2022 |Meso|Star> (contact@meso-star.com)
+#
+# 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 3.1)
+project(sars C)
+enable_testing()
+
+set(SARS_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(SARS_FILES_SRC
+ sars.c
+ sars_log.c)
+set(SARS_FILES_INC
+ sars_c.h
+ sars_log.h)
+set(SARS_FILES_INC_API
+ sars.h)
+
+set(SARS_FILES_DOC COPYING README.md)
+
+# Prepend each file in the `SARS_FILES_<SRC|INC>' list by `SARS_SOURCE_DIR'
+rcmake_prepend_path(SARS_FILES_SRC ${SARS_SOURCE_DIR})
+rcmake_prepend_path(SARS_FILES_INC ${SARS_SOURCE_DIR})
+rcmake_prepend_path(SARS_FILES_INC_API ${SARS_SOURCE_DIR})
+rcmake_prepend_path(SARS_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
+
+add_library(sars SHARED ${SARS_FILES_SRC} ${SARS_FILES_INC} ${SARS_FILES_INC_API})
+target_link_libraries(sars RSys)
+
+if(CMAKE_COMPILER_IS_GNUCC)
+ target_link_libraries(sars m)
+endif()
+
+set_target_properties(sars PROPERTIES
+ DEFINE_SYMBOL SARS_SHARED_BUILD
+ VERSION ${VERSION}
+ SOVERSION ${VERSION_MAJOR})
+
+rcmake_setup_devel(sars StarAerosol ${VERSION} star/sars_version.h)
+
+################################################################################
+# Add tests
+################################################################################
+if(NOT NO_TEST)
+ function(new_test _name)
+ add_executable(${_name} ${SARS_SOURCE_DIR}/${_name}.c)
+ target_link_libraries(${_name} sars RSys ${ARGN})
+ add_test(${_name} ${_name})
+ endfunction()
+
+ #new_test(test_sars)
+ #new_test(test_sars_load)
+endif()
+
+################################################################################
+# Man page
+###############################################################################
+find_program(SCDOC NAMES scdoc)
+if(NOT SCDOC)
+ message(WARNING
+ "The `scdoc' program is missing. "
+ "The Star-CK man page cannot be generated.")
+else()
+ set(_src ${PROJECT_SOURCE_DIR}/../doc/sars.5.scd)
+ add_custom_command(
+ OUTPUT sars.5
+ COMMAND ${SCDOC} < ${_src} > sars.5
+ DEPENDS ${_src}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ COMMENT "Buid ROFF man page sars.5"
+ VERBATIM)
+ add_custom_target(man-roff ALL DEPENDS sars.5)
+ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/sars.5 DESTINATION share/man/man5)
+endif()
+
+################################################################################
+# Define output & install directories
+################################################################################
+install(TARGETS sars
+ ARCHIVE DESTINATION bin
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION bin)
+install(FILES ${SARS_FILES_INC_API} DESTINATION include/star)
+install(FILES ${SARS_FILES_DOC} DESTINATION share/doc/star-aerosol)
+
diff --git a/src/sars.c b/src/sars.c
@@ -0,0 +1,108 @@
+/* Copyright (C) 2022 |Meso|Star> (contact@meso-star.com)
+ *
+ * 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 "sars.h"
+#include "sars_c.h"
+#include "sars_log.h"
+
+#include <unistd.h> /* sysconf support */
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static INLINE res_T
+check_sars_create_args(const struct sars_create_args* args)
+{
+ /* Nothing to check. Only return RES_BAD_ARG if args is NULL */
+ return args ? RES_OK : RES_BAD_ARG;
+}
+
+static void
+release_sars(ref_T* ref)
+{
+ struct sars* sars = NULL;
+ ASSERT(ref);
+ sars = CONTAINER_OF(ref, struct sars, ref);
+ if(sars->logger == &sars->logger__) logger_release(&sars->logger__);
+ darray_band_release(&sars->bands);
+ MEM_RM(sars->allocator, sars);
+}
+
+/*******************************************************************************
+ * Exported functions
+ ******************************************************************************/
+res_T
+sars_create
+ (const struct sars_create_args* args,
+ struct sars** out_sars)
+{
+ struct sars* sars = NULL;
+ struct mem_allocator* allocator = NULL;
+ res_T res = RES_OK;
+
+ if(!out_sars) { res = RES_BAD_ARG; goto error; }
+ res = check_sars_create_args(args);
+ if(res != RES_OK) goto error;
+
+ allocator = args->allocator ? args->allocator : &mem_default_allocator;
+ sars = MEM_CALLOC(allocator, 1, sizeof(*sars));
+ if(!sars) {
+ if(args->verbose) {
+ #define ERR_STR "Could not allocate the Star-Aerosol device.\n"
+ if(args->logger) {
+ logger_print(args->logger, LOG_ERROR, ERR_STR);
+ } else {
+ fprintf(stderr, MSG_ERROR_PREFIX ERR_STR);
+ }
+ #undef ERR_STR
+ }
+ res = RES_MEM_ERR;
+ goto error;
+ }
+
+ ref_init(&sars->ref);
+ sars->allocator = allocator;
+ sars->verbose = args->verbose;
+ sars->pagesize_os = (size_t)sysconf(_SC_PAGESIZE);
+ darray_band_init(allocator, &sars->bands);
+ if(args->logger) {
+ sars->logger = args->logger;
+ } else {
+ setup_log_default(sars);
+ }
+
+exit:
+ if(out_sars) *out_sars = sars ;
+ return res;
+error:
+ if(sars) { SARS(ref_put(sars)); sars = NULL; }
+ goto exit;
+}
+
+res_T
+sars_ref_get(struct sars* sars)
+{
+ if(!sars) return RES_BAD_ARG;
+ ref_get(&sars->ref);
+ return RES_OK;
+}
+
+res_T
+sars_ref_put(struct sars* sars)
+{
+ if(!sars) return RES_BAD_ARG;
+ ref_put(&sars->ref, release_sars);
+ return RES_OK;
+}
diff --git a/src/sars_c.h b/src/sars_c.h
@@ -0,0 +1,53 @@
+/* Copyright (C) 2022 |Meso|Star> (contact@meso-star.com)
+ *
+ * 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 SARS_C_H
+#define SARS_C_H
+
+#include <rsys/dynamic_array.h>
+#include <rsys/logger.h>
+#include <rsys/ref_count.h>
+
+struct band {
+ double low; /* Lower bound in nm */
+ double upp; /* Upper bound in nm */
+ size_t map_len;
+ float* ka_list;
+ float* ks_list;
+};
+
+/* Generate the dynamic array of bands */
+#define DARRAY_NAME band
+#define DARRAY_DATA struct band
+#include <rsys/dynamic_array.h>
+
+struct mem_allocator;
+
+struct sars {
+ /* Loaded data */
+ uint64_t pagesize;
+ uint64_t nnodes;
+ struct darray_band bands;
+
+ size_t pagesize_os;
+
+ struct mem_allocator* allocator;
+ struct logger* logger;
+ struct logger logger__;
+ int verbose;
+ ref_T ref;
+};
+
+#endif /* SARS_C_H */
diff --git a/src/sars_log.c b/src/sars_log.c
@@ -0,0 +1,125 @@
+/* Copyright (C) 2022 |Meso|Star> (contact@meso-star.com)
+ *
+ * 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 "sars_c.h"
+#include "sars_log.h"
+
+#include <rsys/cstr.h>
+#include <rsys/logger.h>
+
+#include <stdarg.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static INLINE void
+log_msg
+ (const struct sars* sars,
+ const enum log_type stream,
+ const char* msg,
+ va_list vargs)
+{
+ ASSERT(sars && msg);
+ if(sars->verbose) {
+ res_T res; (void)res;
+ res = logger_vprint(sars->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 sars* sars)
+{
+ res_T res = RES_OK;
+ ASSERT(sars);
+
+ res = logger_init(sars->allocator, &sars->logger__);
+ if(res != RES_OK) {
+ if(sars->verbose) {
+ fprintf(stderr,
+ MSG_ERROR_PREFIX
+ "Could not setup the AtrCK default logger -- %s.\n",
+ res_to_cstr(res));
+ }
+ goto error;
+ }
+ logger_set_stream(&sars->logger__, LOG_OUTPUT, print_info, NULL);
+ logger_set_stream(&sars->logger__, LOG_ERROR, print_err, NULL);
+ logger_set_stream(&sars->logger__, LOG_WARNING, print_warn, NULL);
+ sars->logger = &sars->logger__;
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
+void
+log_info(const struct sars* sars, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(sars && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(sars, LOG_OUTPUT, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_err(const struct sars* sars, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(sars && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(sars, LOG_ERROR, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_warn(const struct sars* sars, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(sars && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(sars, LOG_WARNING, msg, vargs_list);
+ va_end(vargs_list);
+}
+
diff --git a/src/sars_log.h b/src/sars_log.h
@@ -0,0 +1,68 @@
+/* Copyright (C) 2022 |Meso|Star> (contact@meso-star.com)
+ *
+ * This program is free software: you can redismshbute 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 dismshbuted 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 SARS_LOG_H
+#define SARS_LOG_H
+
+#include <rsys/rsys.h>
+
+#define MSG_INFO_PREFIX "Star-Aerosol:\x1b[1m\x1b[32minfo\x1b[0m: "
+#define MSG_ERROR_PREFIX "Star-Aerosol:\x1b[1m\x1b[31merror\x1b[0m: "
+#define MSG_WARNING_PREFIX "Star-Aerosol:\x1b[1m\x1b[33mwarning\x1b[0m: "
+
+struct sars;
+struct logger;
+
+extern LOCAL_SYM res_T
+setup_log_default
+ (struct sars* sars);
+
+/* Conditionally log a message on the LOG_OUTPUT stream of the sars logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_info
+ (const struct sars* sars,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_ERROR stream of the sars logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_err
+ (const struct sars* sars,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_WARNING stream of the sars logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_warn
+ (const struct sars* sars,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+#endif /* SARS_LOG_H */