Skip to content

Commit

Permalink
net/bufpool: Use SEM_INITIALIZER to init sem
Browse files Browse the repository at this point in the history
Signed-off-by: Zhe Weng <[email protected]>
  • Loading branch information
wengzhe committed Jan 2, 2025
1 parent 611a033 commit 16620c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
18 changes: 3 additions & 15 deletions net/utils/net_bufpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@ struct net_bufnode_s
void net_bufpool_init(FAR struct net_bufpool_s *pool)
{
int i;
unsigned int maxalloc;

if (pool->dynalloc > 0)
{
maxalloc = pool->u.maxalloc > 0 ? pool->u.maxalloc : INT16_MAX;
}
else
{
maxalloc = pool->prealloc;
}

nxsem_init(&pool->u.sem, 0, maxalloc);

sq_init(&pool->freebuffers);
for (i = 0; i < pool->prealloc; i++)
Expand Down Expand Up @@ -107,7 +95,7 @@ FAR void *net_bufpool_timedalloc(FAR struct net_bufpool_s *pool,
int ret;
int i;

ret = net_sem_timedwait_uninterruptible(&pool->u.sem, timeout);
ret = net_sem_timedwait_uninterruptible(&pool->sem, timeout);
if (ret != OK)
{
return NULL;
Expand Down Expand Up @@ -168,7 +156,7 @@ void net_bufpool_free(FAR struct net_bufpool_s *pool, FAR void *node)
sq_addlast(&net_bufnode->node, &pool->freebuffers);
}

nxsem_post(&pool->u.sem);
nxsem_post(&pool->sem);
}

/****************************************************************************
Expand All @@ -187,7 +175,7 @@ int net_bufpool_test(FAR struct net_bufpool_s *pool)
int val = 0;
int ret;

ret = nxsem_get_value(&pool->u.sem, &val);
ret = nxsem_get_value(&pool->sem, &val);
if (ret >= 0)
{
ret = val > 0 ? OK : -ENOSPC;
Expand Down
15 changes: 6 additions & 9 deletions net/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,18 @@
* maxalloc: The number of max allocations, 0 means no limit
*/

#define NET_BUFPOOL_DECLARE(pool,nodesize,prealloc,dynalloc,maxalloc) \
#define NET_BUFPOOL_MAX(prealloc, dynalloc, maxalloc) \
(dynalloc) <= 0 ? (prealloc) : ((maxalloc) > 0 ? (maxalloc) : INT16_MAX)

#define NET_BUFPOOL_DECLARE(pool, nodesize, prealloc, dynalloc, maxalloc) \
static char pool##_buffer[prealloc][nodesize]; \
static struct net_bufpool_s pool = \
{ \
pool##_buffer[0], \
prealloc, \
dynalloc, \
nodesize, \
{ \
maxalloc \
} \
SEM_INITIALIZER(NET_BUFPOOL_MAX(prealloc, dynalloc, maxalloc)) \
};

#define NET_BUFPOOL_INIT(p) net_bufpool_init(&p)
Expand Down Expand Up @@ -128,11 +129,7 @@ struct net_bufpool_s
const int dynalloc; /* The number per dynamic allocations */
const int nodesize; /* The size of each node in the pool */

union
{
int16_t maxalloc; /* The number of max allocations, used before init */
sem_t sem; /* The semaphore for waiting for free buffers */
} u;
sem_t sem; /* The semaphore for waiting for free buffers */

sq_queue_t freebuffers;
};
Expand Down

0 comments on commit 16620c5

Please sign in to comment.