~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSGlobal.h

code clean move Item_func_num1 and Item_func_connection_id to functions directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
2
 
 *
3
 
 * PrimeBase Media Stream for MySQL
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
 
 *
19
 
 * Original author: Paul McCullagh (H&G2JCtL)
20
 
 * Continued development: Barry Leslie
21
 
 *
22
 
 * 2007-05-20
23
 
 *
24
 
 * CORE SYSTEM:
25
 
 * The definitions are used by all code, and is NOT required
26
 
 * by any header file!
27
 
 *
28
 
 */
29
 
 
30
 
#ifndef __CSGLOBAL_H__
31
 
#define __CSGLOBAL_H__
32
 
 
33
 
#include "CSDefs.h"
34
 
#include "CSThread.h"
35
 
 
36
 
/* Those compilers that support the function
37
 
 * macro (in particular the "pretty" function
38
 
 * macro must be defined here.
39
 
 */
40
 
#ifdef OS_WINDOWS
41
 
#define __FUNC__                                __FUNCTION__
42
 
#elif defined(OS_SOLARIS)
43
 
#define __FUNC__                                "__func__"
44
 
#else
45
 
#define __FUNC__                                __PRETTY_FUNCTION__
46
 
#endif
47
 
 
48
 
 
49
 
/* This is the call context: */
50
 
#define CS_CONTEXT                      __FUNC__, __FILE__, __LINE__
51
 
 
52
 
#ifdef DEBUG
53
 
int cs_assert(const char *func, const char *file, int line, const char *message);
54
 
int cs_hope(const char *func, const char *file, int line, const char *message);
55
 
 
56
 
#define ASSERT(x)                       ((x) ? 1 : cs_assert(CS_CONTEXT, #x))
57
 
#define HOPE(x)                         ((x) ? 1 : cs_hope(CS_CONTEXT, #x))
58
 
#else
59
 
#define ASSERT(x)
60
 
#define ASSERT(x)
61
 
#endif
62
 
 
63
 
#define new_(v, t)                      do { v = new t; if (!v) CSException::throwOSError(CS_CONTEXT, ENOMEM); } while (0)
64
 
 
65
 
/*
66
 
 * -----------------------------------------------------------------------
67
 
 * Call stack
68
 
 */
69
 
 
70
 
/*
71
 
 * This macro must be placed at the start of every function.
72
 
 * It records the current context so that we can
73
 
 * dump a type of stack trace later if necessary.
74
 
 *
75
 
 * It also sets up the current thread pointer 'self'.
76
 
 */
77
 
#define inner_()                        int                     cs_frame = self->callTop++; \
78
 
                                                        do { \
79
 
                                                                if (cs_frame< CS_CALL_STACK_SIZE) { \
80
 
                                                                        self->callStack[cs_frame].cs_func = __FUNC__; \
81
 
                                                                        self->callStack[cs_frame].cs_file = __FILE__; \
82
 
                                                                        self->callStack[cs_frame].cs_line = __LINE__; \
83
 
                                                                } \
84
 
                                                        } while (0)
85
 
 
86
 
#define outer_()                        self->callTop = cs_frame;
87
 
 
88
 
#define enter_()                        CSThread        *self = CSThread::getSelf(); \
89
 
                                                        inner_()
90
 
 
91
 
/*
92
 
 * On exit to a function, either exit_() or
93
 
 * return_() must be called.
94
 
 */
95
 
#define exit_()                         do { \
96
 
                                                                outer_(); \
97
 
                                                                return; \
98
 
                                                        } while (0)
99
 
                                        
100
 
 
101
 
#define return_(x)                      do { \
102
 
                                                                outer_(); \
103
 
                                                                return(x); \
104
 
                                                        } while (0)
105
 
 
106
 
 
107
 
/*
108
 
 * -----------------------------------------------------------------------
109
 
 * Throwing and catching (the jump stack)
110
 
 */
111
 
 
112
 
/*
113
 
 * Quote from the C99 spec on setjmp:
114
 
 * All accessible objects have values, and all other components of the abstract 
115
 
 * machine have state, as of the time the longjmp function was called, except that 
116
 
 * the values of objects of automatic storage duration that are local to the function 
117
 
 * containing the invocation of the corresponding setjmp macro that do not have 
118
 
 * volatile-qualified type and have been changed between the setjmp invocation and 
119
 
 * longjmp call are indeterminate. 
120
 
 *
121
 
 * GCC will not put variables into registers for which the address gets taken do use
122
 
 * CLOBBER_PROTECT() to protect variables from being clobbered.
123
 
 * This came from Jens Thoms Toerring. Thanks!
124
 
 */
125
 
#define CLOBBER_PROTECT(a) do { if ((void *)&(a) == (void*) 1) (a) = (a);} while(0)
126
 
 
127
 
int prof_setjmp(void);
128
 
 
129
 
#define TX_CHK_JMP()            if ((self)->jumpDepth < 0 || (self)->jumpDepth >= CS_JUMP_STACK_SIZE) CSException::throwCoreError(__FUNC__, __FILE__, __LINE__, CS_ERR_JUMP_OVERFLOW)
130
 
#ifdef PROFILE
131
 
#define profile_setjmp          prof_setjmp()
132
 
#else
133
 
#define profile_setjmp                  
134
 
