N2587: Minimal Garbage Collection Status API
This is a proposal for a supplemental status API for
N2586: "Minimal Support for Garbage Collection and Reachability-Based Leak Detection (revised)." At the request of the
Library Working Group, this API is split into a separate proposal.
The goal of this proposal is to provide an API that a program can use to determine if garbage collection
or leak detection as described in N2586 is enabled. Reasons this is useful include:
- The concern expressed in the Library Working Group that it may be awkward to detect
when garbage collection is enabled on code that relies on behavior that N2586 specifies as undefined in the presence of garbage collection.
- Some programs may wish to behave differently in the presence of garbage collectors or leak detectors. For example,
code that uses the "XOR trick" may choose to call declare_reachable when a leak detector is enabled to avoid
spurious leaks being reported, but avoid the overhead (likely just the overhead of calling an empty library function)
of calling declare_reachable on each list operation during deployment.
- In a similar vein, this can help leak detectors produce more accurate reports for programs
that are not suitable for running in a garbage collected environment. Since programs that are not suitable for deployment with
garbage collection arguably stand to benefit the most from leak detection, this helps make garbage-collection
based technologies benefit important additional classes programs.
- Analogous APIs have proven useful in memory analysis tools, such as Purify's
purify_is_running() API.
Library Wording
namespace std {
enum invalidly_derived_pointer_behavior {
invalid = 0;
valid = 1;
leak_detection = 2;
};
}
invalidly_derived_pointer_behavior invalidly_derived_pointer_behavior_for_default_allocator()
- Returns:
-
valid if all dynamically allocated memory is implicitly declared reachable [Note This
guarantess C++03 behavior];
invalid if memory allocated by the global operator new is
not declared reachable;
leak_detection in implementation-defined circumstances to indicate that
although C++03 behavior is in effect, external tools such as leak detectors are enabled and may produce
more accurate reports if declare_reachable declarations are used.
Observations and Issues
- We will come up with shorter names prior to the upcoming meeting, but that is a bicycle shed. Essentially, invalid means
(minimal) garbage collection may be enabled, valid means garbage collection is not enabled, and leak_detection
means that that garbage collection is not enabled so program behavior is not affected, but garbage detection is.
- The values above are chosen so that leak detection and validity can be considered as bit maps, easing use.
- Since the library allocator is required to use operator new, this applies to the library allocator as well.