solstice-anim

Geometry animation library of the solstice app
git clone git://git.meso-star.com/solstice-anim.git
Log | Files | Refs | README | LICENSE

commit bf95aaacd786465fc67925271fd46d8c85a08276
parent 9101c18f3096dc5689069b28475cf3d554fcf4a4
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Mon, 10 Oct 2016 18:43:30 +0200

First source files.

Diffstat:
Acmake/CMakeLists.txt | 113+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sanim.h | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sanim_c.h | 21+++++++++++++++++++++
Asrc/sanim_device.c | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sanim_device_c.h | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sanim_node.c | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sanim_node_c.h | 36++++++++++++++++++++++++++++++++++++
Asrc/test_sanim_device.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_sanim_node.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_sanim_utils.h | 41+++++++++++++++++++++++++++++++++++++++++
10 files changed, 783 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,113 @@ +# Copyright (C) CNRS 2016 +# +# 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(solstice-anim C) +enable_testing() + +set(SANIM_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) +option(NO_TEST "Do not build tests" OFF) + +################################################################################ +# Check dependencies +################################################################################ +find_package(RCMake 0.2.3 REQUIRED) +find_package(RSys 0.3 REQUIRED) +find_package(OpenMP 1.2 REQUIRED) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) +include(rcmake) +include(rcmake_runtime) + +include_directories( + ${RSys_INCLUDE_DIR}) + +rcmake_append_runtime_dirs(_runtime_dirs RSys) + +################################################################################ +# Configure and define targets +################################################################################ +set(VERSION_MAJOR 0) +set(VERSION_MINOR 1) +set(VERSION_PATCH 0) +set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) + +set(SANIM_FILES_SRC + sanim_device.c + sanim_node.c) + +set(SANIM_FILES_INC_API + sanim.h) + +set(SANIM_FILES_INC + sanim_c.h + sanim_device_c.h + sanim_node_c.h) + +set(SANIM_FILES_DOC COPYING README.md) + +# Prepend each file in the `SANIM_FILES_<SRC|INC>' list by `SANIM_SOURCE_DIR' +rcmake_prepend_path(SANIM_FILES_SRC ${SANIM_SOURCE_DIR}) +rcmake_prepend_path(SANIM_FILES_INC ${SANIM_SOURCE_DIR}) +rcmake_prepend_path(SANIM_FILES_INC_API ${SANIM_SOURCE_DIR}) +rcmake_prepend_path(SANIM_FILES_DOC ${PROJECT_SOURCE_DIR}/../) + +add_library(solstice-anim SHARED ${SANIM_FILES_SRC} ${SANIM_FILES_INC} ${SANIM_FILES_INC_API}) +target_link_libraries(solstice-anim RSys) + +if(CMAKE_COMPILER_IS_GNUCC) + target_link_libraries(solstice-anim m) +endif() + +set_target_properties(solstice-anim PROPERTIES + DEFINE_SYMBOL SANIM_SHARED_BUILD + COMPILE_FLAGS ${OpenMP_C_FLAGS} + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +if(CMAKE_COMPILER_IS_GNUCC) + set_target_properties(solstice-anim PROPERTIES + LINK_FLAGS ${OpenMP_C_FLAGS}) +endif() + +rcmake_setup_devel(solstice-anim SolAnim ${VERSION} solstice/sanim_version.h) + +################################################################################ +# Add tests +################################################################################ +if(NOT NO_TEST) + function(new_test _name) + add_executable(${_name} + ${SANIM_SOURCE_DIR}/test_sanim_utils.h + ${SANIM_SOURCE_DIR}/${_name}.c) + target_link_libraries(${_name} solstice-anim RSys) + add_test(${_name} ${_name}) + rcmake_set_test_runtime_dirs(${_name} _runtime_dirs) + endfunction() + + new_test(test_sanim_device) + new_test(test_sanim_node) +endif() + +################################################################################ +# Define output & install directories +################################################################################ +install(TARGETS solstice-anim + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${SANIM_FILES_INC_API} DESTINATION include/solstice) +install(FILES ${SANIM_FILES_DOC} DESTINATION share/doc/solstice-anim) + diff --git a/src/sanim.h b/src/sanim.h @@ -0,0 +1,97 @@ +/* Copyright (C) CNRS 2016 +* +* 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 SANIM_H +#define SANIM_H + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(SANIM_SHARED_BUILD) /* Build shared library */ +#define SANIM_API extern EXPORT_SYM +#elif defined(SANIM_STATIC) /* Use/build static library */ +#define SANIM_API extern LOCAL_SYM +#else /* Use shared library */ +#define SANIM_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the Solstice Anim function `Func' +* returns an error. One should use this macro on Solstice Anim function calls for which +* no explicit error checking is performed */ +#ifndef NDEBUG +#define SANIM(Func) ASSERT(sanim_ ## Func == RES_OK) +#else +#define SANIM(Func) sanim_ ## Func +#endif + +/* Syntactic sugar used to inform the Solstice Anim library that it can use +* as many threads as CPU cores */ +#define SANIM_NTHREADS_DEFAULT (~0u) + +/* Forward declaration of external types */ +struct logger; +struct mem_allocator; +struct ssp_rng; + +/* Opaque Solstice Anim types */ +struct sanim_device; +struct sanim_node; + +BEGIN_DECLS + +/******************************************************************************* + * Device API - Main entry point of the Solstice Anim library. Applications + * use the sanim_device to create others Solstice Anim resources. + ******************************************************************************/ +SANIM_API res_T +sanim_device_create +(struct logger* logger, /* May be NULL <=> use default logger */ + struct mem_allocator* allocator, /* May be NULL <=> use default allocator */ + const unsigned nthreads_hint, /* Hint on the number of threads to use */ + const int verbose, /* Make the library more verbose */ + struct sanim_device** dev); + +SANIM_API res_T +sanim_device_ref_get +(struct sanim_device* dev); + +SANIM_API res_T +sanim_device_ref_put +(struct sanim_device* dev); + +/******************************************************************************* + * Node API. + ******************************************************************************/ +SANIM_API res_T +sanim_node_create + (struct sanim_device* dev, + struct sanim_node** node); + +SANIM_API res_T +sanim_node_add_child + (struct sanim_node* node, + struct sanim_node* child); + +SANIM_API res_T +sanim_node_ref_get + (struct sanim_node* node); + +SANIM_API res_T +sanim_node_ref_put + (struct sanim_node* node); + +END_DECLS + +#endif /* SANIM_H */ diff --git a/src/sanim_c.h b/src/sanim_c.h @@ -0,0 +1,21 @@ +/* Copyright (C) CNRS 2016 +* +* 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 SANIM_C_H +#define SANIM_C_H + +#include "sanim.h" + +#endif /* SANIM_C_H */ diff --git a/src/sanim_device.c b/src/sanim_device.c @@ -0,0 +1,135 @@ +/* Copyright (C) CNRS 2016 +* +* 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 "sanim.h" +#include "sanim_device_c.h" + +#include <rsys/logger.h> +#include <rsys/mem_allocator.h> + +#include <omp.h> + +/******************************************************************************* +* Helper functions +******************************************************************************/ +static INLINE void +log_msg + (struct sanim_device* dev, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(dev && msg); + if (dev->verbose) { + res_T res; (void) res; + res = logger_vprint(dev->logger, stream, msg, vargs); + ASSERT(res == RES_OK); + } +} + +static void +device_release(ref_T* ref) +{ + struct sanim_device* dev; + ASSERT(ref); + dev = CONTAINER_OF(ref, struct sanim_device, ref); + MEM_RM(dev->allocator, dev); +} + +/******************************************************************************* +* Exported ssol_device functions +******************************************************************************/ +res_T +sanim_device_create + (struct logger* logger, + struct mem_allocator* mem_allocator, + const unsigned nthreads_hint, + const int verbose, + struct sanim_device** out_dev) +{ + struct sanim_device* dev = NULL; + struct mem_allocator* allocator; + res_T res = RES_OK; + + if (nthreads_hint == 0 || !out_dev) { + res = RES_BAD_ARG; + goto error; + } + + allocator = mem_allocator ? mem_allocator : &mem_default_allocator; + dev = MEM_CALLOC(allocator, 1, sizeof(struct sanim_device)); + if (!dev) { + res = RES_MEM_ERR; + goto error; + } + ref_init(&dev->ref); + dev->logger = logger ? logger : LOGGER_DEFAULT; + dev->allocator = allocator; + dev->verbose = verbose; + dev->nthreads = MMIN(nthreads_hint, (unsigned) omp_get_num_procs()); + omp_set_num_threads((int) dev->nthreads); + +exit: + if (out_dev) *out_dev = dev; + return res; +error: + if (dev) { + SANIM(device_ref_put(dev)); + dev = NULL; + } + goto exit; +} + +res_T +sanim_device_ref_get(struct sanim_device* dev) +{ + if (!dev) return RES_BAD_ARG; + ref_get(&dev->ref); + return RES_OK; +} + +res_T +sanim_device_ref_put(struct sanim_device* dev) +{ + if (!dev) return RES_BAD_ARG; + ref_put(&dev->ref, device_release); + return RES_OK; +} + +/******************************************************************************* +* Local functions +******************************************************************************/ +void +log_error(struct sanim_device* dev, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(dev && msg); + + va_start(vargs_list, msg); + log_msg(dev, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warning(struct sanim_device* dev, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(dev && msg); + + va_start(vargs_list, msg); + log_msg(dev, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} + diff --git a/src/sanim_device_c.h b/src/sanim_device_c.h @@ -0,0 +1,58 @@ +/* Copyright (C) CNRS 2016 +* +* 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 SANIM_DEVICE_C_H +#define SANIM_DEVICE_C_H + +#include <rsys/ref_count.h> +#include <rsys/mem_allocator.h> + +struct logger; + +struct sanim_device { + struct logger* logger; + struct mem_allocator* allocator; + unsigned nthreads; + int verbose; + + ref_T ref; +}; + +/* Conditionally log a message on the LOG_ERROR stream of the device logger, +* with respect to the device verbose flag */ +extern LOCAL_SYM void +log_error +(struct sanim_device* dev, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif + ; + +/* Conditionally log a message on the LOG_WARNING stream of the device logger, +* with respect to the device verbose flag */ +extern LOCAL_SYM void +log_warning +(struct sanim_device* dev, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif + ; + +#endif /* SANIM_DEVICE_C_H */ + diff --git a/src/sanim_node.c b/src/sanim_node.c @@ -0,0 +1,140 @@ +/* Copyright (C) CNRS 2016 +* +* 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 "sanim_node_c.h" +#include "sanim_device_c.h" +#include "sanim.h" + +#include <rsys/mem_allocator.h> +#include <rsys/ref_count.h> + +/******************************************************************************* +* Helper functions +******************************************************************************/ +static void +node_release(ref_T* ref) +{ + struct sanim_device* dev; + struct sanim_node* node = CONTAINER_OF(ref, struct sanim_node, ref); + ASSERT(ref); + dev = node->dev; + ASSERT(dev && dev->allocator); + darray_children_release(&node->children); + /* FIXME: use refcount for father/children? */ + MEM_RM(dev->allocator, node); + SANIM(device_ref_put(dev)); +} + +static int +is_ascendant(const struct sanim_node* node, const struct sanim_node* ascendant) +{ + ASSERT(node && ascendant); + while (node) { + if (node == ascendant) return 1; + node = node->father; + } + return 0; +} + +/******************************************************************************* +* Exported ssol_spectrum functions +******************************************************************************/ +res_T +sanim_node_create + (struct sanim_device* dev, + struct sanim_node** out_node) +{ + struct sanim_node* node = NULL; + res_T res = RES_OK; + + if (!dev || !out_node) { + res = RES_BAD_ARG; + goto error; + } + + node = (struct sanim_node*)MEM_CALLOC + (dev->allocator, 1, sizeof(struct sanim_node)); + if (!node) { + res = RES_MEM_ERR; + goto error; + } + + darray_children_init(dev->allocator, &node->children); + + SANIM(device_ref_get(dev)); + node->dev = dev; + ref_init(&node->ref); + +exit: + if (out_node) *out_node = node; + return res; +error: + if (node) { + SANIM(node_ref_put(node)); + node = NULL; + } + goto exit; +} + +res_T +sanim_node_add_child + (struct sanim_node* node, + struct sanim_node* child) +{ + res_T res = RES_OK; + + if (!node || !child) return RES_BAD_ARG; + if (child->father) { + log_warning + (node->dev, "%s: the node has a father already.\n", FUNC_NAME); + return RES_BAD_ARG; + } + if (is_ascendant(node, child)) { + log_warning + (node->dev, "%s: creating a cycle.\n", FUNC_NAME); + return RES_BAD_ARG; + } + + child->father = node; + res = darray_children_push_back(&node->children, &child); + if (res != RES_OK) { + goto error; + } + /* FIXME: use refcount for father/children? */ + +exit: + return res; +error: + child->father = NULL; + goto exit; +} + +res_T +sanim_node_ref_get + (struct sanim_node* node) +{ + if (!node) return RES_BAD_ARG; + ref_get(&node->ref); + return RES_OK; +} + +res_T +sanim_node_ref_put + (struct sanim_node* node) +{ + if (!node) return RES_BAD_ARG; + ref_put(&node->ref, node_release); + return RES_OK; +} diff --git a/src/sanim_node_c.h b/src/sanim_node_c.h @@ -0,0 +1,36 @@ +/* Copyright (C) CNRS 2016 +* +* 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 SANIM_NODE_C_H +#define SANIM_NODE_C_H + +#include <rsys/ref_count.h> +#include <rsys/ref_count.h> +#include <rsys/dynamic_array.h> + +/* Define the darray_children data structure */ +#define DARRAY_NAME children +#define DARRAY_DATA struct sanim_node* +#include <rsys/dynamic_array.h> + +struct sanim_node { + struct sanim_node* father; /* can be NULL: root node */ + struct darray_children children; + + struct sanim_device* dev; + ref_T ref; +}; + +#endif /* SANIM_NODE_C_H */ diff --git a/src/test_sanim_device.c b/src/test_sanim_device.c @@ -0,0 +1,72 @@ +/* Copyright (C) CNRS 2016 +* +* 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 "sanim.h" +#include "test_sanim_utils.h" + +#include <rsys/logger.h> + +int +main(int argc, char** argv) +{ + struct logger logger; + struct mem_allocator allocator; + struct sanim_device* dev; + (void) argc, (void) argv; + + CHECK(sanim_device_create(NULL, NULL, 0, 0, NULL), RES_BAD_ARG); + CHECK(sanim_device_create(NULL, NULL, SANIM_NTHREADS_DEFAULT, 0, &dev), RES_OK); + + CHECK(sanim_device_ref_get(NULL), RES_BAD_ARG); + CHECK(sanim_device_ref_get(dev), RES_OK); + CHECK(sanim_device_ref_put(NULL), RES_BAD_ARG); + CHECK(sanim_device_ref_put(dev), RES_OK); + CHECK(sanim_device_ref_put(dev), RES_OK); + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHECK(MEM_ALLOCATED_SIZE(&allocator), 0); + CHECK(sanim_device_create(NULL, &allocator, 2, 0, NULL), RES_BAD_ARG); + CHECK(sanim_device_create + (NULL, &allocator, SANIM_NTHREADS_DEFAULT, 0, &dev), RES_OK); + CHECK(sanim_device_ref_put(dev), RES_OK); + CHECK(MEM_ALLOCATED_SIZE(&allocator), 0); + + CHECK(logger_init(&allocator, &logger), RES_OK); + logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); + logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); + logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); + + CHECK(sanim_device_create(&logger, NULL, 4, 0, NULL), RES_BAD_ARG); + CHECK(sanim_device_create + (&logger, NULL, SANIM_NTHREADS_DEFAULT, 0, &dev), RES_OK); + CHECK(sanim_device_ref_put(dev), RES_OK); + + CHECK(sanim_device_create(&logger, &allocator, 2, 0, NULL), RES_BAD_ARG); + CHECK(sanim_device_create + (&logger, &allocator, SANIM_NTHREADS_DEFAULT, 0, &dev), RES_OK); + CHECK(sanim_device_ref_put(dev), RES_OK); + + CHECK(sanim_device_create(&logger, &allocator, 0, 0, &dev), RES_BAD_ARG); + CHECK(sanim_device_create + (&logger, &allocator, SANIM_NTHREADS_DEFAULT, 0, &dev), RES_OK); + CHECK(sanim_device_ref_put(dev), RES_OK); + + logger_release(&logger); + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +} diff --git a/src/test_sanim_node.c b/src/test_sanim_node.c @@ -0,0 +1,70 @@ +/* Copyright (C) CNRS 2016 +* +* 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 "sanim.h" +#include "test_sanim_utils.h" + +#include <rsys/logger.h> + +int +main(int argc, char** argv) +{ + struct logger logger; + struct mem_allocator allocator; + struct sanim_device* dev; + struct sanim_node* node1; + struct sanim_node* node2; + (void) argc, (void) argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHECK(logger_init(&allocator, &logger), RES_OK); + logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); + logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); + logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); + + CHECK(sanim_device_create + (&logger, &allocator, SANIM_NTHREADS_DEFAULT, 0, &dev), RES_OK); + + CHECK(sanim_node_create(NULL, &node1), RES_BAD_ARG); + CHECK(sanim_node_create(dev, NULL), RES_BAD_ARG); + CHECK(sanim_node_create(dev, &node1), RES_OK); + CHECK(sanim_node_ref_get(NULL), RES_BAD_ARG); + CHECK(sanim_node_ref_get(node1), RES_OK); + CHECK(sanim_node_ref_put(NULL), RES_BAD_ARG); + CHECK(sanim_node_ref_put(node1), RES_OK); + CHECK(sanim_node_ref_put(node1), RES_OK); + + CHECK(sanim_node_create(dev, &node1), RES_OK); + CHECK(sanim_node_create(dev, &node2), RES_OK); + + CHECK(sanim_node_add_child(NULL, node1), RES_BAD_ARG); + CHECK(sanim_node_add_child(node1, NULL), RES_BAD_ARG); + CHECK(sanim_node_add_child(node1, node1), RES_BAD_ARG); + CHECK(sanim_node_add_child(node1, node2), RES_OK); + CHECK(sanim_node_add_child(node1, node2), RES_BAD_ARG); + CHECK(sanim_node_add_child(node2, node1), RES_BAD_ARG); + + CHECK(sanim_node_ref_put(node1), RES_OK); + CHECK(sanim_node_ref_put(node2), RES_OK); + + CHECK(sanim_device_ref_put(dev), RES_OK); + + logger_release(&logger); + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +} diff --git a/src/test_sanim_utils.h b/src/test_sanim_utils.h @@ -0,0 +1,41 @@ +/* Copyright (C) CNRS 2016 +* +* 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 TEST_SANIM_UTILS_H +#define TEST_SANIM_UTILS_H + +#include <rsys/mem_allocator.h> +#include <stdio.h> + +static INLINE void +log_stream(const char* msg, void* ctx) +{ + ASSERT(msg); + (void) msg, (void) ctx; + printf("%s\n", msg); +} + +static INLINE void +check_memory_allocator(struct mem_allocator* allocator) +{ + if (MEM_ALLOCATED_SIZE(allocator)) { + char dump[512]; + MEM_DUMP(allocator, dump, sizeof(dump) / sizeof(char)); + fprintf(stderr, "%s\n", dump); + FATAL("Memory leaks\n"); + } +} + +#endif /* TEST_SANIM_UTILS_H */