incubator-nuttx/mm/iob/iob_clone.c

255 lines
6.8 KiB
C
Raw Normal View History

2014-06-06 02:56:00 +08:00
/****************************************************************************
* mm/iob/iob_clone.c
2014-06-06 02:56:00 +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-06 02:56:00 +08:00
*
* http://www.apache.org/licenses/LICENSE-2.0
2014-06-06 02:56:00 +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-06 02:56:00 +08:00
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <sys/param.h>
2014-06-06 02:56:00 +08:00
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/mm/iob.h>
2014-06-06 02:56:00 +08:00
#include "iob.h"
/****************************************************************************
* 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-06 02:56:00 +08:00
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: iob_clone_partial
2014-06-06 02:56:00 +08:00
*
* Description:
* 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-06 02:56:00 +08:00
*
****************************************************************************/
int iob_clone_partial(FAR struct iob_s *iob1, unsigned int len,
int offset1, FAR struct iob_s *iob2,
int offset2, bool throttled, bool block)
2014-06-06 02:56:00 +08:00
{
FAR uint8_t *src;
FAR uint8_t *dest;
unsigned int ncopy;
unsigned int avail1;
unsigned int avail2;
int ret;
2014-06-06 02:56:00 +08:00
/* Copy the total packet size from the I/O buffer at the head of the
* chain.
*/
2014-06-06 02:56:00 +08:00
iob2->io_pktlen = len + offset2;
2014-06-06 02:56:00 +08:00
/* Handle special case where there are empty buffers at the head
* the list, Skip I/O buffer containing the data offset.
2014-06-06 02:56:00 +08:00
*/
while (iob1 != NULL && (int)(offset1 - iob1->io_len) >= 0)
2014-06-06 02:56:00 +08:00
{
offset1 -= iob1->io_len;
iob1 = iob1->io_flink;
2014-06-06 02:56:00 +08:00
}
/* Skip requested offset from the destination iob */
2014-06-06 02:56:00 +08:00
while (iob2 != NULL)
{
avail2 = CONFIG_IOB_BUFSIZE - iob2->io_offset;
if (offset2 < avail2)
{
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-06 02:56:00 +08:00
while (iob1 && len > 0)
2014-06-06 02:56:00 +08: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.
*/
dest = &iob2->io_data[iob2->io_offset + offset2];
avail2 = CONFIG_IOB_BUFSIZE - iob2->io_offset - offset2;
2014-06-06 02:56:00 +08:00
/* Copy the smaller of the two and update the srce and destination
* offsets.
*/
ncopy = MIN(avail1, avail2);
if (ncopy > len)
{
ncopy = len;
}
len -= ncopy;
2014-06-06 02:56:00 +08:00
memcpy(dest, src, ncopy);
offset1 += ncopy;
offset2 += ncopy;
iob2->io_len = offset2;
2014-06-06 02:56:00 +08:00
/* Have we taken all of the data from the source I/O buffer? */
if ((int)(offset1 - iob1->io_len) >= 0)
2014-06-06 02:56:00 +08: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 */
iob1 = iob1->io_flink;
2014-06-06 02:56:00 +08:00
}
while (iob1 && iob1->io_len <= 0);
2014-06-06 02:56:00 +08: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?
*/
if ((int)(offset2 + iob2->io_offset - CONFIG_IOB_BUFSIZE) >= 0 &&
iob1 != NULL)
2014-06-06 02:56:00 +08:00
{
ret = iob_next(iob2, throttled, block);
if (ret < 0)
{
return ret;
}
iob2 = iob2->io_flink;
2014-06-06 02:56:00 +08:00
offset2 = 0;
}
}
return 0;
}
/****************************************************************************
* 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);
}