Implemented a resizable heap abstraction over mmap(). Allows us to
[panda.git] / src / st-utils.h
blob13e4990be4cdad555119641148c91fbf158ff489
1 /*
2 * st-utils.h
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
22 * THE SOFTWARE.
25 #ifndef __ST_UTILS_H__
26 #define __ST_UTILS_H__
28 #include <st-types.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
35 /* bit utilities */
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 "/"
50 #ifdef __GNUC__
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)))
55 #else
56 #define ST_GNUC_CONST
57 #define ST_GNUC_PURE
58 #define ST_GNUC_MALLOC
59 #define ST_GNUC_PRINTF(format_index, argument_index)
60 #endif
62 /* A comile-time assertion */
63 #define assert_static(e) \
64 do { \
65 enum { assert_static__ = 1/(e) }; \
66 } while (0)
68 #define streq(a,b) (strcmp ((a),(b)) == 0)
70 #ifndef MAX
71 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
72 #endif
74 #ifndef MIN
75 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
76 #endif
78 #ifndef CLAMP
79 #define CLAMP(a,low,high) (((a) < (low)) ? (low) : (((a) > (high)) ? (high) : (a)))
80 #endif
82 enum
84 st_tag_mask = ST_NTH_MASK (2),
87 static inline void
88 st_oops_copy (st_oop *to, st_oop *from, st_uint count)
90 memcpy (to, from, sizeof (st_oop) * count);
93 static inline void
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,
109 char **buffer);
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);
131 typedef struct st_list st_list;
133 struct st_list
135 st_pointer data;
136 st_list *next;
139 typedef void (* st_list_foreach_func) (st_pointer data);
141 st_list *st_list_append (st_list *list, st_pointer data);
142 st_list *st_list_prepend (st_list *list, st_pointer data);
143 st_list *st_list_concat (st_list *list1, st_list *list2);
144 void st_list_foreach (st_list *list, st_list_foreach_func func);
145 st_list *st_list_reverse (st_list *list);
146 st_uint st_list_length (st_list *list);
147 void st_list_destroy (st_list *list);
149 typedef struct st_bit_array st_bit_array;
151 struct st_bit_array {
153 st_uchar *data;
154 st_ulong data_size;
157 st_bit_array *st_bit_array_new (st_ulong size, bool clear);
159 st_ulong st_bit_array_size (st_bit_array *array);
161 void st_bit_array_clear (st_bit_array *array);
163 void st_bit_array_destroy (st_bit_array *array);
165 static inline bool
166 st_bit_array_get (st_bit_array *array, st_ulong index)
168 return (array->data[index >> 3] >> (index & 0x7)) & 1;
171 static inline void
172 st_bit_array_set (st_bit_array *array, st_ulong index)
174 array->data[index >> 3] |= 1 << (index & 0x7);
177 #if defined(__GNUC__) && defined(__OPTIMIZE__)
178 #define ST_LIKELY(condition) __builtin_expect (!!(condition), 1)
179 #define ST_UNLIKELY(condition) __builtin_expect (!!(condition), 0)
180 #else
181 #define ST_LIKELY(condition) condition
182 #define ST_UNLIKELY(condition) condition
183 #endif
185 #ifdef __GNUC__
186 #define ST_STMT_START ({
187 #define ST_STMT_END })
188 #else
189 #define ST_STMT_START do {
190 #define ST_STMT_END } while (0)
191 #endif
193 #ifndef ST_DEBUG
194 #define st_assert(condition)
195 #else
196 #define st_assert(condition) \
197 ST_STMT_START \
198 if (!(condition)) { \
199 fprintf (stderr, "%s:%i: %s: assertion `" #condition "' failed\n", \
200 __FILE__, __LINE__, __FUNCTION__); \
201 abort (); \
203 ST_STMT_END
204 #endif
206 #ifndef ST_DEBUG
207 #define st_assert_not_reached()
208 #else
209 #define st_assert_not_reached() \
210 ST_STMT_START \
211 fprintf (stderr, "%s:%i: %s: should not reach here\n", \
212 __FILE__, __LINE__, __FUNCTION__); \
213 abort (); \
214 ST_STMT_END
215 #endif
217 #endif /* __ST_UTILS_H__ */