tizen 2.3.1 release
[external/buxton.git] / src / shared / util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25     #include "config.h"
26 #endif
27
28 #include <alloca.h>
29 #include <inttypes.h>
30 #include <string.h>
31 #include <time.h>
32 #include <sys/time.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <signal.h>
37 #include <sched.h>
38 #include <limits.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <dirent.h>
42 #include <sys/resource.h>
43 #include <stddef.h>
44 #include <unistd.h>
45 #include <locale.h>
46
47 #include "macro.h"
48 #include "buxton.h"
49 #include "buxtonkey.h"
50 #include "backend.h"
51
52 size_t page_size(void);
53 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
54
55 #define buxton_string_pack(s) ((BuxtonString){(s), (uint32_t)strlen(s) + 1})
56
57 #define streq(a,b) (strcmp((a),(b)) == 0)
58 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
59 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
60 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
61
62 bool streq_ptr(const char *a, const char *b) _pure_;
63
64 static inline void freep(void *p)
65 {
66         free(*(void**) p);
67 }
68
69 static inline void free_buxton_data(void *p)
70 {
71         BuxtonData *s = (*(void**) p);
72         if (s && s->type == STRING) {
73                 free(s->store.d_string.value);
74         }
75         free(s);
76 }
77 static inline void free_buxton_string(void *p)
78 {
79         BuxtonString *s = (*(void**) p);
80         if (s) {
81                 free(s->value);
82         }
83         free(s);
84 }
85
86 static inline void free_buxton_key(void *p)
87 {
88         _BuxtonKey *k = (*(void**) p);
89         if (k) {
90                 if (k->group.value) {
91                         free(k->group.value);
92                 }
93                 if (k->name.value) {
94                         free(k->name.value);
95                 }
96                 if (k->layer.value) {
97                         free(k->layer.value);
98                 }
99         }
100         free(k);
101 }
102
103 #define _cleanup_free_ _cleanup_(freep)
104 #define _cleanup_buxton_data_ _cleanup_(free_buxton_data)
105 #define _cleanup_buxton_string_ _cleanup_(free_buxton_string)
106 #define _cleanup_buxton_key_ _cleanup_(free_buxton_key)
107
108 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
109
110 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
111
112 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
113
114 #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
115
116 #define malloc0(n) (calloc((n), 1))
117
118 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
119         if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) {
120                 return NULL;
121         }
122
123         return malloc(a * b);
124 }
125 void* greedy_realloc(void **p, size_t *allocated, size_t need);
126
127 /**
128  * Get the string representation of a BuxtonDataType
129  * @param type The BuxtonDataType to query
130  * @return A string representation of the BuxtonDataType
131  */
132 const char* buxton_type_as_string(BuxtonDataType type)
133         __attribute__((warn_unused_result));
134
135 /**
136  * Retrieve the filesystem path for the given layer
137  * @param layer The layer in question
138  * @return a string containing the filesystem path
139  */
140 char* get_layer_path(BuxtonLayer *layer)
141         __attribute__((warn_unused_result));
142
143 /**
144  * Perform a deep copy of one BuxtonData to another
145  * @param original The data being copied
146  * @param copy Pointer where original should be copied to
147  * @return A boolean indicating success or failure
148  */
149 bool buxton_data_copy(BuxtonData *original, BuxtonData *copy);
150
151 /**
152  * Perform a deep copy of one BuxtonString to another
153  * @param original The BuxtonString being copied
154  * @param copy Pointer where original should be copied to
155  */
156 bool buxton_string_copy(BuxtonString *original, BuxtonString *copy)
157         __attribute__((warn_unused_result));
158
159 /**
160  * Perform a deep copy of one _BuxtonKey to another
161  * @param original The _BuxtonKey being copied
162  * @param copy Pointer where original should be copied to
163  */
164 bool buxton_key_copy(_BuxtonKey *original, _BuxtonKey *copy)
165         __attribute__((warn_unused_result));
166
167 /**
168  * Perform a partial deep copy of a _BuxtonKey, omitting the 'name' member
169  * @param original The _BuxtonKey being copied
170  * @param group Pointer to the destination of the partial copy
171  */
172 bool buxton_copy_key_group(_BuxtonKey *original, _BuxtonKey *group)
173         __attribute__((warn_unused_result));
174
175 /**
176  * Perform a deep free of BuxtonData
177  * @param data The BuxtonData being free'd
178  */
179 void data_free(BuxtonData *data);
180
181 /**
182  * Perform a deep free of BuxtonString
183  * @param string The BuxtonString being free'd
184  */
185 void string_free(BuxtonString *string);
186
187 /**
188  * Perform a deep free of _BuxtonKey
189  * @param string The _BuxtonKey being free'd
190  */
191 void key_free(_BuxtonKey *key);
192
193 /**
194  * Get the group portion of a buxton key
195  * @param key Pointer to _BuxtonKey
196  * @return A pointer to a character string containing the key's group
197  */
198 char *get_group(_BuxtonKey *key)
199         __attribute__((warn_unused_result));
200
201 /**
202  * Get the name portion of a buxton key
203  * @param key Pointer to _BuxtonKey
204  * @return A pointer to a character string containing the key's name
205  */
206 char *get_name(_BuxtonKey *key)
207         __attribute__((warn_unused_result));
208
209 /**
210  * Get the layer portion of a buxton key
211  * @param key Pointer to _BuxtonKey
212  * @return A pointer to a character string containing the key's layer
213  */
214 char *get_layer(_BuxtonKey *key)
215         __attribute__((warn_unused_result));
216
217 /**
218  * Wrapper for nonblocking write, handles EAGAIN
219  * @param fd File descriptor to write to
220  * @param buf Buffer containing data to write
221  * @param len Length of buffer to write
222  * @return A boolean indicating the success of the operation
223  */
224 bool _write(int fd, uint8_t *buf, size_t len)
225         __attribute__((warn_unused_result));
226
227 /*
228  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
229  *
230  * Local variables:
231  * c-basic-offset: 8
232  * tab-width: 8
233  * indent-tabs-mode: t
234  * End:
235  *
236  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
237  * :indentSize=8:tabSize=8:noTabs=false:
238  */