Skip to content

Memory manager for unified EMAC #5750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion features/FEATURE_LWIP/lwip-interface/LWIPInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdbool.h>
#include <string.h>
#include <new>
#include <stdint.h>

#include "lwip/opt.h"
#include "lwip/api.h"
Expand Down Expand Up @@ -271,6 +272,7 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
return NSAPI_ERROR_NO_MEMORY;
}
interface->emac = &emac;
interface->memory_manager = &memory_manager;
interface->ppp = false;

#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
Expand Down Expand Up @@ -401,7 +403,6 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
}
#endif

netif_set_up(&netif);
if (ppp) {
err_t err = ppp_lwip_connect(hw);
if (err) {
Expand Down
8 changes: 6 additions & 2 deletions features/FEATURE_LWIP/lwip-interface/LWIPInterfaceEMAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include "emac_stack_mem.h"
#include "lwip/tcpip.h"
#include "lwip/tcp.h"
#include "lwip/ip.h"
Expand All @@ -29,12 +28,16 @@

err_t LWIP::Interface::emac_low_level_output(struct netif *netif, struct pbuf *p)
{
/* Increase reference counter since lwip stores handle to pbuf and frees
it after output */
pbuf_ref(p);

LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
bool ret = mbed_if->emac->link_out(p);
return ret ? ERR_OK : ERR_IF;
}

void LWIP::Interface::emac_input(emac_stack_mem_t *buf)
void LWIP::Interface::emac_input(emac_mem_buf_t *buf)
{
struct pbuf *p = static_cast<struct pbuf *>(buf);

Expand Down Expand Up @@ -136,6 +139,7 @@ err_t LWIP::Interface::emac_if_init(struct netif *netif)
int err = ERR_OK;
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);

mbed_if->emac->set_memory_manager(*mbed_if->memory_manager);
mbed_if->emac->set_link_input_cb(mbed::callback(mbed_if, &LWIP::Interface::emac_input));
mbed_if->emac->set_link_state_cb(mbed::callback(mbed_if, &LWIP::Interface::emac_state_change));

Expand Down
163 changes: 163 additions & 0 deletions features/FEATURE_LWIP/lwip-interface/LWIPMemoryManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "pbuf.h"
#include "LWIPMemoryManager.h"

emac_mem_buf_t *LWIPMemoryManager::alloc_heap(uint32_t size, uint32_t align)
{
struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, size + align, PBUF_RAM);
if (pbuf == NULL) {
return NULL;
}

align_memory(pbuf, align);

return static_cast<emac_mem_buf_t *>(pbuf);
}

emac_mem_buf_t *LWIPMemoryManager::alloc_pool(uint32_t size, uint32_t align)
{
uint32_t total_align = count_total_align(size, align);

struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, size + total_align, PBUF_POOL);
if (pbuf == NULL) {
return NULL;
}

align_memory(pbuf, align);

return static_cast<emac_mem_buf_t *>(pbuf);
}

uint32_t LWIPMemoryManager::get_pool_alloc_unit(uint32_t align) const
{
uint32_t alloc_unit = LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) - align;
return alloc_unit;
}

void LWIPMemoryManager::free(emac_mem_buf_t *buf)
{
pbuf_free(static_cast<struct pbuf *>(buf));
}

uint32_t LWIPMemoryManager::get_total_len(const emac_mem_buf_t *buf) const
{
return (static_cast<const struct pbuf *>(buf))->tot_len;
}

void LWIPMemoryManager::copy(emac_mem_buf_t *to_buf, const emac_mem_buf_t *from_buf)
{
pbuf_copy(static_cast<struct pbuf *>(to_buf), static_cast<const struct pbuf *>(from_buf));
}

void LWIPMemoryManager::copy_to_buf(emac_mem_buf_t *to_buf, const void *ptr, uint32_t len)
{
pbuf_take(static_cast<struct pbuf *>(to_buf), ptr, len);
}

uint32_t LWIPMemoryManager::copy_from_buf(void *ptr, uint32_t len, const emac_mem_buf_t *from_buf) const
{
return pbuf_copy_partial(static_cast<const struct pbuf *>(from_buf), ptr, len, 0);
}

void LWIPMemoryManager::cat(emac_mem_buf_t *to_buf, emac_mem_buf_t *cat_buf)
{
pbuf_cat(static_cast<struct pbuf *>(to_buf), static_cast<struct pbuf *>(cat_buf));
}

emac_mem_buf_t *LWIPMemoryManager::get_next(const emac_mem_buf_t *buf) const
{
if (!buf) {
return NULL;
}
struct pbuf *next = (static_cast<const struct pbuf *>(buf))->next;
return static_cast<emac_mem_buf_t *>(next);
}

void *LWIPMemoryManager::get_ptr(const emac_mem_buf_t *buf) const
{
return (static_cast<const struct pbuf *>(buf))->payload;
}

uint32_t LWIPMemoryManager::get_len(const emac_mem_buf_t *buf) const
{
return (static_cast<const struct pbuf *>(buf))->len;
}

void LWIPMemoryManager::set_len(emac_mem_buf_t *buf, uint32_t len)
{
struct pbuf *pbuf = static_cast<struct pbuf *>(buf);
pbuf->len = len;
set_total_len(pbuf);
}

uint32_t LWIPMemoryManager::count_total_align(uint32_t size, uint32_t align)
{
uint32_t buffers = size / get_pool_alloc_unit(align);
if (size % get_pool_alloc_unit(align) != 0) {
buffers++;
}
return buffers * align;
}

void LWIPMemoryManager::align_memory(struct pbuf *pbuf, uint32_t align)
{
if (!align) {
return;
}

struct pbuf *pbuf_start = pbuf;

while (pbuf) {
uint32_t remainder = reinterpret_cast<uint32_t>(pbuf->payload) % align;
if (remainder) {
uint32_t offset = align - remainder;
if (offset >= align) {
offset = align;
}
pbuf->payload = static_cast<char *>(pbuf->payload) + offset;
}
pbuf->len -= align;
pbuf = pbuf->next;
}

// Correct total lengths
set_total_len(pbuf_start);
}

void LWIPMemoryManager::set_total_len(struct pbuf *pbuf)
{
if (!pbuf->next) {
pbuf->tot_len = pbuf->len;
return;
}

uint32_t total_len;
struct pbuf *pbuf_tailing;

while (pbuf) {
total_len = pbuf->len;

pbuf_tailing = pbuf->next;
while (pbuf_tailing) {
total_len += pbuf_tailing->len;
pbuf_tailing = pbuf_tailing->next;
}

pbuf->tot_len = total_len;
pbuf = pbuf->next;
}
}
Loading