]>
Dogcows Code - chaz/rasterize/blob - common.hh
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
21 * Define a keyword for use while defining small and fast functions.
24 #define INLINE_MAYBE static inline
26 #define INLINE_MAYBE static
31 * Define a type for scalar values, either float or double.
34 typedef double scal_t
;
37 #define scal_floor floor
38 #define scal_ceil ceil
43 #define scal_sqrt sqrt
47 #define scal_asin asin
48 #define scal_acos acos
49 #define scal_atan atan
54 #define scal_floor floorf
55 #define scal_ceil ceilf
56 #define scal_min fminf
57 #define scal_max fmaxf
58 #define scal_abs fabsf
60 #define scal_sqrt sqrtf
64 #define scal_asin asinf
65 #define scal_acos acosf
66 #define scal_atan atanf
69 #define scal_min2(A,B,C) scal_min(scal_min(A,B),C)
70 #define scal_max2(A,B,C) scal_max(scal_max(A,B),C)
72 #define SCAL_EPSILON (S(0.000001))
75 * Check if two scalars are mostly equal, given a margin of error.
78 bool scal_isequal2(scal_t a
, scal_t b
, scal_t e
)
80 return scal_abs(a
-b
) < e
;
84 * Check if two scalars are mostly equal.
87 bool scal_isequal(scal_t a
, scal_t b
)
89 return scal_isequal2(a
, b
, SCAL_EPSILON
);
93 * Clamp a scalar between two values.
96 scal_t
scal_clamp(scal_t s
, scal_t min
, scal_t max
)
109 * Define min and max functions for integers.
112 int imin(int a
, int b
)
114 return a
< b
? a
: b
;
117 int imax(int a
, int b
)
119 return a
< b
? b
: a
;
123 * Define generic MIN and MAX macros.
126 #define MIN(A,B) (((A) < (B)) ? (A) : (B))
129 #define MAX(A,B) (((A) > (B)) ? (A) : (B))
134 * Try to execute a statement and print a status message. If the statement
135 * resolves to 0, it is considered to have succeeded; any other evaluation is
136 * an error condition and an abort(3) occurs.
140 #define TRY_DO(L, K, ARGS...) \
141 printf("* " L "... ", ##ARGS); \
143 if ((K) == 0) printf("done!\n"); \
146 #define TRY_DO(L, K, ARGS...) \
147 if ((K) != 0) abort()
152 * Declare a type of destructor functions.
154 typedef void (*dtor_t
)(void*);
155 #define DTOR(A) (dtor_t)(A)
159 * Allocate a block of memory of a certain size. This follows the semantics
160 * of malloc(3), except it will never return NULL and will abort(3) if the
161 * memory could not be allocated.
163 void* mem_alloc(size_t size
);
166 * Change the size of a block of memory. This follows the semantics of
167 * realloc(3), except it will never return NULL and will abort(3) if the
168 * memory could not be allocated.
170 void* mem_realloc(void* mem
, size_t size
);
173 * Deallocate a block of memory previously allocated by mem_alloc or malloc(3)
174 * and friends. This is essentially just a call to free(3).
176 void mem_free(void* mem
);
179 * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if
180 * no callback should be called. The callback takes the same arguments as
181 * realloc(3) and may try to fulfill the request. The return value of the
182 * callback function will be returned from the allocation function and must be
183 * a valid pointer to an allocated block of memory. The callback function
184 * should not call mem_alloc or mem_realloc and must not return if a block of
185 * memory could not be allocated.
187 void mem_set_fn(void* (*fn
)(void*, size_t));
190 * Get the number of blocks currently allocated with either mem_alloc or
191 * mem_realloc. This number should be zero at the end of a process running
198 * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
199 * The result needs to be freed with mem_free.
202 char* mem_strdup(const char* str
)
204 size_t size
= strlen(str
) + 1;
205 return (char*)memcpy(mem_alloc(size
), str
, size
);
209 * Get an allocated string made up of two strings concatenated together.
210 * The result needs to be freed with mem_free.
213 char* mem_strcat(const char* str1
, const char* str2
)
215 size_t size1
= strlen(str1
);
216 size_t size2
= strlen(str2
);
217 char* str
= (char*)mem_alloc(size1
+ size2
+ 1);
218 memcpy(str
+ size1
, str2
, size2
+ 1);
219 return (char*)memcpy(str
, str1
, size1
);
224 * Cut a string short at the last matching character. The string will be
225 * modified if the character was matched. Either way, str is returned.
228 char* strcut(char* str
, int c
)
230 char* ptr
= strrchr(str
, c
);
238 * Trim white space off of the right side of a string.
240 void rtrim(char *str
);
243 * Trim white space off of the left side of a string.
245 void ltrim(char *str
);
248 * Trim white space off of both sides of a string.
251 char* trim(char *str
)
260 * Initialize the timer.
265 * Stop the timer and return how many microseconds passed after
271 #endif // _COMMON_HH_
This page took 0.051217 seconds and 4 git commands to generate.