BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file dead.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Dead mechanism definitions.
32 *
33 * The dead mechanism is a way for a piece of code to detect whether some
34 * specific event has occured during some operation (usually during calling
35 * a user-provided handler function), without requiring access to memory
36 * that might no longer be available because of the event.
37 *
38 * It works somehow like that:
39 *
40 * First a dead variable ({@link dead_t}) is allocated somewhere, and
41 * initialized with {@link DEAD_INIT}, e.g.:
42 * DEAD_INIT(dead);
43 *
44 * When the event that needs to be caught occurs, {@link DEAD_KILL} is
45 * called, e.g.:
46 * DEAD_KILL(dead);
47 * The memory used by the dead variable is no longer needed after
48 * that.
49 *
50 * If a piece of code needs to know whether the event occured during some
51 * operation (but it must not have occured before!), it puts {@link DEAD_ENTER}}
52 * in front of the operation, and does {@link DEAD_LEAVE} at the end. If
53 * {@link DEAD_LEAVE} returned nonzero, the event occured, otherwise it did
54 * not. Example:
55 * DEAD_ENTER(dead)
56 * HandlerFunction();
57 * if (DEAD_LEAVE(dead)) {
58 * (event occured)
59 * }
60 *
61 * If is is needed to check for the event more than once in a single block,
62 * {@link DEAD_DECLARE} should be put somewhere before, and {@link DEAD_ENTER2}
63 * should be used instead of {@link DEAD_ENTER}.
64 *
65 * If it is needed to check for multiple events (dead variables) at the same
66 * time, DEAD_*_N macros should be used instead, specifying different
67 * identiers as the first argument for different dead variables.
68 */
69  
70 #ifndef BADVPN_MISC_DEAD_H
71 #define BADVPN_MISC_DEAD_H
72  
73 #include <stdlib.h>
74  
75 /**
76 * Dead variable.
77 */
78 typedef int *dead_t;
79  
80 /**
81 * Initializes a dead variable.
82 */
83 #define DEAD_INIT(ptr) { ptr = NULL; }
84  
85 /**
86 * Kills the dead variable,
87 */
88 #define DEAD_KILL(ptr) { if (ptr) *(ptr) = 1; }
89  
90 /**
91 * Kills the dead variable with the given value, or does nothing
92 * if the value is 0. The value will seen by {@link DEAD_KILLED}.
93 */
94 #define DEAD_KILL_WITH(ptr, val) { if (ptr) *(ptr) = (val); }
95  
96 /**
97 * Declares dead catching variables.
98 */
99 #define DEAD_DECLARE int badvpn__dead; dead_t badvpn__prev_ptr;
100  
101 /**
102 * Enters a dead catching using already declared dead catching variables.
103 * The dead variable must have been initialized with {@link DEAD_INIT},
104 * and {@link DEAD_KILL} must not have been called yet.
105 * {@link DEAD_LEAVE2} must be called before the current scope is left.
106 */
107 #define DEAD_ENTER2(ptr) { badvpn__dead = 0; badvpn__prev_ptr = ptr; ptr = &badvpn__dead; }
108  
109 /**
110 * Declares dead catching variables and enters a dead catching.
111 * The dead variable must have been initialized with {@link DEAD_INIT},
112 * and {@link DEAD_KILL} must not have been called yet.
113 * {@link DEAD_LEAVE2} must be called before the current scope is left.
114 */
115 #define DEAD_ENTER(ptr) DEAD_DECLARE DEAD_ENTER2(ptr)
116  
117 /**
118 * Leaves a dead catching.
119 */
120 #define DEAD_LEAVE2(ptr) { if (!badvpn__dead) ptr = badvpn__prev_ptr; if (badvpn__prev_ptr) *badvpn__prev_ptr = badvpn__dead; }
121  
122 /**
123 * Returns 1 if {@link DEAD_KILL} was called for the dead variable, 0 otherwise.
124 * Must be called after entering a dead catching.
125 */
126 #define DEAD_KILLED (badvpn__dead)
127  
128 #define DEAD_DECLARE_N(n) int badvpn__dead##n; dead_t badvpn__prev_ptr##n;
129 #define DEAD_ENTER2_N(n, ptr) { badvpn__dead##n = 0; badvpn__prev_ptr##n = ptr; ptr = &badvpn__dead##n;}
130 #define DEAD_ENTER_N(n, ptr) DEAD_DECLARE_N(n) DEAD_ENTER2_N(n, ptr)
131 #define DEAD_LEAVE2_N(n, ptr) { if (!badvpn__dead##n) ptr = badvpn__prev_ptr##n; if (badvpn__prev_ptr##n) *badvpn__prev_ptr##n = badvpn__dead##n; }
132 #define DEAD_KILLED_N(n) (badvpn__dead##n)
133  
134 #endif