update dev300-m58
[ooovba.git] / dmake / dbug / malloc / mlc_chn.c
bloba25d906b8d7c70a2613bc8547e86d47d32a91ee9
1 /*
2 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
3 * You may copy, distribute, and use this software as long as this
4 * copyright statement is not removed.
5 */
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include "malloc.h"
11 * Function: malloc_chain_check()
13 * Purpose: to verify malloc chain is intact
15 * Arguments: todo - 0 - just check and return status
16 * 1 - call malloc_warn if error detected
18 * Returns: 0 - malloc chain intact & no overflows
19 * other - problems detected in malloc chain
21 * Narrative:
23 * Notes: If todo is non-zero the malloc_warn function, when called
24 * may not return (i.e. it may exit)
27 #ifndef lint
28 static
29 char rcs_hdr[] = "$Id: mlc_chn.c,v 1.1.1.1 2000-09-22 15:33:26 hr Exp $";
30 #endif
33 int
34 malloc_chain_check(todo)
35 int todo;
37 char * func = "malloc_chain_check";
38 int i;
39 extern char * malloc_data_start;
40 extern char * malloc_data_end;
41 extern struct mlist * malloc_end;
42 extern int malloc_errno;
43 extern struct mlist malloc_start;
44 struct mlist * oldptr;
45 struct mlist * ptr;
46 int rtn = 0;
48 oldptr = &malloc_start;
49 for(ptr = malloc_start.next; ; ptr = ptr->next)
52 * Since the malloc chain is a forward only chain, any
53 * pointer that we get should always be positioned in
54 * memory following the previous pointer. If this is not
55 * so, we must have a corrupted chain.
57 if( ptr )
59 if(ptr < oldptr )
61 malloc_errno = M_CODE_CHAIN_BROKE;
62 if( todo )
64 malloc_fatal(func);
66 rtn++;
67 break;
69 oldptr = ptr;
71 else
73 if( oldptr != malloc_end )
76 * This should never happen. If it does, then
77 * we got a real problem.
79 malloc_errno = M_CODE_NO_END;
80 if( todo )
82 malloc_fatal(func);
84 rtn++;
86 break;
90 * verify that ptr is within the malloc region...
91 * since we started within the malloc chain this should never
92 * happen.
95 if( ((char *)ptr < malloc_data_start) ||
96 ((char *)ptr > malloc_data_end) )
98 malloc_errno = M_CODE_BAD_PTR;
99 if( todo )
101 malloc_fatal(func);
103 rtn++;
104 break;
108 * verify magic flag is set
111 if( (ptr->flag&M_MAGIC) != M_MAGIC )
113 malloc_errno = M_CODE_BAD_MAGIC;
114 if( todo )
116 malloc_warning(func);
118 rtn++;
119 continue;
123 * verify segments are correctly linked together
126 if( (ptr->prev && (ptr->prev->next != ptr) ) ||
127 (ptr->next && (ptr->next->prev != ptr) ) ||
128 ((ptr->next == NULL) && (ptr->prev == NULL)) )
130 malloc_errno = M_CODE_BAD_CONNECT;
131 if( todo )
133 malloc_warning(func);
135 rtn++;
136 continue;
140 * If this segment is allocated
143 if( (ptr->flag & M_INUSE) != 0 )
146 * verify no overflow of data area
149 for(i=ptr->r_size; i < ptr->s.size; i++)
151 if( ptr->data[i] != M_FILL )
153 malloc_errno = M_CODE_OVERRUN;
154 if( todo )
156 malloc_warning(func);
158 rtn++;
159 break;
163 else /* it's not allocated so */
166 * verify no reuse of freed data blocks
169 for(i=0; i < ptr->s.size; i++)
171 if( ptr->data[i] != M_FREE_FILL )
173 malloc_errno = M_CODE_REUSE;
174 if( todo )
176 malloc_warning(func);
178 rtn++;
179 break;
184 } /* for(... */
186 return(rtn);
188 } /* malloc_chain_check(... */