nuttx/mm/iob/iob_trimtail.c

126 lines
3.6 KiB
C
Raw Normal View History

2014-06-03 20:41:34 +02:00
/****************************************************************************
* mm/iob/iob_trimtail.c
2014-06-03 20:41:34 +02:00
*
* SPDX-License-Identifier: Apache-2.0
*
* 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-03 20:41:34 +02:00
*
* http://www.apache.org/licenses/LICENSE-2.0
2014-06-03 20:41:34 +02: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-03 20:41:34 +02:00
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <debug.h>
2014-06-03 20:41:34 +02:00
#include <nuttx/mm/iob.h>
2014-06-03 20:41:34 +02:00
#include "iob.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: iob_trimtail
*
* Description:
* Remove bytes from the end of an I/O chain
*
****************************************************************************/
FAR struct iob_s *iob_trimtail(FAR struct iob_s *iob, unsigned int trimlen)
2014-06-03 20:41:34 +02:00
{
FAR struct iob_s *entry;
FAR struct iob_s *penultimate;
FAR struct iob_s *last;
int len;
iobinfo("iob=%p pktlen=%d trimlen=%d\n", iob, iob->io_pktlen, trimlen);
2014-06-03 20:41:34 +02:00
if (iob && trimlen > 0)
{
len = trimlen;
/* Loop until complete the trim */
while (len > 0)
{
/* Calculate the total length of the data in the I/O buffer
* chain and find the last entry in the chain.
*/
penultimate = NULL;
last = NULL;
for (entry = iob; entry; entry = entry->io_flink)
2014-06-03 20:41:34 +02:00
{
/* Remember the last and the next to the last in the chain */
penultimate = last;
last = entry;
}
/* Trim from the last entry in the chain. Do we trim this entire
* I/O buffer away?
*/
iobinfo("iob=%p len=%d vs %d\n", last, last->io_len, len);
2014-06-03 20:41:34 +02:00
if (last->io_len <= len)
{
2014-06-04 17:03:11 +02:00
/* Yes.. Consume the entire buffer */
2014-06-03 20:41:34 +02:00
2014-06-06 02:50:30 +02:00
iob->io_pktlen -= last->io_len;
len -= last->io_len;
last->io_len = 0;
2014-06-04 17:03:11 +02:00
/* Free the last, empty buffer in the list */
iob_free(last);
2014-06-03 20:41:34 +02:00
/* There should be a buffer before this one */
if (!penultimate)
{
2014-06-04 17:03:11 +02:00
/* No.. we just freed the head of the chain */
return NULL;
2014-06-03 20:41:34 +02:00
}
2014-06-04 17:03:11 +02:00
/* Unlink the penultimate from the freed buffer */
2014-06-03 20:41:34 +02:00
penultimate->io_flink = NULL;
2014-06-03 20:41:34 +02:00
}
2015-10-04 23:28:54 +02:00
2014-06-03 20:41:34 +02:00
else
{
/* No, then just take what we need from this I/O buffer and
* stop the trim.
*/
2014-06-06 02:50:30 +02:00
iob->io_pktlen -= len;
last->io_len -= len;
len = 0;
2014-06-03 20:41:34 +02:00
}
}
}
2014-06-04 17:03:11 +02:00
return iob;
2014-06-03 20:41:34 +02:00
}