/* -*-c-*-
*
- * $Id: utils.h,v 1.1 1997/07/21 13:47:42 mdw Exp $
+ * $Id: utils.h,v 1.3 1997/08/20 16:25:37 mdw Exp $
*
* Miscellaneous useful bits of code.
*
* (c) 1997 Mark Wooding
*/
-/*----- Licencing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of `become'
*
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with `become'; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * along with `become'; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*----- Revision history --------------------------------------------------*
*
* $Log: utils.h,v $
- * Revision 1.1 1997/07/21 13:47:42 mdw
+ * Revision 1.3 1997/08/20 16:25:37 mdw
+ * Add some simple `malloc' tracking.
+ *
+ * Revision 1.2 1997/08/04 10:24:26 mdw
+ * Sources placed under CVS control.
+ *
+ * Revision 1.1 1997/07/21 13:47:42 mdw
* Initial revision
*
*/
(p)[2] = ((unsigned long)(v) >> 16) & 0xFF, \
(p)[3] = ((unsigned long)(v) >> 24) & 0xFF)
+/*----- Other macros ------------------------------------------------------*/
+
+/* --- @burn@ --- *
+ *
+ * Arguments: @obj@ = some object
+ *
+ * Use: Writes zero bytes over the object.
+ */
+
+#define burn(obj) ((void)memset(&obj, 0, sizeof(obj)))
+
/*----- Program name handling ---------------------------------------------*/
/* --- @quis@ --- *
extern void die(const char */*f*/, ...);
+/*----- Trace messages ----------------------------------------------------*/
+
+#if !defined(NDEBUG) && !defined(TRACING)
+# define TRACING
+#endif
+
+#ifdef TRACING
+
+/* --- @trace@ --- *
+ *
+ * Arguments: @unsigned int lvl@ = trace level for output
+ * @const char *f@ = a @printf@-style format string
+ * @...@ = other arguments
+ *
+ * Returns: ---
+ *
+ * Use: Reports a message to the trace output.
+ */
+
+extern void trace(unsigned int /*lvl*/, const char */*f*/, ...);
+
+/* --- @traceblk@ --- *
+ *
+ * Arguments: @unsigned int lvl@ = trace level for output
+ * @const char *hdr@ = some header string to write
+ * @const void *blk@ = pointer to a block of memory to dump
+ * @size_t sz@ = size of the block of memory
+ *
+ * Returns: ---
+ *
+ * Use: Dumps the contents of a block to the trace output.
+ */
+
+extern void traceblk(unsigned int /*lvl*/, const char */*hdr*/,
+ const void */*blk*/, size_t /*sz*/);
+
+/* --- @traceon@ --- *
+ *
+ * Arguments: @FILE *fp@ = a file to trace on
+ * @unsigned int lvl@ = trace level to set
+ *
+ * Returns: ---
+ *
+ * Use: Enables tracing to a file.
+ */
+
+extern void traceon(FILE */*fp*/, unsigned int /*lvl*/);
+
+/* --- @tracesetlvl@ --- *
+ *
+ * Arguments: @unsigned int lvl@ = trace level to set
+ *
+ * Returns: ---
+ *
+ * Use: Sets the tracing level.
+ */
+
+extern void tracesetlvl(unsigned int /*lvl*/);
+
+/* --- @tracing@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: Zero if not tracing, tracing level if tracing.
+ *
+ * Use: Informs the caller whether tracing is enabled.
+ */
+
+extern unsigned int tracing(void);
+
+#endif
+
+/* --- Some hacky macros --- */
+
+#ifdef TRACING
+# define T(x) x
+# define IF_TRACING(lvl, x) if ((lvl) & tracing()) x
+#else
+# define T(x)
+# define IF_TRACING(lvl, x)
+#endif
+
/*----- Memory management functions ---------------------------------------*/
/* --- @xmalloc@ --- *
extern void *xrealloc(void */*p*/, size_t /*sz*/);
+/*----- Simple memory use tracking ----------------------------------------*/
+
+#undef TRACK_MALLOC
+
+#ifdef TRACK_MALLOC
+
+/* --- @track_malloc@ --- *
+ *
+ * Arguments: @size_t sz@ = size requested
+ *
+ * Returns: Pointer to allocated space, or null
+ *
+ * Use: Allocates memory, and tracks how much is allocated.
+ */
+
+extern void *track_malloc(size_t /*sz*/);
+
+/* --- @track_free@ --- *
+ *
+ * Arguments: @void *p@ = pointer to an allocated block
+ *
+ * Returns: ---
+ *
+ * Use: Frees memory, and tracks how much is still allocated.
+ */
+
+extern void track_free(void */*p*/);
+
+/* --- @track_realloc@ --- *
+ *
+ * Arguments: @void *p@ = pointer to an allocated block
+ * @size_t sz@ = how big it wants to be
+ *
+ * Returns: Pointer to the new block.
+ *
+ * Use: Reallocates a block, tracking how much memory is still
+ * available.
+ */
+
+extern void *track_realloc(void */*p*/, size_t /*sz*/);
+
+/* --- @track_memused@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: A count of how much memory is used currently.
+ *
+ * Use: Returns the amount of memory which the @track_@-functions
+ * above have counted as being currently allocated.
+ */
+
+extern unsigned long track_memused(void);
+
+/* --- @track_memlist@ --- *
+ *
+ * Arguments: ---
+ *
+ * Returns: ---
+ *
+ * Use: Dumps a list of allocated blocks to standard output.
+ */
+
+extern void track_memlist(void);
+
+#undef malloc
+#define malloc(sz) track_malloc(sz)
+
+#undef free
+#define free(p) track_free(p)
+
+#undef realloc
+#define realloc(p, sz) track_realloc(p, sz)
+
+#endif
+
/*----- That's all, folks -------------------------------------------------*/
#ifdef __cplusplus