prepare to use FixedQueue

avoiding allocations in real-time code
This commit is contained in:
tildearrow 2023-07-12 19:43:15 -05:00
parent 90b76d20e3
commit dbd200c279
3 changed files with 85 additions and 3 deletions

81
src/engine/fixedQueue.h Normal file
View File

@ -0,0 +1,81 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef _FIXED_QUEUE_H
#define _FIXED_QUEUE_H
#include <stdint.h>
#include "../ta-log.h"
template<typename T, size_t items> struct FixedQueue {
size_t readPos, writePos;
T data[items];
T& front();
bool pop();
bool push(const T& item);
void clear();
bool empty();
size_t size();
FixedQueue():
readPos(0),
writePos(0) {}
};
template <typename T, size_t items> T& FixedQueue<T,items>::front() {
return data[readPos];
}
template <typename T, size_t items> bool FixedQueue<T,items>::pop() {
if (readPos==writePos) return false;
if (++readPos>=items) readPos=0;
return true;
}
template <typename T, size_t items> bool FixedQueue<T,items>::push(const T& item) {
if (writePos==(readPos-1)) {
logW("queue overflow!");
return false;
}
if (writePos==0 && readPos==items-1) {
logW("queue overflow!");
return false;
}
data[writePos]=item;
if (++writePos>=items) writePos=0;
return true;
}
template <typename T, size_t items> void FixedQueue<T,items>::clear() {
readPos=0;
writePos=0;
}
template <typename T, size_t items> bool FixedQueue<T,items>::empty() {
return (readPos==writePos);
}
template <typename T, size_t items> size_t FixedQueue<T,items>::size() {
if (readPos>writePos) {
return items+writePos-readPos;
}
return writePos-readPos;
}
#endif

View File

@ -22,7 +22,7 @@
#include <math.h>
//#define rWrite(a,v) pendingWrites[a]=v;
#define rWrite(a,v) if (!skipRegisterWrites) {writes.emplace(a,v); if (dumpWrites) {addWrite(a,v);} }
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
#define chWrite(c,a,v) \
if (!skipRegisterWrites) { \
if (curChan!=c) { \

View File

@ -21,7 +21,7 @@
#define _PCE_H
#include "../dispatch.h"
#include <queue>
#include "../fixedQueue.h"
#include "../waveSynth.h"
#include "sound/pce_psg.h"
@ -62,9 +62,10 @@ class DivPlatformPCE: public DivDispatch {
struct QueuedWrite {
unsigned char addr;
unsigned char val;
QueuedWrite(): addr(0), val(9) {}
QueuedWrite(unsigned char a, unsigned char v): addr(a), val(v) {}
};
std::queue<QueuedWrite> writes;
FixedQueue<QueuedWrite,512> writes;
unsigned char lastPan;
int cycles, curChan, delay;