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