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