forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixedSizeQueue.h
229 lines (191 loc) · 4.79 KB
/
FixedSizeQueue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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
// the Free Software Foundation, version 2.0 or later versions.
// 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/
#ifndef _FIXED_SIZE_QUEUE_H_
#define _FIXED_SIZE_QUEUE_H_
#include <cstring>
#include "ChunkFile.h"
#include "MemoryUtil.h"
// 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>
class FixedSizeQueue {
public:
FixedSizeQueue() {
// Allocate aligned memory, just because.
//int sizeInBytes = N * sizeof(T);
//storage_ = (T *)AllocateMemoryPages(sizeInBytes);
storage_ = new T[N];
clear();
}
~FixedSizeQueue() {
// FreeMemoryPages((void *)storage_, N * sizeof(T));
delete [] storage_;
}
void clear() {
head_ = 0;
tail_ = 0;
count_ = 0;
}
void push(T t) {
storage_[tail_] = t;
tail_++;
if (tail_ == N)
tail_ = 0;
count_++;
}
// Gets pointers to write to directly.
void pushPointers(size_t size, T **dest1, size_t *sz1, T **dest2, size_t *sz2) {
if (tail_ + (int)size < N) {
*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) {
if ((int)size > count_) size = count_;
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;
}
void pop() {
head_++;
if (head_ == N)
head_ = 0;
count_--;
}
/*
void push_array(const T *ptr, size_t num) {
// TODO: memcpy
for (size_t i = 0; i < num; i++) {
push(ptr[i]);
}
}
void pop_array(T *outptr, size_t num) {
for (size_t i = 0; i < num; i++) {
outptr[i] = front();
pop();
}
}*/
T pop_front() {
const T &temp = storage_[head_];
pop();
return temp;
}
T &front() { return storage_[head_]; }
const T &front() const { return storage_[head_]; }
size_t size() const {
return count_;
}
size_t capacity() const {
return N;
}
int room() const {
return N - count_;
}
bool empty() {
return count_ == 0;
}
void DoState(PointerWrap &p) {
int size = N;
p.Do(size);
if (size != N)
{
ERROR_LOG(COMMON, "Savestate failure: Incompatible queue size.");
return;
}
p.DoArray<T>(storage_, N);
p.Do(head_);
p.Do(tail_);
p.Do(count_);
p.DoMarker("FixedSizeQueue");
}
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.
FixedSizeQueue(FixedSizeQueue &other) { }
};
// 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;
if (nextBlock == NUM_BLOCKS) nextBlock = 0;
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];
}
T *EndPop() {
curReadBlock++;
if (curReadBlock == NUM_BLOCKS)
curReadBlock = 0;
}
private:
enum { NUM_BLOCKS = 16 };
T **blocks[NUM_BLOCKS];
volatile int curReadBlock;
volatile int curWriteBlock;
};
#endif // _FIXED_SIZE_QUEUE_H_