~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Mark Atwood
  • Date: 2011-12-20 02:32:53 UTC
  • mfrom: (2469.1.1 drizzle-build)
  • Revision ID: me@mark.atwood.name-20111220023253-bvu0kr14kwsdvz7g
mergeĀ lp:~brianaker/drizzle/deprecate-pbms

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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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
 
#pragma once
31
 
#ifndef __CSGLOBAL_H__
32
 
#define __CSGLOBAL_H__
33
 
 
34
 
#include "CSDefs.h"
35
 
#include "CSObject.h"
36
 
#include "CSThread.h"
37
 
 
38
 
 
39
 
/* This is the call context: */
40
 
#define CS_CONTEXT                      __FUNC__, __FILE__, __LINE__
41
 
 
42
 
#ifdef DEBUG
43
 
int cs_assert(const char *func, const char *file, int line, const char *message);
44
 
int cs_hope(const char *func, const char *file, int line, const char *message);
45
 
 
46
 
#define ASSERT(x)                       ((x) ? 1 : cs_assert(CS_CONTEXT, #x))
47
 
#define HOPE(x)                         ((x) ? 1 : cs_hope(CS_CONTEXT, #x))
48
 
#else
49
 
#define ASSERT(x)
50
 
#define ASSERT(x)
51
 
#endif
52
 
 
53
 
#ifdef DEBUG
54
 
#define retain()                        retain(__FUNC__, __FILE__, __LINE__)
55
 
#define release()                       release(__FUNC__, __FILE__, __LINE__)
56
 
#endif
57
 
 
58
 
#define new_(v, t)                      do { v = new t; if (!v) CSException::throwOSError(CS_CONTEXT, ENOMEM); } while (0)
59
 
 
60
 
/*
61
 
 * -----------------------------------------------------------------------
62
 
 * Call stack
63
 
 */
64
 
 
65
 
/*
66
 
 * This macro must be placed at the start of every function.
67
 
 * It records the current context so that we can
68
 
 * dump a type of stack trace later if necessary.
69
 
 *
70
 
 * It also sets up the current thread pointer 'self'.
71
 
 */
72
 
#ifdef DEBUG
73
 
#define STACK_CHECK    CSReleasePtr self_reltop = self->relTop
74
 
#else
75
 
#define STACK_CHECK   
76
 
#endif
77
 
 
78
 
#define inner_()                        int                     cs_frame = self->callTop++; \
79
 
                                                        STACK_CHECK ; \
80
 
                                                        do { \
81
 
                                                                if (cs_frame< CS_CALL_STACK_SIZE) { \
82
 
                                                                        self->callStack[cs_frame].cs_func = __FUNC__; \
83
 
                                                                        self->callStack[cs_frame].cs_file = __FILE__; \
84
 
                                                                        self->callStack[cs_frame].cs_line = __LINE__; \
85
 
                                                                } \
86
 
                                                        } while (0)
87
 
 
88
 
#define outer_()                        self->callTop = cs_frame; \
89
 
                                                        ASSERT(self->relTop == self_reltop); 
90
 
 
91
 
#define enter_()                        CSThread        *self = CSThread::getSelf(); \
92
 
                                                        inner_()
93
 
 
94
 
/*
95
 
 * On exit to a function, either exit_() or
96
 
 * return_() must be called.
97
 
 */
98
 
#define exit_()                         do { \
99
 
                                                                outer_(); \
100
 
                                                                return; \
101
 
                                                        } while (0)
102
 
                                        
103
 
 
104
 
#define return_(x)                      do { \
105
 
                                                                outer_(); \
106
 
                                                                return(x); \
107
 
                                                        } while (0)
108
 
 
109
 
 
110
 
/*
111
 
 * -----------------------------------------------------------------------
112
 
 * Throwing and catching (the jump stack)
113
 
 */
114
 
 
115
 
int prof_setjmp(void);
116
 
 
117
 
#define TX_CHK_JMP()            if ((self)->jumpDepth < 0 || (self)->jumpDepth >= CS_JUMP_STACK_SIZE) CSException::throwCoreError(__FUNC__, __FILE__, __LINE__, CS_ERR_JUMP_OVERFLOW)
118
 
#ifdef PROFILE
119
 
#define profile_setjmp          prof_setjmp()
120
 
#else
121
 
#define profile_setjmp                  
122
 
#endif
123
 
 
124
 
#define throw_()                        (self)->throwException()
125
 
#define try_(n)                         int throw_##n; throw_##n = 0; TX_CHK_JMP(); \
126
 
                                                        (self)->jumpEnv[(self)->jumpDepth].jb_res_top = (self)->relTop; \
127
 
                                                        (self)->jumpEnv[(self)->jumpDepth].jb_call_top = (self)->callTop; \
128
 
                                                        (self)->jumpDepth++; profile_setjmp; \
129
 
                                                        if (setjmp((self)->jumpEnv[(self)->jumpDepth-1].jb_buffer)) goto catch_##n;
130
 
#define catch_(n)                       (self)->jumpDepth--; goto cont_##n; catch_##n: (self)->jumpDepth--; self->caught();
131
 
#define cont_(n)                        if (throw_##n) throw_(); cont_##n:
132
 
#define finally_(n)                     (self)->jumpDepth--; goto final_##n; catch_##n: throw_##n = 1; (self)->jumpDepth--; self->caught(); final_##n: {
133
 
#define finally_end_block(n) } if (throw_##n) throw_();
134
 
#define finally_end_block_no_throw(n) }
135
 
 
136
 
