2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// 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
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2014-12-15 22:09:36 +00:00
|
|
|
#pragma once
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-03 02:33:24 +00:00
|
|
|
#include <cstring>
|
2013-02-04 04:31:46 +00:00
|
|
|
#include "ChunkFile.h"
|
2013-06-19 10:00:24 +00:00
|
|
|
#include "MemoryUtil.h"
|
2012-11-03 02:33:24 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// STL-look-a-like interface, but name is mixed case to distinguish it clearly from the
|
|
|
|
// real STL classes.
|
|
|
|
|
|
|
|
// Not fully featured, no safety checking yet. Add features as needed.
|
|
|
|
|
|
|
|
template <class T, int N>
|
2012-11-17 13:20:04 +00:00
|
|
|
class FixedSizeQueue {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
2012-11-17 13:20:04 +00:00
|
|
|
FixedSizeQueue() {
|
2013-06-19 10:00:24 +00:00
|
|
|
// Allocate aligned memory, just because.
|
2013-06-19 11:13:02 +00:00
|
|
|
//int sizeInBytes = N * sizeof(T);
|
|
|
|
//storage_ = (T *)AllocateMemoryPages(sizeInBytes);
|
|
|
|
storage_ = new T[N];
|
2012-11-01 15:19:01 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2012-11-17 13:20:04 +00:00
|
|
|
~FixedSizeQueue() {
|
2013-06-19 11:13:02 +00:00
|
|
|
// FreeMemoryPages((void *)storage_, N * sizeof(T));
|
|
|
|
delete [] storage_;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2012-11-17 13:20:04 +00:00
|
|
|
head_ = 0;
|
|
|
|
tail_ = 0;
|
|
|
|
count_ = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void push(T t) {
|
2012-11-17 13:20:04 +00:00
|
|
|
storage_[tail_] = t;
|
|
|
|
tail_++;
|
|
|
|
if (tail_ == N)
|
|
|
|
tail_ = 0;
|
|
|
|
count_++;
|
|
|
|
}
|
|
|
|
|
2013-06-19 10:00:24 +00:00
|
|
|
// Gets pointers to write to directly.
|
|
|
|
void pushPointers(size_t size, T **dest1, size_t *sz1, T **dest2, size_t *sz2) {
|
2013-06-27 14:28:11 +00:00
|
|
|
if (tail_ + (int)size < N) {
|
2013-06-19 10:00:24 +00:00
|
|
|
*dest1 = &storage_[tail_];
|
|
|
|
*sz1 = size;
|
|
|
|
tail_ += (int)size;
|
|
|
|
if (tail_ == N) tail_ = 0;
|
|
|
|
*dest2 = 0;
|
|
|
|
*sz2 = 0;
|
|
|
|
} else {
|
|
|
|
*dest1 = &storage_[tail_];
|
|
|
|
*sz1 = N - tail_;
|
|
|
|
tail_ = (int)(size - *sz1);
|
|
|
|
*dest2 = &storage_[0];
|
|
|
|
*sz2 = tail_;
|
|
|
|
}
|
|
|
|
count_ += (int)size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void popPointers(size_t size, const T **src1, size_t *sz1, const T **src2, size_t *sz2) {
|
2013-06-27 14:28:11 +00:00
|
|
|
if ((int)size > count_) size = count_;
|
2013-06-19 10:00:24 +00:00
|
|
|
|
|
|
|
if (head_ + size < N) {
|
|
|
|
*src1 = &storage_[head_];
|
|
|
|
*sz1 = size;
|
|
|
|
head_ += (int)size;
|
|
|
|
if (head_ == N) head_ = 0;
|
|
|
|
*src2 = 0;
|
|
|
|
*sz2 = 0;
|
|
|
|
} else {
|
|
|
|
*src1 = &storage_[head_];
|
|
|
|
*sz1 = N - head_;
|
|
|
|
head_ = (int)(size - *sz1);
|
|
|
|
*src2 = &storage_[0];
|
|
|
|
*sz2 = head_;
|
|
|
|
}
|
|
|
|
count_ -= (int)size;
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void pop() {
|
2012-11-17 13:20:04 +00:00
|
|
|
head_++;
|
|
|
|
if (head_ == N)
|
|
|
|
head_ = 0;
|
|
|
|
count_--;
|
|
|
|
}
|
|
|
|
|
2012-11-17 14:33:16 +00:00
|
|
|
/*
|
|
|
|
void push_array(const T *ptr, size_t num) {
|
|
|
|
// TODO: memcpy
|
|
|
|
for (size_t i = 0; i < num; i++) {
|
|
|
|
push(ptr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 13:20:04 +00:00
|
|
|
void pop_array(T *outptr, size_t num) {
|
2012-11-17 14:33:16 +00:00
|
|
|
for (size_t i = 0; i < num; i++) {
|
2012-11-17 13:20:04 +00:00
|
|
|
outptr[i] = front();
|
|
|
|
pop();
|
|
|
|
}
|
2012-11-17 14:33:16 +00:00
|
|
|
}*/
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
T pop_front() {
|
2012-11-17 13:20:04 +00:00
|
|
|
const T &temp = storage_[head_];
|
2012-11-01 15:19:01 +00:00
|
|
|
pop();
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2012-11-17 13:20:04 +00:00
|
|
|
T &front() { return storage_[head_]; }
|
|
|
|
|
|
|
|
const T &front() const { return storage_[head_]; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
size_t size() const {
|
2012-11-17 13:20:04 +00:00
|
|
|
return count_;
|
|
|
|
}
|
|
|
|
|
2012-12-18 10:40:14 +00:00
|
|
|
size_t capacity() const {
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2012-11-17 13:20:04 +00:00
|
|
|
int room() const {
|
|
|
|
return N - count_;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-28 08:05:54 +00:00
|
|
|
bool empty() {
|
|
|
|
return count_ == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoState(PointerWrap &p) {
|
|
|
|
int size = N;
|
|
|
|
p.Do(size);
|
|
|
|
if (size != N)
|
|
|
|
{
|
2013-09-07 20:31:14 +00:00
|
|
|
ERROR_LOG(COMMON, "Savestate failure: Incompatible queue size.");
|
2012-12-28 08:05:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
p.DoArray<T>(storage_, N);
|
|
|
|
p.Do(head_);
|
|
|
|
p.Do(tail_);
|
|
|
|
p.Do(count_);
|
|
|
|
p.DoMarker("FixedSizeQueue");
|
|
|
|
}
|
2012-11-17 13:20:04 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
T *storage_;
|
|
|
|
int head_;
|
|
|
|
int tail_;
|
|
|
|
int count_; // sacrifice 4 bytes for a simpler implementation. may optimize away in the future.
|
|
|
|
|
|
|
|
// Make copy constructor private for now.
|
2015-12-28 08:21:02 +00:00
|
|
|
FixedSizeQueue(FixedSizeQueue &other);
|
|
|
|
FixedSizeQueue& operator=(const FixedSizeQueue &other);
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2013-06-19 10:00:24 +00:00
|
|
|
|
|
|
|
// I'm not sure this is 100% safe but it might be "Good Enough" :)
|
|
|
|
// TODO: Use this, maybe make it safer first by using proper atomics
|
|
|
|
// instead of volatile
|
|
|
|
template<class T, int blockSize, int numBlocks>
|
|
|
|
class LockFreeBlockQueue {
|
|
|
|
public:
|
|
|
|
LockFreeBlockQueue() {
|
|
|
|
curReadBlock = 0;
|
|
|
|
curWriteBlock = 0;
|
|
|
|
for (size_t i = 0; i < numBlocks; i++) {
|
|
|
|
blocks[i] = new T[blockSize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~LockFreeBlockQueue() {
|
|
|
|
for (size_t i = 0; i < numBlocks; i++) {
|
|
|
|
delete [] blocks[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the returned pointer then call EndPush to finish the push.
|
|
|
|
T *BeginPush() {
|
|
|
|
return blocks[curWriteBlock];
|
|
|
|
}
|
|
|
|
void EndPush() {
|
|
|
|
curWriteBlock++;
|
|
|
|
if (curWriteBlock == NUM_BLOCKS)
|
|
|
|
curWriteBlock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanPush() {
|
|
|
|
int nextBlock = curWriteBlock + 1;
|
2013-06-19 13:05:19 +00:00
|
|
|
if (nextBlock == NUM_BLOCKS) nextBlock = 0;
|
2013-06-19 10:00:24 +00:00
|
|
|
return nextBlock != curReadBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanPop() { return curReadBlock != curWriteBlock; }
|
|
|
|
|
|
|
|
// Read from the returned pointer then call EndPush to finish the pop.
|
|
|
|
T *BeginPop() {
|
|
|
|
return blocks[curReadBlock];
|
|
|
|
}
|
2015-12-28 08:21:02 +00:00
|
|
|
void EndPop() {
|
2013-06-19 10:00:24 +00:00
|
|
|
curReadBlock++;
|
|
|
|
if (curReadBlock == NUM_BLOCKS)
|
|
|
|
curReadBlock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum { NUM_BLOCKS = 16 };
|
|
|
|
T **blocks[NUM_BLOCKS];
|
|
|
|
|
|
|
|
volatile int curReadBlock;
|
|
|
|
volatile int curWriteBlock;
|
|
|
|
};
|
|
|
|
|