SnapShotFrameCache.cpp 1.01 KB
#include <cuda_runtime.h>
#include "SnapShotFrameCache.h"

void SnapShotFrameCache::init_cache(unsigned int cache_len, unsigned int cache_count)
{
	this->cache_len = cache_len;
	this->cache_count = cache_count;
	alloc();
}

bool SnapShotFrameCache::alloc()
{
	for (size_t i = 0; i < cache_count; i++)
	{
		void * frame = NULL;
		auto cudaStatus = cudaMalloc((void**)&frame, cache_len);
		if (cudaStatus != cudaSuccess) 
		{
			return false;
		}
		cache.push(frame);
	}
	return true;
}

void * SnapShotFrameCache::get_frame()
{
	if (cache.empty())
	{
		alloc();
		if (!cache.empty())
		{
			void * frame = cache.front();
			cache.pop();
			return frame;
		}
		else
		{
			return nullptr;
		}
	}
	else
	{
		void * frame = cache.front();
		cache.pop();
		return frame;
	}
}

void SnapShotFrameCache::release(void * frame)
{
	cache.push(frame);
}

void SnapShotFrameCache::free()
{
	while (!cache.empty())
	{
		void * frame = cache.front();
		cudaFree(frame);
		cache.pop();
	}
}