Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/CException.c

1/*
2 * contains some ideas from the Mark VanderVoord's CException project
3 *
4 * Copyright (c) 2012-2013 Cadet-Petit Armel
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * Neither the name of Cadet-Petit Armel nor the names of the
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35#include "CException.h"
36#include "libsaio.h"
37#include "modules.h"
38#include "assert.h"
39
40static CEXCEPTION_FRAME_T *CExceptionFrames = NULL;
41static CEXCEPTION_FRAME_T *CExceptionMainFrames = NULL;
42
43#ifdef DEBUG_EXCEPTION
44static unsigned long frameCount = 0;
45#endif
46
47//------------------------------------------------------------------------------------------
48// Install or Restore Default handler
49//------------------------------------------------------------------------------------------
50int Install_Default_Handler(void)
51{
52#define XTRY \
53{\
54pushFrame(); \
55if ((e = setjmp(CExceptionFrames->cx)) == 0) { \
56e = CEXCEPTION_NONE; \
57if (1)
58
59#define XCATCH(e) \
60else { } \
61} \
62}\
63if (e != CEXCEPTION_NONE)
64
65 CEXCEPTION_T e = CEXCEPTION_NONE;
66
67XTRY
68{
69CExceptionMainFrames = CExceptionFrames;
70}
71XCATCH(e)
72{
73 if (e != EXIT_SUCCESS) {
74 popFrame();
75 halt();
76 }
77 }
78 return 1;
79}
80
81//------------------------------------------------------------------------------------------
82// Throw
83//------------------------------------------------------------------------------------------
84void Throw(CEXCEPTION_T ExceptionID)
85{
86longjmp(CExceptionFrames->cx, ExceptionID);
87
88}
89
90void pushFrame(void)
91{
92CEXCEPTION_FRAME_T* frame = (CEXCEPTION_FRAME_T*)malloc(sizeof(CEXCEPTION_FRAME_T));
93assert( frame != NULL );
94bzero(frame,sizeof(CEXCEPTION_FRAME_T));
95frame->pFrame = CExceptionFrames;
96CExceptionFrames = frame;
97#ifdef DEBUG_EXCEPTION
98frameCount++;
99#endif
100}
101
102void popFrame(void)
103{
104
105CEXCEPTION_FRAME_T* frame = NULL;
106assert( CExceptionFrames != NULL );
107frame = CExceptionFrames->pFrame;
108if (CExceptionFrames->zone) execute_hook("destroy_zone", CExceptionFrames->zone, NULL, NULL, NULL, NULL, NULL);
109free(CExceptionFrames);
110CExceptionFrames = frame;
111#ifdef DEBUG_EXCEPTION
112 if (frameCount) frameCount--;
113#endif
114}
115
116#ifdef DEBUG_EXCEPTION
117unsigned long getFramesCount(void)
118{
119return frameCount;
120}
121#endif
122
123CEXCEPTION_FRAME_T * getcurrentFrame(void)
124{
125return CExceptionFrames;
126}
127
128CEXCEPTION_FRAME_T * getMainFrames(void)
129{
130return CExceptionMainFrames;
131}

Archive Download this file

Revision: 2182