From e89696d911732573d17b8abce8c1f9d4ab92e5fa Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Thu, 16 Dec 2021 16:26:01 -0500 Subject: [PATCH] refactor: DRY up pool macro --- runtime/include/pool.h | 173 +++++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 91 deletions(-) diff --git a/runtime/include/pool.h b/runtime/include/pool.h index 26d7527..fb83743 100644 --- a/runtime/include/pool.h +++ b/runtime/include/pool.h @@ -8,95 +8,86 @@ #include "lock.h" #include "ps_list.h" -#define INIT_POOL(STRUCT_NAME, DTOR_FN) \ - struct STRUCT_NAME##_pool { \ - bool use_lock; \ - lock_t lock; \ - struct ps_list_head list; \ - }; \ - \ - static inline bool STRUCT_NAME##_pool_is_empty(struct STRUCT_NAME##_pool *self) \ - { \ - assert(self != NULL); \ - \ - return ps_list_head_empty(&self->list); \ - } \ - \ - static inline void STRUCT_NAME##_pool_init(struct STRUCT_NAME##_pool *self, bool use_lock) \ - { \ - ps_list_head_init(&self->list); \ - self->use_lock = use_lock; \ - if (use_lock) LOCK_INIT(&self->lock); \ - } \ - \ - static inline void STRUCT_NAME##_pool_deinit(struct STRUCT_NAME##_pool *self) \ - { \ - if (STRUCT_NAME##_pool_is_empty(self)) return; \ - struct STRUCT_NAME *iterator = NULL; \ - struct STRUCT_NAME *buffer = NULL; \ - ps_list_foreach_del_d(&self->list, iterator, buffer) \ - { \ - ps_list_rem_d(iterator); \ - DTOR_FN(iterator); \ - } \ - } \ - \ - static inline struct STRUCT_NAME *STRUCT_NAME##_pool_remove_nolock(struct STRUCT_NAME##_pool *self) \ - { \ - assert(self != NULL); \ - assert(!self->use_lock || LOCK_IS_LOCKED(&self->lock)); \ - \ - struct STRUCT_NAME *obj = NULL; \ - \ - if (STRUCT_NAME##_pool_is_empty(self)) return obj; \ - \ - obj = ps_list_head_first_d(&self->list, struct STRUCT_NAME); \ - assert(obj); \ - ps_list_rem_d(obj); \ - \ - return obj; \ - } \ - \ - static inline struct STRUCT_NAME *STRUCT_NAME##_pool_remove(struct STRUCT_NAME##_pool *self) \ - { \ - assert(self != NULL); \ - assert(self->use_lock); \ - \ - struct STRUCT_NAME *obj = NULL; \ - \ - if (STRUCT_NAME##_pool_is_empty(self)) return obj; \ - \ - LOCK_LOCK(&self->lock); \ - if (STRUCT_NAME##_pool_is_empty(self)) { \ - LOCK_UNLOCK(&self->lock); \ - return obj; \ - } \ - \ - obj = ps_list_head_first_d(&self->list, struct STRUCT_NAME); \ - assert(obj); \ - ps_list_rem_d(obj); \ - LOCK_UNLOCK(&self->lock); \ - return obj; \ - } \ - \ - static inline int STRUCT_NAME##_pool_add_nolock(struct STRUCT_NAME##_pool *self, struct STRUCT_NAME *obj) \ - { \ - assert(self != NULL); \ - assert(obj != NULL); \ - assert(!self->use_lock || LOCK_IS_LOCKED(&self->lock)); \ - \ - ps_list_head_add_d(&self->list, obj); \ - return 0; \ - } \ - \ - static inline int STRUCT_NAME##_pool_add(struct STRUCT_NAME##_pool *self, struct STRUCT_NAME *obj) \ - { \ - assert(self != NULL); \ - assert(obj != NULL); \ - assert(self->use_lock); \ - \ - LOCK_LOCK(&self->lock); \ - ps_list_head_add_d(&self->list, obj); \ - LOCK_UNLOCK(&self->lock); \ - return 0; \ +#define INIT_POOL(STRUCT_NAME, DTOR_FN) \ + struct STRUCT_NAME##_pool { \ + bool use_lock; \ + lock_t lock; \ + struct ps_list_head list; \ + }; \ + \ + static inline bool STRUCT_NAME##_pool_is_empty(struct STRUCT_NAME##_pool *self) \ + { \ + assert(self != NULL); \ + \ + return ps_list_head_empty(&self->list); \ + } \ + \ + static inline void STRUCT_NAME##_pool_init(struct STRUCT_NAME##_pool *self, bool use_lock) \ + { \ + ps_list_head_init(&self->list); \ + self->use_lock = use_lock; \ + if (use_lock) LOCK_INIT(&self->lock); \ + } \ + \ + static inline void STRUCT_NAME##_pool_deinit(struct STRUCT_NAME##_pool *self) \ + { \ + if (STRUCT_NAME##_pool_is_empty(self)) return; \ + struct STRUCT_NAME *iterator = NULL; \ + struct STRUCT_NAME *buffer = NULL; \ + ps_list_foreach_del_d(&self->list, iterator, buffer) \ + { \ + ps_list_rem_d(iterator); \ + DTOR_FN(iterator); \ + } \ + } \ + \ + static inline struct STRUCT_NAME *STRUCT_NAME##_pool_remove_nolock(struct STRUCT_NAME##_pool *self) \ + { \ + assert(self != NULL); \ + assert(!self->use_lock || LOCK_IS_LOCKED(&self->lock)); \ + \ + struct STRUCT_NAME *obj = NULL; \ + \ + if (STRUCT_NAME##_pool_is_empty(self)) return obj; \ + \ + obj = ps_list_head_first_d(&self->list, struct STRUCT_NAME); \ + assert(obj); \ + ps_list_rem_d(obj); \ + \ + return obj; \ + } \ + \ + static inline struct STRUCT_NAME *STRUCT_NAME##_pool_remove(struct STRUCT_NAME##_pool *self) \ + { \ + assert(self != NULL); \ + assert(self->use_lock); \ + \ + struct STRUCT_NAME *obj = NULL; \ + bool is_empty = STRUCT_NAME##_pool_is_empty(self); \ + if (is_empty) return obj; \ + \ + LOCK_LOCK(&self->lock); \ + obj = STRUCT_NAME##_pool_remove_nolock(self); \ + LOCK_UNLOCK(&self->lock); \ + return obj; \ + } \ + \ + static inline void STRUCT_NAME##_pool_add_nolock(struct STRUCT_NAME##_pool *self, struct STRUCT_NAME *obj) \ + { \ + assert(self != NULL); \ + assert(obj != NULL); \ + assert(!self->use_lock || LOCK_IS_LOCKED(&self->lock)); \ + \ + ps_list_head_add_d(&self->list, obj); \ + } \ + \ + static inline void STRUCT_NAME##_pool_add(struct STRUCT_NAME##_pool *self, struct STRUCT_NAME *obj) \ + { \ + assert(self != NULL); \ + assert(obj != NULL); \ + assert(self->use_lock); \ + \ + LOCK_LOCK(&self->lock); \ + STRUCT_NAME##_pool_add_nolock(self, obj); \ + LOCK_UNLOCK(&self->lock); \ }