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