commit f2b2a5e9f4136042f42d966b0f89cfe42d98226d
parent 2c01501475a15af6162b33c35abe5aa08c256e43
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 9 Mar 2015 12:15:01 +0100
Implement and test the s3d_device API
Diffstat:
4 files changed, 251 insertions(+), 4 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -30,7 +30,7 @@
# knowledge of the CeCILL license and that you accept its terms.
cmake_minimum_required(VERSION 2.6)
-project(s3d CXX)
+project(star-3d C CXX)
cmake_policy(SET CMP0011 NEW)
enable_testing()
@@ -54,7 +54,7 @@ set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
-set(S3D_FILES_SRC s3d.cpp)
+set(S3D_FILES_SRC s3d_device.c)
set(S3D_FILES_INC s3d.h)
# Prepend each file in the `S3D_FILES_<SRC|INC>' list by `S3D_SOURCE_DIR'
@@ -62,8 +62,10 @@ rcmake_prepend_path(S3D_FILES_SRC ${S3D_SOURCE_DIR})
rcmake_prepend_path(S3D_FILES_INC ${S3D_SOURCE_DIR})
add_library(s3d SHARED ${S3D_FILES_SRC} ${S3D_FILES_INC})
+target_link_libraries(s3d m)
set_target_properties(s3d PROPERTIES
DEFINE_SYMBOL S3D_SHARED_BUILD
+ LINKER_LANGUAGE CXX
VERSION ${VERSION}
SOVERSION ${VERSION_MAJOR})
@@ -74,7 +76,8 @@ rcmake_setup_devel(s3d s3d ${VERSION} se/s3d_version.h)
################################################################################
if(NOT NO_TEST)
function(new_test _name)
- add_executable(${_name} ${RSYS_SOURCE_DIR}/${_name}.c)
+ add_executable(${_name} ${S3D_SOURCE_DIR}/${_name}.c)
+ target_link_libraries(${_name} s3d)
set(_libraries ${ARGN})
foreach(_lib ${_libraries})
target_link_libraries(${_name} ${_lib})
@@ -82,7 +85,7 @@ if(NOT NO_TEST)
add_test(${_name} ${_name})
endfunction(new_test)
- # Declare tests here
+ new_test(test_s3d_device RSys)
endif(NOT NO_TEST)
################################################################################
diff --git a/src/s3d_device.c b/src/s3d_device.c
@@ -0,0 +1,111 @@
+/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
+ *
+ * This software is a computer program whose purpose is to describe a
+ * virtual 3D environment that can be ray-traced and sampled both robustly
+ * and efficiently.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms. */
+
+#include "s3d.h"
+
+#include <rsys/logger.h>
+#include <rsys/mem_allocator.h>
+#include <rsys/ref_count.h>
+
+struct s3d_device {
+ struct logger* logger;
+ struct mem_allocator* allocator;
+ ref_T ref;
+};
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+device_release(ref_T* ref)
+{
+ struct s3d_device* dev;
+ ASSERT(ref);
+ dev = CONTAINER_OF(ref, struct s3d_device, ref);
+ MEM_FREE(dev->allocator, dev);
+}
+
+/*******************************************************************************
+ * Exported s3d_device functions
+ ******************************************************************************/
+res_T
+s3d_device_create
+ (struct logger* logger,
+ struct mem_allocator* mem_allocator,
+ struct s3d_device** out_dev)
+{
+ struct s3d_device* dev = NULL;
+ struct mem_allocator* allocator;
+ res_T res = RES_OK;
+
+ if(!out_dev) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
+ dev = MEM_CALLOC(allocator, 1, sizeof(struct s3d_device));
+ if(!dev) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ dev->logger = logger ? logger : LOGGER_DEFAULT;
+ dev->allocator = allocator;
+ ref_init(&dev->ref);
+
+exit:
+ if(out_dev) *out_dev = dev;
+ return res;
+error:
+ if(dev) {
+ S3D(device_ref_put(dev));
+ dev = NULL;
+ }
+ goto exit;
+}
+
+res_T
+s3d_device_ref_get(struct s3d_device* dev)
+{
+ if(!dev) return RES_BAD_ARG;
+ ref_get(&dev->ref);
+ return RES_OK;
+}
+
+res_T
+s3d_device_ref_put(struct s3d_device* dev)
+{
+ if(!dev) return RES_BAD_ARG;
+ ref_put(&dev->ref, device_release);
+ return RES_OK;
+}
+
diff --git a/src/test_s3d_device.c b/src/test_s3d_device.c
@@ -0,0 +1,88 @@
+/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
+ *
+ * This software is a computer program whose purpose is to describe a
+ * virtual 3D environment that can be ray-traced and sampled both robustly
+ * and efficiently.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms. */
+
+#include "s3d.h"
+#include "test_s3d_utils.h"
+#include <rsys/logger.h>
+
+static void
+log_stream(const char* msg, void* ctx)
+{
+ ASSERT(msg);
+ (void)msg, (void)ctx;
+ printf("%s\n", msg);
+}
+
+int
+main(int argc, char** argv)
+{
+ struct logger logger;
+ struct mem_allocator allocator;
+ struct s3d_device* dev;
+ (void)argc, (void)argv;
+
+ CHECK(s3d_device_create(NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_device_create(NULL, NULL, &dev), RES_OK);
+
+ CHECK(s3d_device_ref_get(NULL), RES_BAD_ARG);
+ CHECK(s3d_device_ref_get(dev), RES_OK);
+ CHECK(s3d_device_ref_put(NULL), RES_BAD_ARG);
+ CHECK(s3d_device_ref_put(dev), RES_OK);
+ CHECK(s3d_device_ref_put(dev), RES_OK);
+
+ mem_init_proxy_allocator(&allocator, &mem_default_allocator);
+
+ CHECK(MEM_ALLOCATED_SIZE(&allocator), 0);
+ CHECK(s3d_device_create(NULL, &allocator, NULL), RES_BAD_ARG);
+ CHECK(s3d_device_create(NULL, &allocator, &dev), RES_OK);
+ CHECK(s3d_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(s3d_device_create(&logger, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_device_create(&logger, NULL, &dev), RES_OK);
+ CHECK(s3d_device_ref_put(dev), RES_OK);
+
+ CHECK(s3d_device_create(&logger, &allocator, NULL), RES_BAD_ARG);
+ CHECK(s3d_device_create(&logger, &allocator, &dev), RES_OK);
+ CHECK(s3d_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_s3d_utils.h b/src/test_s3d_utils.h
@@ -0,0 +1,45 @@
+/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
+ *
+ * This software is a computer program whose purpose is to describe a
+ * virtual 3D environment that can be ray-traced and sampled both robustly
+ * and efficiently.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms. */
+
+#include <rsys/mem_allocator.h>
+#include <stdio.h>
+
+static 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");
+ }
+}