CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/HW/BufferQueue.h
Views: 1401
// Copyright (c) 2013- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <map>20#include <cstdint>21#include <cstring>22#include "Common/Log.h"23#include "Common/Serialize/Serializer.h"2425struct BufferQueue {26BufferQueue(int size = 0x20000) {27alloc(size);28}2930~BufferQueue() {31delete [] bufQueue;32}3334bool alloc(int size) {35_assert_(size > 0);36delete [] bufQueue;37bufQueue = new unsigned char[size];38bufQueueSize = size;39clear();40return true;41}4243void clear() {44start = 0;45end = 0;46filled = 0;47}4849inline int getQueueSize() {50return filled;51}5253inline int getRemainSize() {54return bufQueueSize - getQueueSize();55}5657bool push(const unsigned char *buf, int addsize, s64 pts = 0) {58int space = getRemainSize();59if (space < addsize || addsize < 0)60return false;61savePts(pts);62if (end + addsize <= bufQueueSize) {63// If end is before start, there's enough space. Otherwise, we're nearing the queue size.64memcpy(bufQueue + end, buf, addsize);65end += addsize;66if (end == bufQueueSize)67end = 0;68} else {69// Time to wrap end. Fill what remains, then fill before start.70_assert_(end >= start);71int firstSize = bufQueueSize - end;72memcpy(bufQueue + end, buf, firstSize);73memcpy(bufQueue, buf + firstSize, addsize - firstSize);74end = addsize - firstSize;75}76filled += addsize;77verifyQueueSize();78return true;79}8081int pop_front(unsigned char *buf, int wantedsize, s64 *pts = nullptr) {82if (wantedsize <= 0)83return 0;84int bytesgot = getQueueSize();85if (wantedsize < bytesgot)86bytesgot = wantedsize;87if (pts != nullptr) {88*pts = findPts(bytesgot);89}9091int firstSize = bufQueueSize - start;92if (buf) {93if (bytesgot <= firstSize) {94memcpy(buf, bufQueue + start, bytesgot);95} else {96memcpy(buf, bufQueue + start, firstSize);97memcpy(buf + firstSize, bufQueue, bytesgot - firstSize);98}99}100if (bytesgot <= firstSize)101start += bytesgot;102else103start = bytesgot - firstSize;104if (start == bufQueueSize)105start = 0;106filled -= bytesgot;107verifyQueueSize();108return bytesgot;109}110111int get_front(unsigned char *buf, int wantedsize) {112if (wantedsize <= 0)113return 0;114int bytesgot = getQueueSize();115if (wantedsize < bytesgot)116bytesgot = wantedsize;117int firstSize = bufQueueSize - start;118if (bytesgot <= firstSize) {119memcpy(buf, bufQueue + start, bytesgot);120} else {121memcpy(buf, bufQueue + start, firstSize);122memcpy(buf + firstSize, bufQueue, bytesgot - firstSize);123}124return bytesgot;125}126127void DoState(PointerWrap &p);128129private:130void savePts(u64 pts) {131if (pts != 0) {132ptsMarks[end] = pts;133}134}135136u64 findPts(std::map<u32, s64>::iterator earliest, std::map<u32, s64>::iterator latest) {137u64 pts = 0;138// Take the first one, that is the pts of this packet.139if (earliest != latest) {140pts = earliest->second;141}142ptsMarks.erase(earliest, latest);143return pts;144}145146u64 findPts(int packetSize) {147auto earliest = ptsMarks.lower_bound(start);148auto latest = ptsMarks.lower_bound(start + packetSize);149150u64 pts = findPts(earliest, latest);151152// If it wraps around, we have to look at the other half too.153if (start + packetSize > bufQueueSize) {154earliest = ptsMarks.begin();155latest = ptsMarks.lower_bound(start + packetSize - bufQueueSize);156// This also clears the range, so we always call on wrap.157u64 latePts = findPts(earliest, latest);158if (pts == 0)159pts = latePts;160}161162return pts;163}164165inline int calcQueueSize() {166if (end < start) {167return bufQueueSize + end - start;168}169return end - start;170}171172inline void verifyQueueSize() {173_assert_(calcQueueSize() == filled || (end == start && filled == bufQueueSize));174}175176uint8_t *bufQueue = nullptr;177// Model: end may be less than start, indicating the space between end and start is free.178// If end equals start, we're empty.179int start = 0, end = 0;180int filled = 0;181int bufQueueSize = 0;182183std::map<u32, s64> ptsMarks;184};185186187