HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_SharedMemoryManager.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: UT_SharedMemoryManager.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_SharedMemoryManager__
13 #define __UT_SharedMemoryManager__
14 
15 #include "UT_API.h"
16 
17 #include "UT_Map.h"
18 #include "UT_StringMap.h"
19 #include "UT_SpinLock.h"
20 
21 class SYS_SharedMemory;
22 
24 {
25 public:
26  static UT_SharedMemoryManager &get();
27 
28  // Creates a new memory block with the given identifier and size and marks
29  // it as being used. Returns false if the given key already exists
30  // in the shared memory pool or the constructed memory block is invlaid,
31  // true otherwise.
32  bool add(const char *key, const char *id, int64_t size);
33 
34  // Retrieves an existing memory block, or creates an empty one and marks
35  // it as being used.
36  SYS_SharedMemory *get(const char *key);
37 
38  // Returns true if a shared memory block mapping to the given key exists
39  // in the shared memory pool, false otherwise.
40  bool contains(const char *key);
41 
42  // Mark all shared memory blocks with keys matching the pattern as being
43  // unused. If a block is retrieved with get() it gets marked as used. Any
44  // blocks remaining unused after an operation can be erased by calling
45  // erase() with 'only_unused' set to true.
46  void setUnused(const char *pattern);
47 
48  // Destroys all shared memory blocks with keys matching the given pattern.
49  //
50  void erase(const char *pattern, bool only_unused = false);
51 
52  // Exit callback to destroy all known shared memory blocks, so they don't
53  // linger on-disk or in page file after the owning process dies.
54  static void exitCallback(void *);
55 private:
57 
60 
61  // Finds a list of keys matching the pattern. If all keys match the pattern
62  // the array is empty but the function still returns true.
63  bool find(const char *pattern, UT_Array<const char *> &keys);
64 
65  static UT_SpinLock theInstanceLock;
66  static UT_SharedMemoryManager *theInstance;
67 
68  struct ut_AutoSharedMemory
69  {
70  ut_AutoSharedMemory() : mySharedMemory(0), myUsed(false) {}
71  ~ut_AutoSharedMemory();
72 
73  ut_AutoSharedMemory(const ut_AutoSharedMemory &) = delete;
74  ut_AutoSharedMemory &operator=(const ut_AutoSharedMemory &) = delete;
75 
76  SYS_SharedMemory *mySharedMemory;
77  bool myUsed;
78  };
79 
81 
82  ut_SharedMemPool mySharedMemPool;
83 };
84 
85 #endif // __UT_SharedMemoryManager__
#define UT_API
Definition: UT_API.h:14
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
GLushort pattern
Definition: glad.h:2583
GLsizeiptr size
Definition: glcorearb.h:664
LeafData & operator=(const LeafData &)=delete
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
bool OIIO_UTIL_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?