xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/cdt/dtdisc.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2012 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 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include	"dthdr.h"
23 
24 /*	Change discipline.
25 **	dt :	dictionary
26 **	disc :	discipline
27 **
28 **	Written by Kiem-Phong Vo (5/26/96)
29 */
30 
31 #if __STD_C
32 static Void_t* dtmemory(Dt_t* dt, Void_t* addr, size_t size, Dtdisc_t* disc)
33 #else
34 static Void_t* dtmemory(dt, addr, size, disc)
35 Dt_t* 		dt;	/* dictionary			*/
36 Void_t* 	addr;	/* address to be manipulate	*/
37 size_t		size;	/* size to obtain		*/
38 Dtdisc_t* 	disc;	/* discipline			*/
39 #endif
40 {
41 	if(addr)
42 	{	if(size == 0)
43 		{	free(addr);
44 			return NIL(Void_t*);
45 		}
46 		else	return realloc(addr,size);
47 	}
48 	else	return size > 0 ? malloc(size) : NIL(Void_t*);
49 }
50 
51 #if __STD_C
52 Dtdisc_t* dtdisc(Dt_t* dt, Dtdisc_t* disc, int type)
53 #else
54 Dtdisc_t* dtdisc(dt,disc,type)
55 Dt_t*		dt;
56 Dtdisc_t*	disc;
57 int		type;
58 #endif
59 {
60 	Dtdisc_t	*old;
61 	Dtlink_t	*list;
62 
63 	if(!(old = dt->disc) )	/* initialization call from dtopen() */
64 	{	dt->disc = disc;
65 		if(!(dt->memoryf = disc->memoryf) )
66 			dt->memoryf = dtmemory;
67 		return disc;
68 	}
69 
70 	if(!disc) /* only want to know current discipline */
71 		return old;
72 
73 	if(old->eventf && (*old->eventf)(dt,DT_DISC,(Void_t*)disc,old) < 0)
74 		return NIL(Dtdisc_t*);
75 
76 	if((type & (DT_SAMEHASH|DT_SAMECMP)) != (DT_SAMEHASH|DT_SAMECMP) )
77 		list = dtextract(dt); /* grab the list of objects if any */
78 	else	list = NIL(Dtlink_t*);
79 
80 	dt->disc = disc;
81 	if(!(dt->memoryf = disc->memoryf) )
82 		dt->memoryf = dtmemory;
83 
84 	if(list ) /* reinsert extracted objects (with new discipline) */
85 		dtrestore(dt, list);
86 
87 	return old;
88 }
89