|
| 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 | +class LWIPMemoryManager : public EMACMemoryManager { |
| 22 | +public: |
| 23 | + |
| 24 | + /** |
| 25 | + * Allocates memory buffer from the heap |
| 26 | + * |
| 27 | + * Memory buffer allocated from heap is always contiguous and can be arbitrary size. |
| 28 | + * |
| 29 | + * @param size Size of the memory to allocate in bytes |
| 30 | + * @param align Memory alignment requirement in bytes |
| 31 | + * @return Allocated memory buffer, or NULL in case of error |
| 32 | + */ |
| 33 | + virtual emac_mem_buf_t *alloc_heap(uint32_t size, uint32_t align); |
| 34 | + |
| 35 | + /** |
| 36 | + * Allocates memory buffer chain from a pool |
| 37 | + * |
| 38 | + * Memory allocated from pool is contiguous if size is equal or less than |
| 39 | + * (aligned) allocation unit, otherwise may be chained. Will typically come from |
| 40 | + * fixed-size packet pool memory. |
| 41 | + * |
| 42 | + * @param size Total size of the memory to allocate in bytes |
| 43 | + * @param align Memory alignment requirement for each buffer in bytes |
| 44 | + * @return Allocated memory buffer chain, or NULL in case of error |
| 45 | + */ |
| 46 | + virtual emac_mem_buf_t *alloc_pool(uint32_t size, uint32_t align); |
| 47 | + |
| 48 | + /** |
| 49 | + * Get memory buffer pool allocation unit |
| 50 | + * |
| 51 | + * Returns the maximum size of contiguous memory that can be allocated from a pool. |
| 52 | + * |
| 53 | + * @param align Memory alignment requirement in bytes |
| 54 | + * @return Contiguous memory size |
| 55 | + */ |
| 56 | + virtual uint32_t get_pool_alloc_unit(uint32_t align) const; |
| 57 | + |
| 58 | + /** |
| 59 | + * Free memory buffer chain |
| 60 | + * |
| 61 | + * If memory buffer is chained must point to the start of the chain. Frees all buffers |
| 62 | + * from the chained list. |
| 63 | + * |
| 64 | + * @param buf Memory buffer chain to be freed. |
| 65 | + */ |
| 66 | + virtual void free(emac_mem_buf_t *buf); |
| 67 | + |
| 68 | + /** |
| 69 | + * Return total length of a memory buffer chain |
| 70 | + * |
| 71 | + * Returns a total length of this buffer and any following buffers in the chain. |
| 72 | + * |
| 73 | + * @param buf Memory buffer chain |
| 74 | + * @return Total length in bytes |
| 75 | + */ |
| 76 | + virtual uint32_t get_total_len(const emac_mem_buf_t *buf) const; |
| 77 | + |
| 78 | + /** |
| 79 | + * Copy a memory buffer chain |
| 80 | + * |
| 81 | + * Copies data from one buffer chain to another. Copy operation does not adjust the lengths |
| 82 | + * of the copied-to memory buffer chain, so chain total lengths must be the same. |
| 83 | + * |
| 84 | + * @param to_buf Memory buffer chain to copy to |
| 85 | + * @param from_buf Memory buffer chain to copy from |
| 86 | + */ |
| 87 | + virtual void copy(emac_mem_buf_t *to_buf, const emac_mem_buf_t *from_buf); |
| 88 | + |
| 89 | + /** |
| 90 | + * Concatenate two memory buffer chains |
| 91 | + * |
| 92 | + * Concatenates buffer chain to end of the other buffer chain. Concatenated-to buffer total length |
| 93 | + * is adjusted accordingly. cat_buf must point to the start of a the chain. After concatenation |
| 94 | + * to_buf's chain now owns those buffers, and they will be freed when the to_buf chain is freed. |
| 95 | + * |
| 96 | + * @param to_buf Memory buffer chain to concatenate to |
| 97 | + * @param cat_buf Memory buffer chain to concatenate |
| 98 | + */ |
| 99 | + virtual void cat(emac_mem_buf_t *to_buf, emac_mem_buf_t *cat_buf); |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns the next buffer |
| 103 | + * |
| 104 | + * Returns the next buffer from the memory buffer chain. |
| 105 | + * |
| 106 | + * @param buf Memory buffer |
| 107 | + * @return The next memory buffer, or NULL if last |
| 108 | + */ |
| 109 | + virtual emac_mem_buf_t *get_next(const emac_mem_buf_t *buf) const; |
| 110 | + |
| 111 | + /** |
| 112 | + * Return pointer to the payload of the buffer |
| 113 | + * |
| 114 | + * @param buf Memory buffer |
| 115 | + * @return Pointer to the payload |
| 116 | + */ |
| 117 | + virtual void *get_ptr(const emac_mem_buf_t *buf) const; |
| 118 | + |
| 119 | + /** |
| 120 | + * Return payload size of the buffer |
| 121 | + * |
| 122 | + * @param buf Memory buffer |
| 123 | + * @return Size in bytes |
| 124 | + */ |
| 125 | + virtual uint32_t get_len(const emac_mem_buf_t *buf) const; |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets the payload size of the buffer |
| 129 | + * |
| 130 | + * The allocated payload size will not change. It is not permitted |
| 131 | + * to change the length of a buffer that is not the first (or only) in a chain. |
| 132 | + * |
| 133 | + * @param buf Memory buffer |
| 134 | + * @param len Payload size, must be less or equal allocated size |
| 135 | + */ |
| 136 | + virtual void set_len(emac_mem_buf_t *buf, uint32_t len); |
| 137 | + |
| 138 | +private: |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns a total memory alignment size |
| 142 | + * |
| 143 | + * Calculates the total memory alignment size for a memory buffer chain. |
| 144 | + * Used internally on pool allocation. |
| 145 | + * |
| 146 | + * @param size Size of the memory to allocate in bytes |
| 147 | + * @param align Memory alignment requirement for each buffer in bytes |
| 148 | + * @return Total alignment needed in bytes |
| 149 | + */ |
| 150 | + uint32_t count_total_align(uint32_t size, uint32_t align); |
| 151 | + |
| 152 | + /** |
| 153 | + * Aligns a memory buffer chain |
| 154 | + * |
| 155 | + * Aligns a memory buffer chain and updates lengths and total lengths |
| 156 | + * accordingly. There needs to be enough overhead to do the alignment |
| 157 | + * for all buffers. |
| 158 | + * |
| 159 | + * @param pbuf Memory buffer |
| 160 | + * @param align Memory alignment requirement for each buffer in bytes |
| 161 | + */ |
| 162 | + void align_memory(struct pbuf *pbuf, uint32_t align); |
| 163 | + |
| 164 | + /** |
| 165 | + * Sets total lengths of a memory buffer chain |
| 166 | + * |
| 167 | + * Sets total length fields for a memory buffer chain based on buffer |
| 168 | + * length fields. All total lengths are calculated again. |
| 169 | + * |
| 170 | + * @param pbuf Memory buffer |
| 171 | + */ |
| 172 | + void set_total_len(struct pbuf *pbuf); |
| 173 | +}; |
| 174 | + |
| 175 | +#endif /* LWIP_MEMORY_MANAGER_H */ |
0 commit comments