1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1999-2011 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 *                                                                      *
19 ***********************************************************************/
20 #include	"vmtest.h"
21 
22 typedef union _myalign_u
23 {	void*		v;
24 	unsigned long	l;
25 	double		d;
26 	unsigned int	i;
27 } Align_t;
28 
29 static Align_t		Algn[16*1024];
30 static Vmuchar_t	*Buf, *Endbuf, *Avail;
31 static int		Count = 0;
32 static int		Walk = 0;
33 
34 #if __STD_C
memory(Vmalloc_t * vm,Void_t * caddr,size_t oldsize,size_t newsize,Vmdisc_t * disc)35 static Void_t*	memory(Vmalloc_t* vm, Void_t* caddr,
36 		size_t oldsize, size_t newsize, Vmdisc_t* disc)
37 #else
38 static Void_t*	memory(vm, caddr, oldsize, newsize, disc)
39 Vmalloc_t*	vm;
40 Void_t*		caddr;
41 size_t		oldsize;
42 size_t		newsize;
43 Vmdisc_t*	disc;
44 #endif
45 {
46 	if(!Avail)
47 	{	Avail = Buf = (Vmuchar_t*)(&Algn[0]);
48 		Endbuf = Buf + sizeof(Algn);
49 	}
50 
51 	if(oldsize)
52 		return NIL(Void_t*);
53 
54 	Count += 1;
55 	caddr = (Void_t*)Avail;
56 	Avail += newsize;
57 	if(Avail >= Endbuf)
58 		terror("No more buffer");
59 
60 	return caddr;
61 }
62 
except(Vmalloc_t * vm,int type,Void_t * data,Vmdisc_t * disc)63 static int except(Vmalloc_t* vm, int type, Void_t* data, Vmdisc_t* disc)
64 {
65 	/* make the eventual handle be a part of our memory */
66 	if(type == VM_OPEN && data)
67 		*((Void_t**)data) = data;
68 	return 0;
69 }
70 
71 static Vmdisc_t	Disc = {memory, except, 64};
72 
73 #if __STD_C
walk(Vmalloc_t * vm,Void_t * addr,size_t size,Vmdisc_t * disc,Void_t * handle)74 static int walk(Vmalloc_t* vm, Void_t* addr, size_t size, Vmdisc_t* disc, Void_t* handle)
75 #else
76 static int walk(vm, addr, size, disc, handle)
77 Vmalloc_t*	vm;
78 Void_t*		addr;
79 size_t		size;
80 Vmdisc_t*	disc;
81 VOid_t*		handle;
82 #endif
83 {
84 	if(disc == &Disc)
85 		Walk += 1;
86 	return 0;
87 }
88 
tmain()89 tmain()
90 {
91 	Void_t		*m1, *m2, *m3, *m4;
92 	Vmalloc_t	*vm1, *vm2, *vm3, *vm4;
93 
94 	if(!(vm1 = vmopen(&Disc,Vmbest,0)) )
95 		terror("Failed to open vm1");
96 	if((Vmuchar_t*)vm1 < Buf || (Vmuchar_t*)vm1 >= Endbuf)
97 		terror("Failed to get vm1 memory to be ours");
98 
99 	if(!(vm2 = vmopen(&Disc,Vmbest,0)) )
100 		terror("Failed to open vm2");
101 	if((Vmuchar_t*)vm2 < Buf || (Vmuchar_t*)vm2 >= Endbuf)
102 		terror("Failed to get vm2 memory to be ours");
103 
104 	if(!(vm3 = vmopen(&Disc,Vmbest,0)) )
105 		terror("Failed to open vm3");
106 	if((Vmuchar_t*)vm3 < Buf || (Vmuchar_t*)vm3 >= Endbuf)
107 		terror("Failed to get vm3 memory to be ours");
108 
109 	Disc.exceptf = NIL(Vmexcept_f);
110 	if(!(vm4 = vmopen(&Disc,Vmbest,0)) )
111 		terror("Failed to open vm4");
112 	if((Vmuchar_t*)vm4 >= Buf && (Vmuchar_t*)vm4 < Endbuf)
113 		terror("vm4 memory should not be ours");
114 
115 	if(!(m1 = vmalloc(vm1,1024)) )
116 		terror("vmalloc failed on m1");
117 	if(!(m2 = vmalloc(vm2,1024)) )
118 		terror("vmalloc failed on m2");
119 	if(!(m3 = vmalloc(vm3,1024)) )
120 		terror("vmalloc failed on m3");
121 	if(!(m4 = vmalloc(vm4,1024)) )
122 		terror("vmalloc failed on m4");
123 
124 	if(!(m1 = vmresize(vm1, m1, 4*1024, VM_RSMOVE|VM_RSCOPY)) )
125 		terror("vmresize failed on m1");
126 	if(!(m2 = vmresize(vm2, m2, 4*1024, VM_RSMOVE|VM_RSCOPY)) )
127 		terror("vmresize failed on m2");
128 	if(!(m3 = vmresize(vm3, m3, 4*1024, VM_RSMOVE|VM_RSCOPY)) )
129 		terror("vmresize failed on m3");
130 	if(!(m4 = vmresize(vm4, m4, 4*1024, VM_RSMOVE|VM_RSCOPY)) )
131 		terror("vmresize failed on m4");
132 
133 	vmwalk(NIL(Vmalloc_t*),walk,NIL(Void_t*));
134 	if(Walk != Count)
135 		terror("Wrong walk count\n");
136 	tinfo("Number of segments: %d", Count);
137 
138 	if(vmfree(vm1,m1) < 0 )
139 		terror("vmfree failed on m1");
140 	if(vmfree(vm2,m2) < 0 )
141 		terror("vmfree failed on m2");
142 	if(vmfree(vm3,m3) < 0 )
143 		terror("vmfree failed on m3");
144 	if(vmfree(vm4,m4) < 0 )
145 		terror("vmfree failed on m4");
146 
147 	texit(0);
148 }
149