Loading...
Searching...
No Matches
FunctionPtr.h
Go to the documentation of this file.
#ifndef HALIDE_FUNCTION_PTR_H
#define HALIDE_FUNCTION_PTR_H
#include "IntrusivePtr.h"
namespace Halide {
namespace Internal {
/** Functions are allocated in groups for memory management. Each
* group has a ref count associated with it. All within-group
* references must be weak. If there are any references from outside
* the group, at least one must be strong. Within-group references
* may form cycles, but there may not be reference cycles that span
* multiple groups. These rules are not enforced automatically. */
struct FunctionGroup;
/** The opaque struct describing a Halide function. Wrap it in a
* Function object to access it. */
struct FunctionContents;
/** A possibly-weak pointer to a Halide function. Take care to follow
* the rules mentioned above. Preserves weakness/strength on copy.
*
* Note that Function objects are always strong pointers to Halide
* functions.
*/
struct FunctionPtr {
/** A strong and weak pointer to the group. Only one of these
* should be non-zero. */
// @{
IntrusivePtr<FunctionGroup> strong;
FunctionGroup *weak = nullptr;
// @}
/** The index of the function within the group. */
int idx = 0;
/** Get a pointer to the group this Function belongs to. */
FunctionGroup *group() const {
return weak ? weak : strong.get();
}
/** Get the opaque FunctionContents object this pointer refers
* to. Wrap it in a Function to do anything interesting with it. */
// @{
FunctionContents *get() const;
FunctionContents &operator*() const {
return *get();
}
FunctionContents *operator->() const {
return get();
}
// @}
/** Convert from a strong reference to a weak reference. Does
* nothing if the pointer is undefined, or if the reference is
* already weak. */
void weaken() {
weak = group();
strong = nullptr;
}
/** Convert from a weak reference to a strong reference. Does
* nothing if the pointer is undefined, or if the reference is
* already strong. */
void strengthen() {
strong = group();
weak = nullptr;
}
/** Check if the reference is defined. */
bool defined() const {
return weak || strong.defined();
}
/** Check if two FunctionPtrs refer to the same Function. */
bool same_as(const FunctionPtr &other) const {
return idx == other.idx && group() == other.group();
}
/** Pointer comparison, for using FunctionPtrs as keys in maps and
* sets. */
bool operator<(const FunctionPtr &other) const {
return get() < other.get();
}
};
} // namespace Internal
} // namespace Halide
#endif
IntrusivePtr.h
Support classes for reference-counting via intrusive shared pointers.
Halide::Internal
Definition AbstractGenerator.h:24
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::Internal::FunctionPtr
A possibly-weak pointer to a Halide function.
Definition FunctionPtr.h:27
Halide::Internal::FunctionPtr::weak
FunctionGroup * weak
Definition FunctionPtr.h:32
Halide::Internal::FunctionPtr::weaken
void weaken()
Convert from a strong reference to a weak reference.
Definition FunctionPtr.h:60
Halide::Internal::FunctionPtr::get
FunctionContents * get() const
Get the opaque FunctionContents object this pointer refers to.
Halide::Internal::FunctionPtr::strong
IntrusivePtr< FunctionGroup > strong
A strong and weak pointer to the group.
Definition FunctionPtr.h:31
Halide::Internal::FunctionPtr::operator<
bool operator<(const FunctionPtr &other) const
Pointer comparison, for using FunctionPtrs as keys in maps and sets.
Definition FunctionPtr.h:85
Halide::Internal::FunctionPtr::defined
bool defined() const
Check if the reference is defined.
Definition FunctionPtr.h:74
Halide::Internal::FunctionPtr::operator*
FunctionContents & operator*() const
Definition FunctionPtr.h:48
Halide::Internal::FunctionPtr::strengthen
void strengthen()
Convert from a weak reference to a strong reference.
Definition FunctionPtr.h:68
Halide::Internal::FunctionPtr::group
FunctionGroup * group() const
Get a pointer to the group this Function belongs to.
Definition FunctionPtr.h:39
Halide::Internal::FunctionPtr::same_as
bool same_as(const FunctionPtr &other) const
Check if two FunctionPtrs refer to the same Function.
Definition FunctionPtr.h:79
Halide::Internal::FunctionPtr::operator->
FunctionContents * operator->() const
Definition FunctionPtr.h:52
Halide::Internal::FunctionPtr::idx
int idx
The index of the function within the group.
Definition FunctionPtr.h:36
Halide::Internal::IntrusivePtr
Intrusive shared pointers have a reference count (a RefCount object) stored in the class itself.
Definition IntrusivePtr.h:71