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.
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
23 * Notes: If todo is non-zero the malloc_warn function, when called
24 * may not return (i.e. it may exit)
29 char rcs_hdr
[] = "$Id: mlc_chn.c,v 1.1.1.1 2000-09-22 15:33:26 hr Exp $";
34 malloc_chain_check(todo
)
37 char * func
= "malloc_chain_check";
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
;
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.
61 malloc_errno
= M_CODE_CHAIN_BROKE
;
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
;
90 * verify that ptr is within the malloc region...
91 * since we started within the malloc chain this should never
95 if( ((char *)ptr
< malloc_data_start
) ||
96 ((char *)ptr
> malloc_data_end
) )
98 malloc_errno
= M_CODE_BAD_PTR
;
108 * verify magic flag is set
111 if( (ptr
->flag
&M_MAGIC
) != M_MAGIC
)
113 malloc_errno
= M_CODE_BAD_MAGIC
;
116 malloc_warning(func
);
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
;
133 malloc_warning(func
);
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
;
156 malloc_warning(func
);
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
;
176 malloc_warning(func
);
188 } /* malloc_chain_check(... */