Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
LWIPMemoryManager.cpp
00001 /* Copyright (c) 2017 ARM Limited 00002 * 00003 * Licensed under the Apache License, Version 2.0 (the "License"); 00004 * you may not use this file except in compliance with the License. 00005 * You may obtain a copy of the License at 00006 * 00007 * http://www.apache.org/licenses/LICENSE-2.0 00008 * 00009 * Unless required by applicable law or agreed to in writing, software 00010 * distributed under the License is distributed on an "AS IS" BASIS, 00011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 * See the License for the specific language governing permissions and 00013 * limitations under the License. 00014 */ 00015 00016 #include "pbuf.h" 00017 #include "LWIPMemoryManager.h" 00018 00019 net_stack_mem_buf_t *LWIPMemoryManager::alloc_heap(uint32_t size, uint32_t align) 00020 { 00021 struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, size + align, PBUF_RAM); 00022 if (pbuf == NULL) { 00023 return NULL; 00024 } 00025 00026 align_memory(pbuf, align); 00027 00028 return static_cast<net_stack_mem_buf_t *>(pbuf); 00029 } 00030 00031 net_stack_mem_buf_t *LWIPMemoryManager::alloc_pool(uint32_t size, uint32_t align) 00032 { 00033 uint32_t total_align = count_total_align(size, align); 00034 00035 struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, size + total_align, PBUF_POOL); 00036 if (pbuf == NULL) { 00037 return NULL; 00038 } 00039 00040 align_memory(pbuf, align); 00041 00042 return static_cast<net_stack_mem_buf_t *>(pbuf); 00043 } 00044 00045 uint32_t LWIPMemoryManager::get_pool_alloc_unit(uint32_t align) const 00046 { 00047 uint32_t alloc_unit = LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) - align; 00048 return alloc_unit; 00049 } 00050 00051 void LWIPMemoryManager::free(net_stack_mem_buf_t *buf) 00052 { 00053 pbuf_free(static_cast<struct pbuf *>(buf)); 00054 } 00055 00056 uint32_t LWIPMemoryManager::get_total_len(const net_stack_mem_buf_t *buf) const 00057 { 00058 return (static_cast<const struct pbuf *>(buf))->tot_len; 00059 } 00060 00061 void LWIPMemoryManager::copy(net_stack_mem_buf_t *to_buf, const net_stack_mem_buf_t *from_buf) 00062 { 00063 pbuf_copy(static_cast<struct pbuf *>(to_buf), static_cast<const struct pbuf *>(from_buf)); 00064 } 00065 00066 void LWIPMemoryManager::copy_to_buf(net_stack_mem_buf_t *to_buf, const void *ptr, uint32_t len) 00067 { 00068 pbuf_take(static_cast<struct pbuf *>(to_buf), ptr, len); 00069 } 00070 00071 uint32_t LWIPMemoryManager::copy_from_buf(void *ptr, uint32_t len, const net_stack_mem_buf_t *from_buf) const 00072 { 00073 return pbuf_copy_partial(static_cast<const struct pbuf *>(from_buf), ptr, len, 0); 00074 } 00075 00076 void LWIPMemoryManager::cat(net_stack_mem_buf_t *to_buf, net_stack_mem_buf_t *cat_buf) 00077 { 00078 pbuf_cat(static_cast<struct pbuf *>(to_buf), static_cast<struct pbuf *>(cat_buf)); 00079 } 00080 00081 net_stack_mem_buf_t *LWIPMemoryManager::get_next(const net_stack_mem_buf_t *buf) const 00082 { 00083 if (!buf) { 00084 return NULL; 00085 } 00086 struct pbuf *next = (static_cast<const struct pbuf *>(buf))->next; 00087 return static_cast<net_stack_mem_buf_t *>(next); 00088 } 00089 00090 void *LWIPMemoryManager::get_ptr(const net_stack_mem_buf_t *buf) const 00091 { 00092 return (static_cast<const struct pbuf *>(buf))->payload; 00093 } 00094 00095 uint32_t LWIPMemoryManager::get_len(const net_stack_mem_buf_t *buf) const 00096 { 00097 return (static_cast<const struct pbuf *>(buf))->len; 00098 } 00099 00100 void LWIPMemoryManager::set_len(net_stack_mem_buf_t *buf, uint32_t len) 00101 { 00102 struct pbuf *pbuf = static_cast<struct pbuf *>(buf); 00103 pbuf->len = len; 00104 set_total_len(pbuf); 00105 } 00106 00107 uint32_t LWIPMemoryManager::count_total_align(uint32_t size, uint32_t align) 00108 { 00109 uint32_t buffers = size / get_pool_alloc_unit(align); 00110 if (size % get_pool_alloc_unit(align) != 0) { 00111 buffers++; 00112 } 00113 return buffers * align; 00114 } 00115 00116 void LWIPMemoryManager::align_memory(struct pbuf *pbuf, uint32_t align) 00117 { 00118 if (!align) { 00119 return; 00120 } 00121 00122 struct pbuf *pbuf_start = pbuf; 00123 00124 while (pbuf) { 00125 uint32_t remainder = reinterpret_cast<uint32_t>(pbuf->payload) % align; 00126 if (remainder) { 00127 uint32_t offset = align - remainder; 00128 if (offset >= align) { 00129 offset = align; 00130 } 00131 pbuf->payload = static_cast<char *>(pbuf->payload) + offset; 00132 } 00133 pbuf->len -= align; 00134 pbuf = pbuf->next; 00135 } 00136 00137 // Correct total lengths 00138 set_total_len(pbuf_start); 00139 } 00140 00141 void LWIPMemoryManager::set_total_len(struct pbuf *pbuf) 00142 { 00143 if (!pbuf->next) { 00144 pbuf->tot_len = pbuf->len; 00145 return; 00146 } 00147 00148 uint32_t total_len; 00149 struct pbuf *pbuf_tailing; 00150 00151 while (pbuf) { 00152 total_len = pbuf->len; 00153 00154 pbuf_tailing = pbuf->next; 00155 while (pbuf_tailing) { 00156 total_len += pbuf_tailing->len; 00157 pbuf_tailing = pbuf_tailing->next; 00158 } 00159 00160 pbuf->tot_len = total_len; 00161 pbuf = pbuf->next; 00162 } 00163 }
Generated on Tue Jul 12 2022 13:54:31 by
