Rewrite of ringbuffer, and updated copyright file for Debian.

This commit is contained in:
n-a-c-h 2017-02-22 02:03:21 +02:00
parent e923015d45
commit 3e0f5d89ba
4 changed files with 101 additions and 130 deletions

View File

@ -1,119 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Sindre Aamås *
* aamas@stud.ntnu.no *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 as *
* published by the Free Software Foundation. *
* *
* 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 version 2 for more details. *
* *
* You should have received a copy of the GNU General Public License *
* version 2 along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include "array.h"
#include <algorithm>
#include <cstddef>
#include <cstring>
template <typename T> class RingBuffer
{
Array<T> buf;
std::size_t sz;
std::size_t rpos;
std::size_t wpos;
public:
RingBuffer(const std::size_t sz_in = 0) : sz(0), rpos(0), wpos(0)
{
reset(sz_in);
}
std::size_t avail() const
{
return (wpos < rpos ? 0 : sz) + rpos - wpos - 1;
}
void clear()
{
wpos = rpos = 0;
}
void fill(T value);
void read(T *out, std::size_t num);
void reset(std::size_t sz_in);
std::size_t size() const
{
return sz - 1;
}
std::size_t used() const
{
return (wpos < rpos ? sz : 0) + wpos - rpos;
}
void write(const T *in, std::size_t num);
};
template <typename T> void RingBuffer<T>::fill(const T value)
{
std::fill(buf + 0, buf + sz, value);
rpos = 0;
wpos = sz - 1;
}
template <typename T> void RingBuffer<T>::read(T *out, std::size_t num)
{
if (rpos + num > sz) {
const std::size_t n = sz - rpos;
std::memcpy(out, buf + rpos, n * sizeof(T));
rpos = 0;
num -= n;
out += n;
}
std::memcpy(out, buf + rpos, num * sizeof(T));
if ((rpos += num) == sz)
rpos = 0;
}
template <typename T> void RingBuffer<T>::reset(const std::size_t sz_in)
{
sz = sz_in + 1;
rpos = wpos = 0;
buf.reset(sz_in ? sz : 0);
}
template <typename T> void RingBuffer<T>::write(const T *in, std::size_t num)
{
if (wpos + num > sz) {
const std::size_t n = sz - wpos;
std::memcpy(buf + wpos, in, n * sizeof(T));
wpos = 0;
num -= n;
in += n;
}
std::memcpy(buf + wpos, in, num * sizeof(T));
if ((wpos += num) == sz)
wpos = 0;
}
#endif

View File

@ -18,7 +18,7 @@
#ifndef __VBA_SOUND_SDL_H__
#define __VBA_SOUND_SDL_H__
#include "RingBuffer.h"
#include "ringbuffer.h"
#include "SoundDriver.h"
#include "SDL.h"

95
src/common/ringbuffer.h Normal file
View File

@ -0,0 +1,95 @@
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include <algorithm>
#include <iterator>
#include <cstddef>
#include "array.h"
template <typename T> class RingBuffer
{
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T &reference;
typedef const T &const_reference;
typedef T *pointer;
typedef const T *const_pointer;
typedef T *iterator;
typedef const T *const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
private:
Array<T> m_buffer;
size_type m_size, m_pos_read, m_pos_write;
public:
RingBuffer(size_type size = 0) : m_size(0), m_pos_read(0), m_pos_write(0)
{
this->reset(size);
}
void reset(size_type size)
{
this->m_size = size+1;
this->m_pos_read = this->m_pos_write = 0;
this->m_buffer.reset(size ? this->m_size : 0);
}
size_type size() const
{
return(this->m_size-1);
}
void clear()
{
this->m_pos_read = this->m_pos_write = 0;
}
void fill(T value)
{
std::fill(this->m_buffer+0, this->m_buffer+this->m_buffer->size(), value);
this->m_pos_read = 0;
this->m_pos_write = this->size();
}
size_type avail() const
{
return(this->m_size - this->used());
}
size_type used() const
{
return
(
(this->m_pos_write < this->m_pos_read) ?
this->m_pos_write + (this->m_size - this->m_pos_read) :
this->m_pos_write - this->m_pos_read
);
}
void read(pointer buffer, size_type size)
{
size_type amount = std::min(size, this->m_size-this->m_pos_read);
std::copy(this->m_buffer+this->m_pos_read, this->m_buffer+this->m_pos_read+amount, buffer);
this->m_pos_read = (this->m_pos_read + amount) % this->m_size;
size -= amount;
std::copy(this->m_buffer+this->m_pos_read, this->m_buffer+this->m_pos_read+size, buffer+amount);
this->m_pos_read = (this->m_pos_read + size) % this->m_size;
}
void write(const_pointer buffer, size_type size)
{
size_type amount = std::min(size, this->m_size-this->m_pos_write);
std::copy(buffer, buffer+amount, this->m_buffer+m_pos_write);
this->m_pos_write = (this->m_pos_write + amount) % this->m_size;
std::copy(buffer+amount, buffer+size, this->m_buffer+m_pos_write);
size -= amount;
this->m_pos_write = (this->m_pos_write + size) % this->m_size;
}
};
#endif

View File

@ -6,22 +6,22 @@ Comment: This package was debianized by
Files: *
Copyright: 2007-2015 VBA-M development team
Copyright: 2007-2017 VBA-M development team
2004-2006 VBA development team
1999-2006 Forgotten
License: GPL-2+
Files: src/gtk/*
Copyright: 2008-2015 VBA-M development team
Copyright: 2008-2017 VBA-M development team
2004 VBA development team
License: GPL-2+
Files: src/sdl/*
Copyright: 2007-2015 VBA-M development team
Copyright: 2007-2017 VBA-M development team
License: GPL-2+
Files: src/wx/*
Copyright: 2011-2015 VBA-M development team
Copyright: 2011-2017 VBA-M development team
2011 Thomas J. Moore (aka tjm, aka darktjm) <darktjm@gmail.com>
License: GPL-2+
@ -62,11 +62,6 @@ Copyright: 2007, 2009, 2011, 2012, 2013, 2015 VBA-M development team
1995-2002 Jean-loup Gailly
License: Zlib
Files: src/common/Array.h
src/common/RingBuffer.h
Copyright: 2008 by Sindre Aamås
License: GPL-2
Files: src/filters/hq/asm/hq3x_16.asm
src/filters/hq/asm/hq3x_32.asm
src/filters/hq/asm/hq4x_16.asm
@ -77,7 +72,7 @@ License: GPL-2+
Files: src/filters/hq/asm/macros.mac
Copyright: 1997-2007 ZSNES Team (zsKnight, _Demo_, pagefault, Nach)
License: GPL-2
License: GPL-2+
Files: src/filters/xBRZ/*
Copyright: 2012-2015 Zenju (zenju AT gmx DOT de)