summaryrefslogtreecommitdiff
path: root/cesar/lib/restrack.h
diff options
context:
space:
mode:
Diffstat (limited to 'cesar/lib/restrack.h')
-rw-r--r--cesar/lib/restrack.h137
1 files changed, 137 insertions, 0 deletions
diff --git a/cesar/lib/restrack.h b/cesar/lib/restrack.h
new file mode 100644
index 0000000000..af530583b8
--- /dev/null
+++ b/cesar/lib/restrack.h
@@ -0,0 +1,137 @@
+#ifndef restrack_h
+#define restrack_h
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file lib/restrack.h
+ * \brief Resources tracker.
+ * \ingroup lib
+ *
+ * The resources tracker is a debugging tool to trace resources allocations
+ * and releases.
+ *
+ * It provides services to track resources references with two flavors.
+ *
+ * The first one and simpler one (lite) will record all the places where
+ * allocations and releases are done during the whole resource life time.
+ * This flavor can not determine current resource owner because it can not
+ * associate the source line recorded with the owner.
+ *
+ * The second flavor (full) will request an extra argument for each allocation
+ * and release. This argument is an owner of the resources. This flavor can
+ * track much more errors as it can detect wrong releases and will bark if a
+ * resources is freed while it still own other resources.
+ *
+ * At any time, the user can check for unbalanced resources allocations or
+ * releases.
+ *
+ * Use CONFIG_RESTRACK to enable Resources tracker.
+ *
+ * Use CONFIG_RESTRACK_KEEP to keep records of destructed resources (not safe,
+ * but may be useful for debug in some cases).
+ */
+
+#include "config/restrack.h"
+
+/* Macro helpers. */
+#if CONFIG_RESTRACK
+
+# define _FL const char *function, int line
+# define void_FL _FL
+# define __FL , _FL
+# define _fL __func__, __LINE__
+# define __fL , _fL
+# define _fl function, line
+# define __fl , _fl
+# define _fl_ function, line
+
+#else /* !CONFIG_RESTRACK */
+
+# define _FL
+# define void_FL void
+# define __FL
+# define _fL
+# define __fL
+# define _fl
+# define __fl
+# define _fl_ NULL, 0
+
+#endif /* !CONFIG_RESTRACK */
+
+#if CONFIG_RESTRACK
+
+BEGIN_DECLS
+
+/**
+ * Create a resource reference counter.
+ * \param owner owner of the resource, ignored for the lite flavor
+ * \param resource resource referenced
+ * \param function function name (__func__)
+ * \param line source line
+ * \param initial reference counter initial value
+ */
+void
+restrack_create (void *owner, void *resource, const char *function, int line,
+ int initial);
+
+/**
+ * Update a resource reference counter.
+ * \param owner owner of the resource, ignored for the lite flavor
+ * \param resource resource referenced
+ * \param function function name (__func__)
+ * \param line source line
+ * \param change reference counter change
+ */
+void
+restrack_update (void *owner, void *resource, const char *function, int line,
+ int change);
+
+/**
+ * Destroy a resource reference counter.
+ * \param owner owner of the resource, ignored for the lite flavor
+ * \param resource resource referenced
+ * \param function function name (__func__)
+ * \param line source line
+ * \param change reference counter change
+ *
+ * Will check if the counter reaches zero.
+ */
+void
+restrack_destroy (void *owner, void *resource, const char *function, int line,
+ int change);
+
+/**
+ * Check that the current state of resources allocations is balanced.
+ * \return true if no unbalanced resource.
+ *
+ * Will output statistics for any unbalanced resources.
+ */
+bool
+restrack_check (void);
+
+/**
+ * Free all memory used by the Resources Tracker.
+ *
+ * Useful for memory leaks analysis. Called automatically on exit.
+ */
+void
+restrack_uninit (void);
+
+END_DECLS
+
+#else /* !CONFIG_RESTRACK */
+
+# define restrack_create(...) ((void) 0)
+# define restrack_update(...) ((void) 0)
+# define restrack_destroy(...) ((void) 0)
+# define restrack_check() true
+# define restrack_uninit() ((void) 0)
+
+#endif /* !CONFIG_RESTRACK */
+
+#endif /* restrack_h */