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