Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ByteBuffer.cpp Source File

ByteBuffer.cpp

00001 /*
00002  * Copyright (c) 2018-2019, Arm Limited and affiliates.
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 "ByteBuffer.h"
00019 #include "mbed_assert.h"
00020 #include <string.h>
00021 
00022 ByteBuffer::ByteBuffer(uint32_t size): _head(0), _tail(0), _size(0), _buf(NULL)
00023 {
00024     resize(_size);
00025 }
00026 
00027 ByteBuffer::~ByteBuffer()
00028 {
00029     delete[] _buf;
00030     _buf = 0;
00031 }
00032 
00033 void ByteBuffer::resize(uint32_t size)
00034 {
00035     delete[] _buf;
00036     _head = 0;
00037     _tail = 0;
00038     _size = size + 1;
00039     _buf = new uint8_t[_size]();
00040 }
00041 
00042 void ByteBuffer::push(uint8_t data)
00043 {
00044     _buf[_tail] = data;
00045     _tail++;
00046     if (_tail >= _size) {
00047         _tail -= _size;
00048     }
00049     // Overflow not allowed
00050     MBED_ASSERT(_head != _tail);
00051 }
00052 
00053 void ByteBuffer::write(uint8_t *data, uint32_t size)
00054 {
00055     MBED_ASSERT(size <= free());
00056 
00057     if (size == 0) {
00058         return;
00059     }
00060 
00061     uint32_t new_tail = _tail + size;
00062     if (new_tail >= _size) {
00063         new_tail -= _size;
00064     }
00065 
00066     // Perform first memcpy
00067     uint32_t until_end = _size - _tail;
00068     uint32_t copy_size = until_end < size ? until_end : size;
00069     memcpy(_buf + _tail, data, copy_size);
00070     data += copy_size;
00071     size -= copy_size;
00072 
00073     // Perform second memcpy
00074     if (size > 0) {
00075         memcpy(_buf, data, size);
00076     }
00077 
00078     // Update tail
00079     _tail = new_tail;
00080 }
00081 
00082 uint8_t ByteBuffer::pop()
00083 {
00084     // Underflow not allowed
00085     MBED_ASSERT(_head != _tail);
00086     uint8_t val = _buf[_head];
00087     _head++;
00088     if (_head >= _size) {
00089         _head -= _size;
00090     }
00091     return val;
00092 }
00093 
00094 void ByteBuffer::read(uint8_t *data, uint32_t size)
00095 {
00096     MBED_ASSERT(size <= ByteBuffer::size());
00097 
00098     if (size == 0) {
00099         return;
00100     }
00101 
00102     uint32_t new_head = _head + size;
00103     if (new_head >= _size) {
00104         new_head -= _size;
00105     }
00106 
00107     // Perform first memcpy
00108     uint32_t until_end = _size - _head;
00109     uint32_t copy_size = until_end < size ? until_end : size;
00110     memcpy(data, _buf + _head, copy_size);
00111     data += copy_size;
00112     size -= copy_size;
00113 
00114     // Perform second memcpy
00115     if (size > 0) {
00116         memcpy(data, _buf, size);
00117     }
00118 
00119     // Update head
00120     _head = new_head;
00121 }
00122 
00123 uint32_t ByteBuffer::size()
00124 {
00125     uint32_t size;
00126     if (_tail < _head) {
00127         size = _size + _tail - _head;
00128     } else {
00129         size = _tail - _head;
00130     }
00131     return size;
00132 }
00133 
00134 uint32_t ByteBuffer::free()
00135 {
00136     return _size - size() - 1;
00137 }
00138 
00139 bool ByteBuffer::full()
00140 {
00141     uint32_t next = _tail + 1;
00142     if (next >= _size) {
00143         next -= _size;
00144     }
00145     return next == _head;
00146 }
00147 
00148 bool ByteBuffer::empty()
00149 {
00150     return _head == _tail;
00151 }