Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NetStackMemoryManager.cpp Source File

NetStackMemoryManager.cpp

00001 /*
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "NetStackMemoryManager.h"
00019 
00020 void NetStackMemoryManager::copy_to_buf(net_stack_mem_buf_t *to_buf, const void *ptr, uint32_t len)
00021 {
00022     while (to_buf && len) {
00023         void *copy_to_ptr = get_ptr(to_buf);
00024         uint32_t copy_to_len = get_len(to_buf);
00025 
00026         if (copy_to_len > len) {
00027             copy_to_len = len;
00028             len = 0;
00029         } else {
00030             len -= copy_to_len;
00031         }
00032 
00033         memcpy(copy_to_ptr, ptr, copy_to_len);
00034         ptr = static_cast<const uint8_t *>(ptr) + copy_to_len;
00035 
00036         to_buf = get_next(to_buf);
00037     }
00038 }
00039 
00040 uint32_t NetStackMemoryManager::copy_from_buf(void *ptr, uint32_t len, const net_stack_mem_buf_t *from_buf) const
00041 {
00042     uint32_t copied_len = 0;
00043 
00044     while (from_buf && len) {
00045         void *copy_from_ptr = get_ptr(from_buf);
00046         uint32_t copy_from_len = get_len(from_buf);
00047 
00048         if (copy_from_len > len) {
00049             copy_from_len = len;
00050             len = 0;
00051         } else {
00052             len -= copy_from_len;
00053         }
00054 
00055         memcpy(ptr, copy_from_ptr, copy_from_len);
00056         ptr = static_cast<uint8_t *>(ptr) + copy_from_len;
00057         copied_len += copy_from_len;
00058 
00059         from_buf = get_next(from_buf);
00060     }
00061 
00062     return copied_len;
00063 }
00064