Panda3D
bamCache.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file bamCache.h
10  * @author drose
11  * @date 2006-06-09
12  */
13 
14 #ifndef BAMCACHE_H
15 #define BAMCACHE_H
16 
17 #include "pandabase.h"
18 #include "bamCacheRecord.h"
19 #include "pointerTo.h"
20 #include "filename.h"
21 #include "pmap.h"
22 #include "pvector.h"
23 #include "reMutex.h"
24 #include "reMutexHolder.h"
25 
26 #include <time.h>
27 
28 class BamCacheIndex;
29 
30 /**
31  * This class maintains a cache of Bam and/or Txo objects generated from model
32  * files and texture images (as well as possibly other kinds of loadable
33  * objects that can be stored in bam file format).
34  *
35  * This class also maintains a persistent index that lists all of the cached
36  * objects (see BamCacheIndex). We go through some considerable effort to make
37  * sure this index gets saved correctly to disk, even in the presence of
38  * multiple different processes writing to the same index, and without relying
39  * too heavily on low-level os-provided file locks (which work poorly with C++
40  * iostreams).
41  */
42 class EXPCL_PANDA_PUTIL BamCache {
43 PUBLISHED:
44  BamCache();
45  ~BamCache();
46 
47  INLINE void set_active(bool flag);
48  INLINE bool get_active() const;
49 
50  INLINE void set_cache_models(bool flag);
51  INLINE bool get_cache_models() const;
52 
53  INLINE void set_cache_textures(bool flag);
54  INLINE bool get_cache_textures() const;
55 
56  INLINE void set_cache_compressed_textures(bool flag);
57  INLINE bool get_cache_compressed_textures() const;
58 
59  INLINE void set_cache_compiled_shaders(bool flag);
60  INLINE bool get_cache_compiled_shaders() const;
61 
62  void set_root(const Filename &root);
63  INLINE Filename get_root() const;
64 
65  INLINE void set_flush_time(int flush_time);
66  INLINE int get_flush_time() const;
67 
68  INLINE void set_cache_max_kbytes(int max_kbytes);
69  INLINE int get_cache_max_kbytes() const;
70 
71  INLINE void set_read_only(bool ro);
72  INLINE bool get_read_only() const;
73 
74  PT(BamCacheRecord) lookup(const Filename &source_filename,
75  const std::string &cache_extension);
76  bool store(BamCacheRecord *record);
77 
78  void consider_flush_index();
79  void flush_index();
80 
81  void list_index(std::ostream &out, int indent_level = 0) const;
82 
83  INLINE static BamCache *get_global_ptr();
84  INLINE static void consider_flush_global_index();
85  INLINE static void flush_global_index();
86 
87 PUBLISHED:
88  MAKE_PROPERTY(active, get_active, set_active);
89  MAKE_PROPERTY(cache_models, get_cache_models, set_cache_models);
90  MAKE_PROPERTY(cache_textures, get_cache_textures, set_cache_textures);
91  MAKE_PROPERTY(cache_compressed_textures, get_cache_compressed_textures,
92  set_cache_compressed_textures);
93  MAKE_PROPERTY(cache_compiled_shaders, get_cache_compiled_shaders,
94  set_cache_compiled_shaders);
95  MAKE_PROPERTY(root, get_root, set_root);
96  MAKE_PROPERTY(flush_time, get_flush_time, set_flush_time);
97  MAKE_PROPERTY(cache_max_kbytes, get_cache_max_kbytes, set_cache_max_kbytes);
98  MAKE_PROPERTY(read_only, get_read_only, set_read_only);
99 
100 private:
101  void read_index();
102  bool read_index_pathname(Filename &index_pathname,
103  std::string &index_ref_contents) const;
104  void merge_index(BamCacheIndex *new_index);
105  void rebuild_index();
106  INLINE void mark_index_stale();
107 
108  void add_to_index(const BamCacheRecord *record);
109  void remove_from_index(const Filename &source_filename);
110 
111  void check_cache_size();
112 
113  void emergency_read_only();
114 
115  static BamCacheIndex *do_read_index(const Filename &index_pathname);
116  static bool do_write_index(const Filename &index_pathname, const BamCacheIndex *index);
117 
118  PT(BamCacheRecord) find_and_read_record(const Filename &source_pathname,
119  const Filename &cache_filename);
120  PT(BamCacheRecord) read_record(const Filename &source_pathname,
121  const Filename &cache_filename,
122  int pass);
123  static PT(BamCacheRecord) do_read_record(const Filename &cache_pathname,
124  bool read_data);
125 
126  static std::string hash_filename(const std::string &filename);
127  static void make_global();
128 
129  bool _active;
130  bool _cache_models;
131  bool _cache_textures;
132  bool _cache_compressed_textures;
133  bool _cache_compiled_shaders;
134  bool _read_only;
135  Filename _root;
136  int _flush_time;
137  int _max_kbytes;
138  static BamCache *_global_ptr;
139 
140  BamCacheIndex *_index;
141  time_t _index_stale_since;
142 
143  Filename _index_pathname;
144  std::string _index_ref_contents;
145 
146  ReMutex _lock;
147 };
148 
149 #include "bamCache.I"
150 
151 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class maintains a cache of Bam and/or Txo objects generated from model files and texture images ...
Definition: bamCache.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents the in-memory index that records the list of files stored in the BamCache.
Definition: bamCacheIndex.h:33
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A reentrant mutex.
Definition: reMutex.h:32