leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Packet.cpp Source File

Packet.cpp

00001 /*
00002  * Copyright (c) 2018 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 "Packet.h"
00018 
00019 // =================================================================================================
00020 // Packet class members.
00021 Packet *Packet::new_packet(uint32_t size, volatile uint32_t &heap_usage)
00022 {
00023     Packet *p = new (std::nothrow) Packet(size, heap_usage);
00024     if (p != NULL) {
00025         char *data = new (std::nothrow) char[size];
00026         if (data != NULL) {
00027             p->m_data = data;
00028             uint32_t l = heap_usage;
00029             uint32_t l2 = core_util_atomic_incr_u32(&heap_usage, size);
00030             // tr_debug("%p: allocating heap (%p)%lu+%lu=%lu", p, &heap_usage, l, size, l2);
00031             (void)l;
00032             (void)l2;
00033         } else {
00034             delete p;
00035             p = NULL;
00036         }
00037     }
00038     return p;
00039 }
00040 
00041 uint32_t Packet::append(const char *data, uint32_t len)
00042 {
00043     uint32_t cpy_len = m_size - m_len;
00044     if (cpy_len > len) {
00045         cpy_len = len;
00046     }
00047     // tr_debug("%p::append(%p, %lu){%lu/%lu}: %lu", this, data, len, m_len, m_size, cpy_len);
00048     memcpy(m_data + m_len, data, cpy_len);
00049     m_len += cpy_len;
00050     return cpy_len;
00051 }
00052 
00053 uint32_t Packet::consume(char *data, uint32_t len)
00054 {
00055     uint32_t read = m_len;
00056     if (len < read) {
00057         read = len;
00058     }
00059     // tr_debug("%p::consume(%p, %lu){%lu/%lu}: %lu", this, data, len, m_len, m_size, read);
00060     memcpy(data, m_data, read);
00061     m_len -= read;
00062     memmove(m_data, m_data + read, m_len);
00063     return read;
00064 }
00065 
00066 void Packet::set_next(Packet *next)
00067 {
00068     // if (m_next != NULL) {
00069     //     tr_debug("%p::set_next(%p): detaching from %p", this, next, m_next);
00070     // }
00071     // tr_debug("%p::set_next(%p): attaching to %p", this, next, next);
00072     m_next = next;
00073 }
00074 
00075 Packet::~Packet()
00076 {
00077     if (m_data != NULL) {
00078         delete m_data;
00079     }
00080     if (m_next != NULL) {
00081         // tr_debug("%p: delete next: %p", this, m_next);
00082         delete m_next;
00083     }
00084     uint32_t l = m_ref_heap_usage;
00085     uint32_t l2 = core_util_atomic_decr_u32(&m_ref_heap_usage, m_size);
00086     // tr_debug("%p: freeing heap: (%p)%lu-%lu=%lu", this, &m_ref_heap_usage, l, m_size, l2);
00087     (void)l;
00088     (void)l2;
00089 }