tizen 2.3 release
[kernel/api/system-resource.git] / src / common / macro.h
1 /*
2  * resourced
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /*
21  *  @file: macro.h
22  *
23  *  @desc general macros
24  */
25
26
27 #ifndef _RESOURCED_MACRO_H_
28 #define _RESOURCED_MACRO_H_
29
30 #define execute_once \
31         static int __func__##guardian;                                   \
32         for (; \
33         __func__##guardian == 0; \
34         __func__##guardian = 1)
35
36 #include <assert.h>
37 #include <stdio.h>
38 #include <config.h>
39
40 #define API __attribute__((visibility("default")))
41
42 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
43
44 #define DECLARE_WRAPPER(fn_name, inner_fn) \
45 static void fn_name(void *data, void __attribute__((__unused__)) *not_used) \
46 {\
47         return inner_fn(data);                        \
48 }
49
50 #define UNUSED __attribute__((__unused__))
51
52 #define MAX_SIZE2(a, b) sizeof(a) + sizeof(b) - 1
53 #define MAX_SIZE3(a, b, c) MAX_SIZE2(a, b) + sizeof(c) - 1
54
55 /*
56  * One byte digit has 3 position in decimal representation
57  * 2 - 5
58  * 4 - 10
59  * 8 - 20
60  * >8 - compile time error
61  * plus 1 null termination byte
62  * plus 1 for negative prefix
63  */
64 #define MAX_DEC_SIZE(type) \
65         (2 + (sizeof(type) <= 1 ? 3 : \
66         sizeof(type) <= 2 ? 5 : \
67         sizeof(type) <= 4 ? 10 : \
68         sizeof(type) <= 8 ? 20 : \
69         sizeof(int[-2*(sizeof(type) > 8)])))
70
71 #define ret_msg_if(expr, fmt, arg...) do { \
72         if (expr) { \
73                 _E(fmt, ##arg);                 \
74                 return; \
75         } \
76 } while (0)
77
78 #define ret_value_if(expr, val) do { \
79         if (expr) { \
80                 _E("(%s) -> %s():%d return", #expr, __FUNCTION__, __LINE__); \
81                 return (val); \
82         } \
83 } while (0)
84
85 #define ret_value_msg_if(expr, val, fmt, arg...) do {   \
86         if (expr) {                             \
87                 _E(fmt, ##arg);                 \
88                 return val;                     \
89         }                                       \
90 } while (0)
91
92 #define ret_value_secure_msg_if(expr, val, fmt, arg...) do {    \
93                 if (expr) {                     \
94                         _SE(fmt, ##arg);                \
95                         return val;             \
96                 }                                       \
97         } while (0)
98
99 #define ret_value_errno_msg_if(expr, val, fmt, arg...) do {     \
100         if (expr) {                                     \
101                 ETRACE_ERRNO_MSG(fmt, ##arg);           \
102                 return val;                             \
103         }                                               \
104 } while (0)
105
106 /*
107  * @brief Copy from source to destination
108  * destination should not be on heap.
109  * Destination will be null terminated
110  */
111 #define STRING_SAVE_COPY(destination, source) \
112         do { \
113                 size_t null_pos = strlen(source); \
114                 strncpy(destination, source, sizeof(destination)); \
115                 null_pos = sizeof(destination) - 1 < null_pos ? \
116                         sizeof(destination) - 1 : null_pos; \
117                 destination[null_pos] = '\0'; \
118         } while(0)
119
120 /* FIXME: Do we really need pointers? */
121 #define array_foreach(key, type, array)                                 \
122         guint _array_foreach_index;                                             \
123         type *key;                                                            \
124         for (_array_foreach_index = 0;                                         \
125                 array && _array_foreach_index < array->len && \
126                 (key = &g_array_index(array, type, _array_foreach_index)); \
127                 ++_array_foreach_index)
128
129 #define slist_foreach(key, type, list)                       \
130         type *key;                                                 \
131         GSList *_slist_foreach_copy_list = list;                               \
132         for (;                                                                \
133         _slist_foreach_copy_list && \
134         ((key = _slist_foreach_copy_list->data) || 1); \
135         _slist_foreach_copy_list = _slist_foreach_copy_list->next)
136
137 #define gslist_for_each_item(item, list)                       \
138         for(item = list; item != NULL; item = g_slist_next(item))
139
140 #define gslist_for_each(head, elem, node)       \
141         for (elem = head, node = NULL; elem && ((node = elem->data) != NULL); elem = elem->next, node = NULL)
142
143 #define gslist_for_each_safe(head, elem, elem_next, node) \
144         for (elem = head, elem_next = g_list_next(elem), node = NULL; \
145                         elem && ((node = elem->data) != NULL); \
146                         elem = elem_next, elem_next = g_list_next(elem), node = NULL)
147
148 #define DB_ACTION(command) do {                         \
149         if ((command) != SQLITE_OK) {                   \
150                 error_code = RESOURCED_ERROR_DB_FAILED; \
151                 goto handle_error;                      \
152         }                                               \
153 } while (0)
154
155 #define MODULE_REGISTER(module)                                         \
156         static void __attribute__ ((constructor)) module_init(void)     \
157         {                                                               \
158                 add_module(module);                                     \
159         }                                                               \
160         static void __attribute__ ((destructor)) module_exit(void)      \
161         {                                                               \
162                 remove_module(module);                                  \
163         }
164
165 #endif  /* _RESOURCED_MACRO_H_ */