2014-06-04 20:03:29 +02:00
|
|
|
/****************************************************************************
|
2017-05-09 15:34:59 +02:00
|
|
|
* mm/iob/iob_pack.c
|
2014-06-04 20:03:29 +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-04 20:03:29 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-04 20:03:29 +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-04 20:03:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-05-09 15:34:59 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-04 20:03:29 +02:00
|
|
|
|
|
|
|
#include "iob.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_pack
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Pack all data in the I/O buffer chain so that the data offset is zero
|
|
|
|
* and all but the final buffer in the chain are filled. Any emptied
|
|
|
|
* buffers at the end of the chain are freed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-08 04:21:03 +02:00
|
|
|
FAR struct iob_s *iob_pack(FAR struct iob_s *iob)
|
2014-06-04 20:03:29 +02:00
|
|
|
{
|
|
|
|
FAR struct iob_s *head;
|
|
|
|
FAR struct iob_s *next;
|
|
|
|
unsigned int ncopy;
|
|
|
|
unsigned int navail;
|
|
|
|
|
2023-04-20 06:05:17 +02:00
|
|
|
/* Handle special cases, preserve at least one iob. */
|
2014-06-04 20:03:29 +02:00
|
|
|
|
2023-04-20 06:05:17 +02:00
|
|
|
while (iob->io_len <= 0 && iob->io_flink != NULL)
|
2014-06-04 20:03:29 +02:00
|
|
|
{
|
2022-08-08 04:21:03 +02:00
|
|
|
iob = iob_free(iob);
|
2014-06-04 20:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now remember the head of the chain (for the return value) */
|
|
|
|
|
|
|
|
head = iob;
|
|
|
|
|
|
|
|
/* Pack each entry in the list */
|
|
|
|
|
|
|
|
while (iob)
|
|
|
|
{
|
2014-06-06 17:35:31 +02:00
|
|
|
next = iob->io_flink;
|
2014-06-04 20:03:29 +02:00
|
|
|
|
|
|
|
/* Eliminate the data offset in this entry */
|
|
|
|
|
|
|
|
if (iob->io_offset > 0)
|
|
|
|
{
|
|
|
|
memcpy(iob->io_data, &iob->io_data[iob->io_offset], iob->io_len);
|
|
|
|
iob->io_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is there a buffer after this one? */
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
{
|
|
|
|
/* How many bytes can we copy from the next I/O buffer. Limit the
|
|
|
|
* size of the copy to the amount of free space in current I/O
|
|
|
|
* buffer
|
|
|
|
*/
|
|
|
|
|
|
|
|
ncopy = next->io_len;
|
|
|
|
navail = CONFIG_IOB_BUFSIZE - iob->io_len;
|
|
|
|
if (ncopy > navail)
|
|
|
|
{
|
|
|
|
ncopy = navail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ncopy > 0)
|
|
|
|
{
|
2021-02-08 16:19:12 +01:00
|
|
|
/* Copy the data from the next into
|
|
|
|
* the current I/O buffer iob
|
|
|
|
*/
|
2014-06-04 20:03:29 +02:00
|
|
|
|
|
|
|
memcpy(&iob->io_data[iob->io_len],
|
|
|
|
&next->io_data[next->io_offset],
|
|
|
|
ncopy);
|
|
|
|
|
|
|
|
/* Adjust lengths and offsets */
|
|
|
|
|
|
|
|
iob->io_len += ncopy;
|
|
|
|
next->io_len -= ncopy;
|
|
|
|
next->io_offset += ncopy;
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:58:07 +01:00
|
|
|
/* Have we consumed all of the data in the next entry? */
|
2014-06-04 20:03:29 +02:00
|
|
|
|
2020-02-13 14:58:07 +01:00
|
|
|
if (next->io_len <= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. free the next entry in I/O buffer chain */
|
2014-06-04 20:03:29 +02:00
|
|
|
|
2022-08-08 04:21:03 +02:00
|
|
|
next = iob_free(next);
|
2020-02-13 14:58:07 +01:00
|
|
|
iob->io_flink = next;
|
|
|
|
}
|
2014-06-04 20:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up to pack the next entry in the chain */
|
|
|
|
|
|
|
|
iob = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|