xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/include/vmalloc.h (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2012 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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/1994.
28 */
29 
30 #define VMALLOC_VERSION	20110808L
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 	int	n_region;		/* #parallel regions (Vmregion)	*/
56 	int	n_open;			/* #calls that finds open reg	*/
57 	int	n_lock;			/* #calls where reg was locked	*/
58 	int	n_probe;		/* #probes to find a region	*/
59 	int	mode;			/* region mode bits		*/
60 };
61 
62 struct _vmdisc_s
63 {	Vmemory_f	memoryf;	/* memory manipulator		*/
64 	Vmexcept_f	exceptf;	/* exception handler		*/
65 	size_t		round;		/* rounding requirement		*/
66 	size_t		size;		/* actual size of discipline	*/
67 };
68 
69 struct _vmethod_s
70 {	Void_t*		(*allocf)_ARG_((Vmalloc_t*,size_t,int));
71 	Void_t*		(*resizef)_ARG_((Vmalloc_t*,Void_t*,size_t,int,int));
72 	int		(*freef)_ARG_((Vmalloc_t*,Void_t*,int));
73 	long		(*addrf)_ARG_((Vmalloc_t*,Void_t*,int));
74 	long		(*sizef)_ARG_((Vmalloc_t*,Void_t*,int));
75 	int		(*compactf)_ARG_((Vmalloc_t*,int));
76 	Void_t*		(*alignf)_ARG_((Vmalloc_t*,size_t,size_t,int));
77 	unsigned short	meth;
78 };
79 
80 struct _vmalloc_s
81 {	Vmethod_t	meth;		/* method for allocation	*/
82 	char*		file;		/* file name			*/
83 	int		line;		/* line number			*/
84 	char*		func;		/* calling function		*/
85 	Vmdisc_t*	disc;		/* discipline to get space	*/
86 	Vmdata_t*	data;		/* the real region data		*/
87 	Vmalloc_t*	next;		/* linked list of regions	*/
88 };
89 
90 #define VM_TRUST	0000000		/* obsolete			*/
91 #define VM_TRACE	0000001		/* generate traces of calls	*/
92 #define VM_DBCHECK	0000002		/* check for boundary overwrite	*/
93 #define VM_DBABORT	0000004		/* abort on any warning		*/
94 #define VM_SHARE	0000010		/* sharable across processes	*/
95 #define VM_MEMORYF	0000020		/* vm was allocated by memoryf	*/
96 #define VM_FLAGS	0000017		/* user-settable flags		*/
97 
98 #define VM_MTBEST	0000100		/* Vmbest method		*/
99 #define VM_MTPOOL	0000200		/* Vmpool method		*/
100 #define VM_MTLAST	0000400		/* Vmlast method		*/
101 #define VM_MTDEBUG	0001000		/* Vmdebug method		*/
102 #define VM_MTPROFILE	0002000		/* Vmdebug method		*/
103 #define VM_METHODS	0003700		/* available allocation methods	*/
104 
105 #define VM_RSCOPY	0000001		/* copy old contents		*/
106 #define VM_RSMOVE	0000002		/* old contents is moveable	*/
107 #define VM_RSZERO	0000004		/* clear new space		*/
108 
109 /* exception types */
110 #define VM_OPEN		1		/* region being opened		*/
111 #define VM_ENDOPEN	2		/* end of region opening	*/
112 #define VM_CLOSE	3		/* announce being closed	*/
113 #define VM_ENDCLOSE	4		/* end of region closing	*/
114 #define VM_DISC		5		/* discipline being changed	*/
115 #define VM_NOMEM	6		/* can't obtain memory		*/
116 #define VM_BADADDR	(-1)		/* currently a no-op		*/
117 
118 /* for application-specific data in shared/persistent regions */
119 #define VM_MMGET	0		/* get data value (void*)	*/
120 #define VM_MMSET	1		/* set data value (void*)	*/
121 #define VM_MMADD	2		/* add data value (long)	*/
122 
123 _BEGIN_EXTERNS_	 /* public data */
124 #if _BLD_vmalloc && defined(__EXPORT__)
125 #define extern	extern __EXPORT__
126 #endif
127 #if !_BLD_vmalloc && defined(__IMPORT__)
128 #define extern	extern __IMPORT__
129 #endif
130 
131 extern Vmethod_t*	Vmbest;		/* best allocation		*/
132 extern Vmethod_t*	Vmlast;		/* last-block allocation	*/
133 extern Vmethod_t*	Vmpool;		/* pool allocation		*/
134 extern Vmethod_t*	Vmdebug;	/* allocation with debugging	*/
135 extern Vmethod_t*	Vmprofile;	/* profiling memory usage	*/
136 
137 extern Vmdisc_t*	Vmdcsystem;	/* get memory from the OS	*/
138 extern Vmdisc_t*	Vmdcheap;	/* get memory from Vmheap	*/
139 extern Vmdisc_t*	Vmdcsbrk;	/* like Vmdcsystem - legacy use	*/
140 
141 extern Vmalloc_t	_Vmheap;	/* heap region - use with care! */
142 extern Vmalloc_t*	Vmheap;		/* = &_Vmheap - safe to use	*/
143 extern Vmalloc_t*	Vmregion;	/* malloc region		*/
144 
145 #undef extern
146 _END_EXTERNS_
147 
148 _BEGIN_EXTERNS_ /* public functions */
149 #if _BLD_vmalloc && defined(__EXPORT__)
150 #define extern	__EXPORT__
151 #endif
152 
153 extern Vmalloc_t*	vmopen _ARG_(( Vmdisc_t*, Vmethod_t*, int ));
154 extern int		vmclose _ARG_(( Vmalloc_t* ));
155 extern int		vmclear _ARG_(( Vmalloc_t* ));
156 extern int		vmcompact _ARG_(( Vmalloc_t* ));
157 
158 extern Vmdisc_t*	vmdisc _ARG_(( Vmalloc_t*, Vmdisc_t* ));
159 
160 extern Vmalloc_t*	vmmopen _ARG_(( char*, int, ssize_t ));
161 extern Void_t*		vmmvalue _ARG_(( Vmalloc_t*, int, Void_t*, int ));
162 extern void		vmmrelease _ARG_(( Vmalloc_t*, int ));
163 extern Void_t*		vmmaddress _ARG_(( size_t ));
164 
165 extern Void_t*		vmalloc _ARG_(( Vmalloc_t*, size_t ));
166 extern Void_t*		vmalign _ARG_(( Vmalloc_t*, size_t, size_t ));
167 extern Void_t*		vmresize _ARG_(( Vmalloc_t*, Void_t*, size_t, int ));
168 extern Void_t*		vmgetmem _ARG_(( Vmalloc_t*, Void_t*, size_t ));
169 extern int		vmfree _ARG_(( Vmalloc_t*, Void_t* ));
170 
171 extern long		vmaddr _ARG_(( Vmalloc_t*, Void_t* ));
172 extern long		vmsize _ARG_(( Vmalloc_t*, Void_t* ));
173 
174 extern Vmalloc_t*	vmregion _ARG_(( Void_t* ));
175 extern Void_t*		vmsegment _ARG_(( Vmalloc_t*, Void_t* ));
176 extern int		vmset _ARG_(( Vmalloc_t*, int, int ));
177 
178 extern Void_t*		vmdbwatch _ARG_(( Void_t* ));
179 extern int		vmdbcheck _ARG_(( Vmalloc_t* ));
180 extern int		vmdebug _ARG_(( int ));
181 
182 extern int		vmprofile _ARG_(( Vmalloc_t*, int ));
183 
184 extern int		vmtrace _ARG_(( int ));
185 extern int		vmtrbusy _ARG_((Vmalloc_t*));
186 
187 extern int		vmstat _ARG_((Vmalloc_t*, Vmstat_t*));
188 
189 extern int		vmwalk _ARG_((Vmalloc_t*,
190 					int(*)(Vmalloc_t*,Void_t*,size_t,Vmdisc_t*,Void_t*), Void_t*));
191 extern char*		vmstrdup _ARG_((Vmalloc_t*, const char*));
192 
193 #if !defined(_BLD_vmalloc) && !defined(_AST_STD_H) && \
194 	!defined(__stdlib_h) && !defined(__STDLIB_H) && \
195 	!defined(_STDLIB_INCLUDED) && !defined(_INC_STDLIB)
196 extern Void_t*		malloc _ARG_(( size_t ));
197 extern Void_t*		realloc _ARG_(( Void_t*, size_t ));
198 extern void		free _ARG_(( Void_t* ));
199 extern void		cfree _ARG_(( Void_t* ));
200 extern Void_t*		calloc _ARG_(( size_t, size_t ));
201 extern Void_t*		memalign _ARG_(( size_t, size_t ));
202 extern Void_t*		valloc _ARG_(( size_t ));
203 #endif
204 extern int		setregmax _ARG_(( int ));
205 
206 #undef extern
207 _END_EXTERNS_
208 
209 /* to coerce any value to a Vmalloc_t*, make ANSI happy */
210 #define _VM_(vm)	((Vmalloc_t*)(vm))
211 
212 /* enable recording of where a call originates from */
213 #ifdef VMFL
214 
215 #if defined(__FILE__)
216 #define _VMFILE_(vm)	(_VM_(vm)->file = (char*)__FILE__)
217 #else
218 #define _VMFILE_(vm)	(_VM_(vm)->file = (char*)0)
219 #endif
220 
221 #if defined(__LINE__)
222 #define _VMLINE_(vm)	(_VM_(vm)->line = __LINE__)
223 #else
224 #define _VMLINE_(vm)	(_VM_(vm)->line = 0)
225 #endif
226 
227 #if defined(__FUNCTION__)
228 #define _VMFUNC_(vm)	(_VM_(vm)->func = (char*)__FUNCTION__)
229 #else
230 #define _VMFUNC_(vm)	(_VM_(vm)->func = (char*)0)
231 #endif
232 
233 #define _VMFL_(vm)	(_VMFILE_(vm), _VMLINE_(vm), _VMFUNC_(vm))
234 
235 #define vmalloc(vm,sz)		(_VMFL_(vm), \
236 				 (*(_VM_(vm)->meth.allocf))((vm),(sz),0) )
237 #define vmresize(vm,d,sz,type)	(_VMFL_(vm), \
238 				 (*(_VM_(vm)->meth.resizef))\
239 					((vm),(Void_t*)(d),(sz),(type),0) )
240 #define vmfree(vm,d)		(_VMFL_(vm), \
241 				 (*(_VM_(vm)->meth.freef))((vm),(Void_t*)(d),0) )
242 #define vmalign(vm,sz,align)	(_VMFL_(vm), \
243 				 (*(_VM_(vm)->meth.alignf))((vm),(sz),(align),0) )
244 
245 #undef malloc
246 #undef realloc
247 #undef calloc
248 #undef free
249 #undef memalign
250 #undef valloc
251 
252 #if _map_malloc
253 
254 #define malloc(s)		(_VMFL_(Vmregion), _ast_malloc((size_t)(s)) )
255 #define realloc(d,s)		(_VMFL_(Vmregion), _ast_realloc((Void_t*)(d),(size_t)(s)) )
256 #define calloc(n,s)		(_VMFL_(Vmregion), _ast_calloc((size_t)n, (size_t)(s)) )
257 #define free(d)			(_VMFL_(Vmregion), _ast_free((Void_t*)(d)) )
258 #define memalign(a,s)		(_VMFL_(Vmregion), _ast_memalign((size_t)(a),(size_t)(s)) )
259 #define valloc(s)		(_VMFL_(Vmregion), _ast_valloc((size_t)(s) )
260 
261 #else
262 
263 #if !_std_malloc
264 
265 #if __STD_C || defined(__STDPP__) || defined(__GNUC__)
266 
267 #define malloc(s)		(_VMFL_(Vmregion), malloc((size_t)(s)) )
268 #define realloc(d,s)		(_VMFL_(Vmregion), realloc((Void_t*)(d),(size_t)(s)) )
269 #define calloc(n,s)		(_VMFL_(Vmregion), calloc((size_t)n, (size_t)(s)) )
270 #define free(d)			(_VMFL_(Vmregion), free((Void_t*)(d)) )
271 #define memalign(a,s)		(_VMFL_(Vmregion), memalign((size_t)(a),(size_t)(s)) )
272 #define valloc(s)		(_VMFL_(Vmregion), valloc((size_t)(s) )
273 #ifndef strdup
274 #define strdup(s)		( _VMFL_(Vmregion), (strdup)((char*)(s)) )
275 #endif
276 
277 #else
278 
279 #define _VMNM_(a,b,c,d,e,f)	a/**/b/**/c/**/d/**/e/**/f
280 #define malloc(s)		(_VMFL_(Vmregion), _VMNM_(mallo,/,*,*,/,c)\
281 						((size_t)(s)) )
282 #define realloc(d,s)		(_VMFL_(Vmregion), _VMNM_(reallo,/,*,*,/,c)\
283 						((Void_t*)(d),(size_t)(s)) )
284 #define calloc(n,s)		(_VMFL_(Vmregion), _VMNM_(callo,/,*,*,/,c)\
285 						((size_t)n, (size_t)(s)) )
286 #define free(d)			(_VMFL_(Vmregion), _VMNM_(fre,/,*,*,/,e)((Void_t*)(d)) )
287 #define memalign(a,s)		(_VMFL_(Vmregion), _VMNM_(memalig,/,*,*,/,n)\
288 						((size_t)(a),(size_t)(s)) )
289 #define valloc(s)		(_VMFL_(Vmregion), _VMNM_(vallo,/,*,*,/,c)\
290 						((size_t)(s) )
291 #ifndef strdup
292 #define strdup(s)		( _VMFL_(Vmregion), _VMNM_(strdu,/,*,*,/,p)\
293 						((char*)(s)) )
294 #endif
295 
296 #endif /*__STD_C || defined(__STDPP__) || defined(__GNUC__)*/
297 
298 #define cfree(d)		free(d)
299 
300 #endif /*!_std_malloc*/
301 
302 #endif /*_map_malloc*/
303 
304 #endif /*VMFL*/
305 
306 /* non-debugging/profiling allocation calls */
307 #ifndef vmalloc
308 #define vmalloc(vm,sz)		(*(_VM_(vm)->meth.allocf))((vm),(sz),0)
309 #endif
310 
311 #ifndef vmresize
312 #define vmresize(vm,d,sz,type)	(*(_VM_(vm)->meth.resizef))\
313 					((vm),(Void_t*)(d),(sz),(type),0)
314 #endif
315 
316 #ifndef vmfree
317 #define vmfree(vm,d)		(*(_VM_(vm)->meth.freef))((vm),(Void_t*)(d),0)
318 #endif
319 
320 #ifndef vmalign
321 #define vmalign(vm,sz,align)	(*(_VM_(vm)->meth.alignf))((vm),(sz),(align),0)
322 #endif
323 
324 #define vmaddr(vm,addr)		(*(_VM_(vm)->meth.addrf))((vm),(Void_t*)(addr),0)
325 #define vmsize(vm,addr)		(*(_VM_(vm)->meth.sizef))((vm),(Void_t*)(addr),0)
326 #define vmcompact(vm)		(*(_VM_(vm)->meth.compactf))((vm),0)
327 #define vmoldof(v,p,t,n,x)	(t*)vmresize((v), (p), sizeof(t)*(n)+(x), \
328 					(VM_RSMOVE) )
329 #define vmnewof(v,p,t,n,x)	(t*)vmresize((v), (p), sizeof(t)*(n)+(x), \
330 					(VM_RSMOVE|VM_RSCOPY|VM_RSZERO) )
331 
332 #define vmdata(vm)		((Void_t*)(_VM_(vm)->data) )
333 #define vmlocked(vm)		(*((unsigned int*)(_VM_(vm)->data)) )
334 
335 #endif /* _VMALLOC_H */
336