2014-06-03 20:41:34 +02:00
|
|
|
/****************************************************************************
|
2017-05-09 15:34:59 +02:00
|
|
|
* mm/iob/iob_trimtail.c
|
2014-06-03 20:41:34 +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-03 20:41:34 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-03 20:41:34 +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-03 20:41:34 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2014-06-22 23:27:01 +02:00
|
|
|
#include <debug.h>
|
2014-06-03 20:41:34 +02:00
|
|
|
|
2017-05-09 15:34:59 +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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
FAR struct iob_s *iob_trimtail(FAR struct iob_s *iob, unsigned int trimlen,
|
|
|
|
enum iob_user_e producerid)
|
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;
|
|
|
|
|
2017-04-21 00:08:49 +02:00
|
|
|
iobinfo("iob=%p pktlen=%d trimlen=%d\n", iob, iob->io_pktlen, trimlen);
|
2014-06-22 23:27:01 +02:00
|
|
|
|
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;
|
|
|
|
|
2014-06-06 17:35:31 +02:00
|
|
|
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?
|
|
|
|
*/
|
|
|
|
|
2017-04-21 00:08:49 +02:00
|
|
|
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 */
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
iob_free(last, producerid);
|
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
|
|
|
|
2014-06-06 17:35:31 +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
|
|
|
}
|