2014-06-05 20:56:00 +02:00
|
|
|
/****************************************************************************
|
2019-01-26 16:56:27 +01:00
|
|
|
* mm/iob/iob_clone.c
|
2014-06-05 20:56:00 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01: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-05 20:56:00 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-05 20:56:00 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01: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-05 20:56:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2014-06-05 20:56:00 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-05-09 15:34:59 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-05 20:56:00 +02:00
|
|
|
|
|
|
|
#include "iob.h"
|
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Static Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_next
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate or reinitialize the next node
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int iob_next(FAR struct iob_s *iob, bool throttled, bool block)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *next = iob->io_flink;
|
|
|
|
|
|
|
|
/* Allocate new destination I/O buffer and hook it into the
|
|
|
|
* destination I/O buffer chain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (next == NULL)
|
|
|
|
{
|
|
|
|
if (block)
|
|
|
|
{
|
|
|
|
next = iob_alloc(throttled);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = iob_tryalloc(throttled);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next == NULL)
|
|
|
|
{
|
|
|
|
ioberr("ERROR: Failed to allocate an I/O buffer\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
iob->io_flink = next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next->io_len = 0;
|
|
|
|
next->io_offset = 0;
|
|
|
|
next->io_pktlen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-06-05 20:56:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-12-13 15:10:24 +01:00
|
|
|
* Name: iob_clone_partial
|
2014-06-05 20:56:00 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2022-12-13 15:10:24 +01:00
|
|
|
* Duplicate the data from partial bytes of iob1 to iob2
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* iob1 - Pointer to source iob_s
|
|
|
|
* len - Number of bytes to copy
|
|
|
|
* offset1 - Offset of source iobs_s
|
|
|
|
* iob2 - Pointer to destination iob_s
|
|
|
|
* offset2 - Offset of destination iobs_s
|
|
|
|
* throttled - An indication of the IOB allocation is "throttled"
|
|
|
|
* block - Flag of Enable/Disable nonblocking operation
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* == 0 - Partial clone successfully.
|
|
|
|
* < 0 - No available to clone to destination iob.
|
2014-06-05 20:56:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
int iob_clone_partial(FAR struct iob_s *iob1, unsigned int len,
|
2023-02-07 13:00:59 +01:00
|
|
|
int offset1, FAR struct iob_s *iob2,
|
|
|
|
int offset2, bool throttled, bool block)
|
2014-06-05 20:56:00 +02:00
|
|
|
{
|
|
|
|
FAR uint8_t *src;
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
unsigned int ncopy;
|
|
|
|
unsigned int avail1;
|
|
|
|
unsigned int avail2;
|
2022-12-13 15:10:24 +01:00
|
|
|
int ret;
|
2014-06-05 20:56:00 +02:00
|
|
|
|
2020-04-29 18:18:20 +02:00
|
|
|
/* Copy the total packet size from the I/O buffer at the head of the
|
|
|
|
* chain.
|
|
|
|
*/
|
2014-06-05 20:56:00 +02:00
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
iob2->io_pktlen = len + offset2;
|
2014-06-05 20:56:00 +02:00
|
|
|
|
|
|
|
/* Handle special case where there are empty buffers at the head
|
2022-12-13 15:10:24 +01:00
|
|
|
* the list, Skip I/O buffer containing the data offset.
|
2014-06-05 20:56:00 +02:00
|
|
|
*/
|
|
|
|
|
2023-02-07 13:00:59 +01:00
|
|
|
while (iob1 != NULL && (int)(offset1 - iob1->io_len) >= 0)
|
2014-06-05 20:56:00 +02:00
|
|
|
{
|
2022-12-13 15:10:24 +01:00
|
|
|
offset1 -= iob1->io_len;
|
|
|
|
iob1 = iob1->io_flink;
|
2014-06-05 20:56:00 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
/* Skip requested offset from the destination iob */
|
2014-06-05 20:56:00 +02:00
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
while (iob2 != NULL)
|
|
|
|
{
|
2024-04-23 05:01:02 +02:00
|
|
|
avail2 = IOB_BUFSIZE(iob2) - iob2->io_offset;
|
2023-04-23 04:47:02 +02:00
|
|
|
if ((int)(offset2 - avail2) < 0)
|
2022-12-13 15:10:24 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iob2->io_len = avail2;
|
|
|
|
offset2 -= iob2->io_len;
|
|
|
|
|
|
|
|
ret = iob_next(iob2, throttled, block);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
iob2 = iob2->io_flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pack each entry from iob1 to iob2 */
|
2014-06-05 20:56:00 +02:00
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
while (iob1 && len > 0)
|
2014-06-05 20:56:00 +02:00
|
|
|
{
|
|
|
|
/* Get the source I/O buffer pointer and the number of bytes to copy
|
|
|
|
* from this address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
src = &iob1->io_data[iob1->io_offset + offset1];
|
|
|
|
avail1 = iob1->io_len - offset1;
|
|
|
|
|
|
|
|
/* Get the destination I/O buffer pointer and the number of bytes to
|
|
|
|
* copy to that address.
|
|
|
|
*/
|
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
dest = &iob2->io_data[iob2->io_offset + offset2];
|
2024-04-23 05:01:02 +02:00
|
|
|
avail2 = IOB_BUFSIZE(iob2) - iob2->io_offset - offset2;
|
2014-06-05 20:56:00 +02:00
|
|
|
|
|
|
|
/* Copy the smaller of the two and update the srce and destination
|
|
|
|
* offsets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ncopy = MIN(avail1, avail2);
|
2022-12-13 15:10:24 +01:00
|
|
|
if (ncopy > len)
|
|
|
|
{
|
|
|
|
ncopy = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= ncopy;
|
|
|
|
|
2014-06-05 20:56:00 +02:00
|
|
|
memcpy(dest, src, ncopy);
|
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
offset1 += ncopy;
|
|
|
|
offset2 += ncopy;
|
|
|
|
iob2->io_len = offset2;
|
2014-06-05 20:56:00 +02:00
|
|
|
|
|
|
|
/* Have we taken all of the data from the source I/O buffer? */
|
|
|
|
|
2023-02-07 13:00:59 +01:00
|
|
|
if ((int)(offset1 - iob1->io_len) >= 0)
|
2014-06-05 20:56:00 +02:00
|
|
|
{
|
|
|
|
/* Skip over empty entries in the chain (there should not be any
|
|
|
|
* but just to be safe).
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Yes.. move to the next source I/O buffer */
|
|
|
|
|
2014-06-06 17:35:31 +02:00
|
|
|
iob1 = iob1->io_flink;
|
2014-06-05 20:56:00 +02:00
|
|
|
}
|
2017-03-01 12:13:06 +01:00
|
|
|
while (iob1 && iob1->io_len <= 0);
|
2014-06-05 20:56:00 +02:00
|
|
|
|
|
|
|
/* Reset the offset to the beginning of the I/O buffer */
|
|
|
|
|
|
|
|
offset1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Have we filled the destination I/O buffer? Is there more data to be
|
|
|
|
* transferred?
|
|
|
|
*/
|
|
|
|
|
2024-04-23 05:01:02 +02:00
|
|
|
if ((int)(offset2 + iob2->io_offset - IOB_BUFSIZE(iob2)) >= 0 &&
|
2022-12-13 15:10:24 +01:00
|
|
|
iob1 != NULL)
|
2014-06-05 20:56:00 +02:00
|
|
|
{
|
2022-12-13 15:10:24 +01:00
|
|
|
ret = iob_next(iob2, throttled, block);
|
|
|
|
if (ret < 0)
|
2022-12-13 09:08:27 +01:00
|
|
|
{
|
2022-12-13 15:10:24 +01:00
|
|
|
return ret;
|
2022-12-13 09:08:27 +01:00
|
|
|
}
|
|
|
|
|
2022-12-13 15:10:24 +01:00
|
|
|
iob2 = iob2->io_flink;
|
2014-06-05 20:56:00 +02:00
|
|
|
offset2 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-12-13 15:10:24 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_clone
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate (and pack) the data in iob1 in iob2. iob2 must be empty.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int iob_clone(FAR struct iob_s *iob1, FAR struct iob_s *iob2,
|
|
|
|
bool throttled, bool block)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(iob2->io_len == 0 && iob2->io_offset == 0 &&
|
|
|
|
iob2->io_pktlen == 0 && iob2->io_flink == NULL);
|
|
|
|
|
|
|
|
return iob_clone_partial(iob1, iob1->io_pktlen, 0,
|
|
|
|
iob2, 0, throttled, block);
|
|
|
|
}
|