4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * @desc general macros
27 #ifndef _RESOURCED_MACRO_H_
28 #define _RESOURCED_MACRO_H_
30 #define execute_once \
31 static int __func__##guardian; \
33 __func__##guardian == 0; \
34 __func__##guardian = 1)
40 #define API __attribute__((visibility("default")))
42 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
44 #define DECLARE_WRAPPER(fn_name, inner_fn) \
45 static void fn_name(void *data, void __attribute__((__unused__)) *not_used) \
47 return inner_fn(data); \
50 #define UNUSED __attribute__((__unused__))
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
56 * One byte digit has 3 position in decimal representation
60 * >8 - compile time error
61 * plus 1 null termination byte
62 * plus 1 for negative prefix
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)])))
71 #define ret_msg_if(expr, fmt, arg...) do { \
78 #define ret_value_if(expr, val) do { \
80 _E("(%s) -> %s():%d return", #expr, __FUNCTION__, __LINE__); \
85 #define ret_value_msg_if(expr, val, fmt, arg...) do { \
92 #define ret_value_secure_msg_if(expr, val, fmt, arg...) do { \
99 #define ret_value_errno_msg_if(expr, val, fmt, arg...) do { \
101 ETRACE_ERRNO_MSG(fmt, ##arg); \
107 * @brief Copy from source to destination
108 * destination should not be on heap.
109 * Destination will be null terminated
111 #define STRING_SAVE_COPY(destination, source) \
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'; \
120 /* FIXME: Do we really need pointers? */
121 #define array_foreach(key, type, array) \
122 guint _array_foreach_index; \
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)
129 #define slist_foreach(key, type, list) \
131 GSList *_slist_foreach_copy_list = list; \
133 _slist_foreach_copy_list && \
134 ((key = _slist_foreach_copy_list->data) || 1); \
135 _slist_foreach_copy_list = _slist_foreach_copy_list->next)
137 #define gslist_for_each_item(item, list) \
138 for(item = list; item != NULL; item = g_slist_next(item))
140 #define gslist_for_each(head, elem, node) \
141 for (elem = head, node = NULL; elem && ((node = elem->data) != NULL); elem = elem->next, node = NULL)
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)
148 #define DB_ACTION(command) do { \
149 if ((command) != SQLITE_OK) { \
150 error_code = RESOURCED_ERROR_DB_FAILED; \
155 #define MODULE_REGISTER(module) \
156 static void __attribute__ ((constructor)) module_init(void) \
158 add_module(module); \
160 static void __attribute__ ((destructor)) module_exit(void) \
162 remove_module(module); \
165 #endif /* _RESOURCED_MACRO_H_ */