xref: /onnv-gate/usr/src/lib/libast/common/cdt/dtopen.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #include	"dthdr.h"
234887Schin static char*     Version = "\n@(#)$Id: cdt (AT&T Research) 2005-04-20 $\0\n";
244887Schin 
254887Schin /* 	Make a new dictionary
264887Schin **
274887Schin **	Written by Kiem-Phong Vo (5/25/96)
284887Schin */
294887Schin 
304887Schin #if __STD_C
dtopen(Dtdisc_t * disc,Dtmethod_t * meth)314887Schin Dt_t* dtopen(Dtdisc_t* disc, Dtmethod_t* meth)
324887Schin #else
334887Schin Dt_t*	dtopen(disc, meth)
344887Schin Dtdisc_t*	disc;
354887Schin Dtmethod_t*	meth;
364887Schin #endif
374887Schin {
384887Schin 	Dt_t*		dt = (Dt_t*)Version;	/* shut-up unuse warning */
394887Schin 	reg int		e;
404887Schin 	Dtdata_t*	data;
414887Schin 
424887Schin 	if(!disc || !meth)
434887Schin 		return NIL(Dt_t*);
444887Schin 
454887Schin 	/* allocate space for dictionary */
464887Schin 	if(!(dt = (Dt_t*) malloc(sizeof(Dt_t))))
474887Schin 		return NIL(Dt_t*);
484887Schin 
494887Schin 	/* initialize all absolutely private data */
504887Schin 	dt->searchf = NIL(Dtsearch_f);
514887Schin 	dt->meth = NIL(Dtmethod_t*);
524887Schin 	dt->disc = NIL(Dtdisc_t*);
534887Schin 	dtdisc(dt,disc,0);
544887Schin 	dt->type = DT_MALLOC;
554887Schin 	dt->nview = 0;
564887Schin 	dt->view = dt->walk = NIL(Dt_t*);
574887Schin 	dt->user = NIL(Void_t*);
584887Schin 
594887Schin 	if(disc->eventf)
604887Schin 	{	/* if shared/persistent dictionary, get existing data */
614887Schin 		data = NIL(Dtdata_t*);
624887Schin 		if((e = (*disc->eventf)(dt,DT_OPEN,(Void_t*)(&data),disc)) < 0)
634887Schin 			goto err_open;
644887Schin 		else if(e > 0)
654887Schin 		{	if(data)
664887Schin 			{	if(data->type&meth->type)
674887Schin 					goto done;
684887Schin 				else	goto err_open;
694887Schin 			}
704887Schin 
714887Schin 			if(!disc->memoryf)
724887Schin 				goto err_open;
734887Schin 
744887Schin 			free((Void_t*)dt);
754887Schin 			if(!(dt = (*disc->memoryf)(0, 0, sizeof(Dt_t), disc)) )
764887Schin 				return NIL(Dt_t*);
774887Schin 			dt->searchf = NIL(Dtsearch_f);
784887Schin 			dt->meth = NIL(Dtmethod_t*);
794887Schin 			dt->disc = NIL(Dtdisc_t*);
804887Schin 			dtdisc(dt,disc,0);
814887Schin 			dt->type = DT_MEMORYF;
824887Schin 			dt->nview = 0;
834887Schin 			dt->view = dt->walk = NIL(Dt_t*);
844887Schin 		}
854887Schin 	}
864887Schin 
874887Schin 	/* allocate sharable data */
884887Schin 	if(!(data = (Dtdata_t*)(dt->memoryf)(dt,NIL(Void_t*),sizeof(Dtdata_t),disc)) )
894887Schin 	{ err_open:
904887Schin 		free((Void_t*)dt);
914887Schin 		return NIL(Dt_t*);
924887Schin 	}
934887Schin 
944887Schin 	data->type = meth->type;
954887Schin 	data->here = NIL(Dtlink_t*);
964887Schin 	data->htab = NIL(Dtlink_t**);
974887Schin 	data->ntab = data->size = data->loop = 0;
984887Schin 	data->minp = 0;
994887Schin 
1004887Schin done:
1014887Schin 	dt->data = data;
1024887Schin 	dt->searchf = meth->searchf;
1034887Schin 	dt->meth = meth;
1044887Schin 
1054887Schin 	if(disc->eventf)
1064887Schin 		(*disc->eventf)(dt, DT_ENDOPEN, (Void_t*)dt, disc);
1074887Schin 
1084887Schin 	return dt;
1094887Schin }
110