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