1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
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.
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.
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/>.
39 #include <sys/types.h>
42 #include <sys/resource.h>
49 #include "buxtonkey.h"
52 size_t page_size(void);
53 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
55 #define buxton_string_pack(s) ((BuxtonString){(s), (uint32_t)strlen(s) + 1})
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)
62 bool streq_ptr(const char *a, const char *b) _pure_;
64 static inline void freep(void *p)
69 static inline void free_buxton_data(void *p)
71 BuxtonData *s = (*(void**) p);
72 if (s && s->type == STRING) {
73 free(s->store.d_string.value);
77 static inline void free_buxton_string(void *p)
79 BuxtonString *s = (*(void**) p);
86 static inline void free_buxton_key(void *p)
88 _BuxtonKey *k = (*(void**) p);
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)
108 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
110 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
112 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
114 #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
116 #define malloc0(n) (calloc((n), 1))
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)) {
123 return malloc(a * b);
125 void* greedy_realloc(void **p, size_t *allocated, size_t need);
128 * Get the string representation of a BuxtonDataType
129 * @param type The BuxtonDataType to query
130 * @return A string representation of the BuxtonDataType
132 const char* buxton_type_as_string(BuxtonDataType type)
133 __attribute__((warn_unused_result));
136 * Retrieve the filesystem path for the given layer
137 * @param layer The layer in question
138 * @return a string containing the filesystem path
140 char* get_layer_path(BuxtonLayer *layer)
141 __attribute__((warn_unused_result));
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
149 bool buxton_data_copy(BuxtonData *original, BuxtonData *copy);
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
156 bool buxton_string_copy(BuxtonString *original, BuxtonString *copy)
157 __attribute__((warn_unused_result));
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
164 bool buxton_key_copy(_BuxtonKey *original, _BuxtonKey *copy)
165 __attribute__((warn_unused_result));
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
172 bool buxton_copy_key_group(_BuxtonKey *original, _BuxtonKey *group)
173 __attribute__((warn_unused_result));
176 * Perform a deep free of BuxtonData
177 * @param data The BuxtonData being free'd
179 void data_free(BuxtonData *data);
182 * Perform a deep free of BuxtonString
183 * @param string The BuxtonString being free'd
185 void string_free(BuxtonString *string);
188 * Perform a deep free of _BuxtonKey
189 * @param string The _BuxtonKey being free'd
191 void key_free(_BuxtonKey *key);
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
198 char *get_group(_BuxtonKey *key)
199 __attribute__((warn_unused_result));
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
206 char *get_name(_BuxtonKey *key)
207 __attribute__((warn_unused_result));
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
214 char *get_layer(_BuxtonKey *key)
215 __attribute__((warn_unused_result));
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
224 bool _write(int fd, uint8_t *buf, size_t len)
225 __attribute__((warn_unused_result));
228 * Editor modelines - http://www.wireshark.org/tools/modelines.html
233 * indent-tabs-mode: t
236 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
237 * :indentSize=8:tabSize=8:noTabs=false: