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)
39 #define ST_BYTES_TO_OOPS(m) ((m) / sizeof (st_oop))
40 #define ST_OOPS_TO_BYTES(m) ((m) * sizeof (st_oop))
41 #define ST_ROUNDED_UP_OOPS(m) (ST_BYTES_TO_OOPS ((m) + ST_OOPS_TO_BYTES (1) - 1))
43 #define ST_N_ELEMENTS(static_array) (sizeof(static_array) / sizeof ((static_array) [0]))
45 #define ST_SIZE_OOPS(type) (sizeof (type) / sizeof (st_oop))
47 #define ST_DIR_SEPARATOR '/'
48 #define ST_DIR_SEPARATOR_S "/"
51 #define ST_GNUC_CONST __attribute__ ((const))
52 #define ST_GNUC_PURE __attribute__ ((pure))
53 #define ST_GNUC_MALLOC __attribute__ ((malloc))
54 #define ST_GNUC_PRINTF(format_index, argument_index) __attribute__ ((format (printf, format_index, argument_index)))
58 #define ST_GNUC_MALLOC
59 #define ST_GNUC_PRINTF(format_index, argument_index)
62 /* A compile-time assertion */
63 #define assert_static(e) \
65 enum { assert_static__ = 1/(e) }; \
68 #define streq(a,b) (strcmp ((a),(b)) == 0)
71 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
75 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
79 #define CLAMP(a,low,high) (((a) < (low)) ? (low) : (((a) > (high)) ? (high) : (a)))
84 st_tag_mask
= ST_NTH_MASK (2),
88 st_oops_copy (st_oop
*to
, st_oop
*from
, st_uint count
)
90 memcpy (to
, from
, sizeof (st_oop
) * count
);
94 st_oops_move (st_oop
*to
, st_oop
*from
, st_uint count
)
96 memmove (to
, from
, sizeof (st_oop
) * count
);
99 st_pointer
st_malloc (size_t size
) ST_GNUC_MALLOC
;
100 st_pointer
st_malloc0 (size_t size
) ST_GNUC_MALLOC
;
101 st_pointer
st_realloc (st_pointer mem
, size_t size
);
103 void st_free (st_pointer mem
);
105 #define st_new(struct_type) ((struct_type *) st_malloc (sizeof (struct_type)))
106 #define st_new0(struct_type) ((struct_type *) st_malloc0 (sizeof (struct_type)))
108 bool st_file_get_contents (const char *filename
,
111 char *st_strdup (const char *string
);
112 char *st_strdup_printf (const char *format
, ...) ST_GNUC_PRINTF (1, 2);
113 char *st_strdup_vprintf (const char *format
, va_list args
);
114 char *st_strconcat (const char *first
, ...);
116 char ** st_strsplit (const char *string
, const char *delimiter
, int max_tokens
);
117 void st_strfreev (char **str_array
);
119 char *st_strndup (const char *str
, size_t n
);
122 double st_timespec_to_double_seconds (struct timespec
*t
);
124 void st_timespec_difference (struct timespec
*start
, struct timespec
*end
, struct timespec
*diff
);
126 void st_timespec_add (struct timespec
*t1
, struct timespec
*t2
, struct timespec
*result
);
128 typedef struct st_list st_list
;
136 typedef void (* st_list_foreach_func
) (st_pointer data
);
138 st_list
*st_list_append (st_list
*list
, st_pointer data
);
139 st_list
*st_list_prepend (st_list
*list
, st_pointer data
);
140 st_list
*st_list_concat (st_list
*list1
, st_list
*list2
);
141 void st_list_foreach (st_list
*list
, st_list_foreach_func func
);
142 st_list
*st_list_reverse (st_list
*list
);
143 st_uint
st_list_length (st_list
*list
);
144 void st_list_destroy (st_list
*list
);
146 st_uint
st_string_hash (const char *string
);
148 #if defined(__GNUC__) && defined(__OPTIMIZE__)
149 #define ST_LIKELY(condition) __builtin_expect (!!(condition), 1)
150 #define ST_UNLIKELY(condition) __builtin_expect (!!(condition), 0)
152 #define ST_LIKELY(condition) condition
153 #define ST_UNLIKELY(condition) condition
157 #define ST_STMT_START ({
158 #define ST_STMT_END })
160 #define ST_STMT_START do {
161 #define ST_STMT_END } while (0)
165 #define st_assert(condition)
167 #define st_assert(condition) \
169 if (!(condition)) { \
170 fprintf (stderr, "%s:%i: %s: assertion `" #condition "' failed\n", \
171 __FILE__, __LINE__, __FUNCTION__); \
178 #define st_assert_not_reached()
180 #define st_assert_not_reached() \
182 fprintf (stderr, "%s:%i: %s: should not reach here\n", \
183 __FILE__, __LINE__, __FUNCTION__); \
188 void st_log (const char * restrict domain
, const char * restrict format
, ...);
190 #endif /* __ST_UTILS_H__ */