s2d_buffer.h (2877B)
1 /* Copyright (C) 2016-2021, 2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #if !defined(BUFFER_NAME) && !defined(BUFFER_DARRAY) 17 18 #ifndef S2D_BUFFER_H 19 #define S2D_BUFFER_H 20 21 #include <rsys/mem_allocator.h> 22 #include <rsys/ref_count.h> 23 24 #endif /* S2D_BUFFER_H */ 25 #else 26 /* 27 * Generate the buffer type with respect to the following macros: 28 * - BUFFER_NAME: name of the structure and prefix of the functions; 29 * - BUFFER_DARRAY: type of the dynamic array of the buffer; 30 */ 31 #if !defined(BUFFER_NAME) || !defined(BUFFER_DARRAY) 32 #error "Missing macro definition" 33 #endif 34 35 #define BUFFER_FUNC__(Func) CONCAT(CONCAT(BUFFER_NAME, _), Func) 36 #define BUFFER_DARRAY_FUNC__(Func) CONCAT(CONCAT(BUFFER_DARRAY, _), Func) 37 38 struct BUFFER_NAME { 39 struct BUFFER_DARRAY data; 40 struct mem_allocator* allocator; 41 ref_T ref; 42 }; 43 44 /******************************************************************************* 45 * Helper function 46 ******************************************************************************/ 47 static INLINE void 48 BUFFER_FUNC__(release__)(ref_T* ref) 49 { 50 struct BUFFER_NAME* buffer; 51 ASSERT(ref); 52 buffer = CONTAINER_OF(ref, struct BUFFER_NAME, ref); 53 BUFFER_DARRAY_FUNC__(release)(&buffer->data); 54 MEM_RM(buffer->allocator, buffer); 55 } 56 57 /******************************************************************************* 58 * Buffer function 59 ******************************************************************************/ 60 static INLINE res_T 61 BUFFER_FUNC__(create) 62 (struct mem_allocator* allocator, 63 struct BUFFER_NAME** out_buffer) 64 { 65 struct BUFFER_NAME* buffer; 66 ASSERT(allocator && out_buffer); 67 68 buffer = (struct BUFFER_NAME*)MEM_CALLOC 69 (allocator, 1, sizeof(struct BUFFER_NAME)); 70 if(!buffer) return RES_MEM_ERR; 71 BUFFER_DARRAY_FUNC__(init)(allocator, &buffer->data); 72 buffer->allocator = allocator; 73 ref_init(&buffer->ref); 74 *out_buffer = buffer; 75 return RES_OK; 76 } 77 78 static INLINE void 79 BUFFER_FUNC__(ref_get)(struct BUFFER_NAME* buffer) 80 { 81 ASSERT(buffer); 82 ref_get(&buffer->ref); 83 } 84 85 static INLINE void 86 BUFFER_FUNC__(ref_put)(struct BUFFER_NAME* buffer) 87 { 88 ASSERT(buffer); 89 ref_put(&buffer->ref, BUFFER_FUNC__(release__)); 90 } 91 92 #undef BUFFER_NAME 93 #undef BUFFER_DARRAY 94 95 #endif /* !BUFFER_NAME || !BUFFER_DARRAY */ 96