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