htrdr

Solving radiative transfer in heterogeneous media
git clone git://git.meso-star.fr/htrdr.git
Log | Files | Refs | README | LICENSE

commit 12137598531c69e2df1c6a74e40dd0317f736e9e
parent f5071d2e814514fdf8a12d8f8ad3002eac9ced6e
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 24 May 2018 14:30:34 +0200

Define and implement the buffer API

Diffstat:
Mcmake/CMakeLists.txt | 4+++-
Asrc/htrdr_buffer.c | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/htrdr_buffer.h | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 217 insertions(+), 1 deletion(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -47,14 +47,16 @@ set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) set(HTRDR_FILES_SRC htrdr.c - htrdr_main.c htrdr_args.c + htrdr_buffer.c htrdr_clouds.c + htrdr_main.c htrdr_rectangle.c htrdr_transmission.c) set(HTRDR_FILES_INC htrdr.h htrdr_args.h + htrdr_buffer.h htrdr_clouds.h htrdr_rectangle.h htrdr_solve.h) diff --git a/src/htrdr_buffer.c b/src/htrdr_buffer.c @@ -0,0 +1,141 @@ +/* Copyright (C) 2018 Université Paul Sabatier, |Meso|Star> + * + * 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 "htrdr.h" +#include "htrdr_buffer.h" + +#include <rsys/mem_allocator.h> +#include <rsys/ref_count.h> + +struct htrdr_buffer { + char* mem; + + size_t width; + size_t height; + size_t pitch; + size_t elmtsz; + size_t align; + + struct htrdr* htrdr; + ref_T ref; +}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +buffer_release(ref_T* ref) +{ + struct htrdr_buffer* buf = NULL; + struct htrdr* htrdr = NULL; + ASSERT(ref); + buf = CONTAINER_OF(ref, struct htrdr_buffer, ref); + htrdr = buf->htrdr; + if(buf->mem) MEM_RM(htrdr->allocator, buf->mem); + MEM_RM(htrdr->allocator, buf); + htrdr_ref_put(htrdr); +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +htrdr_buffer_create + (struct htrdr* htrdr, + const size_t width, + const size_t height, + const size_t pitch, + const size_t elmtsz, + const size_t align, + struct htrdr_buffer** out_buf) +{ + struct htrdr_buffer* buf = NULL; + size_t memsz = 0; + res_T res = RES_OK; + ASSERT(htrdr && width && height && pitch && elmt_size && align && out_buf); + ASSERT(IS_POW2(align) && width <= pitch); + + buf = MEM_CALLOC(htrdr->allocator, 1, sizeof(*buf)); + if(!buf) { + res = RES_MEM_ERR; + goto error; + } + ref_init(&buf->ref); + htrdr_ref_get(htrdr); + buf->htrdr = htrdr; + buf->width = width; + buf->height = height; + buf->pitch = pitch; + buf->elmtsz = elmtsz; + buf->align = align; + + memsz = buf->pitch * buf->height * buf->elmtsz; + buf->mem = MEM_ALLOC_ALIGNED(htrdr->allocator, memsz, align); + if(!buf->mem) { + res = RES_MEM_ERR; + goto error; + } + +exit: + *out_buf = buf; + return res; +error: + if(buf) { + htrdr_buffer_ref_put(buf); + buf = NULL; + } + goto exit; +} + +void +htrdr_buffer_ref_get(struct htrdr_buffer* buf) +{ + ASSERT(buf); + ref_get(&buf->ref); +} + +void +htrdr_buffer_ref_put(struct htrdr_buffer* buf) +{ + ASSERT(buf); + ref_put(&buf->ref, buffer_release); +} + +void +htrdr_buffer_get_layout + (const struct htrdr_buffer* buf, + struct htrdr_buffer_layout* layout) +{ + ASSERT(buf && layout); + layout->width = buf->width; + layout->height = buf->height; + layout->pitch = buf->pitch; + layout->elmt_size = buf->elmtsz; + layout->alignment = buf->align; +} + +void* +htrdr_buffer_get_data(struct htrdr_buffer* buf) +{ + ASSERT(buf); + return buf->mem; +} + +void* +htrdr_buffer_at(struct htrdr_buffer* buf, const size_t x, const size_t y) +{ + ASSERT(buf && x < buf->width && y < buf->height); + return buf->mem + y*buf->pitch + x*buf->elmtsz; +} diff --git a/src/htrdr_buffer.h b/src/htrdr_buffer.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2018 Université Paul Sabatier, |Meso|Star> + * + * 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 HTRDR_BUFFER_H +#define HTRDR_BUFFER_H + +#include <rsys/rsys.h> + +/* + * Row major ordered 2D buffer + */ + +struct htrdr_buffer_layout { + size_t width; /* #elements in X */ + size_t height; /* #elements in Y */ + size_t pitch; /* #Bytes between 2 consecutive line */ + size_t elmt_size; /* Size of an element in the buffer */ + size_t alignment; /* Alignement of the memory */ +}; +#define HTRDR_BUFFER_LAYOUT_NULL__ {0,0,0,0,0} +static const struct htrdr_buffer_layout HTRDR_BUFFER_LAYOUT_NULL = + HTRDR_BUFFER_LAYOUT_NULL__; + +/* Forward declarations */ +struct htrdr; +struct htrdr_buffer; + +extern LOCAL_SYM res_T +htrdr_buffer_create + (struct htrdr* htrdr, + const size_t width, + const size_t height, + const size_t pitch, /* #Bytes between 2 consecutive line */ + const size_t elmt_size, /* Size of an element in the buffer */ + const size_t alignment, /* Alignement of the buffer */ + struct htrdr_buffer** buf); + +extern LOCAL_SYM void +htrdr_buffer_ref_get + (struct htrdr_buffer* buf); + +extern LOCAL_SYM void +htrdr_buffer_ref_put + (struct htrdr_buffer* buf); + +extern LOCAL_SYM void +htrdr_buffer_get_layout + (const struct htrdr_buffer* buf, + struct htrdr_buffer_layout* layout); + +extern LOCAL_SYM void* +htrdr_buffer_get_data + (struct htrdr_buffer* buf); + +extern LOCAL_SYM void* +htrdr_buffer_at + (struct htrdr_buffer* buf, + const size_t x, + const size_t y); + +#endif /* HTRDR_BUFFER_H */