net_mem.h File Reference

Memory management. More...

#include "net_config.h"
#include "os_port.h"
#include "error.h"

Go to the source code of this file.

Data Structures

struct  ChunkDesc
 Structure describing a chunk of data. More...
 
struct  NetBuffer
 Structure describing a buffer that spans multiple chunks. More...
 
struct  NetBuffer1
 

Macros

#define NET_MEM_POOL_SUPPORT   DISABLED
 
#define NET_MEM_POOL_BUFFER_COUNT   32
 
#define NET_MEM_POOL_BUFFER_SIZE   1536
 
#define CHUNKED_BUFFER_HEADER_SIZE   (sizeof(NetBuffer) + MAX_CHUNK_COUNT * sizeof(ChunkDesc))
 
#define N(size)   (((size) + NET_MEM_POOL_BUFFER_SIZE - 1) / NET_MEM_POOL_BUFFER_SIZE)
 

Functions

error_t memPoolInit (void)
 Memory pool initialization. More...
 
void * memPoolAlloc (size_t size)
 Allocate a memory block. More...
 
void memPoolFree (void *p)
 Release a memory block. More...
 
void memPoolGetStats (uint_t *currentUsage, uint_t *maxUsage, uint_t *size)
 Get memory pool usage. More...
 
NetBuffernetBufferAlloc (size_t length)
 Allocate a multi-part buffer. More...
 
void netBufferFree (NetBuffer *buffer)
 Dispose a multi-part buffer. More...
 
size_t netBufferGetLength (const NetBuffer *buffer)
 Get the actual length of a multi-part buffer. More...
 
error_t netBufferSetLength (NetBuffer *buffer, size_t length)
 Adjust the length of a multi-part buffer. More...
 
void * netBufferAt (const NetBuffer *buffer, size_t offset, size_t length)
 Returns a pointer to a data segment. More...
 
error_t netBufferConcat (NetBuffer *dest, const NetBuffer *src, size_t srcOffset, size_t length)
 Concatenate two multi-part buffers. More...
 
error_t netBufferCopy (NetBuffer *dest, size_t destOffset, const NetBuffer *src, size_t srcOffset, size_t length)
 Copy data between multi-part buffers. More...
 
error_t netBufferAppend (NetBuffer *dest, const void *src, size_t length)
 Append data a multi-part buffer. More...
 
size_t netBufferWrite (NetBuffer *dest, size_t destOffset, const void *src, size_t length)
 Write data to a multi-part buffer. More...
 
