OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [malloc/] [heap_debug.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
 * libc/stdlib/malloc/heap_debug.c -- optional heap debugging routines
3
 *
4
 *  Copyright (C) 2002  NEC Corporation
5
 *  Copyright (C) 2002  Miles Bader <miles@gnu.org>
6
 *
7
 * This file is subject to the terms and conditions of the GNU Lesser
8
 * General Public License.  See the file COPYING.LIB in the main
9
 * directory of this archive for more details.
10
 *
11
 * Written by Miles Bader <miles@gnu.org>
12
 */
13
 
14
#include <stdlib.h>
15
#include <stdio.h>
16
#include <stdarg.h>
17
#include <string.h>
18
 
19
#include "malloc.h"
20
#include "heap.h"
21
 
22
 
23
#ifdef HEAP_DEBUGGING
24
int __heap_debug = 0;
25
#endif
26
 
27
 
28
static void
29
__heap_dump_freelist (struct heap *heap)
30
{
31
  struct heap_free_area *fa;
32
  for (fa = heap->free_areas; fa; fa = fa->next)
33
    __malloc_debug_printf (0,
34
                           "0x%lx:  0x%lx - 0x%lx  (%d)\tP=0x%lx, N=0x%lx",
35
                           (long)fa,
36
                           (long)HEAP_FREE_AREA_START (fa),
37
                           (long)HEAP_FREE_AREA_END (fa),
38
                           fa->size,
39
                           (long)fa->prev,
40
                           (long)fa->next);
41
}
42
 
43
/* Output a text representation of HEAP to stderr, labelling it with STR.  */
44
void
45
__heap_dump (struct heap *heap, const char *str)
46
{
47
  static int recursed = 0;
48
 
49
  if (! recursed)
50
    {
51
      __heap_check (heap, str);
52
 
53
      recursed = 1;
54
 
55
      __malloc_debug_printf (1, "%s: heap @0x%lx:", str, (long)heap);
56
      __heap_dump_freelist (heap);
57
      __malloc_debug_indent (-1);
58
 
59
      recursed = 0;
60
    }
61
}
62
 
63
 
64
/* Output an error message to stderr, and exit.  STR is printed with the
65
   failure message.  */
66
static void
67
__heap_check_failure (struct heap *heap, struct heap_free_area *fa,
68
                      const char *str, char *fmt, ...)
69
{
70
  va_list val;
71
 
72
  if (str)
73
    fprintf (stderr, "\nHEAP CHECK FAILURE %s: ", str);
74
  else
75
    fprintf (stderr, "\nHEAP CHECK FAILURE: ");
76
 
77
  va_start (val, fmt);
78
  vfprintf (stderr, fmt, val);
79
  va_end (val);
80
 
81
  putc ('\n', stderr);
82
 
83
  __malloc_debug_set_indent (0);
84
  __malloc_debug_printf (1, "heap dump:");
85
  __heap_dump_freelist (heap);
86
 
87
  exit (22);
88
}
89
 
90
/* Do some consistency checks on HEAP.  If they fail, output an error
91
   message to stderr, and exit.  STR is printed with the failure message.  */
92
void
93
__heap_check (struct heap *heap, const char *str)
94
{
95
  typedef unsigned long ul_t;
96
  struct heap_free_area *fa, *prev;
97
  struct heap_free_area *first_fa = heap->free_areas;
98
 
99
  if (first_fa && first_fa->prev)
100
    __heap_check_failure (heap, first_fa, str,
101
"first free-area has non-zero prev pointer:\n\
102
    first free-area = 0x%lx\n\
103
    (0x%lx)->prev   = 0x%lx\n",
104
                              (ul_t)first_fa,
105
                              (ul_t)first_fa, (ul_t)first_fa->prev);
106
 
107
  for (prev = 0, fa = first_fa; fa; prev = fa, fa = fa->next)
108
    {
109
      if (((ul_t)HEAP_FREE_AREA_END (fa) & (HEAP_GRANULARITY - 1))
110
          || (fa->size & (HEAP_GRANULARITY - 1)))
111
        __heap_check_failure (heap, fa, str, "alignment error:\n\
112
    (0x%lx)->start = 0x%lx\n\
113
    (0x%lx)->size  = 0x%lx\n",
114
                              (ul_t)fa,
115
                              (ul_t)HEAP_FREE_AREA_START (fa),
116
                              (ul_t)fa, fa->size);
117
 
118
      if (fa->prev != prev)
119
        __heap_check_failure (heap, fa, str, "prev pointer corrupted:\n\
120
    (0x%lx)->next = 0x%lx\n\
121
    (0x%lx)->prev = 0x%lx\n",
122
                              (ul_t)prev, (ul_t)prev->next,
123
                              (ul_t)fa, (ul_t)fa->prev);
124
 
125
      if (prev)
126
        {
127
          ul_t start = (ul_t)HEAP_FREE_AREA_START (fa);
128
          ul_t prev_end = (ul_t)HEAP_FREE_AREA_END (prev);
129
 
130
          if (prev_end >= start)
131
            __heap_check_failure (heap, fa, str,
132
                                  "start %s with prev free-area end:\n\
133
    (0x%lx)->prev  = 0x%lx\n\
134
    (0x%lx)->start = 0x%lx\n\
135
    (0x%lx)->end   = 0x%lx\n",
136
                                  (prev_end == start ? "unmerged" : "overlaps"),
137
                                  (ul_t)fa, (ul_t)prev,
138
                                  (ul_t)fa, start,
139
                                  (ul_t)prev, prev_end);
140
        }
141
    }
142
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.