2014-06-04 02:41:34 +08:00
|
|
|
/****************************************************************************
|
2017-05-09 21:34:59 +08:00
|
|
|
* mm/iob/iob_alloc.c
|
2014-06-04 02:41:34 +08:00
|
|
|
*
|
2024-09-12 22:44:55 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-06-04 02:41:34 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-04 02:41:34 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-06-04 02:41:34 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-06-23 01:27:57 +08:00
|
|
|
#include <assert.h>
|
2016-01-23 07:35:06 +08:00
|
|
|
#include <errno.h>
|
2014-06-23 01:27:57 +08:00
|
|
|
|
2016-02-14 22:38:44 +08:00
|
|
|
#include <nuttx/irq.h>
|
2014-06-06 23:35:31 +08:00
|
|
|
#include <nuttx/arch.h>
|
2017-05-11 07:36:08 +08:00
|
|
|
#include <nuttx/sched.h>
|
2024-04-01 14:09:10 +08:00
|
|
|
#ifdef CONFIG_IOB_ALLOC
|
|
|
|
# include <nuttx/kmalloc.h>
|
|
|
|
#endif
|
2017-05-09 21:34:59 +08:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-04 02:41:34 +08:00
|
|
|
|
|
|
|
#include "iob.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-23 01:27:57 +08:00
|
|
|
* Private Functions
|
2014-06-04 02:41:34 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-18 11:27:47 +08:00
|
|
|
static clock_t iob_allocwait_gettimeout(clock_t start, unsigned int timeout)
|
|
|
|
{
|
|
|
|
sclock_t tick;
|
|
|
|
|
|
|
|
tick = clock_systime_ticks() - start;
|
|
|
|
if (tick >= MSEC2TICK(timeout))
|
|
|
|
{
|
|
|
|
tick = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tick = MSEC2TICK(timeout) - tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tick;
|
|
|
|
}
|
|
|
|
|
2017-05-17 01:03:35 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_alloc_committed
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer by taking the buffer at the head of the committed
|
|
|
|
* list.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
static FAR struct iob_s *iob_alloc_committed(void)
|
2017-05-17 01:03:35 +08:00
|
|
|
{
|
|
|
|
FAR struct iob_s *iob = NULL;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* We don't know what context we are called from so we use extreme measures
|
|
|
|
* to protect the committed list: We disable interrupts very briefly.
|
|
|
|
*/
|
|
|
|
|
2023-12-13 17:24:07 +08:00
|
|
|
flags = enter_critical_section();
|
2017-05-17 01:03:35 +08:00
|
|
|
|
|
|
|
/* Take the I/O buffer from the head of the committed list */
|
|
|
|
|
|
|
|
iob = g_iob_committed;
|
|
|
|
if (iob != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the I/O buffer from the committed list */
|
|
|
|
|
|
|
|
g_iob_committed = iob->io_flink;
|
|
|
|
|
|
|
|
/* Put the I/O buffer in a known state */
|
|
|
|
|
|
|
|
iob->io_flink = NULL; /* Not in a chain */
|
|
|
|
iob->io_len = 0; /* Length of the data in the entry */
|
|
|
|
iob->io_offset = 0; /* Offset to the beginning of data */
|
|
|
|
iob->io_pktlen = 0; /* Total length of the packet */
|
|
|
|
}
|
|
|
|
|
2023-12-13 17:24:07 +08:00
|
|
|
leave_critical_section(flags);
|
2017-05-17 01:03:35 +08:00
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
|
2014-06-23 01:27:57 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_allocwait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer, waiting if necessary. This function cannot be
|
|
|
|
* called from any interrupt level logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
static FAR struct iob_s *iob_allocwait(bool throttled, unsigned int timeout)
|
2014-06-23 01:27:57 +08:00
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
irqstate_t flags;
|
2014-06-25 01:53:19 +08:00
|
|
|
FAR sem_t *sem;
|
2023-05-18 11:27:47 +08:00
|
|
|
clock_t start;
|
2015-07-02 04:33:37 +08:00
|
|
|
int ret = OK;
|
2014-06-23 01:27:57 +08:00
|
|
|
|
2014-06-25 01:53:19 +08:00
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
/* Select the semaphore count to check. */
|
|
|
|
|
2014-06-25 05:38:00 +08:00
|
|
|
sem = (throttled ? &g_throttle_sem : &g_iob_sem);
|
2014-06-25 01:53:19 +08:00
|
|
|
#else
|
|
|
|
sem = &g_iob_sem;
|
|
|
|
#endif
|
|
|
|
|
2014-06-23 01:27:57 +08:00
|
|
|
/* The following must be atomic; interrupt must be disabled so that there
|
|
|
|
* is no conflict with interrupt level I/O buffer allocations. This is
|
|
|
|
* not as bad as it sounds because interrupts will be re-enabled while
|
|
|
|
* we are waiting for I/O buffers to become free.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 22:38:44 +08:00
|
|
|
flags = enter_critical_section();
|
2017-05-17 01:03:35 +08:00
|
|
|
|
|
|
|
/* Try to get an I/O buffer. If successful, the semaphore count will be
|
|
|
|
* decremented atomically.
|
|
|
|
*/
|
|
|
|
|
2023-05-18 11:27:47 +08:00
|
|
|
start = clock_systime_ticks();
|
|
|
|
iob = iob_tryalloc(throttled);
|
2017-05-17 01:03:35 +08:00
|
|
|
while (ret == OK && iob == NULL)
|
2014-06-23 01:27:57 +08:00
|
|
|
{
|
2017-05-17 01:03:35 +08:00
|
|
|
/* If not successful, then the semaphore count was less than or equal
|
|
|
|
* to zero (meaning that there are no free buffers). We need to wait
|
|
|
|
* for an I/O buffer to be released and placed in the committed
|
|
|
|
* list.
|
2014-06-23 01:27:57 +08:00
|
|
|
*/
|
|
|
|
|
2022-02-17 12:34:36 +08:00
|
|
|
if (timeout == UINT_MAX)
|
|
|
|
{
|
|
|
|
ret = nxsem_wait_uninterruptible(sem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-18 11:27:47 +08:00
|
|
|
ret = nxsem_tickwait_uninterruptible(sem,
|
|
|
|
iob_allocwait_gettimeout(start, timeout));
|
2022-02-17 12:34:36 +08:00
|
|
|
}
|
|
|
|
|
2020-01-03 00:49:34 +08:00
|
|
|
if (ret >= 0)
|
2017-05-17 01:03:35 +08:00
|
|
|
{
|
|
|
|
/* When we wake up from wait successfully, an I/O buffer was
|
2019-01-26 23:24:12 +08:00
|
|
|
* freed and we hold a count for one IOB.
|
2017-05-17 01:03:35 +08:00
|
|
|
*/
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
iob = iob_alloc_committed();
|
2014-06-23 01:27:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 22:38:44 +08:00
|
|
|
leave_critical_section(flags);
|
2014-06-23 01:27:57 +08:00
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
|
2024-04-01 14:09:10 +08:00
|
|
|
#ifdef CONFIG_IOB_ALLOC
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_free_dynamic
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dummy free callback function, do nothing.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* data -
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void iob_free_dynamic(FAR void *data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-23 01:27:57 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-02-17 12:34:36 +08:00
|
|
|
* Name: iob_timedalloc
|
2014-06-23 01:27:57 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2021-02-08 23:19:12 +08:00
|
|
|
* Allocate an I/O buffer by taking the buffer at the head of the free list.
|
2022-02-17 12:34:36 +08:00
|
|
|
* This wait will be terminated when the specified timeout expires.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* throttled - An indication of the IOB allocation is "throttled"
|
|
|
|
* timeout - Timeout value in milliseconds.
|
2014-06-23 01:27:57 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
FAR struct iob_s *iob_timedalloc(bool throttled, unsigned int timeout)
|
2014-06-23 01:27:57 +08:00
|
|
|
{
|
|
|
|
/* Were we called from the interrupt level? */
|
|
|
|
|
2022-02-17 12:34:36 +08:00
|
|
|
if (up_interrupt_context() || sched_idletask() || timeout == 0)
|
2014-06-23 01:27:57 +08:00
|
|
|
{
|
|
|
|
/* Yes, then try to allocate an I/O buffer without waiting */
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
return iob_tryalloc(throttled);
|
2014-06-23 01:27:57 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Then allocate an I/O buffer, waiting as necessary */
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
return iob_allocwait(throttled, timeout);
|
2014-06-23 01:27:57 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-28 11:23:42 +08:00
|
|
|
|
2022-02-17 12:34:36 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer by taking the buffer at the head of the free list.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
FAR struct iob_s *iob_alloc(bool throttled)
|
2022-02-17 12:34:36 +08:00
|
|
|
{
|
2022-08-08 10:21:03 +08:00
|
|
|
return iob_timedalloc(throttled, UINT_MAX);
|
2022-02-17 12:34:36 +08:00
|
|
|
}
|
|
|
|
|
2015-01-28 11:23:42 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_tryalloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Try to allocate an I/O buffer by taking the buffer at the head of the
|
|
|
|
* free list without waiting for a buffer to become free.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-08 10:21:03 +08:00
|
|
|
FAR struct iob_s *iob_tryalloc(bool throttled)
|
2015-01-28 11:23:42 +08:00
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
irqstate_t flags;
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
FAR sem_t *sem;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
/* Select the semaphore count to check. */
|
|
|
|
|
|
|
|
sem = (throttled ? &g_throttle_sem : &g_iob_sem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We don't know what context we are called from so we use extreme measures
|
|
|
|
* to protect the free list: We disable interrupts very briefly.
|
|
|
|
*/
|
|
|
|
|
2023-12-13 17:24:07 +08:00
|
|
|
flags = enter_critical_section();
|
2015-01-28 11:23:42 +08:00
|
|
|
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
|
|
|
/* If there are free I/O buffers for this allocation */
|
|
|
|
|
2024-06-13 04:18:17 +08:00
|
|
|
if (sem->semcount > 0)
|
2015-01-28 11:23:42 +08:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Take the I/O buffer from the head of the free list */
|
|
|
|
|
|
|
|
iob = g_iob_freelist;
|
2017-05-17 01:03:35 +08:00
|
|
|
if (iob != NULL)
|
2015-01-28 11:23:42 +08:00
|
|
|
{
|
|
|
|
/* Remove the I/O buffer from the free list and decrement the
|
|
|
|
* counting semaphore(s) that tracks the number of available
|
|
|
|
* IOBs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_iob_freelist = iob->io_flink;
|
|
|
|
|
|
|
|
/* Take a semaphore count. Note that we cannot do this in
|
2017-10-05 21:59:06 +08:00
|
|
|
* in the orthodox way by calling nxsem_wait() or nxsem_trywait()
|
2015-01-28 11:23:42 +08:00
|
|
|
* because this function may be called from an interrupt
|
|
|
|
* handler. Fortunately we know at at least one free buffer
|
|
|
|
* so a simple decrement is all that is needed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_iob_sem.semcount--;
|
|
|
|
DEBUGASSERT(g_iob_sem.semcount >= 0);
|
|
|
|
|
|
|
|
#if CONFIG_IOB_THROTTLE > 0
|
2024-06-13 04:18:17 +08:00
|
|
|
/* The throttle semaphore is used to throttle the number of
|
|
|
|
* free buffers that are available. It is used to prevent
|
|
|
|
* the overrunning of the free buffer list. Please note that
|
|
|
|
* it can only be decremented to zero, which indicates no
|
|
|
|
* throttled buffers are available.
|
2015-01-28 11:23:42 +08:00
|
|
|
*/
|
|
|
|
|
2024-06-13 04:18:17 +08:00
|
|
|
if (g_throttle_sem.semcount > 0)
|
|
|
|
{
|
|
|
|
g_throttle_sem.semcount--;
|
|
|
|
}
|
2015-01-28 11:23:42 +08:00
|
|
|
#endif
|
2019-08-17 06:42:25 +08:00
|
|
|
|
2023-12-13 17:24:07 +08:00
|
|
|
leave_critical_section(flags);
|
2015-01-28 11:23:42 +08:00
|
|
|
|
|
|
|
/* Put the I/O buffer in a known state */
|
|
|
|
|
|
|
|
iob->io_flink = NULL; /* Not in a chain */
|
|
|
|
iob->io_len = 0; /* Length of the data in the entry */
|
|
|
|
iob->io_offset = 0; /* Offset to the beginning of data */
|
|
|
|
iob->io_pktlen = 0; /* Total length of the packet */
|
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 17:24:07 +08:00
|
|
|
leave_critical_section(flags);
|
2015-01-28 11:23:42 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-01 14:09:10 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_IOB_ALLOC
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_alloc_dynamic
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer and playload from heap
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* size - The size of the io_data that is allocated.
|
|
|
|
*
|
|
|
|
* +---------+
|
|
|
|
* | IOB |
|
|
|
|
* | io_data |--+
|
|
|
|
* | buffer |<-+
|
|
|
|
* +---------+
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct iob_s *iob_alloc_dynamic(uint16_t size)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
size_t alignsize;
|
|
|
|
|
|
|
|
alignsize = ROUNDUP(sizeof(struct iob_s), CONFIG_IOB_ALIGNMENT) + size;
|
|
|
|
|
|
|
|
iob = kmm_memalign(CONFIG_IOB_ALIGNMENT, alignsize);
|
|
|
|
if (iob)
|
|
|
|
{
|
|
|
|
iob->io_flink = NULL; /* Not in a chain */
|
|
|
|
iob->io_len = 0; /* Length of the data in the entry */
|
|
|
|
iob->io_offset = 0; /* Offset to the beginning of data */
|
|
|
|
iob->io_bufsize = size; /* Total length of the iob buffer */
|
|
|
|
iob->io_pktlen = 0; /* Total length of the packet */
|
|
|
|
iob->io_free = iob_free_dynamic; /* Customer free callback */
|
|
|
|
iob->io_data = (FAR uint8_t *)ROUNDUP((uintptr_t)(iob + 1),
|
|
|
|
CONFIG_IOB_ALIGNMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_alloc_with_data
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate an I/O buffer from heap and attach the external payload
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* data - Make io_data point to a specific address, the caller is
|
|
|
|
* responsible for the memory management. The caller should
|
|
|
|
* ensure that the memory is not freed before the iob is freed.
|
|
|
|
*
|
|
|
|
* +---------+ +-->+--------+
|
|
|
|
* | IOB | | | data |
|
|
|
|
* | io_data |--+ +--------+
|
|
|
|
* +---------+
|
|
|
|
*
|
|
|
|
* size - The size of the data parameter
|
|
|
|
* free_cb - Notify the caller when the iob is freed. The caller can
|
|
|
|
* perform additional operations on the data before it is freed.
|
|
|
|
* The free_cb is called when the iob is freed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct iob_s *iob_alloc_with_data(FAR void *data, uint16_t size,
|
|
|
|
iob_free_cb_t free_cb)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
|
|
|
|
DEBUGASSERT(free_cb != NULL);
|
|
|
|
|
|
|
|
iob = kmm_malloc(sizeof(struct iob_s));
|
|
|
|
if (iob)
|
|
|
|
{
|
|
|
|
iob->io_flink = NULL; /* Not in a chain */
|
|
|
|
iob->io_len = 0; /* Length of the data in the entry */
|
|
|
|
iob->io_offset = 0; /* Offset to the beginning of data */
|
|
|
|
iob->io_bufsize = size; /* Total length of the iob buffer */
|
|
|
|
iob->io_pktlen = 0; /* Total length of the packet */
|
|
|
|
iob->io_free = free_cb; /* Customer free callback */
|
|
|
|
iob->io_data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iob;
|
|
|
|
}
|
|
|
|
#endif
|