00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __G_LOW_MEM_H__
00020 #define __G_LOW_MEM_H__
00021
00022 #include <unistd.h>
00023 #include <setjmp.h>
00024
00025
00026 typedef struct _cleanUpStack cleanUpStack ;
00027 typedef struct _mem_info mem_info ;
00028
00029 struct _cleanUpStack
00030 {
00031 void *ptr[1000];
00032 int top;
00033 };
00034
00035 struct _mem_info
00036 {
00037 int is_setjmp_called;
00038 jmp_buf buf;
00039 cleanUpStack stack;
00040 };
00041
00042
00043
00044 #ifdef __cplusplus
00045 extern "C"
00046 {
00047 #endif
00048
00049 IMPORT_C mem_info * _get_thread_specific_data();
00050 IMPORT_C int _set_thread_specific_data(mem_info *m);
00051 IMPORT_C int _push(cleanUpStack *cs,void *ptr);
00052 IMPORT_C void *_pop(cleanUpStack *cs);
00053 IMPORT_C void _findAndDestroy(cleanUpStack *cs,void *ptr);
00054 IMPORT_C void _destroCleanUpStack(cleanUpStack *cs);
00055 IMPORT_C void _clearCleanUpStack(cleanUpStack *cs);
00056 IMPORT_C void * _pAlloc(size_t size);
00057 IMPORT_C void _dummy1();
00058 IMPORT_C void _dummy2();
00059 IMPORT_C void _dummy3();
00060
00061 #ifdef __cplusplus
00062 }
00063 #endif
00064
00065
00066
00067 #define SET_LOW_MEMORY_TRAP_VOID() \
00068 gboolean did_i_set = FALSE;\
00069 {\
00070 mem_info *m = _get_thread_specific_data();\
00071 if(m == NULL)\
00072 {\
00073 m = (mem_info *)_pAlloc(sizeof(mem_info));\
00074 if(!m)\
00075 return;\
00076 m->is_setjmp_called = FALSE;\
00077 _clearCleanUpStack(&(m->stack));\
00078 if(_set_thread_specific_data(m))\
00079 return;\
00080 }\
00081 if(!m->is_setjmp_called)\
00082 {\
00083 if(setjmp(m->buf) > 0)\
00084 {\
00085 m->is_setjmp_called = FALSE;\
00086 _destroCleanUpStack(&(m->stack));\
00087 return ;\
00088 }\
00089 m->is_setjmp_called = TRUE;\
00090 did_i_set = TRUE;\
00091 }\
00092 }
00093
00094 #define SET_LOW_MEMORY_TRAP(failure_value) \
00095 gboolean did_i_set = FALSE;\
00096 {\
00097 mem_info *m = _get_thread_specific_data();\
00098 if(m == NULL)\
00099 {\
00100 m = (mem_info *)_pAlloc(sizeof(mem_info));\
00101 if(!m)\
00102 return failure_value;\
00103 m->is_setjmp_called = FALSE;\
00104 _clearCleanUpStack(&(m->stack));\
00105 if(_set_thread_specific_data(m))\
00106 return failure_value;\
00107 }\
00108 if(!m->is_setjmp_called)\
00109 {\
00110 if(setjmp(m->buf) > 0)\
00111 {\
00112 m->is_setjmp_called = FALSE;\
00113 _destroCleanUpStack(&(m->stack));\
00114 return failure_value;\
00115 }\
00116 m->is_setjmp_called = TRUE;\
00117 did_i_set = TRUE;\
00118 }\
00119 }
00120
00121
00122 #define REMOVE_LOW_MEMORY_TRAP() {\
00123 if(did_i_set)\
00124 {\
00125 mem_info *m = _get_thread_specific_data();\
00126 if(m)\
00127 m->is_setjmp_called = FALSE;\
00128 _clearCleanUpStack(&(m->stack));\
00129 }\
00130 }
00131
00132 #endif