xref: /titanic_51/usr/src/lib/libast/common/cdt/dtopen.c (revision 20c794b39650d115e17a15983b6b82e46238cf45)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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 #include	"dthdr.h"
23 static char*     Version = "\n@(#)$Id: cdt (AT&T Research) 2005-04-20 $\0\n";
24 
25 /* 	Make a new dictionary
26 **
27 **	Written by Kiem-Phong Vo (5/25/96)
28 */
29 
30 #if __STD_C
31 Dt_t* dtopen(Dtdisc_t* disc, Dtmethod_t* meth)
32 #else
33 Dt_t*	dtopen(disc, meth)
34 Dtdisc_t*	disc;
35 Dtmethod_t*	meth;
36 #endif
37 {
38 	Dt_t*		dt = (Dt_t*)Version;	/* shut-up unuse warning */
39 	reg int		e;
40 	Dtdata_t*	data;
41 
42 	if(!disc || !meth)
43 		return NIL(Dt_t*);
44 
45 	/* allocate space for dictionary */
46 	if(!(dt = (Dt_t*) malloc(sizeof(Dt_t))))
47 		return NIL(Dt_t*);
48 
49 	/* initialize all absolutely private data */
50 	dt->searchf = NIL(Dtsearch_f);
51 	dt->meth = NIL(Dtmethod_t*);
52 	dt->disc = NIL(Dtdisc_t*);
53 	dtdisc(dt,disc,0);
54 	dt->type = DT_MALLOC;
55 	dt->nview = 0;
56 	dt->view = dt->walk = NIL(Dt_t*);
57 	dt->user = NIL(Void_t*);
58 
59 	if(disc->eventf)
60 	{	/* if shared/persistent dictionary, get existing data */
61 		data = NIL(Dtdata_t*);
62 		if((e = (*disc->eventf)(dt,DT_OPEN,(Void_t*)(&data),disc)) < 0)
63 			goto err_open;
64 		else if(e > 0)
65 		{	if(data)
66 			{	if(data->type&meth->type)
67 					goto done;
68 				else	goto err_open;
69 			}
70 
71 			if(!disc->memoryf)
72 				goto err_open;
73 
74 			free((Void_t*)dt);
75 			if(!(dt = (*disc->memoryf)(0, 0, sizeof(Dt_t), disc)) )
76 				return NIL(Dt_t*);
77 			dt->searchf = NIL(Dtsearch_f);
78 			dt->meth = NIL(Dtmethod_t*);
79 			dt->disc = NIL(Dtdisc_t*);
80 			dtdisc(dt,disc,0);
81 			dt->type = DT_MEMORYF;
82 			dt->nview = 0;
83 			dt->view = dt->walk = NIL(Dt_t*);
84 		}
85 	}
86 
87 	/* allocate sharable data */
88 	if(!(data = (Dtdata_t*)(dt->memoryf)(dt,NIL(Void_t*),sizeof(Dtdata_t),disc)) )
89 	{ err_open:
90 		free((Void_t*)dt);
91 		return NIL(Dt_t*);
92 	}
93 
94 	data->type = meth->type;
95 	data->here = NIL(Dtlink_t*);
96 	data->htab = NIL(Dtlink_t**);
97 	data->ntab = data->size = data->loop = 0;
98 	data->minp = 0;
99 
100 done:
101 	dt->data = data;
102 	dt->searchf = meth->searchf;
103 	dt->meth = meth;
104 
105 	if(disc->eventf)
106 		(*disc->eventf)(dt, DT_ENDOPEN, (Void_t*)dt, disc);
107 
108 	return dt;
109 }
110