X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/c846879ccf3e86ea293c157f4aa2ff8716fb5b4c..d4efbcd93c940ad522fcf8c601ec1829d2e0b10d:/track.c diff --git a/track.c b/track.c index 20caeef..f790fc1 100644 --- a/track.c +++ b/track.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: track.c,v 1.2 1999/05/05 18:50:31 mdw Exp $ + * $Id: track.c,v 1.5 2004/04/08 01:36:13 mdw Exp $ * * Tracing functions for debugging * * (c) 1998 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,26 +15,16 @@ * 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. - */ - -/*----- Revision history --------------------------------------------------* - * - * $Log: track.c,v $ - * Revision 1.2 1999/05/05 18:50:31 mdw - * Change licensing conditions to LGPL. - * - * Revision 1.1.1.1 1998/06/17 23:44:42 mdw - * Initial version of mLib * + * 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 ------------------------------------------------------*/ @@ -59,35 +49,35 @@ * This gets prefixed to every block I manage. */ -typedef union track__block { +typedef union block { struct { - union track__block *next; /* Link to previous block */ - union track__block *prev; /* Link to next block */ + 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 */ -} track__block; +} block; /*----- Private state -----------------------------------------------------*/ /* --- Tracking memory usage --- */ -static unsigned int track__used = 0; /* Count of bytes occupied */ -static track__block *track__list; /* List of allocated blocks */ +static unsigned int used = 0; /* Count of bytes occupied */ +static block *list; /* List of allocated blocks */ /* --- Trace level for verbose messages --- */ -static unsigned int track__vLevel = 0; +static unsigned int vLevel = 0; /* --- Context tracking --- */ -static track_ctx track__baseContext = { +static track_ctx baseContext = { 0, "[unknown context]" }; -static track_ctx *track__context = &track__baseContext; +static track_ctx *context = &baseContext; /*----- Functions provided ------------------------------------------------*/ @@ -102,7 +92,7 @@ static track_ctx *track__context = &track__baseContext; void track_setLevel(unsigned int l) { - track__vLevel = l; + vLevel = l; } /* --- @track_pushContext@ --- * @@ -116,8 +106,8 @@ void track_setLevel(unsigned int l) void track_pushContext(track_ctx *ctx) { - ctx->next = track__context; - track__context = ctx; + ctx->next = context; + context = ctx; } /* --- @track_popContext@ --- * @@ -131,7 +121,7 @@ void track_pushContext(track_ctx *ctx) void track_popContext(track_ctx *ctx) { - track__context = ctx->next; + context = ctx->next; } /* --- @track_malloc@ --- * @@ -145,20 +135,20 @@ void track_popContext(track_ctx *ctx) void *track_malloc(size_t sz) { - track__block *q = (malloc)(sz + sizeof(track__block)); + block *q = (malloc)(sz + sizeof(block)); if (q) { - track__used += sz; - if (track__vLevel) { - trace(track__vLevel, "(track) allocated %lu at %p in %s", - (unsigned long)sz, (void *)(q + 1), track__context->s); + 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 = track__list; + q->x.next = list; q->x.prev = 0; - q->x.ctx = track__context->s; + q->x.ctx = context->s; if (q->x.next) q->x.next->x.prev = q; - track__list = q; + list = q; return (q + 1); } return (0); @@ -175,23 +165,23 @@ void *track_malloc(size_t sz) void track_free(void *p) { - track__block *q; + block *q; if (!p) return; - q = (track__block *)p - 1; - if (track__vLevel) { - trace(track__vLevel, "(track) freed %lu at %p for %s in %s", + 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, track__context->s); + 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 - track__list = q->x.next; - track__used -= q->x.sz; + list = q->x.next; + used -= q->x.sz; (free)(q); } @@ -209,36 +199,36 @@ void track_free(void *p) void *track_realloc(void *p, size_t sz) { size_t osz; - track__block *q, *qq; + block *q, *qq; if (p) { - q = (track__block *)p - 1; + 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 - track__list = q->x.next; + list = q->x.next; } else { q = 0; osz = 0; } - qq = (realloc)(q, sz + sizeof(track__block)); + qq = (realloc)(q, sz + sizeof(block)); if (qq) { - if (track__vLevel) { - trace(track__vLevel, + 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, track__context->s); + (unsigned long)sz, (void *)(qq + 1), + qq->x.ctx, context->s); } qq->x.sz = sz; - qq->x.next = track__list; + qq->x.next = list; qq->x.prev = 0; if (qq->x.next) qq->x.next->x.prev = qq; - track__list = qq; - track__used += sz - osz; + list = qq; + used += sz - osz; qq->x.sz = sz; return (qq + 1); } @@ -257,7 +247,7 @@ void *track_realloc(void *p, size_t sz) unsigned long track_used(void) { - return (track__used); + return (used); } /* --- @track_list@ --- * @@ -275,7 +265,7 @@ unsigned long track_used(void) void track_list(unsigned int l) { - track__block *q = track__list; + block *q = list; if (!(tracing() & l)) return;