1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-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 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #ifndef _CDTLIB_H
23 #define _CDTLIB_H	1
24 
25 /*	cdt library/method implementation header
26 **	this header is exported to the method libraries
27 **	Written by Kiem-Phong Vo (5/25/96)
28 */
29 
30 #if _PACKAGE_ast
31 #include	<ast.h>
32 #if !_BLD_cdt
33 #include	<dlldefs.h>
34 #endif
35 #endif
36 
37 #include	<cdt.h>
38 #include	<unistd.h>
39 #include	<aso.h>
40 
41 #include	"debug.h"
42 
43 /* short-hand notations */
44 #define NIL(t)	((t)0)
45 #define reg	register
46 
47 /* min #bits for a hash table. (1<<this) is table size */
48 #define DT_HTABLE	10
49 
50 /* convenient types */
51 #if !defined(uint)
52 #define uint	unsigned int
53 #endif
54 #if !defined(uchar)
55 #define uchar	unsigned char
56 #endif
57 
58 /* This struct holds private method data created on DT_OPEN */
59 struct _dtdata_s
60 {	unsigned int	lock;	/* general dictionary lock	*/
61 	Dtuser_t	user;	/* application's data		*/
62 	unsigned int	type;	/* method type, control flags	*/
63 	ssize_t		size;	/* number of objects		*/
64 	Dt_t		dict;	/* when DT_INDATA is requested	*/
65 };
66 
67 /* this structure holds the plugin information */
68 typedef struct _dtlib_s
69 {
70 	char*		name;		/* short name */
71 	char*		description;	/* short description */
72 	char*		release;	/* release info */
73 	char*		prefix;		/* name prefix */
74 	Dtmethod_t**	methods;	/* method list */
75 } Dtlib_t;
76 
77 #if _BLD_cdt
78 
79 #if defined(__STDC__)
80 #define CDTLIB(m)	__DEFINE__(Dtmethod_t*,m,&_##m);
81 #else
82 #define CDTLIB(m)	__DEFINE__(Dtmethod_t*,m,&_/**/m);
83 #endif
84 
85 #else
86 
87 #if defined(__STDC__)
88 #define CDTLIB(m) \
89 	void* cdt_lib(const char* name, Dtdisc_t* disc, const char* type) \
90 	{ \
91 		int	i; \
92 		int	n; \
93 		if (!type) \
94 			return &cdt_lib_##m; \
95 		n = strlen(cdt_lib_##m.prefix); \
96 		if (!strncmp(type, cdt_lib_##m.prefix, n)) \
97 			type += n; \
98 		for (i = 0; cdt_lib_##m.methods[i]; i++) \
99 			if (!strcmp(type, cdt_lib_##m.methods[i]->name + n)) \
100 				return cdt_lib_##m.methods[i]; \
101 		return 0; \
102 	} \
103 	unsigned long plugin_version(void) { return CDT_PLUGIN_VERSION; }
104 #else
105 #define CDTLIB(m) \
106 	void* cdt_lib(name, disc, type) const char* name; Dtdisc_t* disc; const char* type; \
107 	{ \
108 		int	i; \
109 		int	n; \
110 		if (!type) \
111 			return &cdt_lib_/**/m; \
112 		n = strlen(cdt_lib_/**/m.prefix); \
113 		if (!strncmp(type, cdt_lib_/**/m.prefix, n)) \
114 			type += n; \
115 		for (i = 0; cdt_lib_/**/m.methods[i]; i++) \
116 			if (!strcmp(type, cdt_lib_/**/m.methods[i]->name + n)) \
117 				return cdt_lib_/**/m.methods[i]; \
118 		return 0; \
119 	} \
120 	unsigned long plugin_version() { return CDT_PLUGIN_VERSION; }
121 #endif
122 
123 #endif /* _BLD_cdt */
124 
125 /* these macros lock/unlock dictionaries. DTRETURN substitutes for "return" */
126 #define DTSETLOCK(dt)		(((dt)->data->type&DT_SHARE) ? asolock(&(dt)->data->lock,1,ASO_SPINLOCK) : 0 )
127 #define DTCLRLOCK(dt)		(((dt)->data->type&DT_SHARE) ? asolock(&(dt)->data->lock,1,ASO_UNLOCK) : 0 )
128 #define DTRETURN(ob,rv)		do { (ob) = (rv); goto dt_return; } while(0)
129 #define DTERROR(dt, mesg) 	(!((dt)->disc && (dt)->disc->eventf) ? 0 : \
130 				  (*(dt)->disc->eventf)((dt),DT_ERROR,(Void_t*)(mesg),(dt)->disc) )
131 
132 /* announce completion of an operation of type (ty) on some object (ob) in dictionary (dt) */
133 #define DTANNOUNCE(dt,ob,ty)	( ((ob) && ((ty)&DT_TOANNOUNCE) && ((dt)->data->type&DT_ANNOUNCE) && \
134 				   (dt)->disc && (dt)->disc->eventf ) ? \
135 					(*(dt)->disc->eventf)((dt), DT_ANNOUNCE|(ty), (ob), (dt)->disc) : 0 )
136 
137 /* map bits for upward compabitibility */
138 #define DTTYPE(dt,ty)		((dt)->typef ? (*(dt)->typef)((dt), (ty)) : (ty) )
139 
140 /* short-hands for fields in Dtlink_t.
141 ** note that __hash is used as a hash value
142 ** or as the position in the parent table.
143 */
144 #define _left	lh.__left
145 #define _hash	lh.__hash
146 #define _ppos	lh.__hash
147 
148 #define _rght	rh.__rght
149 #define _ptbl	rh.__ptbl
150 
151 /* tree rotation/linking functions */
152 #define rrotate(x,y)	((x)->_left = (y)->_rght, (y)->_rght = (x))
153 #define lrotate(x,y)	((x)->_rght = (y)->_left, (y)->_left = (x))
154 #define rlink(r,x)	((r) = (r)->_left = (x) )
155 #define llink(l,x)	((l) = (l)->_rght = (x) )
156 
157 #define RROTATE(x,y)	(rrotate(x,y), (x) = (y))
158 #define LROTATE(x,y)	(lrotate(x,y), (x) = (y))
159 #define RRSHIFT(x,t)	((t) = (x)->_left->_left, (x)->_left->_left = (t)->_rght, \
160 			 (t)->_rght = (x), (x) = (t) )
161 #define LLSHIFT(x,t)	((t) = (x)->_rght->_rght, (x)->_rght->_rght = (t)->_left, \
162 			 (t)->_left = (x), (x) = (t) )
163 
164 _BEGIN_EXTERNS_
165 
166 #if _BLD_cdt && defined(__EXPORT__)
167 #define extern	__EXPORT__
168 #endif
169 
170 extern Dtlink_t*	_dtmake _ARG_((Dt_t*, Void_t*, int));
171 extern void		_dtfree _ARG_((Dt_t*, Dtlink_t*, int));
172 extern int		_dtlock _ARG_((Dt_t*, int));
173 
174 #undef	extern
175 
176 #if !_PACKAGE_ast
177 extern Void_t*		malloc _ARG_((size_t));
178 extern Void_t*		realloc _ARG_((Void_t*, size_t));
179 extern void		free _ARG_((Void_t*));
180 #endif
181 _END_EXTERNS_
182 
183 #endif /* _CDTLIB_H */
184