+/*
+ * MAX_ZTRACE_DEPTH configures how deep of a stack trace is taken on each zalloc in the zone of interest. 15
+ * levels is usually enough to get past all the layers of code in kalloc and IOKit and see who the actual
+ * caller is up above these lower levels.
+ *
+ * This is used both for the zone leak detector and the zone corruption log.
+ */
+
+#define MAX_ZTRACE_DEPTH 15
+
+/*
+ * Structure for keeping track of a backtrace, used for leak detection.
+ * This is in the .h file because it is used during panic, see kern/debug.c
+ * A non-zero size indicates that the trace is in use.
+ */
+struct ztrace {
+ vm_size_t zt_size; /* How much memory are all the allocations referring to this trace taking up? */
+ uint32_t zt_depth; /* depth of stack (0 to MAX_ZTRACE_DEPTH) */
+ void* zt_stack[MAX_ZTRACE_DEPTH]; /* series of return addresses from OSBacktrace */
+ uint32_t zt_collisions; /* How many times did a different stack land here while it was occupied? */
+ uint32_t zt_hit_count; /* for determining effectiveness of hash function */
+};
+
+#if CONFIG_ZLEAKS
+
+/* support for the kern.zleak.* sysctls */
+
+extern kern_return_t zleak_activate(void);
+extern vm_size_t zleak_max_zonemap_size;
+extern vm_size_t zleak_global_tracking_threshold;
+extern vm_size_t zleak_per_zone_tracking_threshold;
+
+extern int get_zleak_state(void);
+
+#endif /* CONFIG_ZLEAKS */
+
+/* These functions used for leak detection both in zalloc.c and mbuf.c */
+extern uint32_t fastbacktrace(uintptr_t* bt, uint32_t max_frames) __attribute__((noinline));
+extern uintptr_t hash_mix(uintptr_t);
+extern uint32_t hashbacktrace(uintptr_t *, uint32_t, uint32_t);
+extern uint32_t hashaddr(uintptr_t, uint32_t);
+
+#define lock_zone(zone) \
+MACRO_BEGIN \
+ lck_mtx_lock_spin(&(zone)->lock); \
+MACRO_END
+
+#define unlock_zone(zone) \
+MACRO_BEGIN \
+ lck_mtx_unlock(&(zone)->lock); \
+MACRO_END
+
+#if CONFIG_GZALLOC
+void gzalloc_init(vm_size_t);
+void gzalloc_zone_init(zone_t);
+void gzalloc_configure(void);
+void gzalloc_reconfigure(zone_t);
+boolean_t gzalloc_enabled(void);
+
+vm_offset_t gzalloc_alloc(zone_t, boolean_t);
+boolean_t gzalloc_free(zone_t, void *);
+#endif /* CONFIG_GZALLOC */
+