4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #ifndef __ST_UTILS_H__
26 #define __ST_UTILS_H__
36 #define ST_NTH_BIT(n) (1 << (n))
37 #define ST_NTH_MASK(n) (ST_NTH_BIT(n) - 1)
40 #define ST_ROUNDING_MASK 0x7
42 #define ST_ROUNDING_MASK 0x3
45 #define ST_BYTES_TO_OOPS(m) ((m) / sizeof (st_oop))
46 #define ST_OOPS_TO_BYTES(m) ((m) * sizeof (st_oop))
48 #define ST_ROUND_BYTES(size) (((size) ^ ((size) & ST_ROUNDING_MASK)) + (!!((size) & ST_ROUNDING_MASK) * sizeof (st_pointer)))
50 #define ST_ROUNDED_UP_OOPS(m) (ST_BYTES_TO_OOPS ((m) + ST_OOPS_TO_BYTES (1) - 1))
52 #define ST_N_ELEMENTS(static_array) (sizeof(static_array) / sizeof ((static_array) [0]))
54 #define ST_DIR_SEPARATOR '/'
55 #define ST_DIR_SEPARATOR_S "/"
58 #define ST_GNUC_CONST __attribute__ ((const))
59 #define ST_GNUC_PURE __attribute__ ((pure))
60 #define ST_GNUC_MALLOC __attribute__ ((malloc))
61 #define ST_GNUC_PRINTF(format_index, argument_index) __attribute__ ((format (printf, format_index, argument_index)))
65 #define ST_GNUC_MALLOC
66 #define ST_GNUC_PRINTF(format_index, argument_index)
69 /* A comile-time assertion */
70 #define assert_static(e) \
72 enum { assert_static__ = 1/(e) }; \
75 #define streq(a,b) (strcmp ((a),(b)) == 0)
77 /* returns the size of a type, in oop's */
78 #define ST_TYPE_SIZE(type) (sizeof (type) / sizeof (st_oop))
81 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
85 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
89 #define CLAMP(a,low,high) (((a) < (low)) ? (low) : (((a) > (high)) ? (high) : (a)))
94 st_tag_mask
= ST_NTH_MASK (2),
98 st_oops_copy (st_oop
*to
, st_oop
*from
, st_uint count
)
100 memcpy (to
, from
, sizeof (st_oop
) * count
);
104 st_oops_move (st_oop
*to
, st_oop
*from
, st_uint count
)
106 memmove (to
, from
, sizeof (st_oop
) * count
);
109 st_pointer
st_malloc (size_t size
) ST_GNUC_MALLOC
;
110 st_pointer
st_malloc0 (size_t size
) ST_GNUC_MALLOC
;
111 st_pointer
st_realloc (st_pointer mem
, size_t size
);
113 void st_free (st_pointer mem
);
115 #define st_new(struct_type) ((struct_type *) st_malloc (sizeof (struct_type)))
116 #define st_new0(struct_type) ((struct_type *) st_malloc0 (sizeof (struct_type)))
118 bool st_file_get_contents (const char *filename
,
121 char *st_strdup (const char *string
);
122 char *st_strdup_printf (const char *format
, ...) ST_GNUC_PRINTF (1, 2);
123 char *st_strdup_vprintf (const char *format
, va_list args
);
124 char *st_strconcat (const char *first
, ...);
126 char ** st_strsplit (const char *string
, const char *delimiter
, int max_tokens
);
127 void st_strfreev (char **str_array
);
129 char *st_strndup (const char *str
, size_t n
);
132 double st_timespec_to_double_seconds (struct timespec
*t
);
134 void st_timespec_difference (struct timespec
*start
, struct timespec
*end
, struct timespec
*diff
);
136 void st_timespec_add (struct timespec
*t1
, struct timespec
*t2
, struct timespec
*result
);
141 typedef struct st_list st_list
;
149 typedef void (* st_list_foreach_func
) (st_pointer data
);
151 st_list
*st_list_append (st_list
*list
, st_pointer data
);
152 st_list
*st_list_prepend (st_list
*list
, st_pointer data
);
153 st_list
*st_list_concat (st_list
*list1
, st_list
*list2
);
154 void st_list_foreach (st_list
*list
, st_list_foreach_func func
);
155 st_list
*st_list_reverse (st_list
*list
);
156 st_uint
st_list_length (st_list
*list
);
157 void st_list_destroy (st_list
*list
);
159 typedef struct st_bit_array st_bit_array
;
161 struct st_bit_array
{
167 st_bit_array
*st_bit_array_new (st_ulong size
, bool clear
);
169 st_ulong
st_bit_array_size (st_bit_array
*array
);
171 void st_bit_array_clear (st_bit_array
*array
);
173 void st_bit_array_destroy (st_bit_array
*array
);
176 st_bit_array_get (st_bit_array
*array
, st_ulong index
)
178 return (array
->data
[index
>> 3] >> (index
& 0x7)) & 1;
182 st_bit_array_set (st_bit_array
*array
, st_ulong index
)
184 array
->data
[index
>> 3] |= 1 << (index
& 0x7);
187 #if defined(__GNUC__) && defined(__OPTIMIZE__)
188 #define ST_LIKELY(condition) __builtin_expect (!!(condition), 1)
189 #define ST_UNLIKELY(condition) __builtin_expect (!!(condition), 0)
191 #define ST_LIKELY(condition) condition
192 #define ST_UNLIKELY(condition) condition
196 #define ST_STMT_START ({
197 #define ST_STMT_END })
199 #define ST_STMT_START do {
200 #define ST_STMT_END } while (0)
204 #define st_assert(condition)
206 #define st_assert(condition) \
208 if (!(condition)) { \
209 fprintf (stderr, "%s:%i: %s: assertion `" #condition "' failed\n", \
210 __FILE__, __LINE__, __FUNCTION__); \
217 #define st_assert_not_reached()
219 #define st_assert_not_reached() \
221 fprintf (stderr, "%s:%i: %s: should not reach here\n", \
222 __FILE__, __LINE__, __FUNCTION__); \
227 #endif /* __ST_UTILS_H__ */