/*
137
 
 * -----------------------------------------------------------------------
138
 
 * The release stack
139
 
 */
140
 
 
141
 
#define push_(r)                        do { \
142
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
143
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
144
 
                                                                } \
145
 
                                                                (self)->relTop->r_type = CS_RELEASE_OBJECT; \
146
 
                                                                (self)->relTop->x.r_object = (r); \
147
 
                                                                (self)->relTop++; \
148
 
                                                        } while (0)
149
 
 
150
 
#define push_ptr_(r)                    do { \
151
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
152
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
153
 
                                                                } \
154
 
                                                                (self)->relTop->r_type = CS_RELEASE_MEM; \
155
 
                                                                (self)->relTop->x.r_mem = (r); \
156
 
                                                                (self)->relTop++; \
157
 
                                                        } while (0)
158
 
 
159
 
#define push_ref_(r)                    do { \
160
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
161
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
162
 
                                                                } \
163
 
                                                                (self)->relTop->r_type = CS_RELEASE_OBJECT_PTR; \
164
 
                                                                (self)->relTop->x.r_objectPtr = (CSObject **)&(r); \
165
 
                                                                (self)->relTop++; \
166
 
                                                        } while (0)
167
 
 
168
 
#define pop_(r)                         do { \
169
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
170
 
                                                                if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT) {\
171
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_object == ((CSObject *) r)); \
172
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX) {\
173
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_mutex == ((CSMutex *) r)); \
174
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_POOLED) {\
175
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_pooled == ((CSPooled *) r)); \
176
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_MEM) {\
177
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_mem == ((void *) r)); \
178
 
                                                                }  else if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT_PTR) {\
179
 
                                                                        ASSERT(((self)->relTop - 1)->x.r_objectPtr == ((CSObject **) &(r))); \
180
 
                                                                }  else {\
181
 
                                                                        ASSERT(false); \
182
 
                                                                } \
183
 
                                                                (self)->relTop--; \
184
 
                                                        } while (0)
185
 
 
186
 
#define retain_(r)                      do { \
187
 
                                                                (r)->retain(); \
188
 
                                                                push_(r); \
189
 
                                                        } while (0)
190
 
 
191
 
#define release_(r)                     do {  \
192
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
193
 
                                                                if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT) {\
194
 
                                                                        register CSObject *rp; \
195
 
                                                                        rp = ((self)->relTop - 1)->x.r_object; \
196
 
                                                                        ASSERT(rp == (CSObject *)(r)); \
197
 
                                                                        (self)->relTop--; \
198
 
                                                                        rp->release(); \
199
 
                                                                } else if (((self)->relTop - 1)->r_type == CS_RELEASE_MEM) {\
200
 
                                                                        register void *mem; \
201
 
                                                                        mem = ((self)->relTop - 1)->x.r_mem; \
202
 
                                                                        ASSERT(mem == (void *)(r)); \
203
 
                                                                        (self)->relTop--; \
204
 
                                                                        cs_free(mem); \
205
 
                                                                }  else if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT_PTR) {\
206
 
                                                                        register CSObject **rp; \
207
 
                                                                        rp = ((self)->relTop - 1)->x.r_objectPtr; \
208
 
                                                                        ASSERT(rp == (CSObject **)&(r)); \
209
 
                                                                        (self)->relTop--; \
210
 
                                                                        if(*rp) (*rp)->release(); \
211
 
                                                                } else {\
212
 
                                                                        ASSERT(false); \
213
 
                                                                } \
214
 
                                                        } while (0)
215
 
 
216
 
#define lock_(r)                        do { \
217
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
218
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
219
 
                                                                } \
220
 
                                                                (r)->lock(); \
221
 
                                                                (self)->relTop->r_type = CS_RELEASE_MUTEX; \
222
 
                                                                (self)->relTop->x.r_mutex = (r); \
223
 
                                                                (self)->relTop++; \
224
 
                                                        } while (0)
225
 
 
226
 
#define locked_(r)                      do { \
227
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) \
228
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
229
 
                                                                (self)->relTop->r_type = CS_RELEASE_MUTEX; \
230
 
                                                                (self)->relTop->x.r_mutex = (r); \
231
 
                                                                (self)->relTop++; \
232
 
                                                        } while (0)
233
 
 
234
 
#define unlock_(r)                      do {  \
235
 
                                                                register CSMutex *rp; \
236
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
237
 
                                                                ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX); \
238
 
                                                                rp = ((self)->relTop - 1)->x.r_mutex; \
239
 
                                                                ASSERT(rp == (r)); \
240
 
                                                                (self)->relTop--; \
241
 
                                                                rp->unlock(); \
242
 
                                                        } while (0)
243
 
 
244
 
#define frompool_(r)            do { \
245
 
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) {\
246
 
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
247
 
                                                                } \
248
 
                                                                (self)->relTop->r_type = CS_RELEASE_POOLED; \
249
 
                                                                (self)->relTop->x.r_pooled = (r); \
250
 
                                                                (self)->relTop++; \
251
 
                                                        } while (0)
252
 
 
253
 
#define backtopool_(r)          do {  \
254
 
                                                                register CSPooled *rp; \
255
 
                                                                ASSERT((self)->relTop > (self)->relStack); \
256
 
                                                                ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_POOLED); \
257
 
                                                                rp = ((self)->relTop - 1)->x.r_pooled; \
258
 
                                                                ASSERT(rp == (r)); \
259
 
                                                                (self)->relTop--; \
260
 
                                                                rp->returnToPool(); \
261
 
                                                        } while (0)
262
 
 
263
 
#endif