* Fixed bug where GC got the wrong size of block context objects
[panda.git] / src / st-utils.h
blob2322effc18458431f2684ae10aa159be30fed19b
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 #if 1
40 #define ST_ROUNDING_MASK 0x7
41 #else
42 #define ST_ROUNDING_MASK 0x3
43 #endif
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 "/"
57 #ifdef __GNUC__
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)))
62 #else
63 #define ST_GNUC_CONST
64 #define ST_GNUC_PURE
65 #define ST_GNUC_MALLOC
66 #define ST_GNUC_PRINTF(format_index, argument_index)
67 #endif
69 /* A comile-time assertion */
70 #define assert_static(e) \
71 do { \
72 enum { assert_static__ = 1/(e) }; \
73 } while (0)
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))
80 #ifndef MAX
81 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
82 #endif
84 #ifndef MIN
85 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
86 #endif
88 #ifndef CLAMP
89 #define CLAMP(a,low,high) (((a) < (low)) ? (low) : (((a) > (high)) ? (high) : (a)))
90 #endif
92 enum
94 st_tag_mask = ST_NTH_MASK (2),
97 static inline void
98 st_oops_copy (st_oop *to, st_oop *from, st_uint count)
100 memcpy (to, from, sizeof (st_oop) * count);
103 static inline void
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,
119 char **buffer);
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;
143 struct st_list
145 st_pointer data;
146 st_list *next;
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 {
163 st_uchar *data;
164 st_ulong data_size;
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);
175 static inline bool
176 st_bit_array_get (st_bit_array *array, st_ulong index)
178 return (array->data[index >> 3] >> (index & 0x7)) & 1;
181 static inline void
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)
190 #else
191 #define ST_LIKELY(condition) condition
192 #define ST_UNLIKELY(condition) condition
193 #endif
195 #ifdef __GNUC__
196 #define ST_STMT_START ({
197 #define ST_STMT_END })
198 #else
199 #define ST_STMT_START do {
200 #define ST_STMT_END } while (0)
201 #endif
203 #ifndef ST_DEBUG
204 #define st_assert(condition)
205 #else
206 #define st_assert(condition) \
207 ST_STMT_START \
208 if (!(condition)) { \
209 fprintf (stderr, "%s:%i: %s: assertion `" #condition "' failed\n", \
210 __FILE__, __LINE__, __FUNCTION__); \
211 abort (); \
213 ST_STMT_END
214 #endif
216 #ifndef ST_DEBUG
217 #define st_assert_not_reached()
218 #else
219 #define st_assert_not_reached() \
220 ST_STMT_START \
221 fprintf (stderr, "%s:%i: %s: should not reach here\n", \
222 __FILE__, __LINE__, __FUNCTION__); \
223 abort (); \
224 ST_STMT_END
225 #endif
227 #endif /* __ST_UTILS_H__ */