xref: /onnv-gate/usr/src/lib/libast/common/disc/memfatal.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin 
24*4887Schin /*
25*4887Schin  * install error message handler for fatal malloc exceptions
26*4887Schin  */
27*4887Schin 
28*4887Schin #include <ast.h>
29*4887Schin #include <error.h>
30*4887Schin #include <vmalloc.h>
31*4887Schin 
32*4887Schin #include "FEATURE/vmalloc"
33*4887Schin 
34*4887Schin #if _std_malloc
35*4887Schin 
36*4887Schin void
37*4887Schin memfatal(void)
38*4887Schin {
39*4887Schin }
40*4887Schin 
41*4887Schin #else
42*4887Schin 
43*4887Schin /*
44*4887Schin  * print message and fail on VM_BADADDR,VM_NOMEM
45*4887Schin  */
46*4887Schin 
47*4887Schin static int
48*4887Schin nomalloc(Vmalloc_t* region, int type, void* obj, Vmdisc_t* disc)
49*4887Schin {
50*4887Schin 	Vmstat_t	st;
51*4887Schin 
52*4887Schin 	NoP(disc);
53*4887Schin 	switch (type)
54*4887Schin 	{
55*4887Schin 	case VM_BADADDR:
56*4887Schin 		error(ERROR_SYSTEM|3, "invalid pointer %p passed to free or realloc", obj);
57*4887Schin 		return(-1);
58*4887Schin 	case VM_NOMEM:
59*4887Schin 		vmstat(region, &st);
60*4887Schin 		error(ERROR_SYSTEM|3, "storage allocator out of space on %lu byte request ( region %lu segments %lu busy %lu:%lu:%lu free %lu:%lu:%lu )", (size_t)obj, st.extent, st.n_seg, st.n_busy, st.s_busy, st.m_busy, st.n_free, st.s_free, st.m_free);
61*4887Schin 		return(-1);
62*4887Schin 	}
63*4887Schin 	return(0);
64*4887Schin }
65*4887Schin 
66*4887Schin /*
67*4887Schin  * initialize the malloc exception handler
68*4887Schin  */
69*4887Schin 
70*4887Schin void
71*4887Schin memfatal(void)
72*4887Schin {
73*4887Schin 	Vmdisc_t*	disc;
74*4887Schin 
75*4887Schin 	if (disc = vmdisc(Vmheap, NiL))
76*4887Schin 		disc->exceptf = nomalloc;
77*4887Schin }
78*4887Schin 
79*4887Schin #endif
80