1
/* Copyright (c) 2007 SNAP Innovation GmbH
3
* BLOB Streaming for MySQL
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.
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.
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
19
* Paul McCullagh (H&G2JCtL)
24
* The definitions are used by all code, and is NOT required
29
#ifndef __CSGLOBAL_H__
30
#define __CSGLOBAL_H__
35
/* Those compilers that support the function
36
* macro (in particular the "pretty" function
37
* macro must be defined here.
40
#define __FUNC__ __PRETTY_FUNCTION__
42
#define __FUNC__ __FUNCTION__
45
/* This is the call context: */
46
#define CS_CONTEXT __FUNC__, __FILE__, __LINE__
49
int cs_assert(const char *func, const char *file, int line, const char *message);
50
int cs_hope(const char *func, const char *file, int line, const char *message);
52
#define ASSERT(x) ((x) ? 1 : cs_assert(CS_CONTEXT, #x))
53
#define HOPE(x) ((x) ? 1 : cs_hope(CS_CONTEXT, #x))
59
#define new_(v, t) do { v = new t; if (!v) CSException::throwOSError(CS_CONTEXT, ENOMEM); } while (0)
62
* -----------------------------------------------------------------------
67
* This macro must be placed at the start of every function.
68
* It records the current context so that we can
69
* dump a type of stack trace later if necessary.
71
* It also sets up the current thread pointer 'self'.
73
#define inner_() int cs_frame = self->callTop++; \
75
if (cs_frame< CS_CALL_STACK_SIZE) { \
76
self->callStack[cs_frame].cs_func = __FUNC__; \
77
self->callStack[cs_frame].cs_file = __FILE__; \
78
self->callStack[cs_frame].cs_line = __LINE__; \
82
#define outer_() self->callTop = cs_frame;
84
#define enter_() CSThread *self = CSThread::getSelf(); \
88
* On exit to a function, either exit_() or
89
* return_() must be called.
91
#define exit_() do { \
97
#define return_(x) do { \
104
* -----------------------------------------------------------------------
105
* Throwing and catching (the jump stack)
108
int prof_setjmp(void);
110
#define TX_CHK_JMP() if ((self)->jumpDepth < 0 || (self)->jumpDepth >= CS_JUMP_STACK_SIZE) CSException::throwCoreError(__FUNC__, __FILE__, __LINE__, CS_ERR_JUMP_OVERFLOW)
112
#define profile_setjmp prof_setjmp()
114
#define profile_setjmp
117
#define throw_() (self)->throwException()
118
#define try_(n) int throw_##n; throw_##n = 0; TX_CHK_JMP(); \
119
(self)->jumpEnv[(self)->jumpDepth].jb_res_top = (self)->relTop; \
120
(self)->jumpEnv[(self)->jumpDepth].jb_call_top = (self)->callTop; \
121
(self)->jumpDepth++; profile_setjmp; \
122
if (setjmp((self)->jumpEnv[(self)->jumpDepth-1].jb_buffer)) goto catch_##n;
123
#define catch_(n) (self)->jumpDepth--; goto cont_##n; catch_##n: (self)->jumpDepth--; self->caught();
124
#define finally_(n) (self)->jumpDepth--; goto final_##n; catch_##n: throw_##n = 1; (self)->jumpDepth--; self->caught(); final_##n:
125
#define cont_(n) if (throw_##n) throw_(); cont_##n:
128
* -----------------------------------------------------------------------
132
#define push_(r) do { \
133
if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) \
134
CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
135
(self)->relTop->r_type = CS_RELEASE_OBJECT; \
136
(self)->relTop->x.r_object = (r); \
140
#define pop_(r) do { \
141
ASSERT((self)->relTop > (self)->relStack); \
142
if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT) \
143
ASSERT(((self)->relTop - 1)->x.r_object == ((CSObject *) r)); \
144
else if (((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX) \
145
ASSERT(((self)->relTop - 1)->x.r_mutex == ((CSMutex *) r)); \
147
ASSERT(((self)->relTop - 1)->x.r_pooled == ((CSPooled *) r)); \
151
#define retain_(r) do { \
156
#define release_(r) do { \
157
register CSObject *rp; \
158
ASSERT((self)->relTop > (self)->relStack); \
159
ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT); \
160
rp = ((self)->relTop - 1)->x.r_object; \
166
#define lock_(r) do { \
167
if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) \
168
CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
170
(self)->relTop->r_type = CS_RELEASE_MUTEX; \
171
(self)->relTop->x.r_mutex = (r); \
175
#define unlock_(r) do { \
176
register CSMutex *rp; \
177
ASSERT((self)->relTop > (self)->relStack); \
178
ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX); \
179
rp = ((self)->relTop - 1)->x.r_mutex; \
185
#define frompool_(r) do { \
186
if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) \
187
CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
188
(self)->relTop->r_type = CS_RELEASE_POOLED; \
189
(self)->relTop->x.r_pooled = (r); \
193
#define backtopool_(r) do { \
194
register CSPooled *rp; \
195
ASSERT((self)->relTop > (self)->relStack); \
196
ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_POOLED); \
197
rp = ((self)->relTop - 1)->x.r_pooled; \
200
rp->returnToPool(); \