-/*
- *=====================================================================
- * debug_caller_loc -- keep track of all allocation callers
- *
- * char *debug_caller_loc(char *file, int line)
- *
- * entry: file = source file
- * line = source line
- * exit: a string like "genversion.c@999"
- *
- * The debug malloc library has a concept of a call stack that can be used
- * to fine tune what was running when a particular allocation was done.
- * We use it to tell who called our various allocation wrappers since
- * it wouldn't do much good to tell us a problem happened because of
- * the malloc call in alloc (they are all from there at some point).
- *
- * But the library expects the string passed to malloc_enter/malloc_leave
- * to be static, so we build a linked list of each one we get (there are
- * not really that many during a given execution). When we get a repeat
- * we return the previously allocated string. For a bit of performance,
- * we keep the list in least recently used order, which helps because
- * the calls to us come in pairs (one for malloc_enter and one right
- * after for malloc_leave).
- *=====================================================================
- */
-
-char *
-debug_caller_loc(file, line)
- char *file;
- int line;
-{
- struct loc_str {
- char *str;
- struct loc_str *next;
- };
- static struct loc_str *root = NULL;
- struct loc_str *ls, *ls_last;
- int len;
- int size;
- char *p;
- static char *loc = NULL;
- static int loc_size = 0;
-
- if ((p = strrchr(file, '/')) == NULL) {
- p = file; /* keep the whole name */
- } else {
- p++; /* just the last path element */
- }
-
- len = strlen (p);
- size = len + 1 + NUM_STR_SIZE + 1;
- if (size > loc_size) {
- size = ((size + 64 - 1) / 64) * 64; /* might as well get a bunch */
- /*
- * We should free the previous loc area, but we have marked it
- * as a non-leak and the library considers it an error to free
- * such an area, so we just ignore it. We probably grabbed
- * enough the first time that this will not even happen.
- */
- loc = malloc (size);
- if (loc == NULL) {
- return "??"; /* not much better than abort */
- }
- malloc_mark (loc);
- loc_size = size;
- }
-
- strcpy (loc, p);
- ap_snprintf(loc + len, 1 + NUM_STR_SIZE, "@%d", line);
-
- for (ls_last = NULL, ls = root; ls != NULL; ls_last = ls, ls = ls->next) {
- if (strcmp (loc, ls->str) == 0) {
- break;
- }
- }
-
- if (ls == NULL) {
- /*
- * This is a new entry. Put it at the head of the list.
- */
- ls = malloc (sizeof (*ls));
- if (ls == NULL) {
- return "??"; /* not much better than abort */
- }
- malloc_mark (ls);
- size = strlen (loc) + 1;
- ls->str = malloc (size);
- if (ls->str == NULL) {
- free (ls);
- return "??"; /* not much better than abort */
- }
- malloc_mark (ls->str);
- strcpy (ls->str, loc);
- ls->next = root;
- root = ls;
- } else if (ls_last != NULL) {
- /*
- * This is a repeat and was not at the head of the list.
- * Unlink it and move it to the front.
- */
- ls_last->next = ls->next;
- ls->next = root;
- root = ls;
- } else {
- /*
- * This is a repeat but was already at the head of the list,
- * so nothing else needs to be done.
- */
- }
- return ls->str;
-}
-
-/*
- *=====================================================================
- * Save the current source line for vstralloc/newvstralloc.
- *
- * int debug_alloc_push (char *s, int l)
- *
- * entry: s = source file
- * l = source line
- * exit: always zero
- *
- * See the comments in amanda.h about what this is used for.
- *=====================================================================
- */
-
-#define DEBUG_ALLOC_SAVE_MAX 10
-
-static struct {
- char *file;
- int line;
-} debug_alloc_loc_info[DEBUG_ALLOC_SAVE_MAX];
-static int debug_alloc_ptr = 0;
-
-static char *saved_file;
-static int saved_line;
-
-int
-debug_alloc_push (s, l)
- char *s;
- int l;
-{
- debug_alloc_loc_info[debug_alloc_ptr].file = s;
- debug_alloc_loc_info[debug_alloc_ptr].line = l;
- debug_alloc_ptr = (debug_alloc_ptr + 1) % DEBUG_ALLOC_SAVE_MAX;
- return 0;
-}
-
-/*
- *=====================================================================
- * Pop the current source line information for vstralloc/newvstralloc.
- *
- * int debug_alloc_pop (void)
- *
- * entry: none
- * exit: none
- *
- * See the comments in amanda.h about what this is used for.
- *=====================================================================
- */