htrdr_buffer.c (3811B)
1 /* Copyright (C) 2018-2019, 2022-2025 Centre National de la Recherche Scientifique 2 * Copyright (C) 2020-2022 Institut Mines Télécom Albi-Carmaux 3 * Copyright (C) 2022-2025 Institut Pierre-Simon Laplace 4 * Copyright (C) 2022-2025 Institut de Physique du Globe de Paris 5 * Copyright (C) 2018-2025 |Méso|Star> (contact@meso-star.com) 6 * Copyright (C) 2022-2025 Observatoire de Paris 7 * Copyright (C) 2022-2025 Université de Reims Champagne-Ardenne 8 * Copyright (C) 2022-2025 Université de Versaille Saint-Quentin 9 * Copyright (C) 2018-2019, 2022-2025 Université Paul Sabatier 10 * 11 * This program is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation, either version 3 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 23 24 #include "core/htrdr.h" 25 #include "core/htrdr_buffer.h" 26 #include "core/htrdr_log.h" 27 28 #include <rsys/math.h> 29 #include <rsys/mem_allocator.h> 30 #include <rsys/ref_count.h> 31 32 struct htrdr_buffer { 33 struct htrdr_buffer_layout layout; 34 char* mem; 35 36 struct htrdr* htrdr; 37 ref_T ref; 38 }; 39 40 /******************************************************************************* 41 * Helper functions 42 ******************************************************************************/ 43 static void 44 buffer_release(ref_T* ref) 45 { 46 struct htrdr_buffer* buf = NULL; 47 struct htrdr* htrdr = NULL; 48 ASSERT(ref); 49 buf = CONTAINER_OF(ref, struct htrdr_buffer, ref); 50 htrdr = buf->htrdr; 51 if(buf->mem) MEM_RM(htrdr_get_allocator(htrdr), buf->mem); 52 MEM_RM(htrdr_get_allocator(htrdr), buf); 53 htrdr_ref_put(htrdr); 54 } 55 56 /******************************************************************************* 57 * Local functions 58 ******************************************************************************/ 59 res_T 60 htrdr_buffer_create 61 (struct htrdr* htrdr, 62 const struct htrdr_buffer_layout* layout, 63 struct htrdr_buffer** out_buf) 64 { 65 struct htrdr_buffer* buf = NULL; 66 size_t memsz = 0; 67 res_T res = RES_OK; 68 ASSERT(htrdr && layout && out_buf); 69 70 if((res = htrdr_buffer_layout_check(layout)) != RES_OK) { 71 htrdr_log_err(htrdr, "Invalid buffer memory layout.\n"); 72 goto error; 73 } 74 75 buf = MEM_CALLOC(htrdr_get_allocator(htrdr), 1, sizeof(*buf)); 76 if(!buf) { 77 res = RES_MEM_ERR; 78 goto error; 79 } 80 ref_init(&buf->ref); 81 buf->layout = *layout; 82 htrdr_ref_get(htrdr); 83 buf->htrdr = htrdr; 84 85 memsz = buf->layout.pitch * buf->layout.height; 86 buf->mem = MEM_ALLOC_ALIGNED 87 (htrdr_get_allocator(htrdr), memsz, buf->layout.alignment); 88 if(!buf->mem) { 89 res = RES_MEM_ERR; 90 goto error; 91 } 92 93 exit: 94 *out_buf = buf; 95 return res; 96 error: 97 if(buf) { 98 htrdr_buffer_ref_put(buf); 99 buf = NULL; 100 } 101 goto exit; 102 } 103 104 void 105 htrdr_buffer_ref_get(struct htrdr_buffer* buf) 106 { 107 ASSERT(buf); 108 ref_get(&buf->ref); 109 } 110 111 void 112 htrdr_buffer_ref_put(struct htrdr_buffer* buf) 113 { 114 ASSERT(buf); 115 ref_put(&buf->ref, buffer_release); 116 } 117 118 void 119 htrdr_buffer_get_layout 120 (const struct htrdr_buffer* buf, 121 struct htrdr_buffer_layout* layout) 122 { 123 ASSERT(buf && layout); 124 *layout = buf->layout; 125 } 126 127 void* 128 htrdr_buffer_get_data(struct htrdr_buffer* buf) 129 { 130 ASSERT(buf); 131 return buf->mem; 132 } 133 134 void* 135 htrdr_buffer_at(struct htrdr_buffer* buf, const size_t x, const size_t y) 136 { 137 ASSERT(buf && x < buf->layout.width && y < buf->layout.height); 138 return buf->mem + y*buf->layout.pitch + x*buf->layout.elmt_size; 139 } 140