Skip to content

Commit 2b38320

Browse files
committed
mm/iob: add iob_init method to support external buffer init as iob structure
This interface allows different protocol modules to use their own unique IOB buffer sources and allocation strategies without interfering with each other. Representative examples are the IP protocol and the CAN protocol. The IP protocol generally transmits packets of varying lengths and requires relatively high peak throughput. In this case, to ensure higher performance, IOB_BUFSIZE is often configured to be relatively large, such as greater than 500. The CAN protocol generally transmits short packets of fixed length. In this case, to improve memory utilization, IOB_BUFSIZE is often configured to be relatively small, such as 16 or 64. To optimize the memory utilization when the IP protocol and the CAN protocol share the same core, this interface was added. Signed-off-by: zhanghongyu <[email protected]>
1 parent 1569621 commit 2b38320

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

include/nuttx/mm/iob.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,33 @@ FAR struct iob_s *iob_alloc_dynamic(uint16_t size);
266266

267267
FAR struct iob_s *iob_alloc_with_data(FAR void *data, uint16_t size,
268268
iob_free_cb_t free_cb);
269+
270+
/****************************************************************************
271+
* Name: iob_init_with_data
272+
*
273+
* Description:
274+
* Initialize an I/O buffer and playload
275+
*
276+
* Input Parameters:
277+
* data - Make io_data point to a specific address, the caller is
278+
* responsible for the memory management. The caller should
279+
* ensure that the memory is not freed before the iob is freed,
280+
* and caller need to reserve space for alignment.
281+
* size - The size of the data parameter
282+
* free_cb - Notify the caller when the iob is freed. The caller can
283+
* perform additional operations on the data before it is freed.
284+
*
285+
* +---------+
286+
* | IOB |
287+
* | io_data |--+
288+
* | buffer |<-+
289+
* +---------+
290+
*
291+
****************************************************************************/
292+
293+
FAR struct iob_s *iob_init_with_data(FAR void *data, uint16_t size,
294+
iob_free_cb_t free_cb);
295+
269296
#endif
270297

271298
/****************************************************************************

mm/iob/iob_alloc.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static FAR struct iob_s *iob_allocwait(bool throttled, unsigned int timeout)
226226
* Name: iob_free_dynamic
227227
*
228228
* Description:
229-
* Dummy free callback function, do nothing.
229+
* Free the I/O buffer and payload to the heap
230230
*
231231
* Input Parameters:
232232
* data -
@@ -235,6 +235,7 @@ static FAR struct iob_s *iob_allocwait(bool throttled, unsigned int timeout)
235235

236236
static void iob_free_dynamic(FAR void *data)
237237
{
238+
kmm_free(data);
238239
}
239240
#endif
240241

@@ -396,4 +397,45 @@ FAR struct iob_s *iob_alloc_with_data(FAR void *data, uint16_t size,
396397

397398
return iob;
398399
}
400+
401+
/****************************************************************************
402+
* Name: iob_init_with_data
403+
*
404+
* Description:
405+
* Initialize an I/O buffer and playload
406+
*
407+
* Input Parameters:
408+
* data - Make io_data point to a specific address, the caller is
409+
* responsible for the memory management. The caller should
410+
* ensure that the memory is not freed before the iob is freed,
411+
* and caller need to reserve space for alignment.
412+
* size - The size of the data parameter
413+
* free_cb - Notify the caller when the iob is freed. The caller can
414+
* perform additional operations on the data before it is freed.
415+
*
416+
* +---------+
417+
* | IOB |
418+
* | io_data |--+
419+
* | buffer |<-+
420+
* +---------+
421+
*
422+
****************************************************************************/
423+
424+
FAR struct iob_s *iob_init_with_data(FAR void *data, uint16_t size,
425+
iob_free_cb_t free_cb)
426+
{
427+
FAR struct iob_s *iob = (FAR struct iob_s *)data;
428+
429+
iob->io_flink = NULL; /* Not in a chain */
430+
iob->io_len = 0; /* Length of the data in the entry */
431+
iob->io_offset = 0; /* Offset to the beginning of data */
432+
iob->io_pktlen = 0; /* Total length of the packet */
433+
iob->io_free = free_cb; /* Customer free callback */
434+
iob->io_data = (FAR uint8_t *)ALIGN_UP((uintptr_t)(iob + 1),
435+
CONFIG_IOB_ALIGNMENT);
436+
iob->io_bufsize = ((FAR uint8_t *)data + size) - iob->io_data;
437+
438+
return iob;
439+
}
440+
399441
#endif

mm/iob/iob_free.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#ifdef CONFIG_IOB_ALLOC
3636
# include <nuttx/kmalloc.h>
3737
#endif
38+
#include <nuttx/nuttx.h>
3839
#include <nuttx/mm/iob.h>
3940

4041
#include "iob.h"
@@ -120,8 +121,18 @@ FAR struct iob_s *iob_free(FAR struct iob_s *iob)
120121
#ifdef CONFIG_IOB_ALLOC
121122
if (iob->io_free != NULL)
122123
{
123-
iob->io_free(iob->io_data);
124-
kmm_free(iob);
124+
FAR uint8_t *io_data = (FAR uint8_t *)ALIGN_UP((uintptr_t)(iob + 1),
125+
CONFIG_IOB_ALIGNMENT);
126+
if (iob->io_data == io_data)
127+
{
128+
iob->io_free(iob);
129+
}
130+
else
131+
{
132+
iob->io_free(iob->io_data);
133+
kmm_free(iob);
134+
}
135+
125136
return next;
126137
}
127138
#endif

0 commit comments

Comments
 (0)