Skip to content

Commit

Permalink
Refactor mpool_calloc function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Happy authored and Happy committed May 12, 2024
1 parent c469828 commit 955f497
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/mpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 955f497

Please sign in to comment.