summaryrefslogtreecommitdiff
path: root/src/lib/libast/cdt/cdtlib.h
blob: 1972dd24d3cfe8c4cc91f716ea63fc01bb2d7b7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2011 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                 Eclipse Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*          http://www.eclipse.org/org/documents/epl-v10.html           *
*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#ifndef _CDTLIB_H
#define _CDTLIB_H	1

/*	cdt library/method implementation header
**	this header is exported to the method libraries
**	Written by Kiem-Phong Vo (5/25/96)
*/

#if _PACKAGE_ast
#include	<ast.h>
#if !_BLD_cdt
#include	<dlldefs.h>
#endif
#endif

#include	<cdt.h>
#include	<unistd.h>
#include	<aso.h>

#include	"debug.h"

/* short-hand notations */
#define NIL(t)	((t)0)
#define reg	register

/* min #bits for a hash table. (1<<this) is table size */
#define DT_HTABLE	10

/* convenient types */
#if !defined(uint)
#define uint	unsigned int
#endif
#if !defined(uchar)
#define uchar	unsigned char
#endif

/* This struct holds private method data created on DT_OPEN */
struct _dtdata_s
{	unsigned int	lock;	/* general dictionary lock	*/
	Dtuser_t	user;	/* application's data		*/
	unsigned int	type;	/* method type, control flags	*/
	ssize_t		size;	/* number of objects		*/
	Dt_t		dict;	/* when DT_INDATA is requested	*/
};

/* this structure holds the plugin information */
typedef struct _dtlib_s
{
	char*		name;		/* short name */
	char*		description;	/* short description */
	char*		release;	/* release info */
	char*		prefix;		/* name prefix */
	Dtmethod_t**	methods;	/* method list */
} Dtlib_t;

#if _BLD_cdt

#if defined(__STDC__)
#define CDTLIB(m)	__DEFINE__(Dtmethod_t*,m,&_##m);
#else
#define CDTLIB(m)	__DEFINE__(Dtmethod_t*,m,&_/**/m);
#endif

#else

#if defined(__STDC__)
#define CDTLIB(m) \
	void* cdt_lib(const char* name, Dtdisc_t* disc, const char* type) \
	{ \
		int	i; \
		int	n; \
		if (!type) \
			return &cdt_lib_##m; \
		n = strlen(cdt_lib_##m.prefix); \
		if (!strncmp(type, cdt_lib_##m.prefix, n)) \
			type += n; \
		for (i = 0; cdt_lib_##m.methods[i]; i++) \
			if (!strcmp(type, cdt_lib_##m.methods[i]->name + n)) \
				return cdt_lib_##m.methods[i]; \
		return 0; \
	} \
	unsigned long plugin_version(void) { return CDT_PLUGIN_VERSION; }
#else
#define CDTLIB(m) \
	void* cdt_lib(name, disc, type) const char* name; Dtdisc_t* disc; const char* type; \
	{ \
		int	i; \
		int	n; \
		if (!type) \
			return &cdt_lib_/**/m; \
		n = strlen(cdt_lib_/**/m.prefix); \
		if (!strncmp(type, cdt_lib_/**/m.prefix, n)) \
			type += n; \
		for (i = 0; cdt_lib_/**/m.methods[i]; i++) \
			if (!strcmp(type, cdt_lib_/**/m.methods[i]->name + n)) \
				return cdt_lib_/**/m.methods[i]; \
		return 0; \
	} \
	unsigned long plugin_version() { return CDT_PLUGIN_VERSION; }
#endif

#endif /* _BLD_cdt */

/* these macros lock/unlock dictionaries. DTRETURN substitutes for "return" */
#define DTSETLOCK(dt)		(((dt)->data->type&DT_SHARE) ? asolock(&(dt)->data->lock,1,ASO_SPINLOCK) : 0 )
#define DTCLRLOCK(dt)		(((dt)->data->type&DT_SHARE) ? asolock(&(dt)->data->lock,1,ASO_UNLOCK) : 0 )
#define DTRETURN(ob,rv)		do { (ob) = (rv); goto dt_return; } while(0)
#define DTERROR(dt, mesg) 	(!((dt)->disc && (dt)->disc->eventf) ? 0 : \
				  (*(dt)->disc->eventf)((dt),DT_ERROR,(Void_t*)(mesg),(dt)->disc) )

/* announce completion of an operation of type (ty) on some object (ob) in dictionary (dt) */
#define DTANNOUNCE(dt,ob,ty)	( ((ob) && ((ty)&DT_TOANNOUNCE) && ((dt)->data->type&DT_ANNOUNCE) && \
				   (dt)->disc && (dt)->disc->eventf ) ? \
					(*(dt)->disc->eventf)((dt), DT_ANNOUNCE|(ty), (ob), (dt)->disc) : 0 )

/* map bits for upward compabitibility */
#define DTTYPE(dt,ty)		((dt)->typef ? (*(dt)->typef)((dt), (ty)) : (ty) )

/* short-hands for fields in Dtlink_t.
** note that __hash is used as a hash value
** or as the position in the parent table.
*/
#define _left	lh.__left
#define _hash	lh.__hash	
#define _ppos	lh.__hash

#define _rght	rh.__rght
#define _ptbl	rh.__ptbl

/* tree rotation/linking functions */
#define rrotate(x,y)	((x)->_left = (y)->_rght, (y)->_rght = (x))
#define lrotate(x,y)	((x)->_rght = (y)->_left, (y)->_left = (x))
#define rlink(r,x)	((r) = (r)->_left = (x) )
#define llink(l,x)	((l) = (l)->_rght = (x) )

#define RROTATE(x,y)	(rrotate(x,y), (x) = (y))
#define LROTATE(x,y)	(lrotate(x,y), (x) = (y))
#define RRSHIFT(x,t)	((t) = (x)->_left->_left, (x)->_left->_left = (t)->_rght, \
			 (t)->_rght = (x), (x) = (t) )
#define LLSHIFT(x,t)	((t) = (x)->_rght->_rght, (x)->_rght->_rght = (t)->_left, \
			 (t)->_left = (x), (x) = (t) )

_BEGIN_EXTERNS_

#if _BLD_cdt && defined(__EXPORT__)
#define extern	__EXPORT__
#endif

extern Dtlink_t*	_dtmake _ARG_((Dt_t*, Void_t*, int));
extern void		_dtfree _ARG_((Dt_t*, Dtlink_t*, int));
extern int		_dtlock _ARG_((Dt_t*, int));

#undef	extern

#if !_PACKAGE_ast
extern Void_t*		malloc _ARG_((size_t));
extern Void_t*		realloc _ARG_((Void_t*, size_t));
extern void		free _ARG_((Void_t*));
#endif
_END_EXTERNS_

#endif /* _CDTLIB_H */