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