From 955f497d771c0068d8c51b8dfc9ae29249e25cda Mon Sep 17 00:00:00 2001 From: Happy Date: Sat, 11 May 2024 00:27:57 +0800 Subject: [PATCH] Refactor mpool_calloc function The mpool_calloc function contains code identical to that found in the mpool_alloc function. Replacing part of the code in the mpool_alloc and mpool_calloc functions with the mpool_alloc_helper function. --- src/mpool.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/mpool.c b/src/mpool.c index ae71d976d..9435537f8 100644 --- a/src/mpool.c +++ b/src/mpool.c @@ -107,25 +107,26 @@ static void *mpool_extend(mpool_t *mp) return p; } -void *mpool_alloc(mpool_t *mp) +FORCE_INLINE void *mpool_alloc_helper(mpool_t *mp) { - if (!mp->chunk_count && !(mpool_extend(mp))) - return NULL; - char *ptr = (char *) mp->free_chunk_head + sizeof(memchunk_t); mp->free_chunk_head = mp->free_chunk_head->next; mp->chunk_count--; return ptr; } -void *mpool_calloc(mpool_t *mp) +void *mpool_alloc(mpool_t *mp) { if (!mp->chunk_count && !(mpool_extend(mp))) return NULL; + return mpool_alloc_helper(mp); +} - char *ptr = (char *) mp->free_chunk_head + sizeof(memchunk_t); - mp->free_chunk_head = mp->free_chunk_head->next; - mp->chunk_count--; +void *mpool_calloc(mpool_t *mp) +{ + if (!mp->chunk_count && !(mpool_extend(mp))) + return NULL; + char *ptr = mpool_alloc_helper(mp); memset(ptr, 0, mp->chunk_size); return ptr; }