HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_CachedObject.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RE_CachedObject.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * Base class for objects that can be cached in the RE_TextureCache and
10  * RE_BufferCache..
11  */
12 #ifndef RE_CachedObject_h
13 #define RE_CachedObject_h
14 
15 #include "RE_API.h"
16 
17 #include <UT/UT_Assert.h>
18 #include <UT/UT_BoundingBox.h>
19 #include <UT/UT_DeepString.h>
20 #include <UT/UT_IntrusivePtr.h>
21 #include <UT/UT_NonCopyable.h>
22 #include <UT/UT_StackTrace.h>
23 #include <SYS/SYS_AtomicInt.h>
24 #include <SYS/SYS_Types.h>
25 
26 #include <iostream>
27 #include <stddef.h>
28 
29 
30 /// Simple class for a mutli-integer cache tag.
32 {
33 public:
35  {
36  myVersion[0] = 0;
37  myVersion[1] = 0;
38  myVersion[2] = 0;
39  myVersion[3] = 0;
40  }
42  {
43  myVersion[0] = cv.myVersion[0];
44  myVersion[1] = cv.myVersion[1];
45  myVersion[2] = cv.myVersion[2];
46  myVersion[3] = cv.myVersion[3];
47  }
48 
50  {
51  myVersion[0] = cv.myVersion[0];
52  myVersion[1] = cv.myVersion[1];
53  myVersion[2] = cv.myVersion[2];
54  myVersion[3] = cv.myVersion[3];
55  return *this;
56  }
57  bool operator==(const RE_CacheVersion &cv) const
58  {
59  return (myVersion[0] == cv.myVersion[0] &&
60  myVersion[1] == cv.myVersion[1] &&
61  myVersion[2] == cv.myVersion[2] &&
62  myVersion[3] == cv.myVersion[3]);
63  }
64  bool operator!=(const RE_CacheVersion &cv) const
65  {
66  return (myVersion[0] != cv.myVersion[0] ||
67  myVersion[1] != cv.myVersion[1] ||
68  myVersion[2] != cv.myVersion[2] ||
69  myVersion[3] != cv.myVersion[3]);
70  }
72  {
73  ++myVersion[0];
74  if(myVersion[0] < 0)
75  {
76  myVersion[0]=0;
77  ++myVersion[1];
78  if(myVersion[1] < 0)
79  {
80  myVersion[1]=0;
81  ++myVersion[2];
82  if(myVersion[2] < 0)
83  {
84  myVersion[2]=0;
85  ++myVersion[3];
86  }
87  }
88  }
89  return *this;
90  }
91 
92  void setElement(int i, int64 v)
93  {
94  UT_ASSERT(i>=0 && i<=3);
95  myVersion[i] = v;
96  }
97  int64 getElement(int i) const
98  {
99  UT_ASSERT(i>=0 && i<=3);
100  return myVersion[i];
101  }
102 
103  void setElementLow32(int i, int32 v)
104  {
105  UT_ASSERT(i>=0 && i<=3);
106  myVersion[i] =
107  int64( (uint64(myVersion[i])&(uint64(0xFFFFFFFF)<<uint64(32)))
108  | uint64(v) );
109  }
110  void setElementHigh32(int i, int32 v)
111  {
112  UT_ASSERT(i>=0 && i<=3);
113  myVersion[i] = int64((uint64(myVersion[i])&uint64(0xFFFFFFFF))
114  | (uint64(v)<<uint64(32)));
115  }
116  void clear()
117  {
118  myVersion[0] = 0;
119  myVersion[1] = 0;
120  myVersion[2] = 0;
121  myVersion[3] = 0;
122  }
123 
124  std::ostream &operator<<(std::ostream &os) const
125  {
126  os << myVersion[0] << "," << myVersion[1] << "," << myVersion[2] << ","
127  << myVersion[3];
128  return os;
129  }
130 
131 private:
132  /// I/O friends
133  // @{
134  friend std::ostream &operator<<(std::ostream &os, const RE_CacheVersion &v)
135  {
136  os << v.myVersion[0] << "," << v.myVersion[1] << ","
137  << v.myVersion[2] << "," << v.myVersion[3];
138  return os;
139  }
140  // @}
141 
142 private:
143  int64 myVersion[4];
144 };
145 
146 // ----------------------------------------------------------------------
147 /// This class is bumped whenever a cached item is evicted.
149 {
150 public:
151  RE_CacheTag() : myVersion(0), myRefCount(0) {}
152 
153  void bump() { myVersion++; }
154  void reset() { myVersion=0; }
155  int64 getVersion() const { return myVersion; }
156 
157  void incref() { myRefCount.add(1); }
158  void decref()
159  {
160  if (!myRefCount.add(-1))
161  delete this;
162  }
163 private:
164  int64 myVersion;
165  SYS_AtomicInt32 myRefCount;
166 };
168 
169 // -------------------------------------------------------------------------
170 // Derive from this class to stash data in a cached object.
172 {
173 public:
175 
177 
178  virtual const char *className() const { return "RE_CachedExtraData"; }
179 
180  // Called when the parent object is removed from the cache.
181  virtual void cacheFree() {}
182 
183  /// Returns the amount of main memory (NOT graphics memory!)
184  /// owned by this RE_CachedExtraData.
185  /// If inclusive, include sizeof(*this), else only count
186  /// other memory owned by this.
187  virtual int64 getMemoryUsage(bool inclusive) const = 0;
188 
189  void incref() { myRefCount++; }
190  void decref()
191  {
192  myRefCount--;
193  if(myRefCount == 0)
194  delete this;
195  }
196  int refCount() const { return myRefCount; }
197 protected:
198  virtual ~RE_CachedExtraData();
199 private:
200  int myRefCount;
201 };
203 
204 
205 
206 // ------------------------------------------------------------------------
207 /// Basic cached object, with version and extra data only.
209 {
210 public:
212  virtual ~RE_CachedObjectBase();
213 
215 
216  void setVersion(RE_CacheVersion v){ myVersion = v; }
217  RE_CacheVersion getVersion() const { return myVersion; }
218 
219  // Add extra data to be cached along with the object (derive from the class
220  // below). If 'give_own' is true, this will be deleted when the cached
221  // object is, otherwise only 'cacheFree()' will be called.
223  { myExtraData = data; }
224  const RE_CachedExtraDataHandle &getExtraData() const { return myExtraData; }
225 
226 
227  // Cache tags notify other interested parties that we are deleted.
229  { myCacheTagHandle = h; }
231  { myCacheTagHandle = nullptr; }
232 
233  void setInCache(bool c)
234  {
235  myCachedFlag = c;
236  if(!c && myExtraData)
237  myExtraData->cacheFree();
238  }
239  bool isInCache() const { return myCachedFlag; }
240 
241  /// Returns the amount of main memory (not VRAM) owned by this object.
242  virtual int64 getMemoryUsage(bool inclusive) const;
243 
244 public:
248 
250 };
251 
252 
253 
254 // ----------------------------------------------------------------------
255 /// Cached object implementation for RE_TextureCache.
257 {
258 public:
259  RE_CachedObject();
260  ~RE_CachedObject() override;
261 
262  // If this object is being actively used by GL
263  void setInUse(bool inc);
264  virtual bool inUse() { return myUsage > 0; }
265  int getUsage() const { return myUsage; }
266 
267  // How many objects currently point to this object. Decrementing to zero
268  // does not delete the object. Handle object will not delete this object if
269  // it is still referenced by another handle.
270  void incRef() { myRefCount++; }
271  void decRef() { myRefCount--; }
272  int getRefCount() const { return myRefCount; }
273 
274  void setCached(bool cached,const char *mapname);
275  bool isCached() const { return myCachedFlag; }
276 
277  const char *getName() const { return myName; }
278 
279  // Add a removal callback to this object, to be notified when this object
280  // is about to be deleted from the cache.
281  bool setRemovalCallback(bool (*relCB)(void *, void *),
282  void *relObject);
283 
284  bool clearRemovalCallback(void *relObject);
285 
286  /// Returns the amount of main memory (NOT graphics memory!)
287  /// owned by this RE_CachedObject.
288  int64 getMemoryUsage(bool inclusive) const override;
289 
290 private:
291  int myUsage;
292  int myRefCount;
293  UT_DeepString myName;
294 };
295 
296 
298 {
299 public:
300  RE_BBoxData(const UT_BoundingBox &bbox) : myBBox(bbox) {}
301  ~RE_BBoxData() override {}
302 
303  const char *className() const override { return "RE_BBoxData"; }
304 
305  /// Returns the amount of main memory (NOT graphics memory!)
306  /// owned by this RE_BBoxData.
307  int64 getMemoryUsage(bool inclusive) const override
308  {
309  int64 mem = inclusive ? sizeof(*this) : 0;
310  return mem;
311  }
312 
314 };
315 
316 
317 inline void
319 {
320  myUsage += inc ? 1 : -1;
321 #ifdef UT_DEBUG
322  if(myUsage < 0)
323  std::cerr << "Bad object usage " << myUsage << " " << this << "\n";
324  else if(myUsage == 32767)
325  std::cerr << "Apparent overflow " << myUsage << " " << this << "\n";
326 #endif
327  UT_ASSERT_MSG(myUsage >= 0, "Underflow");
328  UT_ASSERT_MSG(myUsage < 32767, "Overflow");
329  if(myUsage < 0)
330  myUsage = 0;
331 }
332 
333 RE_API size_t format(char *buf, size_t bufsize, const RE_CacheVersion &v);
334 
335 static inline void intrusive_ptr_add_ref(RE_CacheTag *i) { i->incref(); }
336 static inline void intrusive_ptr_release(RE_CacheTag *i) { i->decref(); }
337 static inline void intrusive_ptr_add_ref(RE_CachedExtraData *i) { i->incref(); }
338 static inline void intrusive_ptr_release(RE_CachedExtraData *i) { i->decref(); }
339 
340 #endif
bool operator==(const RE_CacheVersion &cv) const
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
bool operator!=(const RE_CacheVersion &cv) const
void setElementLow32(int i, int32 v)
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1818
int int32
Definition: SYS_Types.h:39
std::ostream & operator<<(std::ostream &os) const
#define RE_API
Definition: RE_API.h:10
void setInCache(bool c)
void
Definition: png.h:1083
GLboolean * data
Definition: glcorearb.h:131
const GLdouble * v
Definition: glcorearb.h:837
UT_BoundingBoxD myBBox
UT_IntrusivePtr< RE_CacheTag > RE_CacheTagHandle
int getUsage() const
unsigned long long uint64
Definition: SYS_Types.h:117
RE_API size_t format(char *buf, size_t bufsize, const RE_CacheVersion &v)
RE_CacheVersion getVersion() const
int refCount() const
int64 getElement(int i) const
void setExtraData(RE_CachedExtraData *data)
RE_CacheVersion & operator=(const RE_CacheVersion &cv)
int64 getVersion() const
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:159
RE_CacheTagHandle myCacheTagHandle
void setElementHigh32(int i, int32 v)
void setInUse(bool inc)
int64 getMemoryUsage(bool inclusive) const override
void intrusive_ptr_release(T *x)
Definition: refcnt.h:217
RE_BBoxData(const UT_BoundingBox &bbox)
RE_CacheVersion myVersion
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
OPENVDB_API void setVersion(std::ios_base &, const VersionId &libraryVersion, uint32_t fileVersion)
Associate specific file format and library version numbers with the given stream. ...
RE_CacheVersion(const RE_CacheVersion &cv)
long long int64
Definition: SYS_Types.h:116
void setElement(int i, int64 v)
const char * className() const override
friend std::ostream & operator<<(std::ostream &os, const RE_CacheVersion &v)
I/O friends.
const RE_CachedExtraDataHandle & getExtraData() const
void setCacheTag(RE_CacheTagHandle h)
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
Cached object implementation for RE_TextureCache.
void intrusive_ptr_add_ref(T *x)
Definition: refcnt.h:208
virtual bool inUse()
Basic cached object, with version and extra data only.
UT_IntrusivePtr< RE_CachedExtraData > RE_CachedExtraDataHandle
bool isInCache() const
bool isCached() const
Simple class for a mutli-integer cache tag.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
~RE_BBoxData() override
const char * getName() const
virtual int64 getMemoryUsage(bool inclusive) const
Returns the amount of main memory (not VRAM) owned by this object.
RE_CachedExtraDataHandle myExtraData
RE_CacheVersion & operator++()
virtual void cacheFree()
This class is bumped whenever a cached item is evicted.
Definition: format.h:1821
int getRefCount() const