2014-06-07 19:05:31 +02:00
|
|
|
/****************************************************************************
|
2017-05-09 15:34:59 +02:00
|
|
|
* mm/iob/iob_contig.c
|
2014-06-07 19:05:31 +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-07 19:05:31 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-07 19:05:31 +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-07 19:05:31 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2022-12-18 16:08:04 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2017-05-09 15:34:59 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-07 19:05:31 +02:00
|
|
|
|
|
|
|
#include "iob.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_contig
|
|
|
|
*
|
|
|
|
* Description:
|
2022-12-18 16:08:04 +01:00
|
|
|
* Ensure that there is'len' bytes of contiguous space at the beginning
|
|
|
|
* of the I/O buffer chain starting at 'iob'.
|
2014-06-07 19:05:31 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
int iob_contig(FAR struct iob_s *iob, unsigned int len)
|
2014-06-07 19:05:31 +02:00
|
|
|
{
|
|
|
|
FAR struct iob_s *next;
|
|
|
|
unsigned int ncopy;
|
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
/* We can't make more contiguous space that the size of one I/O buffer.
|
|
|
|
* If you get this assertion and really need that much contiguous data,
|
|
|
|
* then you will need to increase CONFIG_IOB_BUFSIZE.
|
|
|
|
*/
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
DEBUGASSERT(len <= CONFIG_IOB_BUFSIZE);
|
|
|
|
|
|
|
|
/* Check if there is already sufficient, contiguous space at the beginning
|
|
|
|
* of the packet
|
|
|
|
*/
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
if (len <= iob->io_len)
|
2022-12-18 09:14:25 +01:00
|
|
|
{
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Yes we are good */
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2022-12-18 09:14:25 +01:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Can we get the required amount of contiguous data by just packing the
|
|
|
|
* head I/0 buffer?
|
|
|
|
*/
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
else if (len <= iob->io_pktlen)
|
2014-06-07 19:05:31 +02:00
|
|
|
{
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Yes.. First eliminate any leading offset */
|
2022-12-18 09:14:25 +01:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
if (iob->io_offset > 0)
|
2014-06-07 19:05:31 +02:00
|
|
|
{
|
2022-12-18 16:08:04 +01:00
|
|
|
memcpy(iob->io_data, &iob->io_data[iob->io_offset], iob->io_len);
|
|
|
|
iob->io_offset = 0;
|
2014-06-07 19:05:31 +02:00
|
|
|
}
|
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Then move what we need from the next I/O buffer(s) */
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
do
|
2014-06-07 19:05:31 +02:00
|
|
|
{
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Get the next I/O buffer in the chain */
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
next = iob->io_flink;
|
|
|
|
DEBUGASSERT(next != NULL);
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Copy what we need or what we can from the next buffer */
|
|
|
|
|
|
|
|
ncopy = len - iob->io_len;
|
|
|
|
ncopy = MIN(ncopy, next->io_len);
|
|
|
|
memcpy(&iob->io_data[iob->io_len],
|
|
|
|
&next->io_data[next->io_offset], ncopy);
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Adjust counts and offsets */
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
iob->io_len += ncopy;
|
|
|
|
next->io_offset += ncopy;
|
|
|
|
next->io_len -= ncopy;
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Handle a (improbable) case where we just emptied the second
|
|
|
|
* buffer in the chain.
|
|
|
|
*/
|
2014-06-07 19:05:31 +02:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
if (next->io_len == 0)
|
|
|
|
{
|
|
|
|
iob->io_flink = iob_free(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (len > iob->io_len);
|
|
|
|
|
|
|
|
/* This should always succeed because we know that:
|
|
|
|
*
|
|
|
|
* pktlen >= CONFIG_IOB_BUFSIZE >= len
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
2014-06-07 19:05:31 +02:00
|
|
|
}
|
2022-12-18 09:14:25 +01:00
|
|
|
|
2022-12-18 16:08:04 +01:00
|
|
|
/* Otherwise, the request for contiguous data is larger then the entire
|
|
|
|
* packet. We can't do that without extending the I/O buffer chain with
|
|
|
|
* garbage (which would probably not be what the caller wants).
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ioberr("ERROR: pktlen=%u < requested len=%u\n", iob->io_pktlen, len);
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
2014-06-07 19:05:31 +02:00
|
|
|
}
|