tizen 2.3 release
[framework/system/deviced.git] / src / core / common.h
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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 #ifndef __CORE_COMMON_H__
21 #define __CORE_COMMON_H__
22
23 #include <Ecore.h>
24 #include <stdio.h>
25 #include <error.h>
26 #include <stdbool.h>
27 #include <unistd.h>
28
29 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
30
31 /*
32  * One byte digit has 3 position in decimal representation
33  * 2 - 5
34  * 4 - 10
35  * 8 - 20
36  * >8 - compile time error
37  * plus 1 null termination byte
38  * plus 1 for negative prefix
39  */
40 #define MAX_DEC_SIZE(type) \
41         (2 + (sizeof(type) <= 1 ? 3 : \
42         sizeof(type) <= 2 ? 5 : \
43         sizeof(type) <= 4 ? 10 : \
44         sizeof(type) <= 8 ? 20 : \
45         sizeof(int[-2*(sizeof(type) > 8)])))
46
47 #ifndef __CONSTRUCTOR__
48 #define __CONSTRUCTOR__ __attribute__ ((constructor))
49 #endif
50
51 #ifndef __DESTRUCTOR__
52 #define __DESTRUCTOR__ __attribute__ ((destructor))
53 #endif
54
55 #ifndef __WEAK__
56 #define __WEAK__ __attribute__ ((weak))
57 #endif
58
59 #ifndef max
60 #define max(a, b)                       \
61         __extension__ ({                \
62                 typeof(a) _a = (a);     \
63                 typeof(b) _b = (b);     \
64                 _a > _b ? _a : _b;      \
65         })
66 #endif
67
68 #ifndef min
69 #define min(a, b)                       \
70         __extension__ ({                \
71                 typeof(a) _a = (a);     \
72                 typeof(b) _b = (b);     \
73                 _a < _b ? _a : _b;      \
74         })
75 #endif
76
77 #ifndef clamp
78 #define clamp(x, low, high)                                             \
79         __extension__ ({                                                \
80                 typeof(x) _x = (x);                                     \
81                 typeof(low) _low = (low);                               \
82                 typeof(high) _high = (high);                            \
83                 ((_x > _high) ? _high : ((_x < _low) ? _low : _x));     \
84         })
85 #endif
86
87 #ifndef SEC_TO_MSEC
88 #define SEC_TO_MSEC(x)          ((x)*1000)
89 #endif
90 #ifndef NSEC_TO_MSEC
91 #define NSEC_TO_MSEC(x)         ((double)x/1000000)
92 #endif
93 #ifndef USEC_TO_MSEC
94 #define USEC_TO_MSEC(x)         ((double)x/1000)
95 #endif
96
97 #ifndef safe_free
98 #define safe_free(x) safe_free_memory((void**)&(x))
99 #endif
100
101 static inline void safe_free_memory(void** mem)
102 {
103         if (mem && *mem) {
104                 free(*mem);
105                 *mem = NULL;
106         }
107 }
108
109 #define ret_value_if(expr, val) do { \
110         if (expr) { \
111                 _E("(%s)", #expr); \
112                 return (val); \
113         } \
114 } while (0)
115
116 #define ret_value_msg_if(expr, val, fmt, arg...) do {   \
117         if (expr) {                             \
118                 _E(fmt, ##arg);                 \
119                 return val;                     \
120         }                                       \
121 } while (0)
122
123 #define ret_msg_if(expr, fmt, arg...) do {      \
124         if (expr) {                             \
125                 _E(fmt, ##arg);                 \
126                 return;                 \
127         }                                       \
128 } while (0)
129
130 FILE * open_proc_oom_score_adj_file(int pid, const char *mode);
131 int get_exec_pid(const char *execpath);
132 int get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size);
133 int is_vip(int pid);
134 int run_child(int argc, const char *argv[]);
135 int remove_dir(const char *path, int del_dir);
136 int sys_get_int(char *fname, int *val);
137 int sys_set_int(char *fname, int val);
138 int terminate_process(char* partition, bool force);
139 int mount_check(const char* path);
140 void print_time(const char *prefix);
141
142 #endif  /* __CORE_COMMON_H__ */
143