1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #ifndef _VMALLOC_H
19 #define _VMALLOC_H	1
20 
21 /*	Public header file for the virtual malloc package.
22 **
23 **	Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
24 */
25 
26 #define VMALLOC_VERSION	19990805L
27 
28 #include "config.h"
29 
30 #ifdef HAVE_SYS_TYPES_H
31 #   include <sys/types.h>
32 #endif // HAVE_SYS_TYPES_H
33 
34     typedef struct _vmalloc_s Vmalloc_t;
35     typedef struct _vmstat_s Vmstat_t;
36     typedef struct _vmdisc_s Vmdisc_t;
37     typedef struct _vmethod_s Vmethod_t;
38     typedef void *(*Vmemory_f)
39 	(Vmalloc_t *, void *, size_t, size_t, Vmdisc_t *);
40     typedef int (*Vmexcept_f)
41 	(Vmalloc_t *, int, void *, Vmdisc_t *);
42 
43     struct _vmstat_s {
44 	int n_busy;		/* number of busy blocks        */
45 	int n_free;		/* number of free blocks        */
46 	size_t s_busy;		/* total amount of busy space   */
47 	size_t s_free;		/* total amount of free space   */
48 	size_t m_busy;		/* largest busy piece           */
49 	size_t m_free;		/* largest free piece           */
50 	int n_seg;		/* number of segments           */
51 	size_t extent;		/* total size of region         */
52     };
53 
54     struct _vmdisc_s {
55 	Vmemory_f memoryf;	/* memory manipulator           */
56 	Vmexcept_f exceptf;	/* exception handler            */
57 	size_t round;		/* rounding requirement         */
58     };
59 
60     struct _vmethod_s {
61 	void *(*allocf) (Vmalloc_t *, size_t);
62 	void *(*resizef) (Vmalloc_t *, void *, size_t, int);
63 	int (*freef) (Vmalloc_t *, void *);
64 	long (*addrf) (Vmalloc_t *, void *);
65 	long (*sizef) (Vmalloc_t *, void *);
66 	int (*compactf) (Vmalloc_t *);
67 	void *(*alignf) (Vmalloc_t *, size_t, size_t);
68 	unsigned short meth;
69     };
70 
71     struct _vmalloc_s {
72 	Vmethod_t meth;		/* method for allocation        */
73 	char *file;		/* file name                    */
74 	int line;		/* line number                  */
75 #ifdef _VM_PRIVATE_
76 	 _VM_PRIVATE_
77 #endif
78     };
79 
80 #define VM_TRUST	0000001	/* forgo some security checks   */
81 #define VM_TRACE	0000002	/* generate trace               */
82 #define VM_DBCHECK	0000004	/* check for boundary overwrite */
83 #define VM_DBABORT	0000010	/* abort on any warning         */
84 #define VM_FLAGS	0000017	/* user-settable flags          */
85 
86 #define VM_MTBEST	0000100	/* Vmbest method                */
87 #define VM_MTPOOL	0000200	/* Vmpool method                */
88 #define VM_MTLAST	0000400	/* Vmlast method                */
89 #define VM_MTDEBUG	0001000	/* Vmdebug method               */
90 #define VM_MTPROFILE	0002000	/* Vmdebug method               */
91 #define VM_METHODS	0003700	/* available allocation methods */
92 
93 #define VM_RSCOPY	0000001	/* copy old contents            */
94 #define VM_RSMOVE	0000002	/* old contents is moveable     */
95 #define VM_RSZERO	0000004	/* clear new space              */
96 
97 /* exception types */
98 #define VM_OPEN		0	/* region being opened          */
99 #define VM_CLOSE	1	/* region being closed          */
100 #define VM_NOMEM	2	/* can't obtain memory          */
101 #define VM_BADADDR	3	/* bad addr in vmfree/vmresize  */
102 #define VM_DISC		4	/* discipline being changed     */
103 
104 
105     extern Vmethod_t *Vmbest;	/* best allocation              */
106     extern Vmethod_t *Vmlast;	/* last-block allocation        */
107     extern Vmethod_t *Vmpool;	/* pool allocation              */
108     extern Vmethod_t *Vmdebug;	/* allocation with debugging    */
109     extern Vmethod_t *Vmprofile;	/* profiling memory usage       */
110 
111     extern Vmdisc_t *Vmdcheap;	/* heap discipline              */
112     extern Vmdisc_t *Vmdcsbrk;	/* sbrk discipline              */
113 
114     extern Vmalloc_t *Vmheap;	/* heap region                  */
115     extern Vmalloc_t *Vmregion;	/* malloc region                */
116 
117     extern Vmalloc_t *vmopen(Vmdisc_t *, Vmethod_t *, int);
118     extern int vmclose(Vmalloc_t *);
119     extern int vmclear(Vmalloc_t *);
120     extern int vmcompact(Vmalloc_t *);
121 
122     extern Vmdisc_t *vmdisc(Vmalloc_t *, Vmdisc_t *);
123 
124     extern void *vmalloc(Vmalloc_t *, size_t);
125     extern void *vmalign(Vmalloc_t *, size_t, size_t);
126     extern void *vmresize(Vmalloc_t *, void *, size_t, int);
127     extern int vmfree(Vmalloc_t *, void *);
128 
129     extern long vmaddr(Vmalloc_t *, void *);
130     extern long vmsize(Vmalloc_t *, void *);
131 
132     extern Vmalloc_t *vmregion(void *);
133     extern void *vmsegment(Vmalloc_t *, void *);
134     extern int vmset(Vmalloc_t *, int, int);
135 
136     extern void *vmdbwatch(void *);
137     extern int vmdbcheck(Vmalloc_t *);
138 
139     extern int vmprofile(Vmalloc_t *, int);
140 
141     extern int vmtrace(int);
142     extern int vmtrbusy(Vmalloc_t *);
143 
144     extern int vmstat(Vmalloc_t *, Vmstat_t *);
145 
146     extern int vmwalk(Vmalloc_t *,
147 			     int (*)(Vmalloc_t *, void *, size_t,
148 				     Vmdisc_t *));
149     extern char *vmstrdup(Vmalloc_t *, const char *);
150 
151 
152 /* to coerce any value to a Vmalloc_t*, make ANSI happy */
153 #define _VM_(vm)	((Vmalloc_t*)(vm))
154 /* enable recording of where a call originates from */
155 #if defined(VMFL) && defined(__FILE__) && defined(__LINE__)
156 #define _VMFL_(vm)		(_VM_(vm)->file = __FILE__, _VM_(vm)->line = __LINE__)
157 #define vmalloc(vm,sz)		(_VMFL_(vm), \
158 				 (*(_VM_(vm)->meth.allocf))((vm),(sz)) )
159 #define vmresize(vm,d,sz,type)	(_VMFL_(vm), \
160 				 (*(_VM_(vm)->meth.resizef))\
161 					((vm),(void*)(d),(sz),(type)) )
162 #define vmfree(vm,d)		(_VMFL_(vm), \
163 				 (*(_VM_(vm)->meth.freef))((vm),(void*)(d)) )
164 #define vmalign(vm,sz,align)	(_VMFL_(vm), \
165 				 (*(_VM_(vm)->meth.alignf))((vm),(sz),(align)) )
166 #define malloc(s)		(_VMFL_(Vmregion), malloc((size_t)(s)) )
167 #define realloc(d,s)		(_VMFL_(Vmregion), realloc((void*)(d),(size_t)(s)) )
168 #define calloc(n,s)		(_VMFL_(Vmregion), calloc((size_t)n, (size_t)(s)) )
169 #define free(d)			(_VMFL_(Vmregion), free((void*)(d)) )
170 #define memalign(a,s)		(_VMFL_(Vmregion), memalign((size_t)(a),(size_t)(s)) )
171 #define valloc(s)		(_VMFL_(Vmregion), valloc((size_t)(s) )
172 #define cfree(d)		free(d)
173 #endif				/*defined(VMFL) && defined(__FILE__) && defined(__LINE__) */
174 /* non-debugging/profiling allocation calls */
175 #ifndef vmalloc
176 #define vmalloc(vm,sz)		(*(_VM_(vm)->meth.allocf))((vm),(sz))
177 #endif
178 #ifndef vmresize
179 #define vmresize(vm,d,sz,type)	(*(_VM_(vm)->meth.resizef))\
180 					((vm),(void*)(d),(sz),(type))
181 #endif
182 #ifndef vmfree
183 #define vmfree(vm,d)		(*(_VM_(vm)->meth.freef))((vm),(void*)(d))
184 #endif
185 #ifndef vmalign
186 #define vmalign(vm,sz,align)	(*(_VM_(vm)->meth.alignf))((vm),(sz),(align))
187 #endif
188 #define vmaddr(vm,addr)		(*(_VM_(vm)->meth.addrf))((vm),(void*)(addr))
189 #define vmsize(vm,addr)		(*(_VM_(vm)->meth.sizef))((vm),(void*)(addr))
190 #define vmcompact(vm)		(*(_VM_(vm)->meth.compactf))((vm))
191 #define vmoldof(v,p,t,n,x)	(t*)vmresize((v), (p), sizeof(t)*(n)+(x), \
192 					(VM_RSMOVE) )
193 #define vmnewof(v,p,t,n,x)	(t*)vmresize((v), (p), sizeof(t)*(n)+(x), \
194 					(VM_RSMOVE|VM_RSCOPY|VM_RSZERO) )
195 #endif				/* _VMALLOC_H */
196 #ifdef __cplusplus
197 }
198 #endif
199