Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / tests / plugin_resources.c
1 #include <gio/gio.h>
2
3 #if defined (__ELF__) && ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6))
4 # define SECTION __attribute__ ((section (".gresource.g_plugin"), aligned (8)))
5 #else
6 # define SECTION
7 #endif
8
9 static const SECTION union { const guint8 data[180]; const double alignment; void * const ptr;}  _g_plugin_resource_data = { {
10   0x47, 0x56, 0x61, 0x72, 0x69, 0x61, 0x6e, 0x74, 
11   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
12   0x18, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 
13   0x00, 0x00, 0x00, 0x28, 0x03, 0x00, 0x00, 0x00, 
14   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
15   0x02, 0x00, 0x00, 0x00, 0xba, 0x2c, 0xfd, 0xa6, 
16   0x02, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 
17   0x0f, 0x00, 0x4c, 0x00, 0x84, 0x00, 0x00, 0x00, 
18   0x88, 0x00, 0x00, 0x00, 0x59, 0xea, 0x29, 0x39, 
19   0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 
20   0x09, 0x00, 0x76, 0x00, 0x98, 0x00, 0x00, 0x00, 
21   0xae, 0x00, 0x00, 0x00, 0xd4, 0xb5, 0x02, 0x00, 
22   0xff, 0xff, 0xff, 0xff, 0xae, 0x00, 0x00, 0x00, 
23   0x01, 0x00, 0x4c, 0x00, 0xb0, 0x00, 0x00, 0x00, 
24   0xb4, 0x00, 0x00, 0x00, 0x72, 0x65, 0x73, 0x6f, 
25   0x75, 0x72, 0x63, 0x65, 0x70, 0x6c, 0x75, 0x67, 
26   0x69, 0x6e, 0x2f, 0x00, 0x01, 0x00, 0x00, 0x00, 
27   0x74, 0x65, 0x73, 0x74, 0x31, 0x2e, 0x74, 0x78, 
28   0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
29   0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
30   0x74, 0x65, 0x73, 0x74, 0x31, 0x0a, 0x00, 0x00, 
31   0x28, 0x75, 0x75, 0x61, 0x79, 0x29, 0x2f, 0x00, 
32   0x00, 0x00, 0x00, 0x00
33 } };
34
35 static GStaticResource static_resource = { _g_plugin_resource_data.data, sizeof (_g_plugin_resource_data.data) };
36 extern GResource *_g_plugin_get_resource (void);
37 GResource *_g_plugin_get_resource (void)
38 {
39   return g_static_resource_get_resource (&static_resource);
40 }
41 /*
42   If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
43   destructors, in a sane way, including e.g. on library unload. If not you're on
44   your own.
45
46   Some compilers need #pragma to handle this, which does not work with macros,
47   so the way you need to use this is (for constructors):
48
49   #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
50   #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
51   #endif
52   G_DEFINE_CONSTRUCTOR(my_constructor)
53   static void my_constructor(void) {
54    ...
55   }
56
57 */
58
59 #if  __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
60
61 #define G_HAS_CONSTRUCTORS 1
62
63 #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
64 #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
65
66 #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
67 /* Visual studio 2008 and later has _Pragma */
68
69 #define G_HAS_CONSTRUCTORS 1
70
71 #define G_DEFINE_CONSTRUCTOR(_func) \
72   static void _func(void); \
73   static int _func ## _wrapper(void) { _func(); return 0; } \
74   __pragma(section(".CRT$XCU",read)) \
75   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
76
77 #define G_DEFINE_DESTRUCTOR(_func) \
78   static void _func(void); \
79   static int _func ## _constructor(void) { atexit (_func); return 0; } \
80   __pragma(section(".CRT$XCU",read)) \
81   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
82
83 #elif defined (_MSC_VER)
84
85 #define G_HAS_CONSTRUCTORS 1
86
87 /* Pre Visual studio 2008 must use #pragma section */
88 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
89 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
90
91 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
92   section(".CRT$XCU",read)
93 #define G_DEFINE_CONSTRUCTOR(_func) \
94   static void _func(void); \
95   static int _func ## _wrapper(void) { _func(); return 0; } \
96   __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
97
98 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
99   section(".CRT$XCU",read)
100 #define G_DEFINE_DESTRUCTOR(_func) \
101   static void _func(void); \
102   static int _func ## _constructor(void) { atexit (_func); return 0; } \
103   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
104
105 #elif defined(__SUNPRO_C)
106
107 /* This is not tested, but i believe it should work, based on:
108  * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
109  */
110
111 #define G_HAS_CONSTRUCTORS 1
112
113 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
114 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
115
116 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
117   init(_func)
118 #define G_DEFINE_CONSTRUCTOR(_func) \
119   static void _func(void);
120
121 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
122   fini(_func)
123 #define G_DEFINE_DESTRUCTOR(_func) \
124   static void _func(void);
125
126 #else
127
128 /* constructors not supported for this compiler */
129
130 #endif
131
132
133 #ifdef G_HAS_CONSTRUCTORS
134
135 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
136 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(resource_constructor)
137 #endif
138 G_DEFINE_CONSTRUCTOR(resource_constructor)
139 #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
140 #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(resource_destructor)
141 #endif
142 G_DEFINE_DESTRUCTOR(resource_destructor)
143
144 #else
145 #warning "Constructor not supported on this compiler, linking in resources will not work"
146 #endif
147
148 static void resource_constructor (void)
149 {
150   g_static_resource_init (&static_resource);
151 }
152
153 static void resource_destructor (void)
154 {
155   g_static_resource_fini (&static_resource);
156 }