1 glowmem.h |
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
|
3 |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions are met: |
|
6 |
|
7 * Redistributions of source code must retain the above copyright notice, this |
|
8 * list of conditions and the following disclaimer. |
|
9 * Redistributions in binary form must reproduce the above copyright notice, |
|
10 * this list of conditions and the following disclaimer in the documentation |
|
11 * and/or other materials provided with the distribution. |
|
12 * Neither the name of Nokia Corporation nor the names of its contributors |
|
13 * may be used to endorse or promote products derived from this software |
|
14 * without specific prior written permission. |
|
15 |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
26 * |
|
27 * Description: |
|
28 * |
|
29 */ |
|
30 |
|
31 #ifndef __G_LOW_MEM_H__ |
|
32 #define __G_LOW_MEM_H__ |
|
33 |
|
34 #include <unistd.h> |
|
35 #include <setjmp.h> |
|
36 |
|
37 /*-------------------------- Data Structure Decleration ----------------*/ |
|
38 typedef struct _cleanUpStack cleanUpStack ; |
|
39 typedef struct _mem_info mem_info ; |
|
40 |
|
41 struct _cleanUpStack |
|
42 { |
|
43 void *ptr[1000]; |
|
44 int top; |
|
45 }; |
|
46 |
|
47 struct _mem_info |
|
48 { |
|
49 int is_setjmp_called; |
|
50 jmp_buf buf; |
|
51 cleanUpStack stack; |
|
52 }; |
|
53 |
|
54 |
|
55 /*--------------------Function declerations------------------*/ |
|
56 #ifdef __cplusplus |
|
57 extern "C" |
|
58 { |
|
59 #endif /* __cplusplus */ |
|
60 |
|
61 IMPORT_C mem_info * _get_thread_specific_data(); |
|
62 IMPORT_C int _set_thread_specific_data(mem_info *m); |
|
63 IMPORT_C int _push(cleanUpStack *cs,void *ptr); |
|
64 IMPORT_C void *_pop(cleanUpStack *cs); |
|
65 IMPORT_C void _findAndDestroy(cleanUpStack *cs,void *ptr); |
|
66 IMPORT_C void _destroCleanUpStack(cleanUpStack *cs); |
|
67 IMPORT_C void _clearCleanUpStack(cleanUpStack *cs); |
|
68 IMPORT_C void * _pAlloc(size_t size); |
|
69 IMPORT_C void _dummy1(); |
|
70 IMPORT_C void _dummy2(); |
|
71 IMPORT_C void _dummy3(); |
|
72 |
|
73 #ifdef __cplusplus |
|
74 } |
|
75 #endif /* __cplusplus */ |
|
76 |
|
77 /*--------------------MACRO declerations------------------*/ |
|
78 |
|
79 #define SET_LOW_MEMORY_TRAP_VOID() \ |
|
80 gboolean did_i_set = FALSE;\ |
|
81 {\ |
|
82 mem_info *m = _get_thread_specific_data();\ |
|
83 if(m == NULL)\ |
|
84 {\ |
|
85 m = (mem_info *)_pAlloc(sizeof(mem_info));\ |
|
86 if(!m)\ |
|
87 return;\ |
|
88 m->is_setjmp_called = FALSE;\ |
|
89 _clearCleanUpStack(&(m->stack));\ |
|
90 if(_set_thread_specific_data(m))\ |
|
91 return;\ |
|
92 }\ |
|
93 if(!m->is_setjmp_called)\ |
|
94 {\ |
|
95 if(setjmp(m->buf) > 0)\ |
|
96 {\ |
|
97 m->is_setjmp_called = FALSE;\ |
|
98 _destroCleanUpStack(&(m->stack));\ |
|
99 return ;\ |
|
100 }\ |
|
101 m->is_setjmp_called = TRUE;\ |
|
102 did_i_set = TRUE;\ |
|
103 }\ |
|
104 } |
|
105 |
|
106 #define SET_LOW_MEMORY_TRAP(failure_value) \ |
|
107 gboolean did_i_set = FALSE;\ |
|
108 {\ |
|
109 mem_info *m = _get_thread_specific_data();\ |
|
110 if(m == NULL)\ |
|
111 {\ |
|
112 m = (mem_info *)_pAlloc(sizeof(mem_info));\ |
|
113 if(!m)\ |
|
114 return failure_value;\ |
|
115 m->is_setjmp_called = FALSE;\ |
|
116 _clearCleanUpStack(&(m->stack));\ |
|
117 if(_set_thread_specific_data(m))\ |
|
118 return failure_value;\ |
|
119 }\ |
|
120 if(!m->is_setjmp_called)\ |
|
121 {\ |
|
122 if(setjmp(m->buf) > 0)\ |
|
123 {\ |
|
124 m->is_setjmp_called = FALSE;\ |
|
125 _destroCleanUpStack(&(m->stack));\ |
|
126 return failure_value;\ |
|
127 }\ |
|
128 m->is_setjmp_called = TRUE;\ |
|
129 did_i_set = TRUE;\ |
|
130 }\ |
|
131 } |
|
132 |
|
133 |
|
134 #define REMOVE_LOW_MEMORY_TRAP() {\ |
|
135 if(did_i_set)\ |
|
136 {\ |
|
137 mem_info *m = _get_thread_specific_data();\ |
|
138 if(m)\ |
|
139 m->is_setjmp_called = FALSE;\ |
|
140 _clearCleanUpStack(&(m->stack));\ |
|
141 }\ |
|
142 } |
|
143 |
|
144 #endif /* __G_LOW_MEM_H__ */ |