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 
176  virtual const char *className() const { return "RE_CachedExtraData"; }
177 
178  // Called when the parent object is removed from the cache.
179  virtual void cacheFree() {}
180 
181  /// Returns the amount of main memory (NOT graphics memory!)
182  /// owned by this RE_CachedExtraData.
183  /// If inclusive, include sizeof(*this), else only count
184  /// other memory owned by this.
185  virtual int64 getMemoryUsage(bool inclusive) const = 0;
186 
187  void incref() { myRefCount++; }
188  void decref()
189  {
190  myRefCount--;
191  if(myRefCount == 0)
192  delete this;
193  }
194  int refCount() const { return myRefCount; }
195 protected:
196  virtual ~RE_CachedExtraData();
197 private:
198  int myRefCount;
199 };
201 
202 
203 
204 // ------------------------------------------------------------------------
205 /// Basic cached object, with version and extra data only.
207 {
208 public:
210  virtual ~RE_CachedObjectBase();
211 
212  void setVersion(RE_CacheVersion v){ myVersion = v; }
213  RE_CacheVersion getVersion() const { return myVersion; }
214 
215  // Add extra data to be cached along with the object (derive from the class
216  // below). If 'give_own' is true, this will be deleted when the cached
217  // object is, otherwise only 'cacheFree()' will be called.
219  { myExtraData = data; }
220  const RE_CachedExtraDataHandle &getExtraData() const { return myExtraData; }
221 
222 
223  // Cache tags notify other interested parties that we are deleted.
225  { myCacheTagHandle = h; }
227  { myCacheTagHandle = nullptr; }
228 
229  void setInCache(bool c)
230  {
231  myCachedFlag = c;
232  if(!c && myExtraData)
233  myExtraData->cacheFree();
234  }
235  bool isInCache() const { return myCachedFlag; }
236 
237  /// Returns the amount of main memory (not VRAM) owned by this object.
238  virtual int64 getMemoryUsage(bool inclusive) const;
239 
240 public:
244 
246 };
247 
248 
249 
250 // ----------------------------------------------------------------------
251 /// Cached object implementation for RE_TextureCache.
253 {
254 public:
255  RE_CachedObject();
256  ~RE_CachedObject() override;
257 
258  // If this object is being actively used by GL
259  void setInUse(bool inc);
260  virtual bool inUse() { return myUsage > 0; }
261  int getUsage() const { return myUsage; }
262 
263  // How many objects currently point to this object. Decrementing to zero
264  // does not delete the object. Handle object will not delete this object if
265  // it is still referenced by another handle.
266  void incRef() { myRefCount++; }
267  void decRef() { myRefCount--; }
268  int getRefCount() const { return myRefCount; }
269 
270  void setCached(bool cached,const char *mapname);
271  bool isCached() const { return myCachedFlag; }
272 
273  const char *getName() const { return myName; }
274 
275  // Add a removal callback to this object, to be notified when this object
276  // is about to be deleted from the cache.
277  bool setRemovalCallback(bool (*relCB)(void *, void *),
278  void *relObject);
279 
280  bool clearRemovalCallback(void *relObject);
281 
282  /// Returns the amount of main memory (NOT graphics memory!)
283  /// owned by this RE_CachedObject.
284  int64 getMemoryUsage(bool inclusive) const override;
285 
286 private:
287  int myUsage;
288  int myRefCount;
289  UT_DeepString myName;
290 };
291 
292 
294 {
295 public:
296  RE_BBoxData(const UT_BoundingBox &bbox) : myBBox(bbox) {}
297  ~RE_BBoxData() override {}
298 
299  const char *className() const override { return "RE_BBoxData"; }
300 
301  /// Returns the amount of main memory (NOT graphics memory!)
302  /// owned by this RE_BBoxData.
303  int64 getMemoryUsage(bool inclusive) const override
304  {
305  int64 mem = inclusive ? sizeof(*this) : 0;
306  return mem;
307  }
308 
310 };
311 
312 
313 inline void
315 {
316  myUsage += inc ? 1 : -1;
317 #ifdef UT_DEBUG
318  if(myUsage < 0)
319  std::cerr << "Bad object usage " << myUsage << " " << this << "\n";
320  else if(myUsage == 32767)
321  std::cerr << "Apparent overflow " << myUsage << " " << this << "\n";
322 #endif
323  UT_ASSERT_MSG(myUsage >= 0, "Underflow");
324  UT_ASSERT_MSG(myUsage < 32767, "Overflow");
325  if(myUsage < 0)
326  myUsage = 0;
327 }
328 
329 RE_API size_t format(char *buf, size_t bufsize, const RE_CacheVersion &v);
330 
331 static inline void intrusive_ptr_add_ref(RE_CacheTag *i) { i->incref(); }
332 static inline void intrusive_ptr_release(RE_CacheTag *i) { i->decref(); }
333 static inline void intrusive_ptr_add_ref(RE_CachedExtraData *i) { i->incref(); }
334 static inline void intrusive_ptr_release(RE_CachedExtraData *i) { i->decref(); }
335 
336 #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)
GLboolean * data
Definition: glcorearb.h:131
const GLdouble * v
Definition: glcorearb.h:837
UT_BoundingBoxD myBBox
UT_IntrusivePtr< RE_CacheTag > RE_CacheTagHandle
virtual const char * className() const
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
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 setVersion(RE_CacheVersion v)
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:895
int getRefCount() const