#endif
135
 
 
136
 
#define throw_()                        (self)->throwException()
137
 
#define try_(n)                         int throw_##n; throw_##n = 0; TX_CHK_JMP(); \
138
 
                                                        (self)->jumpEnv[(self)->jumpDepth].jb_res_top = (self)->relTop; \
139
 
                                                        (self)->jumpEnv[(self)->jumpDepth].jb_call_top = (self)->callTop; \
140
 
                                                        (self)->jumpDepth++; profile_setjmp; \
141
 
                                                        if (setjmp((self)->jumpEnv[(self)->jumpDepth-1].jb_buffer)) goto catch_##n;
142
 
#define catch_(n)                       (self)->jumpDepth--; goto cont_##n; catch_##n: (self)->jumpDepth--; self->caught();
143
 
#define cont_(n)                        if (throw_##n) throw_(); cont_##n:
144
 
#define finally_(n)                     (self)->jumpDepth--; goto final_##n; catch_##n: throw_##n = 1; (self)->jumpDepth--; self->caught(); final_##n: {
145
 
#define finally_end_block(n) } if (throw_##n) throw_();
146
 
#define finally_end_block_no_throw(n) }
147
 
 
148
 
/*
149
 
 * -----------------------------------------------------------------------
150
 
 * The release stack
151
 
 */
152
 
 
153
 
#define push_(r)                        do { \
154
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
155
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
156
 
                                                                } \
157
 
                                                                (self)->relTop->r_type = CS_RELEASE_OBJECT; \
158
 
                                                                (self)->relTop->x.r_object = (r); \
159
 
                                                                (self)->relTop++; \
160
 
                                                        } while (0)
161
 
 
162
 
#define push_ptr_(r)                    do { \
163
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
164
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
165
 
                                                                } \
166
 
                                                                (self)->relTop->r_type = CS_RELEASE_MEM; \
167
 
                                                                (self)->relTop->x.r_mem = (r); \
168
 
                                                                (self)->relTop++; \
169
 
                                                        } while (0)
170
 
 
171
 
#define pop_(r)                         do { \
172
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
173
 
                                                                if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT) {\
174
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_object == ((CSObject *) r)); \
175
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX) {\
176
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_mutex == ((CSMutex *) r)); \
177
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_POOLED) {\
178
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_pooled == ((CSPooled *) r)); \
179
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_MEM) {\
180
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_mem == ((CSPooled *) r)); \
181
 
                                                                }  else {\
182
 
                                                                        ASSERT(false); \
183
 
                                                                } \
184
 
                                                                (self)->relTop--; \
185
 
                                                        } while (0)
186
 
 
187
 
#define retain_(r)                      do { \
188
 
                                                                (r)->retain(); \
189
 
                                                                push_(r); \
190
 
                                                        } while (0)
191
 
 
192
 
#define release_(r)                     do {  \
193
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
194
 
                                                                if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT) {\
195
 
                                                                        register CSObject *rp; \
196
 
                                                                        rp = ((self)->relTop - 1)->x.r_object; \
197
 
                                                                        ASSERT(rp == (CSObject *)(r)); \
198
 
                                                                        (self)->relTop--; \
199
 
                                                                        rp->release(); \
200
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_MEM) {\
201
 
                                                                        register void *mem; \
202
 
                                                                        mem = ((self)->relTop - 1)->x.r_mem; \
203
 
                                                                        ASSERT(mem == (void *)(r)); \
204
 
                                                                        (self)->relTop--; \
205
 
                                                                        cs_free(mem); \
206
 
                                                                }  else {\
207
 
                                                                        ASSERT(false); \
208
 
                                                                } \
209
 
                                                        } while (0)
210
 
 
211
 
#define lock_(r)                        do { \
212
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
213
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
214
 
                                                                } \
215
 
                                                                (r)->lock(); \
216
 
                                                                (self)->relTop->r_type = CS_RELEASE_MUTEX; \
217
 
                                                                (self)->relTop->x.r_mutex = (r); \
218
 
                                                                (self)->relTop++; \
219
 
                                                        } while (0)
220
 
 
221
 
#define unlock_(r)                      do {  \
222
 
                                                                register CSMutex *rp; \
223
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
224
 
                                                                ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX); \
225
 
                                                                rp = ((self)->relTop - 1)->x.r_mutex; \
226
 
                                                                ASSERT(rp == (r)); \
227
 
                                                                (self)->relTop--; \
228
 
                                                                rp->unlock(); \
229
 
                                                        } while (0)
230
 
 
231
 
#define frompool_(r)            do { \
232
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
233
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
234
 
                                                                } \
235
 
                                                                (self)->relTop->r_type = CS_RELEASE_POOLED; \
236
 
                                                                (self)->relTop->x.r_pooled = (r); \
237
 
                                                                (self)->relTop++; \
238
 
                                                        } while (0)
239
 
 
240
 
#define backtopool_(r)          do {  \
241
 
                                                                register CSPooled *rp; \
242
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
243
 
                                                                ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_POOLED); \
244
 
                                                                rp = ((self)->relTop - 1)->x.r_pooled; \
245
 
                                                                ASSERT(rp == (r)); \
246
 
                                                                (self)->relTop--; \
247
 
                                                                rp->returnToPool(); \
248
 
                                                        } while (0)
249
 
 
250
 
#endif