| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: track.c,v 1.4 1999/05/19 20:27:11 mdw Exp $ |
| 4 | * |
| 5 | * Tracing functions for debugging |
| 6 | * |
| 7 | * (c) 1998 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the mLib utilities library. |
| 13 | * |
| 14 | * mLib is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * mLib is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with mLib; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: track.c,v $ |
| 33 | * Revision 1.4 1999/05/19 20:27:11 mdw |
| 34 | * Change naming to match newer mLib conventions. |
| 35 | * |
| 36 | * Revision 1.3 1999/05/06 19:51:36 mdw |
| 37 | * Reformatted the LGPL notice a little bit. |
| 38 | * |
| 39 | * Revision 1.2 1999/05/05 18:50:31 mdw |
| 40 | * Change licensing conditions to LGPL. |
| 41 | * |
| 42 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
| 43 | * Initial version of mLib |
| 44 | * |
| 45 | */ |
| 46 | |
| 47 | /*----- Header files ------------------------------------------------------*/ |
| 48 | |
| 49 | /* --- ANSI headers --- */ |
| 50 | |
| 51 | #include <ctype.h> |
| 52 | #include <stdarg.h> |
| 53 | #include <stdio.h> |
| 54 | #include <stdlib.h> |
| 55 | #include <string.h> |
| 56 | |
| 57 | /* --- Local headers --- */ |
| 58 | |
| 59 | #include "trace.h" |
| 60 | #include "track.h" |
| 61 | |
| 62 | /*----- Type definitions --------------------------------------------------*/ |
| 63 | |
| 64 | /* --- A track block --- * |
| 65 | * |
| 66 | * This gets prefixed to every block I manage. |
| 67 | */ |
| 68 | |
| 69 | typedef union block { |
| 70 | struct { |
| 71 | union block *next; /* Link to previous block */ |
| 72 | union block *prev; /* Link to next block */ |
| 73 | size_t sz; /* Size of the block */ |
| 74 | const char *ctx; /* Pointer to allocating context */ |
| 75 | } x; /* Main data area */ |
| 76 | long double _ld; /* Long double for alignment */ |
| 77 | void *_p; /* Void pointer for alignment */ |
| 78 | } block; |
| 79 | |
| 80 | /*----- Private state -----------------------------------------------------*/ |
| 81 | |
| 82 | /* --- Tracking memory usage --- */ |
| 83 | |
| 84 | static unsigned int used = 0; /* Count of bytes occupied */ |
| 85 | static block *list; /* List of allocated blocks */ |
| 86 | |
| 87 | /* --- Trace level for verbose messages --- */ |
| 88 | |
| 89 | static unsigned int vLevel = 0; |
| 90 | |
| 91 | /* --- Context tracking --- */ |
| 92 | |
| 93 | static track_ctx baseContext = { |
| 94 | 0, "[unknown context]" |
| 95 | }; |
| 96 | |
| 97 | static track_ctx *context = &baseContext; |
| 98 | |
| 99 | /*----- Functions provided ------------------------------------------------*/ |
| 100 | |
| 101 | /* --- @track_setLevel@ --- * |
| 102 | * |
| 103 | * Arguments: @unsigned int l@ = tracing level for allocation messages |
| 104 | * |
| 105 | * Returns: --- |
| 106 | * |
| 107 | * Use: Sets the trace level for allocation messages. |
| 108 | */ |
| 109 | |
| 110 | void track_setLevel(unsigned int l) |
| 111 | { |
| 112 | vLevel = l; |
| 113 | } |
| 114 | |
| 115 | /* --- @track_pushContext@ --- * |
| 116 | * |
| 117 | * Arguments: @track_ctx *ctx@ = context holder to push |
| 118 | * |
| 119 | * Returns: --- |
| 120 | * |
| 121 | * Use: Pushes the given context block onto the stack. |
| 122 | */ |
| 123 | |
| 124 | void track_pushContext(track_ctx *ctx) |
| 125 | { |
| 126 | ctx->next = context; |
| 127 | context = ctx; |
| 128 | } |
| 129 | |
| 130 | /* --- @track_popContext@ --- * |
| 131 | * |
| 132 | * Arguments: @track_ctx *ctx@ = context holder to pop |
| 133 | * |
| 134 | * Returns: --- |
| 135 | * |
| 136 | * Use: Removes the given context block from the stack. |
| 137 | */ |
| 138 | |
| 139 | void track_popContext(track_ctx *ctx) |
| 140 | { |
| 141 | context = ctx->next; |
| 142 | } |
| 143 | |
| 144 | /* --- @track_malloc@ --- * |
| 145 | * |
| 146 | * Arguments: @size_t sz@ = size requested |
| 147 | * |
| 148 | * Returns: Pointer to allocated space, or null |
| 149 | * |
| 150 | * Use: Allocates memory, and tracks how much is allocated. |
| 151 | */ |
| 152 | |
| 153 | void *track_malloc(size_t sz) |
| 154 | { |
| 155 | block *q = (malloc)(sz + sizeof(block)); |
| 156 | if (q) { |
| 157 | used += sz; |
| 158 | if (vLevel) { |
| 159 | trace(vLevel, "(track) allocated %lu at %p in %s", |
| 160 | (unsigned long)sz, (void *)(q + 1), context->s); |
| 161 | } |
| 162 | q->x.sz = sz; |
| 163 | q->x.next = list; |
| 164 | q->x.prev = 0; |
| 165 | q->x.ctx = context->s; |
| 166 | if (q->x.next) |
| 167 | q->x.next->x.prev = q; |
| 168 | list = q; |
| 169 | return (q + 1); |
| 170 | } |
| 171 | return (0); |
| 172 | } |
| 173 | |
| 174 | /* --- @track_free@ --- * |
| 175 | * |
| 176 | * Arguments: @void *p@ = pointer to an allocated block |
| 177 | * |
| 178 | * Returns: --- |
| 179 | * |
| 180 | * Use: Frees memory, and tracks how much is still allocated. |
| 181 | */ |
| 182 | |
| 183 | void track_free(void *p) |
| 184 | { |
| 185 | block *q; |
| 186 | |
| 187 | if (!p) |
| 188 | return; |
| 189 | q = (block *)p - 1; |
| 190 | if (vLevel) { |
| 191 | trace(vLevel, "(track) freed %lu at %p for %s in %s", |
| 192 | (unsigned long)q->x.sz, (void *)(q + 1), |
| 193 | q->x.ctx, context->s); |
| 194 | } |
| 195 | if (q->x.next) |
| 196 | q->x.next->x.prev = q->x.prev; |
| 197 | if (q->x.prev) |
| 198 | q->x.prev->x.next = q->x.next; |
| 199 | else |
| 200 | list = q->x.next; |
| 201 | used -= q->x.sz; |
| 202 | (free)(q); |
| 203 | } |
| 204 | |
| 205 | /* --- @track_realloc@ --- * |
| 206 | * |
| 207 | * Arguments: @void *p@ = pointer to an allocated block |
| 208 | * @size_t sz@ = how big it wants to be |
| 209 | * |
| 210 | * Returns: Pointer to the new block. |
| 211 | * |
| 212 | * Use: Reallocates a block, tracking how much memory is still |
| 213 | * available. |
| 214 | */ |
| 215 | |
| 216 | void *track_realloc(void *p, size_t sz) |
| 217 | { |
| 218 | size_t osz; |
| 219 | block *q, *qq; |
| 220 | if (p) { |
| 221 | q = (block *)p - 1; |
| 222 | osz = q->x.sz; |
| 223 | if (q->x.next) |
| 224 | q->x.next->x.prev = q->x.prev; |
| 225 | if (q->x.prev) |
| 226 | q->x.prev->x.next = q->x.next; |
| 227 | else |
| 228 | list = q->x.next; |
| 229 | } else { |
| 230 | q = 0; |
| 231 | osz = 0; |
| 232 | } |
| 233 | qq = (realloc)(q, sz + sizeof(block)); |
| 234 | if (qq) { |
| 235 | if (vLevel) { |
| 236 | trace(vLevel, |
| 237 | "(track) reallocated %lu at %p to %lu for %s in %s", |
| 238 | (unsigned long)osz, (void *)(q + 1), |
| 239 | (unsigned long)sz, (void *)(qq + 1), |
| 240 | qq->x.ctx, context->s); |
| 241 | } |
| 242 | qq->x.sz = sz; |
| 243 | qq->x.next = list; |
| 244 | qq->x.prev = 0; |
| 245 | if (qq->x.next) |
| 246 | qq->x.next->x.prev = qq; |
| 247 | list = qq; |
| 248 | used += sz - osz; |
| 249 | qq->x.sz = sz; |
| 250 | return (qq + 1); |
| 251 | } |
| 252 | return (0); |
| 253 | } |
| 254 | |
| 255 | /* --- @track_used@ --- * |
| 256 | * |
| 257 | * Arguments: --- |
| 258 | * |
| 259 | * Returns: A count of how much memory is used currently. |
| 260 | * |
| 261 | * Use: Returns the amount of memory which the @track_@-functions |
| 262 | * above have counted as being currently allocated. |
| 263 | */ |
| 264 | |
| 265 | unsigned long track_used(void) |
| 266 | { |
| 267 | return (used); |
| 268 | } |
| 269 | |
| 270 | /* --- @track_list@ --- * |
| 271 | * |
| 272 | * Arguments: @unsigned int l@ = trace level to use |
| 273 | * |
| 274 | * Returns: --- |
| 275 | * |
| 276 | * Use: Traces a dump of the currently known blocks. Combined with |
| 277 | * a verbose dump of allocations and deallocations, and a |
| 278 | * good idea of which blocks were allocated where, this can |
| 279 | * be useful for locating memory leaks. It's not exactly a |
| 280 | * picnic, though. |
| 281 | */ |
| 282 | |
| 283 | void track_list(unsigned int l) |
| 284 | { |
| 285 | block *q = list; |
| 286 | |
| 287 | if (!(tracing() & l)) |
| 288 | return; |
| 289 | |
| 290 | trace(l, "(track dump) Dumping all blocks. Stand well back..."); |
| 291 | while (q) { |
| 292 | trace(l, "(track dump) %p: %lu in %s", |
| 293 | (void *)(q + 1), (unsigned long)q->x.sz, q->x.ctx); |
| 294 | q = q->x.next; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /*----- That's all, folks -------------------------------------------------*/ |