From fe8085484038b83d431459ac7fc3980cbbac9137 Mon Sep 17 00:00:00 2001 From: dufourj Date: Sun, 9 Apr 2006 15:53:14 +0000 Subject: Data: - ajout du buffer circulaire (enfin !) --- i/marvin/src/data/Makefile.defs | 6 +- i/marvin/src/data/data_buffer.hh | 4 +- i/marvin/src/data/data_circular_buffer.cc | 123 +++++++++++++++++++++++++ i/marvin/src/data/data_circular_buffer.hh | 64 +++++++++++++ i/marvin/src/data/test_data_circular_buffer.cc | 92 ++++++++++++++++++ 5 files changed, 285 insertions(+), 4 deletions(-) create mode 100644 i/marvin/src/data/data_circular_buffer.cc create mode 100644 i/marvin/src/data/data_circular_buffer.hh create mode 100644 i/marvin/src/data/test_data_circular_buffer.cc diff --git a/i/marvin/src/data/Makefile.defs b/i/marvin/src/data/Makefile.defs index 54e6cb6..aa64e47 100644 --- a/i/marvin/src/data/Makefile.defs +++ b/i/marvin/src/data/Makefile.defs @@ -1,8 +1,10 @@ -PROGRAMS += test_data test_data_buffer +PROGRAMS += test_data test_data_buffer test_data_circular_buffer data_OBJECTS = data_input.o data_input_file.o data_input_zlib.o \ - data_buffer.o data_output.o -lz + data_buffer.o data_output.o data_circular_buffer.o -lz test_data_OBJECTS = test_data.o $(data_OBJECTS) test_data_buffer_OBJECTS = test_data_buffer.o data_buffer.o + +test_data_circular_buffer_OBJECTS = test_data_circular_buffer.o data_circular_buffer.o diff --git a/i/marvin/src/data/data_buffer.hh b/i/marvin/src/data/data_buffer.hh index 3d2201d..e0d526e 100644 --- a/i/marvin/src/data/data_buffer.hh +++ b/i/marvin/src/data/data_buffer.hh @@ -51,8 +51,6 @@ class DataBuffer : public DataInput, public DataOutput dataType_e type_; /// Augmente la taille du buffer pour correspondre à size. void grow (const unsigned size); - /// Echange les buffers. - void swap (DataBuffer &d); public: /// Constructeur par défaut. DataBuffer (void); @@ -73,6 +71,8 @@ class DataBuffer : public DataInput, public DataOutput void write (const uint8_t *buf, unsigned size); /// Taille utile du buffer. unsigned size (void) const { return tailPos_ - headPos_; } + /// Echange les buffers. + void swap (DataBuffer &d); /// Type de données du buffer. dataType_e type (void) const { return type_; } /// Change le type de données du buffer. diff --git a/i/marvin/src/data/data_circular_buffer.cc b/i/marvin/src/data/data_circular_buffer.cc new file mode 100644 index 0000000..be17526 --- /dev/null +++ b/i/marvin/src/data/data_circular_buffer.cc @@ -0,0 +1,123 @@ +// data_circular_buffer.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_circular_buffer.hh" + +#include // std::memcpy +#include // std::runtime_error +#include // std::swap, std::min + +#include // XXX + +/// Default constructor. +/// Size is the allocated size in memory. +DataCircularBuffer::DataCircularBuffer (unsigned size) + : size_ (size + 1), startPos_ (0), endPos_ (0) +{ + // The + 1 for size is because, in some cases, we will have an empty case. + buffer_ = new uint8_t [size_]; +} + +/// Destructor. +DataCircularBuffer::~DataCircularBuffer (void) +{ + delete [] buffer_; +} + +/// Read data of maximum size in the data pointer (data must be allocated of +/// size). +unsigned +DataCircularBuffer::read (uint8_t *data, unsigned size) +{ + if (!data || !size) + throw std::invalid_argument ("Invalid parameter"); + return internalRead (data, size); +} + +/// Internal read, do the real job. +unsigned +DataCircularBuffer::internalRead (uint8_t *data, unsigned size) +{ + // Size of data to read + unsigned sizeToRead = std::min (maxReadableData (), size); + // Size of data to read for the end of the buffer + unsigned sizeToReadEnd = std::min (size_ - startPos_, sizeToRead); + // Copy end part of buffer + if (data) + std::memcpy (&data[0], &buffer_[startPos_], sizeToReadEnd); + // Need to do the begin part of buffer ? + int sizeToReadStart = sizeToRead - sizeToReadEnd; + if (sizeToReadStart > 0) + { + if (data) + std::memcpy (&data[sizeToReadEnd], &buffer_[0], sizeToReadStart); + // Move start of data + startPos_ = 0 + sizeToReadStart; + } + else + // Move start of data + startPos_ = (startPos_ + sizeToRead) % size_; + return sizeToRead; +} + +/// Write data of size length. +void +DataCircularBuffer::write (const uint8_t *data, unsigned size) +{ + if (!data || !size) + throw std::invalid_argument ("Invalid parameter"); + + // Max size we can write + unsigned sizeToWrite = std::min (size, size_ - 1); + + // Read data before overwrite it if needed + int sizeToRead = sizeToWrite - (size_ - maxReadableData ()) + 1; + if (sizeToRead > 0) + if (internalRead (0, sizeToRead) != (unsigned) sizeToRead) + throw std::runtime_error ("Unexpected error"); + + // Max size we can write until the end of the buffer + unsigned sizeToWriteEnd = std::min (size_ - endPos_, sizeToWrite); + // Write first part. + std::memcpy (&buffer_[endPos_], &data[size - sizeToWrite + 0], sizeToWriteEnd); + // More to write ? + unsigned sizeToWriteStart = sizeToWrite - sizeToWriteEnd; + if (sizeToWriteStart > 0) + { + std::memcpy (&buffer_[0], &data[size - sizeToWrite + sizeToWriteEnd], sizeToWriteStart); + endPos_ = 0 + sizeToWriteStart; + } + else + endPos_ = (endPos_ + sizeToWrite) % size_; +} + +/// Swap data between tow DataCircularBuffer. +void +DataCircularBuffer::swap (DataCircularBuffer &d) +{ + std::swap (buffer_, d.buffer_); + std::swap (size_, d.size_); + std::swap (startPos_, d.startPos_); + std::swap (endPos_, d.endPos_); +} diff --git a/i/marvin/src/data/data_circular_buffer.hh b/i/marvin/src/data/data_circular_buffer.hh new file mode 100644 index 0000000..f08426d --- /dev/null +++ b/i/marvin/src/data/data_circular_buffer.hh @@ -0,0 +1,64 @@ +#ifndef data_circular_buffer_hh +#define data_circular_buffer_hh +// data_circular_buffer.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +#include "data_input.hh" +#include "data_output.hh" + +#include // uint8_t + +class DataCircularBuffer : public DataInput, public DataOutput +{ + private: + /// Calculate the quantity of maximum data we can read. + unsigned maxReadableData (void) + { return (endPos_ + size_ - startPos_) % size_; } + /// Internal read, do the real job. + unsigned internalRead (uint8_t *data, unsigned size); + /// Buffer. + uint8_t *buffer_; + /// Size of the buffer. + unsigned size_; + /// Start and end positions of data in the buffer. + unsigned startPos_, endPos_; + public: + /// Default constructor. + /// Size is the allocated size in memory. + DataCircularBuffer (unsigned size); + /// Destructor. + ~DataCircularBuffer (void); + /// Read data of maximum size in the data pointer (data must be allocated + /// of size). + unsigned read (uint8_t *data, unsigned size); + /// Write data of size length. + void write (const uint8_t *data, unsigned size); + /// Size of the buffer. + unsigned size (void) const { return size_; } + /// Swap data between tow DataCircularBuffer. + void swap (DataCircularBuffer &d); +}; + +#endif // data_circular_buffer_hh diff --git a/i/marvin/src/data/test_data_circular_buffer.cc b/i/marvin/src/data/test_data_circular_buffer.cc new file mode 100644 index 0000000..9bdc063 --- /dev/null +++ b/i/marvin/src/data/test_data_circular_buffer.cc @@ -0,0 +1,92 @@ +// test_data_circular_buffer.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_circular_buffer.hh" + +#include +#include +#include +#include + +#define MAX_SIZE_BUFF 101 +#define MAX_SIZE_CIRC 99 + +unsigned +randVal (double max) +{ + return 1 + (int) (max * std::rand () / (RAND_MAX + 1.0)); +} + +int +main (void) +{ + // Init random + std::srand (std::time (NULL)); + try + { + DataCircularBuffer d (MAX_SIZE_CIRC); + uint8_t buffOrig[MAX_SIZE_BUFF]; + uint8_t buffRes[MAX_SIZE_BUFF]; + + int i, compt, tmp; + // Fill buffer + for (i = 0; i < MAX_SIZE_BUFF; i++) + memset (&buffOrig[i], i, 1); + + // Fill circular buffer + compt = 0; + for (i = 0; i < 42; i++) + { + tmp = randVal (MAX_SIZE_BUFF - compt); + d.write (&buffOrig[compt], tmp); + compt = (tmp + compt) % MAX_SIZE_BUFF; + } + + // Read + compt = 0; + unsigned res; + do + { + tmp = randVal (7.); + res = d.read (&buffRes[compt], tmp); + compt += tmp; + } while (res != 0); + + // Compare + for (i = 0; i < MAX_SIZE_CIRC - 1; i++) + { + if (buffRes [i] != buffOrig [(buffRes[0] +i) % MAX_SIZE_BUFF]) + throw std::runtime_error ("Test failed !"); + } + + } + + catch (const std::exception &e) + { + std::cerr << e.what () << std::endl; + return 1; + } + return 0; +} + -- cgit v1.2.3