+++ /dev/null
-/* -*-c-*-
- *
- * Tracing functions for debugging
- *
- * (c) 1998 Straylight/Edgeware
- */
-
-/*----- Licensing notice --------------------------------------------------*
- *
- * This file is part of the mLib utilities library.
- *
- * mLib is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Library General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * mLib is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with mLib; if not, write to the Free
- * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
- * MA 02111-1307, USA.
- */
-
-/*----- Header files ------------------------------------------------------*/
-
-/* --- ANSI headers --- */
-
-#include <ctype.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-/* --- Local headers --- */
-
-#include "trace.h"
-#include "track.h"
-
-/*----- Type definitions --------------------------------------------------*/
-
-/* --- A track block --- *
- *
- * This gets prefixed to every block I manage.
- */
-
-typedef union block {
- struct {
- union block *next; /* Link to previous block */
- union block *prev; /* Link to next block */
- size_t sz; /* Size of the block */
- const char *ctx; /* Pointer to allocating context */
- } x; /* Main data area */
- long double _ld; /* Long double for alignment */
- void *_p; /* Void pointer for alignment */
-} block;
-
-/*----- Private state -----------------------------------------------------*/
-
-/* --- Tracking memory usage --- */
-
-static unsigned int used = 0; /* Count of bytes occupied */
-static block *list; /* List of allocated blocks */
-
-/* --- Trace level for verbose messages --- */
-
-static unsigned int vLevel = 0;
-
-/* --- Context tracking --- */
-
-static track_ctx baseContext = {
- 0, "[unknown context]"
-};
-
-static track_ctx *context = &baseContext;
-
-/*----- Functions provided ------------------------------------------------*/
-
-/* --- @track_setLevel@ --- *
- *
- * Arguments: @unsigned int l@ = tracing level for allocation messages
- *
- * Returns: ---
- *
- * Use: Sets the trace level for allocation messages.
- */
-
-void track_setLevel(unsigned int l)
-{
- vLevel = l;
-}
-
-/* --- @track_pushContext@ --- *
- *
- * Arguments: @track_ctx *ctx@ = context holder to push
- *
- * Returns: ---
- *
- * Use: Pushes the given context block onto the stack.
- */
-
-void track_pushContext(track_ctx *ctx)
-{
- ctx->next = context;
- context = ctx;
-}
-
-/* --- @track_popContext@ --- *
- *
- * Arguments: @track_ctx *ctx@ = context holder to pop
- *
- * Returns: ---
- *
- * Use: Removes the given context block from the stack.
- */
-
-void track_popContext(track_ctx *ctx)
-{
- context = ctx->next;
-}
-
-/* --- @track_malloc@ --- *
- *
- * Arguments: @size_t sz@ = size requested
- *
- * Returns: Pointer to allocated space, or null
- *
- * Use: Allocates memory, and tracks how much is allocated.
- */
-
-void *track_malloc(size_t sz)
-{
- block *q = (malloc)(sz + sizeof(block));
- if (q) {
- used += sz;
- if (vLevel) {
- trace(vLevel, "(track) allocated %lu at %p in %s",
- (unsigned long)sz, (void *)(q + 1), context->s);
- }
- q->x.sz = sz;
- q->x.next = list;
- q->x.prev = 0;
- q->x.ctx = context->s;
- if (q->x.next)
- q->x.next->x.prev = q;
- list = q;
- return (q + 1);
- }
- return (0);
-}
-
-/* --- @track_free@ --- *
- *
- * Arguments: @void *p@ = pointer to an allocated block
- *
- * Returns: ---
- *
- * Use: Frees memory, and tracks how much is still allocated.
- */
-
-void track_free(void *p)
-{
- block *q;
-
- if (!p)
- return;
- q = (block *)p - 1;
- if (vLevel) {
- trace(vLevel, "(track) freed %lu at %p for %s in %s",
- (unsigned long)q->x.sz, (void *)(q + 1),
- q->x.ctx, context->s);
- }
- if (q->x.next)
- q->x.next->x.prev = q->x.prev;
- if (q->x.prev)
- q->x.prev->x.next = q->x.next;
- else
- list = q->x.next;
- used -= q->x.sz;
- (free)(q);
-}
-
-/* --- @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.
- */
-
-void *track_realloc(void *p, size_t sz)
-{
- size_t osz;
- block *q, *qq;
- if (p) {
- q = (block *)p - 1;
- osz = q->x.sz;
- if (q->x.next)
- q->x.next->x.prev = q->x.prev;
- if (q->x.prev)
- q->x.prev->x.next = q->x.next;
- else
- list = q->x.next;
- } else {
- q = 0;
- osz = 0;
- }
- qq = (realloc)(q, sz + sizeof(block));
- if (qq) {
- if (vLevel) {
- trace(vLevel,
- "(track) reallocated %lu at %p to %lu for %s in %s",
- (unsigned long)osz, (void *)(q + 1),
- (unsigned long)sz, (void *)(qq + 1),
- qq->x.ctx, context->s);
- }
- qq->x.sz = sz;
- qq->x.next = list;
- qq->x.prev = 0;
- if (qq->x.next)
- qq->x.next->x.prev = qq;
- list = qq;
- used += sz - osz;
- qq->x.sz = sz;
- return (qq + 1);
- }
- return (0);
-}
-
-/* --- @track_used@ --- *
- *
- * 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.
- */
-
-unsigned long track_used(void)
-{
- return (used);
-}
-
-/* --- @track_list@ --- *
- *
- * Arguments: @unsigned int l@ = trace level to use
- *
- * Returns: ---
- *
- * Use: Traces a dump of the currently known blocks. Combined with
- * a verbose dump of allocations and deallocations, and a
- * good idea of which blocks were allocated where, this can
- * be useful for locating memory leaks. It's not exactly a
- * picnic, though.
- */
-
-void track_list(unsigned int l)
-{
- block *q = list;
-
- if (!(tracing() & l))
- return;
-
- trace(l, "(track dump) Dumping all blocks. Stand well back...");
- while (q) {
- trace(l, "(track dump) %p: %lu in %s",
- (void *)(q + 1), (unsigned long)q->x.sz, q->x.ctx);
- q = q->x.next;
- }
-}
-
-/*----- That's all, folks -------------------------------------------------*/
+++ /dev/null
-/* -*-c-*-
- *
- * Tracing functions for debugging
- *
- * (c) 1998 Straylight/Edgeware
- */
-
-/*----- Licensing notice --------------------------------------------------*
- *
- * This file is part of the mLib utilities library.
- *
- * mLib is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Library General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * mLib is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with mLib; if not, write to the Free
- * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
- * MA 02111-1307, USA.
- */
-
-#ifndef MLIB_TRACK_H
-#define MLIB_TRACK_H
-
-#ifdef __cplusplus
- extern "C" {
-#endif
-
-#include <stdlib.h>
-
-/*----- Options and conventions -------------------------------------------*
- *
- * The following macros affect the tracking system:
- *
- * @TRACK_ENABLE@: Enable tracking of memory allocations
- * @TRACK_BLAME@: Register my context blocks in allocations
- *
- * The reason there are two switches is simple. It's often the case that a
- * library routine allocates memory for its client. Therefore, whether we
- * want to record the library or the client depends on how much we trust
- * the two pieces of software. Setting @TRACK_ENABLE@ and @TRACK_BLAME@
- * suggests that the current source file might leak memory, so we want its
- * context markers in the list. Setting @TRACK_ENABLE@ but not
- * @TRACK_BLAME@ suggests that we trust this code, but not the code which
- * calls it, so we want to preserve the caller's context markers.
- *
- * Got it? Good.
- */
-
-/*----- Type definitions --------------------------------------------------*/
-
-/* --- A context buffer --- */
-
-typedef struct track_ctx {
- struct track_ctx *next;
- const char *s;
-} track_ctx;
-
-/*----- Functions provided ------------------------------------------------*/
-
-/* --- @track_level@ --- *
- *
- * Arguments: @unsigned int l@ = tracing level for allocation messages
- *
- * Returns: ---
- *
- * Use: Sets the trace level for allocation messages.
- */
-
-extern void track_level(unsigned int /*l*/);
-
-/* --- @track_push@ --- *
- *
- * Arguments: @track_ctx *ctx@ = context holder to push
- *
- * Returns: ---
- *
- * Use: Pushes the given context block onto the stack.
- */
-
-extern void track_push(track_ctx */*ctx*/);
-
-/* --- @track_pop@ --- *
- *
- * Arguments: @track_ctx *ctx@ = context holder to pop
- *
- * Returns: ---
- *
- * Use: Removes the given context block from the stack.
- */
-
-extern void track_pop(track_ctx */*ctx*/);
-
-/* --- @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_used@ --- *
- *
- * 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_used(void);
-
-/* --- @track_list@ --- *
- *
- * Arguments: @unsigned int l@ = trace level to use
- *
- * Returns: ---
- *
- * Use: Traces a dump of the currently known blocks. Combined with
- * a verbose dump of allocations and deallocations, and a
- * good idea of which blocks were allocated where, this can
- * be useful for locating memory leaks. It's not exactly a
- * picnic, though.
- */
-
-extern void track_list(unsigned int l);
-
-/*----- Macro wrappers ----------------------------------------------------*/
-
-/* --- If tracking is to be done, set it up --- */
-
-#ifdef TRACK_ENABLE
-# 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
-
-/* --- Provide a context for doing track-related things --- */
-
-#ifdef TRACK_ENABLE
-# define TRACK(x) x
-#else
-# define TRACK(x)
-#endif
-
-/* --- Handle contexts --- */
-
-#if defined(TRACK_ENABLE) && defined(TRACK_BLAME)
-# define TRACK_NCTX(name, string) track_ctx name = { 0, string }
-# define TRACK_NPUSH(name) track_push(name)
-# define TRACK_NPOP(name) track_pop(name)
-# define TRACK_CTX(string) TRACK_NCTX(__track_ctx, string)
-# define TRACK_PUSH TRACK_NPUSH(__track_ctx)
-# define TRACK_POP TRACK_NPOP(__track_ctx)
-#else
-# define TRACK_NCTX(name, string)
-# define TRACK_NPUSH(name) ((void)0)
-# define TRACK_NPOP(name) ((void)0)
-# define TRACK_CTX(string)
-# define TRACK_PUSH ((void)0)
-# define TRACK_POP ((void)0)
-#endif
-
-/*----- That's all, folks -------------------------------------------------*/
-
-#ifdef __cplusplus
- }
-#endif
-
-#endif