Halide  17.0.2
Halide compiler and libraries
ObjectInstanceRegistry.h
Go to the documentation of this file.
1 #ifndef HALIDE_OBJECT_INSTANCE_REGISTRY_H
2 #define HALIDE_OBJECT_INSTANCE_REGISTRY_H
3 
4 /** \file
5  *
6  * Provides a single global registry of Generators, GeneratorParams,
7  * and Params indexed by this pointer. This is used for finding the
8  * parameters inside of a Generator.
9  */
10 
11 #include <cstddef>
12 #include <cstdint>
13 #include <map>
14 #include <mutex>
15 #include <vector>
16 
17 namespace Halide {
18 namespace Internal {
19 
21 public:
22  enum Kind {
29  };
30 
31  /** Add an instance to the registry. The size may be 0 for Param Kinds,
32  * but not for Generator. subject_ptr is the value actually associated
33  * with this instance; it is usually (but not necessarily) the same
34  * as this_ptr. Assert if this_ptr is already registered.
35  *
36  * If 'this' is directly heap allocated (not a member of a
37  * heap-allocated object) and you want the introspection subsystem
38  * to know about it and its members, set the introspection_helper
39  * argument to a pointer to a global variable with the same true
40  * type as 'this'. For example:
41  *
42  * MyObject *obj = new MyObject;
43  * static MyObject *introspection_helper = nullptr;
44  * register_instance(obj, sizeof(MyObject), kind, obj, &introspection_helper);
45  *
46  * I.e. introspection_helper should be a pointer to a pointer to
47  * an object instance. The inner pointer can be null. The
48  * introspection subsystem will then assume this new object is of
49  * the matching type, which will help its members deduce their
50  * names on construction.
51  */
52  static void register_instance(void *this_ptr, size_t size, Kind kind, void *subject_ptr,
53  const void *introspection_helper);
54 
55  /** Remove an instance from the registry. Assert if not found.
56  */
57  static void unregister_instance(void *this_ptr);
58 
59  /** Returns the list of subject pointers for objects that have
60  * been directly registered within the given range. If there is
61  * another containing object inside the range, instances within
62  * that object are skipped.
63  */
64  static std::vector<std::pair<void *, Kind>> instances_in_range(void *start, size_t size);
65 
66 private:
67  static ObjectInstanceRegistry &get_registry();
68 
69  struct InstanceInfo {
70  void *subject_ptr = nullptr; // May be different from the this_ptr in the key
71  size_t size = 0; // May be 0 for params
72  Kind kind = Invalid;
73  bool registered_for_introspection = false;
74 
75  InstanceInfo() = default;
76  InstanceInfo(size_t size, Kind kind, void *subject_ptr, bool registered_for_introspection)
77  : subject_ptr(subject_ptr), size(size), kind(kind), registered_for_introspection(registered_for_introspection) {
78  }
79  };
80 
81  std::mutex mutex;
82  std::map<uintptr_t, InstanceInfo> instances;
83 
84  ObjectInstanceRegistry() = default;
85 
86 public:
91 };
92 
93 } // namespace Internal
94 } // namespace Halide
95 
96 #endif // HALIDE_OBJECT_INSTANCE_REGISTRY_H
static std::vector< std::pair< void *, Kind > > instances_in_range(void *start, size_t size)
Returns the list of subject pointers for objects that have been directly registered within the given ...
ObjectInstanceRegistry & operator=(ObjectInstanceRegistry &&)=delete
ObjectInstanceRegistry(const ObjectInstanceRegistry &)=delete
ObjectInstanceRegistry & operator=(const ObjectInstanceRegistry &)=delete
static void unregister_instance(void *this_ptr)
Remove an instance from the registry.
static void register_instance(void *this_ptr, size_t size, Kind kind, void *subject_ptr, const void *introspection_helper)
Add an instance to the registry.
ObjectInstanceRegistry(ObjectInstanceRegistry &&)=delete
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.