Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/cdt/tshare.c
1810 views
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 <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "dttest.h"
21
22
static char Space[16*1024*1024];
23
static char *Current = &Space[0];
24
static int Close = 0;
25
static int Free = 0;
26
27
#if __STD_C
28
static int event(Dt_t* dt, int type, Void_t* obj, Dtdisc_t* disc)
29
#else
30
static int event(dt, type, obj, disc)
31
Dt_t* dt;
32
int type;
33
Void_t* obj;
34
Dtdisc_t* disc;
35
#endif
36
{ if(type == DT_OPEN)
37
{ /* opening first dictionary */
38
if(obj)
39
{ if(Current == &Space[0])
40
return 0;
41
else /* opening a dictionary sharing with some previous one */
42
{ *((Void_t**)obj) = (Void_t*)(&Space[0]);
43
return 1;
44
}
45
}
46
else return 0;
47
}
48
else if(type == DT_CLOSE)
49
{ if(Close == 0 ) /* do not free objects */
50
return 1;
51
else return 0;
52
}
53
else return 0;
54
}
55
56
#if __STD_C
57
static Void_t* memory(Dt_t* dt, Void_t* buf, size_t size, Dtdisc_t* disc)
58
#else
59
static Void_t* memory(dt,buf,size,disc)
60
Dt_t* dt;
61
Void_t* buf;
62
size_t size;
63
Dtdisc_t* disc;
64
#endif
65
{
66
if(!buf)
67
{ size = ((size + sizeof(Void_t*)-1)/sizeof(Void_t*))*sizeof(Void_t*);
68
buf = (Void_t*)Current;
69
Current += size;
70
}
71
else
72
{ if(Close > 0)
73
Free += 1;
74
}
75
return buf;
76
}
77
78
Dtdisc_t Disc =
79
{ 0, sizeof(long), -1,
80
newint, NIL(Dtfree_f), compare, hashint,
81
memory, event
82
};
83
84
tmain()
85
{
86
Dt_t *dt1, *dt2;
87
long i, k;
88
89
if(!(dt1 = dtopen(&Disc,Dtoset)) )
90
terror("Opening Dtoset1");
91
if((long)dtinsert(dt1,1L) != 1)
92
terror("Inserting 1");
93
if((long)dtinsert(dt1,3L) != 3)
94
terror("Inserting 3");
95
if((long)dtinsert(dt1,5L) != 5)
96
terror("Inserting 5");
97
98
if(!(dt2 = dtopen(&Disc,Dtoset)) )
99
terror("Opening Dtoset2");
100
if((long)dtinsert(dt2,2L) != 2)
101
terror("Inserting 2");
102
if((long)dtinsert(dt2,4L) != 4)
103
terror("Inserting 4");
104
if((long)dtinsert(dt2,6L) != 6)
105
terror("Inserting 6");
106
107
for(i = 1; i <= 6; ++i)
108
if((long)dtsearch(dt1,i) != i)
109
terror("Didn't find a long");
110
111
for(i = (long)dtlast(dt2), k = 6; i != 0; i = (long)dtprev(dt2,i), k -= 1)
112
if(i != k)
113
terror("Didn't walk a long");
114
115
/* this test makes sure that dtclose() does not free objects */
116
Close = 0;
117
dtclose(dt1);
118
if(Free > 0 )
119
terror("Memory should not have been freed");
120
121
/* this test makes sure that all objects are freed */
122
Close = 1;
123
dtclose(dt2);
124
if(Free <= 0 )
125
terror("Memory should have been freed");
126
127
/* test to make sure that shared dictionaries use the same method */
128
Current = &Space[0];
129
if(!(dt1 = dtopen(&Disc, Dtrhset)) )
130
terror("Opening first dictionary");
131
if((dt2 = dtopen(&Disc, Dtset)) )
132
terror("This open should have failed");
133
134
texit(0);
135
}
136
137