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.
NanostackMemoryManager.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2016 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "nsdynmemLIB.h" 00018 #include <string.h> 00019 #include "mbed_assert.h" 00020 #include "NanostackMemoryManager.h" 00021 00022 struct ns_stack_mem_t 00023 { 00024 ns_stack_mem_t *next; 00025 void *payload; 00026 uint32_t len; 00027 uint8_t mem[]; 00028 }; 00029 00030 emac_mem_buf_t *NanostackMemoryManager::alloc_heap(uint32_t size, uint32_t align) 00031 { 00032 ns_stack_mem_t *buf = static_cast<ns_stack_mem_t *>(ns_dyn_mem_temporary_alloc(sizeof(ns_stack_mem_t) + size + align)); 00033 if (buf == NULL) { 00034 return NULL; 00035 } 00036 00037 buf->next = NULL; 00038 buf->payload = buf->mem; 00039 buf->len = size; 00040 00041 if (align) { 00042 uint32_t remainder = reinterpret_cast<uint32_t>(buf->payload) % align; 00043 if (remainder) { 00044 uint32_t offset = align - remainder; 00045 if (offset >= align) { 00046 offset = align; 00047 } 00048 00049 buf->payload = static_cast<char *>(buf->payload) + offset; 00050 } 00051 } 00052 00053 return static_cast<emac_mem_buf_t *>(buf); 00054 } 00055 00056 emac_mem_buf_t *NanostackMemoryManager::alloc_pool(uint32_t size, uint32_t align) 00057 { 00058 return alloc_heap(size, align); 00059 } 00060 00061 uint32_t NanostackMemoryManager::get_pool_alloc_unit(uint32_t align) const 00062 { 00063 return 1536; // arbitrary nicely-aligned number big enough for Ethernet 00064 } 00065 00066 void NanostackMemoryManager::free(emac_mem_buf_t *mem) 00067 { 00068 ns_dyn_mem_free(mem); 00069 } 00070 00071 uint32_t NanostackMemoryManager::get_total_len(const emac_mem_buf_t *buf) const 00072 { 00073 const ns_stack_mem_t *mem = static_cast<const ns_stack_mem_t *>(buf); 00074 uint32_t total = 0; 00075 00076 while (mem) { 00077 total += mem->len; 00078 mem = mem->next; 00079 } 00080 return total; 00081 } 00082 00083 void NanostackMemoryManager::copy(emac_mem_buf_t *to, const emac_mem_buf_t *from) 00084 { 00085 ns_stack_mem_t *to_mem = static_cast<ns_stack_mem_t *>(to); 00086 const ns_stack_mem_t *from_mem = static_cast<const ns_stack_mem_t *>(from); 00087 MBED_ASSERT(get_total_len(to) >= get_total_len(from)); 00088 00089 uint32_t to_offset = 0; 00090 uint32_t from_offset = 0; 00091 while (from_mem) { 00092 uint32_t to_avail = to_mem->len - to_offset; 00093 uint32_t from_avail = from_mem->len - from_offset; 00094 uint32_t chunk = to_avail < from_avail ? to_avail : from_avail; 00095 uint8_t *to_ptr = static_cast<uint8_t *>(to_mem->payload) + to_offset; 00096 const uint8_t *from_ptr = static_cast<const uint8_t *>(from_mem->payload) + from_offset; 00097 memcpy(to_ptr, from_ptr, chunk); 00098 to_offset += chunk; 00099 if (to_offset == to_mem->len) { 00100 to_mem = to_mem->next; 00101 to_offset = 0; 00102 } 00103 from_offset += chunk; 00104 if (from_offset == from_mem->len) { 00105 from_mem = from_mem->next; 00106 from_offset = 0; 00107 } 00108 } 00109 } 00110 00111 void NanostackMemoryManager::cat(emac_mem_buf_t *to_buf, emac_mem_buf_t *cat_buf) 00112 { 00113 ns_stack_mem_t *to_mem = static_cast<ns_stack_mem_t *>(to_buf); 00114 ns_stack_mem_t *cat_mem = static_cast<ns_stack_mem_t *>(cat_buf); 00115 00116 while (to_mem->next) { 00117 to_mem = to_mem->next; 00118 } 00119 00120 to_mem->next = cat_mem; 00121 } 00122 00123 emac_mem_buf_t *NanostackMemoryManager::get_next(const emac_mem_buf_t *buf) const 00124 { 00125 return static_cast<const ns_stack_mem_t *>(buf)->next; 00126 } 00127 00128 void *NanostackMemoryManager::get_ptr(const emac_mem_buf_t *buf) const 00129 { 00130 return static_cast<const ns_stack_mem_t *>(buf)->payload; 00131 } 00132 00133 uint32_t NanostackMemoryManager::get_len(const emac_mem_buf_t *buf) const 00134 { 00135 return static_cast<const ns_stack_mem_t *>(buf)->len; 00136 } 00137 00138 void NanostackMemoryManager::set_len(emac_mem_buf_t *buf, uint32_t len) 00139 { 00140 ns_stack_mem_t *mem = static_cast<ns_stack_mem_t *>(buf); 00141 00142 mem->len = len; 00143 }
Generated on Tue Jul 12 2022 12:45:35 by
