mirror of
https://github.com/apache/nuttx.git
synced 2026-05-27 03:05:40 +08:00
net/bufpool: Use SEM_INITIALIZER to init sem
Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
+3
-15
@@ -61,18 +61,6 @@ struct net_bufnode_s
|
|||||||
void net_bufpool_init(FAR struct net_bufpool_s *pool)
|
void net_bufpool_init(FAR struct net_bufpool_s *pool)
|
||||||
{
|
{
|
||||||
int i;
|
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);
|
sq_init(&pool->freebuffers);
|
||||||
for (i = 0; i < pool->prealloc; i++)
|
for (i = 0; i < pool->prealloc; i++)
|
||||||
@@ -107,7 +95,7 @@ FAR void *net_bufpool_timedalloc(FAR struct net_bufpool_s *pool,
|
|||||||
int ret;
|
int ret;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
ret = net_sem_timedwait_uninterruptible(&pool->u.sem, timeout);
|
ret = net_sem_timedwait_uninterruptible(&pool->sem, timeout);
|
||||||
if (ret != OK)
|
if (ret != OK)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -168,7 +156,7 @@ void net_bufpool_free(FAR struct net_bufpool_s *pool, FAR void *node)
|
|||||||
sq_addlast(&net_bufnode->node, &pool->freebuffers);
|
sq_addlast(&net_bufnode->node, &pool->freebuffers);
|
||||||
}
|
}
|
||||||
|
|
||||||
nxsem_post(&pool->u.sem);
|
nxsem_post(&pool->sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -187,7 +175,7 @@ int net_bufpool_test(FAR struct net_bufpool_s *pool)
|
|||||||
int val = 0;
|
int val = 0;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = nxsem_get_value(&pool->u.sem, &val);
|
ret = nxsem_get_value(&pool->sem, &val);
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
ret = val > 0 ? OK : -ENOSPC;
|
ret = val > 0 ? OK : -ENOSPC;
|
||||||
|
|||||||
+6
-9
@@ -84,7 +84,10 @@
|
|||||||
* maxalloc: The number of max allocations, 0 means no limit
|
* 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 char pool##_buffer[prealloc][nodesize]; \
|
||||||
static struct net_bufpool_s pool = \
|
static struct net_bufpool_s pool = \
|
||||||
{ \
|
{ \
|
||||||
@@ -92,9 +95,7 @@
|
|||||||
prealloc, \
|
prealloc, \
|
||||||
dynalloc, \
|
dynalloc, \
|
||||||
nodesize, \
|
nodesize, \
|
||||||
{ \
|
SEM_INITIALIZER(NET_BUFPOOL_MAX(prealloc, dynalloc, maxalloc)) \
|
||||||
maxalloc \
|
|
||||||
} \
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define NET_BUFPOOL_INIT(p) net_bufpool_init(&p)
|
#define NET_BUFPOOL_INIT(p) net_bufpool_init(&p)
|
||||||
@@ -128,11 +129,7 @@ struct net_bufpool_s
|
|||||||
const int dynalloc; /* The number per dynamic allocations */
|
const int dynalloc; /* The number per dynamic allocations */
|
||||||
const int nodesize; /* The size of each node in the pool */
|
const int nodesize; /* The size of each node in the pool */
|
||||||
|
|
||||||
union
|
sem_t sem; /* The semaphore for waiting for free buffers */
|
||||||
{
|
|
||||||
int16_t maxalloc; /* The number of max allocations, used before init */
|
|
||||||
sem_t sem; /* The semaphore for waiting for free buffers */
|
|
||||||
} u;
|
|
||||||
|
|
||||||
sq_queue_t freebuffers;
|
sq_queue_t freebuffers;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user