net_mem.h
Go to the documentation of this file.
1 /**
2  * @file net_mem.h
3  * @brief Memory management
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _NET_MEM_H
32 #define _NET_MEM_H
33 
34 //Dependencies
35 #include "net_config.h"
36 #include "os_port.h"
37 #include "error.h"
38 
39 //Use fixed-size blocks allocation?
40 #ifndef NET_MEM_POOL_SUPPORT
41  #define NET_MEM_POOL_SUPPORT DISABLED
42 #elif (NET_MEM_POOL_SUPPORT != ENABLED && NET_MEM_POOL_SUPPORT != DISABLED)
43  #error NET_MEM_POOL_SUPPORT parameter is not valid
44 #endif
45 
46 //Number of buffers available
47 #ifndef NET_MEM_POOL_BUFFER_COUNT
48  #define NET_MEM_POOL_BUFFER_COUNT 32
49 #elif (NET_MEM_POOL_BUFFER_COUNT < 1)
50  #error NET_MEM_POOL_BUFFER_COUNT parameter is not valid
51 #endif
52 
53 //Size of the buffers
54 #ifndef NET_MEM_POOL_BUFFER_SIZE
55  #define NET_MEM_POOL_BUFFER_SIZE 1536
56 #elif (NET_MEM_POOL_BUFFER_SIZE < 128)
57  #error NET_MEM_POOL_BUFFER_SIZE parameter is not valid
58 #endif
59 
60 //Size of the header part of the buffer
61 #define CHUNKED_BUFFER_HEADER_SIZE (sizeof(NetBuffer) + MAX_CHUNK_COUNT * sizeof(ChunkDesc))
62 
63 //Helper macro for defining a buffer
64 #define N(size) (((size) + NET_MEM_POOL_BUFFER_SIZE - 1) / NET_MEM_POOL_BUFFER_SIZE)
65 
66 //C++ guard
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
71 
72 /**
73  * @brief Structure describing a chunk of data
74  **/
75 
76 typedef struct
77 {
78  void *address;
79  uint16_t length;
80  uint16_t size;
81 } ChunkDesc;
82 
83 
84 /**
85  * @brief Structure describing a buffer that spans multiple chunks
86  **/
87 
88 typedef struct
89 {
92  ChunkDesc chunk[];
93 } NetBuffer;
94 
95 
96 typedef struct
97 {
100  ChunkDesc chunk[1];
101 } NetBuffer1;
102 
103 
104 //Memory management functions
105 error_t memPoolInit(void);
106 void *memPoolAlloc(size_t size);
107 void memPoolFree(void *p);
108 void memPoolGetStats(uint_t *currentUsage, uint_t *maxUsage, uint_t *size);
109 
111 void netBufferFree(NetBuffer *buffer);
112 
113 size_t netBufferGetLength(const NetBuffer *buffer);
114 error_t netBufferSetLength(NetBuffer *buffer, size_t length);
115 
116 void *netBufferAt(const NetBuffer *buffer, size_t offset, size_t length);
117 
119  const NetBuffer *src, size_t srcOffset, size_t length);
120 
121 error_t netBufferCopy(NetBuffer *dest, size_t destOffset,
122  const NetBuffer *src, size_t srcOffset, size_t length);
123 
124 error_t netBufferAppend(NetBuffer *dest, const void *src, size_t length);
125 
126 size_t netBufferWrite(NetBuffer *dest,
127  size_t destOffset, const void *src, size_t length);
128 
129 size_t netBufferRead(void *dest, const NetBuffer *src,
130  size_t srcOffset, size_t length);
131 
132 //C++ guard
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 #endif
void * memPoolAlloc(size_t size)
Allocate a memory block.
Definition: net_mem.c:100
void memPoolFree(void *p)
Release a memory block.
Definition: net_mem.c:166
NetBuffer * netBufferAlloc(size_t length)
Allocate a multi-part buffer.
Definition: net_mem.c:243
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:690
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:418
uint_t chunkCount
Definition: net_mem.h:98
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint_t chunkCount
Definition: net_mem.h:90
error_t netBufferCopy(NetBuffer *dest, size_t destOffset, const NetBuffer *src, size_t srcOffset, size_t length)
Copy data between multi-part buffers.
Definition: net_mem.c:522
void netBufferFree(NetBuffer *buffer)
Dispose a multi-part buffer.
Definition: net_mem.c:282
uint16_t length
Definition: net_mem.h:79
error_t netBufferConcat(NetBuffer *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Concatenate two multi-part buffers.
Definition: net_mem.c:460
size_t netBufferWrite(NetBuffer *dest, size_t destOffset, const void *src, size_t length)
Write data to a multi-part buffer.
Definition: net_mem.c:637
error_t
Error codes.
Definition: error.h:43
Structure describing a chunk of data.
Definition: net_mem.h:77
void * address
Definition: net_mem.h:78
void memPoolGetStats(uint_t *currentUsage, uint_t *maxUsage, uint_t *size)
Get memory pool usage.
Definition: net_mem.c:207
uint_t maxChunkCount
Definition: net_mem.h:99
uint_t maxChunkCount
Definition: net_mem.h:91
Error codes description.
uint8_t length
Definition: tcp.h:368
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
error_t memPoolInit(void)
Memory pool initialization.
Definition: net_mem.c:70
unsigned int uint_t
Definition: compiler_port.h:50
uint16_t size
Definition: net_mem.h:80
RTOS abstraction layer.
error_t netBufferAppend(NetBuffer *dest, const void *src, size_t length)
Append data a multi-part buffer.
Definition: net_mem.c:604
error_t netBufferSetLength(NetBuffer *buffer, size_t length)
Adjust the length of a multi-part buffer.
Definition: net_mem.c:322