summaryrefslogtreecommitdiff
path: root/utils/dx_proxy/filememcache.h
blob: 36d5bf7003ba0b7cc3c4111565b5e7b49bedffc6 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

#ifndef FILEMEMCACHE_H
#define FILEMEMCACHE_H
#ifdef _WIN32
#pragma once
#endif

#include "tier0/platform.h"
#include "tier1/generichash.h"
#include <unordered_map>

#pragma warning ( disable : 4200 )

class CachedFileData
{
	friend class FileCache;

protected: // Constructed by FileCache
	CachedFileData() {}
	static CachedFileData *Create( char const *szFilename );
	void Free( void );

public:
	static CachedFileData *GetByDataPtr( void const *pvDataPtr );
	
	char const * GetFileName() const;
	void const * GetDataPtr() const;
	int GetDataLen() const;

	int UpdateRefCount( int iDeltaRefCount ) { return m_numRefs += iDeltaRefCount; }

	bool IsValid() const;

protected:
	enum { eHeaderSize = 256 };
	char m_chFilename[256 - 12];
	int m_numRefs;
	int m_numDataBytes;
	int m_signature;
	unsigned char m_data[0]; // file data spans further
};

class FileCache
{
public:
	FileCache();
	~FileCache() { Clear(); }

public:
	CachedFileData *Get( char const *szFilename );
	void Clear( void );

protected:
	struct eqstr {
		inline size_t operator()( const char *s ) const {
			if ( !s )
				return 0;
			return HashString( s );
		}
		inline bool operator()( const char *s1, const char *s2 ) const {
			return _stricmp( s1, s2 ) < 0;
		}
	};

	typedef std::unordered_map< const char *, CachedFileData *, eqstr, eqstr > Mapping;
	Mapping m_map;
};

#endif // #ifndef FILEMEMCACHE_H