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