Mistake on this page?
Report an issue in GitHub or email us
LWIPMemoryManager.h
1 /* Copyright (c) 2017 ARM Limited
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LWIP_MEMORY_MANAGER_H
17 #define LWIP_MEMORY_MANAGER_H
18 
19 #include "EMACMemoryManager.h"
20 
21 
22 class LWIPMemoryManager final : public EMACMemoryManager {
23 public:
24 
25  /**
26  * Allocates memory buffer from the heap
27  *
28  * Memory buffer allocated from heap is always contiguous and can be arbitrary size.
29  *
30  * @param size Size of the memory to allocate in bytes
31  * @param align Memory alignment requirement in bytes
32  * @return Allocated memory buffer, or NULL in case of error
33  */
34  net_stack_mem_buf_t *alloc_heap(uint32_t size, uint32_t align) override;
35 
36  /**
37  * Allocates memory buffer chain from a pool
38  *
39  * Memory allocated from pool is contiguous if size is equal or less than
40  * (aligned) allocation unit, otherwise may be chained. Will typically come from
41  * fixed-size packet pool memory.
42  *
43  * @param size Total size of the memory to allocate in bytes
44  * @param align Memory alignment requirement for each buffer in bytes
45  * @return Allocated memory buffer chain, or NULL in case of error
46  */
47  net_stack_mem_buf_t *alloc_pool(uint32_t size, uint32_t align) override;
48 
49  /**
50  * Get memory buffer pool allocation unit
51  *
52  * Returns the maximum size of contiguous memory that can be allocated from a pool.
53  *
54  * @param align Memory alignment requirement in bytes
55  * @return Contiguous memory size
56  */
57  uint32_t get_pool_alloc_unit(uint32_t align) const override;
58 
59  /**
60  * Free memory buffer chain
61  *
62  * If memory buffer is chained must point to the start of the chain. Frees all buffers
63  * from the chained list.
64  *
65  * @param buf Memory buffer chain to be freed.
66  */
67  void free(net_stack_mem_buf_t *buf) override;
68 
69  /**
70  * Return total length of a memory buffer chain
71  *
72  * Returns a total length of this buffer and any following buffers in the chain.
73  *
74  * @param buf Memory buffer chain
75  * @return Total length in bytes
76  */
77  uint32_t get_total_len(const net_stack_mem_buf_t *buf) const override;
78 
79  /**
80  * Copy a memory buffer chain
81  *
82  * Copies data from one buffer chain to another. Copy operation does not adjust the lengths
83  * of the copied-to memory buffer chain, so chain total lengths must be the same.
84  *
85  * @param to_buf Memory buffer chain to copy to
86  * @param from_buf Memory buffer chain to copy from
87  */
88  void copy(net_stack_mem_buf_t *to_buf, const net_stack_mem_buf_t *from_buf) override;
89 
90  /**
91  * Copy to a memory buffer chain
92  *
93  * Copies data to a buffer chain. Copy operation does not adjust the lengths
94  * of the copied-to memory buffer chain, so chain total length must match the
95  * copied length.
96  *
97  * @param to_buf Memory buffer chain to copy to
98  * @param ptr Pointer to data
99  * @param len Data length
100  */
101  void copy_to_buf(net_stack_mem_buf_t *to_buf, const void *ptr, uint32_t len) override;
102 
103  /**
104  * Copy from a memory buffer chain
105  *
106  * Copies data from a memory buffer chain.
107  *
108  * @param len Data length
109  * @param ptr Pointer to data
110  * @param from_buf Memory buffer chain to copy from
111  * @return Length of the data that was copied
112  */
113  uint32_t copy_from_buf(void *ptr, uint32_t len, const net_stack_mem_buf_t *from_buf) const override;
114 
115  /**
116  * Concatenate two memory buffer chains
117  *
118  * Concatenates buffer chain to end of the other buffer chain. Concatenated-to buffer total length
119  * is adjusted accordingly. cat_buf must point to the start of a the chain. After concatenation
120  * to_buf's chain now owns those buffers, and they will be freed when the to_buf chain is freed.
121  *
122  * @param to_buf Memory buffer chain to concatenate to
123  * @param cat_buf Memory buffer chain to concatenate
124  */
125  void cat(net_stack_mem_buf_t *to_buf, net_stack_mem_buf_t *cat_buf) override;
126 
127  /**
128  * Returns the next buffer
129  *
130  * Returns the next buffer from the memory buffer chain.
131  *
132  * @param buf Memory buffer
133  * @return The next memory buffer, or NULL if last
134  */
135  net_stack_mem_buf_t *get_next(const net_stack_mem_buf_t *buf) const override;
136 
137  /**
138  * Return pointer to the payload of the buffer
139  *
140  * @param buf Memory buffer
141  * @return Pointer to the payload
142  */
143  void *get_ptr(const net_stack_mem_buf_t *buf) const override;
144 
145  /**
146  * Return payload size of the buffer
147  *
148  * @param buf Memory buffer
149  * @return Size in bytes
150  */
151  uint32_t get_len(const net_stack_mem_buf_t *buf) const override;
152 
153  /**
154  * Sets the payload size of the buffer
155  *
156  * The allocated payload size will not change. It is not permitted
157  * to change the length of a buffer that is not the first (or only) in a chain.
158  *
159  * @param buf Memory buffer
160  * @param len Payload size, must be less or equal allocated size
161  */
162  void set_len(net_stack_mem_buf_t *buf, uint32_t len) override;
163 
164 private:
165 
166  /**
167  * Returns a total memory alignment size
168  *
169  * Calculates the total memory alignment size for a memory buffer chain.
170  * Used internally on pool allocation.
171  *
172  * @param size Size of the memory to allocate in bytes
173  * @param align Memory alignment requirement for each buffer in bytes
174  * @return Total alignment needed in bytes
175  */
176  uint32_t count_total_align(uint32_t size, uint32_t align);
177 
178  /**
179  * Aligns a memory buffer chain
180  *
181  * Aligns a memory buffer chain and updates lengths and total lengths
182  * accordingly. There needs to be enough overhead to do the alignment
183  * for all buffers.
184  *
185  * @param pbuf Memory buffer
186  * @param align Memory alignment requirement for each buffer in bytes
187  */
188  void align_memory(struct pbuf *pbuf, uint32_t align);
189 
190  /**
191  * Sets total lengths of a memory buffer chain
192  *
193  * Sets total length fields for a memory buffer chain based on buffer
194  * length fields. All total lengths are calculated again.
195  *
196  * @param pbuf Memory buffer
197  */
198  void set_total_len(struct pbuf *pbuf);
199 };
200 
201 #endif /* LWIP_MEMORY_MANAGER_H */
void * get_ptr(const net_stack_mem_buf_t *buf) const override
Return pointer to the payload of the buffer.
net_stack_mem_buf_t * alloc_pool(uint32_t size, uint32_t align) override
Allocates memory buffer chain from a pool.
void copy(net_stack_mem_buf_t *to_buf, const net_stack_mem_buf_t *from_buf) override
Copy a memory buffer chain.
void copy_to_buf(net_stack_mem_buf_t *to_buf, const void *ptr, uint32_t len) override
Copy to a memory buffer chain.
void set_len(net_stack_mem_buf_t *buf, uint32_t len) override
Sets the payload size of the buffer.
uint32_t get_total_len(const net_stack_mem_buf_t *buf) const override
Return total length of a memory buffer chain.
void free(net_stack_mem_buf_t *buf) override
Free memory buffer chain.
uint32_t get_pool_alloc_unit(uint32_t align) const override
Get memory buffer pool allocation unit.
net_stack_mem_buf_t * alloc_heap(uint32_t size, uint32_t align) override
Allocates memory buffer from the heap.
net_stack_mem_buf_t * get_next(const net_stack_mem_buf_t *buf) const override
Returns the next buffer.
Main packet buffer struct.
uint32_t copy_from_buf(void *ptr, uint32_t len, const net_stack_mem_buf_t *from_buf) const override
Copy from a memory buffer chain.
void cat(net_stack_mem_buf_t *to_buf, net_stack_mem_buf_t *cat_buf) override
Concatenate two memory buffer chains.
uint32_t get_len(const net_stack_mem_buf_t *buf) const override
Return payload size of the buffer.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.