size_t netBufferRead (void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
 Read data from a multi-part buffer. More...
 

Detailed Description

Memory management.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.4

Definition in file net_mem.h.

Macro Definition Documentation

◆ CHUNKED_BUFFER_HEADER_SIZE

#define CHUNKED_BUFFER_HEADER_SIZE   (sizeof(NetBuffer) + MAX_CHUNK_COUNT * sizeof(ChunkDesc))

Definition at line 61 of file net_mem.h.

◆ N

#define N (   size)    (((size) + NET_MEM_POOL_BUFFER_SIZE - 1) / NET_MEM_POOL_BUFFER_SIZE)

Definition at line 64 of file net_mem.h.

◆ NET_MEM_POOL_BUFFER_COUNT

#define NET_MEM_POOL_BUFFER_COUNT   32

Definition at line 48 of file net_mem.h.

◆ NET_MEM_POOL_BUFFER_SIZE

#define NET_MEM_POOL_BUFFER_SIZE   1536

Definition at line 55 of file net_mem.h.

◆ NET_MEM_POOL_SUPPORT

#define NET_MEM_POOL_SUPPORT   DISABLED

Definition at line 41 of file net_mem.h.

Function Documentation

◆ memPoolAlloc()

void* memPoolAlloc ( size_t  size)

Allocate a memory block.

Parameters
[in]sizeBytes to allocate
Returns
Pointer to the allocated space or NULL if there is insufficient memory available

Definition at line 100 of file net_mem.c.

◆ memPoolFree()

void memPoolFree ( void *  p)

Release a memory block.

Parameters
[in]pPreviously allocated memory block to be freed

Definition at line 166 of file net_mem.c.

◆ memPoolGetStats()

void memPoolGetStats ( uint_t currentUsage,
uint_t maxUsage,
uint_t size 
)

Get memory pool usage.

Parameters
[out]currentUsageNumber of buffers currently allocated
[out]maxUsageMaximum number of buffers that have been allocated so far
[out]sizeTotal number of buffers in the memory pool

Definition at line 207 of file net_mem.c.

◆ memPoolInit()

error_t memPoolInit ( void  )

Memory pool initialization.

Returns
Error code

Definition at line 70 of file net_mem.c.

◆ netBufferAlloc()

NetBuffer* netBufferAlloc ( size_t  length)

Allocate a multi-part buffer.

Parameters
[in]lengthDesired length
Returns
Pointer to the allocated buffer or NULL if there is insufficient memory available

Definition at line 243 of file net_mem.c.

◆ netBufferAppend()

error_t netBufferAppend ( NetBuffer dest,
const void *  src,
size_t  length 
)

Append data a multi-part buffer.

Parameters
[out]destPointer to a multi-part buffer
[in]srcUser buffer containing the data to be appended
[in]lengthNumber of bytes in the user buffer
Returns
Error code

Definition at line 604 of file net_mem.c.

◆ netBufferAt()

void* netBufferAt ( const NetBuffer buffer,
size_t  offset,
size_t  length 
)

Returns a pointer to a data segment.

Parameters
[in]bufferPointer to a multi-part buffer
[in]offsetOffset from the beginning of the buffer
[in]lengthLength of the data segment
Returns
Pointer the data segment

Definition at line 418 of file net_mem.c.

◆ netBufferConcat()

error_t netBufferConcat ( NetBuffer dest,
const NetBuffer src,
size_t  srcOffset,
size_t  length 
)

Concatenate two multi-part buffers.

Parameters
[out]destPointer to the destination buffer
[in]srcPointer to the source buffer
[in]srcOffsetRead offset
[in]lengthNumber of bytes to read from the source buffer
Returns
Error code

Definition at line 460 of file net_mem.c.

◆ netBufferCopy()

error_t netBufferCopy ( NetBuffer dest,
size_t  destOffset,
const NetBuffer src,
size_t  srcOffset,
size_t  length 
)

Copy data between multi-part buffers.

Parameters
[out]destPointer to the destination buffer
[in]destOffsetWrite offset
[in]srcPointer to the source buffer
[in]srcOffsetRead offset
[in]lengthNumber of bytes to be copied
Returns
Error code

Definition at line 522 of file net_mem.c.

◆ netBufferFree()

void netBufferFree ( NetBuffer buffer)

Dispose a multi-part buffer.

Parameters
[in]bufferPointer to the multi-part buffer to be released

Definition at line 282 of file net_mem.c.

◆ netBufferGetLength()

size_t netBufferGetLength ( const NetBuffer buffer)

Get the actual length of a multi-part buffer.

Parameters
[in]bufferPointer to a multi-part buffer
Returns
Actual length in bytes

Definition at line 297 of file net_mem.c.

◆ netBufferRead()

size_t netBufferRead ( void *  dest,
const NetBuffer src,
size_t  srcOffset,
size_t  length 
)

Read data from a multi-part buffer.

Parameters
[out]destPointer to the buffer where to return the data
[in]srcPointer to a multi-part buffer
[in]srcOffsetOffset from the beginning of the multi-part buffer
[in]lengthNumber of bytes to copy
Returns
Actual number of bytes copied

Definition at line 690 of file net_mem.c.

◆ netBufferSetLength()

error_t netBufferSetLength ( NetBuffer buffer,
size_t  length 
)

Adjust the length of a multi-part buffer.

Parameters
[in]bufferPointer to the multi-part buffer whose length is to be changed
[in]lengthDesired length
Returns
Error code

Definition at line 322 of file net_mem.c.

◆ netBufferWrite()

size_t netBufferWrite ( NetBuffer dest,
size_t  destOffset,
const void *  src,
size_t  length 
)

Write data to a multi-part buffer.

Parameters
[out]destPointer to a multi-part buffer
[in]destOffsetOffset from the beginning of the multi-part buffer
[in]srcUser buffer containing the data to be written
[in]lengthNumber of bytes to copy
Returns
Actual number of bytes copied

Definition at line 637 of file net_mem.c.