#pragma once #include #include #ifdef __cplusplus extern "C" { #endif struct rt_pool; void *rt_pool_get(struct rt_pool *); void rt_pool_put(struct rt_pool *, void *); struct rt_pool { struct rt_mutex mutex; struct rt_cond cond; void **ptrs; size_t avail; }; #define RT_POOL_ARRAY_COUNT(arr) (sizeof(arr) / sizeof((arr)[0])) #define RT_POOL_INIT(name, ptrs_, num) \ { \ .mutex = RT_MUTEX_INIT(name.mutex), \ .cond = RT_COND_INIT(name.cond), \ .ptrs = (ptrs_), \ .avail = (num), \ } #define RT_POOL_COMMON(name, arr, storage) \ static void *name##_ptrs[RT_POOL_ARRAY_COUNT(arr)]; \ __attribute__((constructor)) static void name##_ptrs_init(void) \ { \ for (size_t i = 0; i < RT_POOL_ARRAY_COUNT(arr); ++i) \ { \ name##_ptrs[i] = &(arr)[i]; \ } \ } \ storage struct rt_pool name = \ RT_POOL_INIT(name, name##_ptrs, RT_POOL_ARRAY_COUNT(arr)) #define RT_POOL(name, objects) RT_POOL_COMMON(name, objects, ) #define RT_POOL_STATIC(name, objects) RT_POOL_COMMON(name, objects, static) #ifdef __cplusplus } #endif