xref: /titanic_41/usr/src/lib/libast/common/include/vmalloc.h (revision 489b7c4ab76ae8df137fbfcc2214f7baa52883a0)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #ifndef _VMALLOC_H
23 #define _VMALLOC_H	1
24 
25 /*	Public header file for the virtual malloc package.
26 **
27 **	Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
28 */
29 
30 #define VMALLOC_VERSION	20070911L
31 
32 #if _PACKAGE_ast
33 #include	<ast_std.h>
34 #else
35 #include	<ast_common.h>
36 #endif
37 
38 typedef struct _vmalloc_s	Vmalloc_t;
39 typedef struct _vmstat_s	Vmstat_t;
40 typedef struct _vmdisc_s	Vmdisc_t;
41 typedef struct _vmethod_s	Vmethod_t;
42 typedef Void_t*	(*Vmemory_f)_ARG_((Vmalloc_t*, Void_t*, size_t, size_t, Vmdisc_t*));
43 typedef int	(*Vmexcept_f)_ARG_((Vmalloc_t*, int, Void_t*, Vmdisc_t*));
44 
45 struct _vmstat_s
46 {	int	n_busy;			/* number of busy blocks	*/
47 	int	n_free;			/* number of free blocks	*/
48 	size_t	s_busy;			/* total amount of busy space	*/
49 	size_t	s_free;			/* total amount of free space	*/
50 	size_t	m_busy;			/* largest busy piece		*/
51 	size_t	m_free;			/* largest free piece		*/
52 	int	n_seg;			/* number of segments		*/
53 	size_t	extent;			/* total size of region		*/
54 };
55 
56 struct _vmdisc_s
57 {	Vmemory_f	memoryf;	/* memory manipulator		*/
58 	Vmexcept_f	exceptf;	/* exception handler		*/
59 	size_t		round;		/* rounding requirement		*/
60 };
61 
62 struct _vmethod_s
63 {	Void_t*		(*allocf)_ARG_((Vmalloc_t*,size_t));
64 	Void_t*		(*resizef)_ARG_((Vmalloc_t*,Void_t*,size_t,int));
65 	int		(*freef)_ARG_((Vmalloc_t*,Void_t*));
66 	long		(*addrf)_ARG_((Vmalloc_t*,Void_t*));
67 	long		(*sizef)_ARG_((Vmalloc_t*,Void_t*));
68 	int		(*compactf)_ARG_((Vmalloc_t*));
69 	Void_t*		(*alignf)_ARG_((Vmalloc_t*,size_t,size_t));
70 	unsigned short	meth;
71 };
72 
73 struct _vmalloc_s
74 {	Vmethod_t	meth;		/* method for allocation	*/
75 	char*		file;		/* file name			*/
76 	int		line;		/* line number			*/
77 	Void_t*		func;		/* calling function		*/
78 #ifdef _VM_PRIVATE_
79 	_VM_PRIVATE_
80 #endif
81 };
82 
83 #undef	VM_FLAGS			/* solaris sys kernel clash	*/
84 
85 #define VM_TRUST	0000001		/* forgo some security checks	*/
86 #define VM_TRACE	0000002		/* generate trace 		*/
87 #define VM_DBCHECK	0000004		/* check for boundary overwrite	*/
88 #define VM_DBABORT	0000010		/* abort on any warning		*/
89 #define VM_FLAGS	0000017		/* user-settable flags		*/
90 
91 #define VM_MTBEST	0000100		/* Vmbest method		*/
92 #define VM_MTPOOL	0000200		/* Vmpool method		*/
93 #define VM_MTLAST	0000400		/* Vmlast method		*/
94 #define VM_MTDEBUG	0001000		/* Vmdebug method		*/
95 #define VM_MTPROFILE	0002000		/* Vmdebug method		*/
96 #define VM_METHODS	0003700		/* available allocation methods	*/
97 
98 #define VM_RSCOPY	0000001		/* copy old contents		*/
99 #define VM_RSMOVE	0000002		/* old contents is moveable	*/
100 #define VM_RSZERO	0000004		/* clear new space		*/
101 
102 /* exception types */
103 #define VM_OPEN		0		/* region being opened		*/
104 #define VM_CLOSE	1		/* announce being closed	*/
105 #define VM_NOMEM	2		/* can't obtain memory		*/
106 #define VM_BADADDR	3		/* bad addr in vmfree/vmresize	*/
107 #define VM_DISC		4		/* discipline being changed	*/
108 #define VM_ALLOC	5		/* announcement from vmalloc()	*/
109 #define VM_FREE		6		/* announcement from vmfree()	*/
110 #define VM_RESIZE	7		/* announcement from vmresize()	*/
111 
112 _BEGIN_EXTERNS_	 /* public data */
113 #if _BLD_vmalloc && defined(__EXPORT__)
114 #define extern		extern __EXPORT__
115 #endif
116 #if !_BLD_vmalloc && defined(__IMPORT__)
117 #define extern		extern __IMPORT__
118 #endif
119 
120 extern Vmethod_t*	Vmbest;		/* best allocation		*/
121 extern Vmethod_t*	Vmlast;		/* last-block allocation	*/
122 extern Vmethod_t*	Vmpool;		/* pool allocation		*/
123 extern Vmethod_t*	Vmdebug;	/* allocation with debugging	*/
124 extern Vmethod_t*	Vmprofile;	/* profiling memory usage	*/
125 
126 extern Vmdisc_t*	Vmdcheap;	/* heap discipline		*/
127 extern Vmdisc_t*	Vmdcsbrk;	/* sbrk discipline		*/
128 
129 extern Vmalloc_t*	Vmheap;		/* heap region			*/
130 extern Vmalloc_t*	Vmregion;	/* malloc region		*/
131 
132 #undef extern
133 _END_EXTERNS_
134 
135 _BEGIN_EXTERNS_ /* public functions */
136 #if _BLD_vmalloc && defined(__EXPORT__)
137 #define extern	__EXPORT__
138 #endif
139 
140 extern Vmalloc_t*	vmopen _ARG_(( Vmdisc_t*, Vmethod_t*, int ));
141 extern int		vmclose _ARG_(( Vmalloc_t* ));
142 extern int		vmclear _ARG_(( Vmalloc_t* ));
143 extern int		vmcompact _ARG_(( Vmalloc_t* ));
144 
145 extern Vmdisc_t*	vmdisc _ARG_(( Vmalloc_t*, Vmdisc_t* ));
146 
147 extern Vmalloc_t*	vmmopen _ARG_(( char*, Void_t*, size_t ));
148 extern Void_t*		vmmset _ARG_((Vmalloc_t*, int, Void_t*, int));
149 
150 extern Void_t*		vmalloc _ARG_(( Vmalloc_t*, size_t ));
151 extern Void_t*		vmalign _ARG_(( Vmalloc_t*, size_t, size_t ));
152 extern Void_t*		vmresize _ARG_(( Vmalloc_t*, Void_t*, size_t, int ));
153 extern Void_t*		vmgetmem _ARG_(( Vmalloc_t*, Void_t*, size_t ));
154 extern int		vmfree _ARG_(( Vmalloc_t*, Void_t* ));
155 
156 extern long		vmaddr _ARG_(( Vmalloc_t*, Void_t* ));
157 extern long		vmsize _ARG_(( Vmalloc_t*, Void_t* ));
158 
159 extern Vmalloc_t*	vmregion _ARG_(( Void_t* ));
160 extern Void_t*		vmsegment _ARG_(( Vmalloc_t*, Void_t* ));
161 extern int		vmset _ARG_(( Vmalloc_t*, int, int ));
162 
163 extern Void_t*		vmdbwatch _ARG_(( Void_t* ));
164 extern int		vmdbcheck _ARG_(( Vmalloc_t* ));
165 extern int		vmdebug _ARG_(( int ));
166 
167 extern int		vmprofile _ARG_(( Vmalloc_t*, int ));
168 
169 extern int		vmtrace _ARG_(( int ));
170 extern int		vmtrbusy _ARG_((Vmalloc_t*));
171 
172 extern int		vmstat _ARG_((Vmalloc_t*, Vmstat_t*));
173 
174 extern int		vmwalk _ARG_((Vmalloc_t*,
175 					int(*)(Vmalloc_t*,Void_t*,size_t,Vmdisc_t*)));
176 extern char*		vmstrdup _ARG_((Vmalloc_t*, const char*));
177 
178 #if !defined(_BLD_vmalloc) && !defined(_AST_STD_H) && \
179 	!defined(__stdlib_h) && !defined(__STDLIB_H) && \
180 	!defined(_STDLIB_INCLUDED) && !defined(_INC_STDLIB)
181 extern Void_t*		malloc _ARG_(( size_t ));
182 extern Void_t*		realloc _ARG_(( Void_t*, size_t ));
183 extern void		free _ARG_(( Void_t* ));
184 extern void		cfree _ARG_(( Void_t* ));
185 extern Void_t*		calloc _ARG_(( size_t, size_t ));
186 extern Void_t*		memalign _ARG_(( size_t, size_t ));
187 extern Void_t*		valloc _ARG_(( size_t ));
188 #endif
189 
190 #undef extern
191 _END_EXTERNS_
192 
193 /* to coerce any value to a Vmalloc_t*, make ANSI happy */
194 #define _VM_(vm)	((Vmalloc_t*)(vm))
195 
196 /* enable recording of where a call originates from */
197 #ifdef VMFL
198 
199 #if defined(__FILE__)
200 #define _VMFILE_(vm)	(_VM_(vm)->file = (char*)__FILE__)
201 #else
202 #define _VMFILE_(vm)	(_VM_(vm)->file = 0)
203 #endif
204 
205 #if defined(__LINE__)
206 #define _VMLINE_(vm)	(_VM_(vm)->line = __LINE__)
207 #else
208 #define _VMLINE_(vm)	(_VM_(vm)->line = 0)
209 #endif
210 
211 #if defined(__FUNCTION__)
212 #define _VMFUNC_(vm)	(_VM_(vm)->func = (Void_t*)__FUNCTION__)
213 #else
214 #define _VMFUNC_(vm)	(_VM_(vm)->func = 0)
215 #endif
216 
217 #define _VMFL_(vm)	(_VMFILE_(vm), _VMLINE_(vm), _VMFUNC_(vm))
218 
219 #define vmalloc(vm,sz)		(_VMFL_(vm), \
220 				 (*(_VM_(vm)->meth.allocf))((vm),(sz)) )
221 #define vmresize(vm,d,sz,type)	(_VMFL_(vm), \
222 				 (*(_VM_(vm)->meth.resizef))\
223 					((vm),(Void_t*)(d),(sz),(type)) )
224 #define vmfree(vm,d)		(_VMFL_(vm), \
225 				 (*(_VM_(vm)->meth.freef))((vm),(Void_t*)(d)) )
226 #define vmalign(vm,sz,align)	(_VMFL_(vm), \
227 				 (*(_VM_(vm)->meth.alignf))((vm),(sz),(align)) )
228 
229 #undef malloc
230 #undef realloc
231 #undef calloc
232 #undef free
233 #undef memalign
234 #undef valloc
235 
236 #if _map_malloc
237 
238 #define malloc(s)		(_VMFL_(Vmregion), _ast_malloc((size_t)(s)) )
239 #define realloc(d,s)		(_VMFL_(Vmregion), _ast_realloc((Void_t*)(d),(size_t)(s)) )
240 #define calloc(n,s)		(_VMFL_(Vmregion), _ast_calloc((size_t)n, (size_t)(s)) )
241 #define free(d)			(_VMFL_(Vmregion), _ast_free((Void_t*)(d)) )
242 #define memalign(a,s)		(_VMFL_(Vmregion), _ast_memalign((size_t)(a),(size_t)(s)) )
243 #define valloc(s)		(_VMFL_(Vmregion), _ast_valloc((size_t)(s) )
244 
245 #else
246 
247 #if !_std_malloc
248 
249 #if __STD_C || defined(__STDPP__) || defined(__GNUC__)
250 #define malloc(s)		( _VMFL_(Vmregion), (malloc)((size_t)(s)) )
251 #define realloc(d,s)		( _VMFL_(Vmregion), (realloc)((Void_t*)(d),(size_t)(s)) )
252 #define calloc(n,s)		( _VMFL_(Vmregion), (calloc)((size_t)n, (size_t)(s)) )
253 #define free(d)			( _VMFL_(Vmregion), (free)((Void_t*)(d)) )
254 #define memalign(a,s)		( _VMFL_(Vmregion), (memalign)((size_t)(a),(size_t)(s)) )
255 #define valloc(s)		( _VMFL_(Vmregion), (valloc)((size_t)(s)) )
256 #ifndef strdup
257 #define strdup(s)		( _VMFL_(Vmregion), (strdup)((char*)(s)) )
258 #endif
259 
260 #else
261 
262 #define _VMNM_(a,b,c,d,e,f)	a/**/b/**/c/**/d/**/e/**/f
263 #define malloc(s)		( _VMFL_(Vmregion), _VMNM_(mallo,/,*,*,/,c)\
264 						( (size_t)(s)) )
265 #define realloc(d,s)		( _VMFL_(Vmregion), _VMNM_(reallo,/,*,*,/,c)\
266 						( (Void_t*)(d),(size_t)(s)) )
267 #define calloc(n,s)		( _VMFL_(Vmregion), _VMNM_(callo,/,*,*,/,c)\
268 						( (size_t)n, (size_t)(s)) )
269 #define free(d)			( _VMFL_(Vmregion), _VMNM_(fre,/,*,*,/,e)((Void_t*)(d)) )
270 #define memalign(a,s)		( _VMFL_(Vmregion), _VMNM_(memalig,/,*,*,/,n)\
271 						( (size_t)(a),(size_t)(s)) )
272 #define valloc(s)		( _VMFL_(Vmregion), _VMNM_(vallo,/,*,*,/,c)\
273 						( (size_t)(s)) )
274 #ifndef strdup
275 #define strdup(s)		( _VMFL_(Vmregion), _VMNM_(strdu,/,*,*,/,p)\
276 						((char*)(s)) )
277 #endif
278 
279 #endif /*__STD_C || defined(__STDPP__) || defined(__GNUC__)*/
280 
281 #define cfree(d)		free(d)
282 
283 #endif /* !_std_malloc */
284 
285 #endif /* _map_malloc */
286 
287 #endif /*VMFL*/
288 
289 /* non-debugging/profiling allocation calls */
290 #ifndef vmalloc
291 #define vmalloc(vm,sz)		(*(_VM_(vm)->meth.allocf))((vm),(sz))
292 #endif
293 
294 #ifndef vmresize
295 #define vmresize(vm,d,sz,type)	(*(_VM_(vm)->meth.resizef))\
296 					((vm),(Void_t*)(d),(sz),(type))
297 #endif
298 
299 #ifndef vmfree
300 #define vmfree(vm,d)		(*(_VM_(vm)->meth.freef))((vm),(Void_t*)(d))
301 #endif
302 
303 #ifndef vmalign
304 #define vmalign(vm,sz,align)	(*(_VM_(vm)->meth.alignf))((vm),(sz),(align))
305 #endif
306 
307 #define vmaddr(vm,addr)		(*(_VM_(vm)->meth.addrf))((vm),(Void_t*)(addr))
308 #define vmsize(vm,addr)		(*(_VM_(vm)->meth.sizef))((vm),(Void_t*)(addr))
309 #define vmcompact(vm)		(*(_VM_(vm)->meth.compactf))((vm))
310 #define vmoldof(v,p,t,n,x)	(t*)vmresize((v), (p), sizeof(t)*(n)+(x), \
311 					(VM_RSMOVE) )
312 #define vmnewof(v,p,t,n,x)	(t*)vmresize((v), (p), sizeof(t)*(n)+(x), \
313 					(VM_RSMOVE|VM_RSCOPY|VM_RSZERO) )
314 
315 #endif /* _VMALLOC_H */
316