2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_CACHE_H__
|
|
|
|
#define __UTIL_CACHE_H__
|
|
|
|
|
|
|
|
/* Disk Cache based on Hashing
|
|
|
|
*
|
|
|
|
* To be used to cache expensive computations. The hash key is created from an
|
|
|
|
* arbitrary number of bytes, by hashing the bytes using MD5, which then gives
|
|
|
|
* the file name containing the data. This data then is read from the file
|
|
|
|
* again into the appropriate data structures.
|
|
|
|
*
|
|
|
|
* This way we do not need to accurately track changes, compare dates and
|
2014-08-17 02:18:40 +00:00
|
|
|
* invalidate cache entries, at the cost of extra computation. If everything
|
2011-04-27 11:58:34 +00:00
|
|
|
* is stored in a global cache, computations can perhaps even be shared between
|
|
|
|
* different scenes where it may be hard to detect duplicate work.
|
|
|
|
*/
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
#include "util_set.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_string.h"
|
|
|
|
#include "util_vector.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
class CacheBuffer {
|
|
|
|
public:
|
|
|
|
const void *data;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
CacheBuffer(const void *data_, size_t size_)
|
|
|
|
{ data = data_; size = size_; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class CacheData {
|
|
|
|
public:
|
|
|
|
vector<CacheBuffer> buffers;
|
|
|
|
string name;
|
2012-01-16 13:13:37 +00:00
|
|
|
string filename;
|
|
|
|
bool have_filename;
|
2011-04-27 11:58:34 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
CacheData(const string& name = "");
|
|
|
|
~CacheData();
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
const string& get_filename();
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
template<typename T> void add(const vector<T>& data)
|
|
|
|
{
|
2012-01-16 13:13:37 +00:00
|
|
|
CacheBuffer buffer(data.size()? &data[0]: NULL, data.size()*sizeof(T));
|
|
|
|
buffers.push_back(buffer);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> void add(const array<T>& data)
|
|
|
|
{
|
2012-01-16 13:13:37 +00:00
|
|
|
CacheBuffer buffer(data.size()? &data[0]: NULL, data.size()*sizeof(T));
|
|
|
|
buffers.push_back(buffer);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 11:02:51 +00:00
|
|
|
void add(const void *data, size_t size)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-10-03 15:31:45 +00:00
|
|
|
if(size) {
|
|
|
|
CacheBuffer buffer(data, size);
|
|
|
|
buffers.push_back(buffer);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 11:02:51 +00:00
|
|
|
void add(const int& data)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
CacheBuffer buffer(&data, sizeof(int));
|
|
|
|
buffers.push_back(buffer);
|
|
|
|
}
|
|
|
|
|
2013-02-13 11:02:51 +00:00
|
|
|
void add(const float& data)
|
2012-01-16 13:13:37 +00:00
|
|
|
{
|
|
|
|
CacheBuffer buffer(&data, sizeof(float));
|
|
|
|
buffers.push_back(buffer);
|
|
|
|
}
|
|
|
|
|
2013-02-13 11:02:51 +00:00
|
|
|
void add(const size_t& data)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
CacheBuffer buffer(&data, sizeof(size_t));
|
|
|
|
buffers.push_back(buffer);
|
|
|
|
}
|
|
|
|
|
2014-09-01 12:03:03 +00:00
|
|
|
template<typename T> bool read(array<T>& data)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if(!fread(&size, sizeof(size), 1, f)) {
|
|
|
|
fprintf(stderr, "Failed to read vector size from cache.\n");
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 15:31:45 +00:00
|
|
|
if(!size)
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
2011-10-03 15:31:45 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
data.resize(size/sizeof(T));
|
|
|
|
|
|
|
|
if(!fread(&data[0], size, 1, f)) {
|
2011-12-02 14:26:28 +00:00
|
|
|
fprintf(stderr, "Failed to read vector data from cache (%lu).\n", (unsigned long)size);
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2014-09-01 12:03:03 +00:00
|
|
|
return true;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 12:03:03 +00:00
|
|
|
bool read(int& data)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-01-16 13:13:37 +00:00
|
|
|
size_t size;
|
|
|
|
|
2014-09-01 12:03:03 +00:00
|
|
|
if(!fread(&size, sizeof(size), 1, f)) {
|
2012-01-16 13:13:37 +00:00
|
|
|
fprintf(stderr, "Failed to read int size from cache.\n");
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!fread(&data, sizeof(data), 1, f)) {
|
2011-04-27 11:58:34 +00:00
|
|
|
fprintf(stderr, "Failed to read int from cache.\n");
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 12:03:03 +00:00
|
|
|
bool read(float& data)
|
2012-01-16 13:13:37 +00:00
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
2014-09-01 12:03:03 +00:00
|
|
|
if(!fread(&size, sizeof(size), 1, f)) {
|
2012-01-16 13:13:37 +00:00
|
|
|
fprintf(stderr, "Failed to read float size from cache.\n");
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!fread(&data, sizeof(data), 1, f)) {
|
2012-01-16 13:13:37 +00:00
|
|
|
fprintf(stderr, "Failed to read float from cache.\n");
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-01-16 13:13:37 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 12:03:03 +00:00
|
|
|
bool read(size_t& data)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-01-16 13:13:37 +00:00
|
|
|
size_t size;
|
|
|
|
|
2014-09-01 12:03:03 +00:00
|
|
|
if(!fread(&size, sizeof(size), 1, f)) {
|
2012-01-16 13:13:37 +00:00
|
|
|
fprintf(stderr, "Failed to read size_t size from cache.\n");
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!fread(&data, sizeof(data), 1, f)) {
|
2011-04-27 11:58:34 +00:00
|
|
|
fprintf(stderr, "Failed to read size_t from cache.\n");
|
2014-09-01 12:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Cache {
|
|
|
|
public:
|
|
|
|
static Cache global;
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
void insert(CacheData& key, CacheData& value);
|
|
|
|
bool lookup(CacheData& key, CacheData& value);
|
|
|
|
|
|
|
|
void clear_except(const string& name, const set<string>& except);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
protected:
|
2012-01-16 13:13:37 +00:00
|
|
|
string data_filename(CacheData& key);
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __UTIL_CACHE_H__ */
|
|
|
|
|