2011-10-11 08:44:27 -05:00
|
|
|
/**
|
|
|
|
|
* \file
|
|
|
|
|
* \brief Source code for the Cache class
|
|
|
|
|
* \author Copyright (c) 2011 Jonathan Thomas
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "../include/Cache.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2012-10-10 15:21:33 -05:00
|
|
|
// Default constructor, no max frames
|
2012-10-11 17:30:32 -05:00
|
|
|
Cache::Cache() : max_bytes(0), total_bytes(0) { };
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Constructor that sets the max frames to cache
|
2012-10-11 17:30:32 -05:00
|
|
|
Cache::Cache(int64 max_bytes) : max_bytes(max_bytes), total_bytes(0) { };
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Add a Frame to the cache
|
2012-10-14 03:43:52 -05:00
|
|
|
void Cache::Add(int frame_number, tr1::shared_ptr<Frame> frame)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2012-10-12 16:41:23 -05:00
|
|
|
// Remove frame if it already exists
|
|
|
|
|
if (Exists(frame_number))
|
|
|
|
|
// Move frame to front of queue
|
|
|
|
|
MoveToFront(frame_number);
|
|
|
|
|
else
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
// Add frame to queue and map
|
|
|
|
|
frames[frame_number] = frame;
|
2011-10-26 00:34:48 -05:00
|
|
|
frame_numbers.push_front(frame_number);
|
2012-10-11 17:30:32 -05:00
|
|
|
|
|
|
|
|
// Increment total bytes (of cache)
|
|
|
|
|
total_bytes += frame->GetBytes();
|
|
|
|
|
|
|
|
|
|
// Clean up old frames
|
|
|
|
|
CleanUp();
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for the existance of a frame in the cache
|
|
|
|
|
bool Cache::Exists(int frame_number)
|
|
|
|
|
{
|
|
|
|
|
// Is frame number cached
|
|
|
|
|
if (frames.count(frame_number))
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get a frame from the cache
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> Cache::GetFrame(int frame_number)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
// Does frame exists in cache?
|
|
|
|
|
if (Exists(frame_number))
|
|
|
|
|
{
|
2012-10-12 16:41:23 -05:00
|
|
|
// move it to the front of the cache
|
|
|
|
|
MoveToFront(frame_number);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// return the Frame object
|
|
|
|
|
return frames[frame_number];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// throw an exception for the missing frame
|
|
|
|
|
throw OutOfBoundsFrame("Frame not found in the cache", frame_number, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
// Get the smallest frame number
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> Cache::GetSmallestFrame()
|
2011-10-24 08:22:21 -05:00
|
|
|
{
|
2011-10-26 00:34:48 -05:00
|
|
|
// Loop through frame numbers
|
|
|
|
|
deque<int>::iterator itr;
|
|
|
|
|
int smallest_frame = -1;
|
|
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr < smallest_frame || smallest_frame == -1)
|
|
|
|
|
smallest_frame = *itr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return frame (or error if no frame found)
|
|
|
|
|
return GetFrame(smallest_frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove a specific frame
|
2012-10-14 21:09:22 -05:00
|
|
|
void Cache::Remove(int frame_number)
|
2011-10-26 00:34:48 -05:00
|
|
|
{
|
|
|
|
|
// Get the frame (or throw exception)
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> f = GetFrame(frame_number);
|
2011-10-26 00:34:48 -05:00
|
|
|
|
2012-10-11 17:30:32 -05:00
|
|
|
// Decrement the total bytes (for this cache)
|
|
|
|
|
total_bytes -= f->GetBytes();
|
|
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
// Loop through frame numbers
|
2012-07-05 00:01:42 -05:00
|
|
|
deque<int>::iterator itr;
|
|
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr == frame_number)
|
|
|
|
|
{
|
|
|
|
|
// erase frame number
|
|
|
|
|
frame_numbers.erase(itr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-26 00:34:48 -05:00
|
|
|
|
2012-07-05 00:01:42 -05:00
|
|
|
// Remove frame from map
|
|
|
|
|
frames.erase(frame_number);
|
2011-10-24 08:22:21 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
// Move frame to front of queue (so it lasts longer)
|
|
|
|
|
void Cache::MoveToFront(int frame_number)
|
|
|
|
|
{
|
|
|
|
|
// Does frame exists in cache?
|
|
|
|
|
if (Exists(frame_number))
|
|
|
|
|
{
|
|
|
|
|
// Loop through frame numbers
|
|
|
|
|
deque<int>::iterator itr;
|
|
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr == frame_number)
|
|
|
|
|
{
|
|
|
|
|
// erase frame number
|
|
|
|
|
frame_numbers.erase(itr);
|
|
|
|
|
|
|
|
|
|
// add frame number to 'front' of queue
|
|
|
|
|
frame_numbers.push_front(frame_number);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// throw an exception for the missing frame
|
|
|
|
|
throw OutOfBoundsFrame("Frame not found in the cache", frame_number, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Clear the cache of all frames
|
|
|
|
|
void Cache::Clear()
|
|
|
|
|
{
|
2012-08-15 17:27:14 -05:00
|
|
|
deque<int>::iterator itr;
|
|
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
// Remove frame from map
|
|
|
|
|
frames.erase(*itr);
|
|
|
|
|
|
|
|
|
|
// pop each of the frames from the queue... which empties the queue
|
2011-10-26 00:34:48 -05:00
|
|
|
while(!frame_numbers.empty()) frame_numbers.pop_back();
|
2012-10-11 17:30:32 -05:00
|
|
|
|
|
|
|
|
// Reset total bytes (of cache)
|
|
|
|
|
total_bytes = 0;
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Count the frames in the queue
|
|
|
|
|
int Cache::Count()
|
|
|
|
|
{
|
|
|
|
|
// Return the number of frames in the cache
|
|
|
|
|
return frames.size();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 17:30:32 -05:00
|
|
|
// Clean up cached frames that exceed the number in our max_bytes variable
|
2011-10-11 08:44:27 -05:00
|
|
|
void Cache::CleanUp()
|
|
|
|
|
{
|
2012-10-10 17:27:46 -05:00
|
|
|
// Do we auto clean up?
|
2012-10-11 17:30:32 -05:00
|
|
|
if (max_bytes > 0)
|
2012-10-10 15:21:33 -05:00
|
|
|
{
|
2012-10-11 17:30:32 -05:00
|
|
|
// check against max bytes
|
|
|
|
|
while (total_bytes > max_bytes)
|
2012-10-10 17:27:46 -05:00
|
|
|
{
|
|
|
|
|
// Remove the oldest frame
|
|
|
|
|
int frame_to_remove = frame_numbers.back();
|
2012-07-05 00:01:42 -05:00
|
|
|
|
2012-10-10 17:27:46 -05:00
|
|
|
// Remove frame_number and frame
|
|
|
|
|
Remove(frame_to_remove);
|
2012-07-05 00:01:42 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-01 01:43:06 -05:00
|
|
|
|
|
|
|
|
// Display a list of cached frame numbers
|
|
|
|
|
void Cache::Display()
|
|
|
|
|
{
|
|
|
|
|
cout << "----- Cache List (" << frames.size() << ") ------" << endl;
|
|
|
|
|
deque<int>::iterator itr;
|
|
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
cout << " " << i << ") --- Frame " << *itr << endl;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|