]>
Dogcows Code - chaz/rasterize/blob - common.h
ec7913ea4b13853d1880611bf02e877348f476d8
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
22 * Define a keyword for use while defining small and fast functions.
25 #define INLINE_MAYBE static inline
27 #define INLINE_MAYBE static
32 * Define a type for scalar values, either float or double.
35 typedef double scal_t
;
38 #define scal_floor floor
39 #define scal_ceil ceil
44 #define scal_sqrt sqrt
48 #define scal_asin asin
49 #define scal_acos acos
50 #define scal_atan atan
55 #define scal_floor floorf
56 #define scal_ceil ceilf
57 #define scal_min fminf
58 #define scal_max fmaxf
59 #define scal_abs fabsf
61 #define scal_sqrt sqrtf
65 #define scal_asin asinf
66 #define scal_acos acosf
67 #define scal_atan atanf
70 #define scal_min2(A,B,C) scal_min(scal_min(A,B),C)
71 #define scal_max2(A,B,C) scal_max(scal_max(A,B),C)
73 #define SCAL_EPSILON (S(0.000001))
76 * Check if two scalars are mostly equal, given a margin of error.
79 bool scal_isequal2(scal_t a
, scal_t b
, scal_t e
)
81 return scal_abs(a
-b
) < e
;
85 * Check if two scalars are mostly equal.
88 bool scal_isequal(scal_t a
, scal_t b
)
90 return scal_isequal2(a
, b
, SCAL_EPSILON
);
94 * Clamp a scalar between two values.
97 scal_t
scal_clamp(scal_t s
, scal_t min
, scal_t max
)
110 * Define min and max functions for integers.
113 int imin(int a
, int b
)
115 return a
< b
? a
: b
;
118 int imax(int a
, int b
)
120 return a
< b
? b
: a
;
124 * Define generic MIN and MAX macros.
127 #define MIN(A,B) (((A) < (B)) ? (A) : (B))
130 #define MAX(A,B) (((A) > (B)) ? (A) : (B))
135 * Try to execute a statement and print a status message. If the statement
136 * resolves to 0, it is considered to have succeeded; any other evaluation is
137 * an error condition and an abort(3) occurs.
141 #define TRY_DO(L, K, ARGS...) \
142 printf("* " L "... ", ##ARGS); \
144 if ((K) == 0) printf("done!\n"); \
147 #define TRY_DO(L, K, ARGS...) \
148 if ((K) != 0) abort()
153 * Declare a type of destructor functions.
155 typedef void (*dtor_t
)(void*);
156 #define DTOR(A) (dtor_t)(A)
160 * Allocate a block of memory of a certain size. This follows the semantics
161 * of malloc(3), except it will never return NULL and will abort(3) if the
162 * memory could not be allocated.
164 void* mem_alloc(size_t size
);
167 * Change the size of a block of memory. This follows the semantics of
168 * realloc(3), except it will never return NULL and will abort(3) if the
169 * memory could not be allocated.
171 void* mem_realloc(void* mem
, size_t size
);
174 * Deallocate a block of memory previously allocated by mem_alloc or malloc(3)
175 * and friends. This is essentially just a call to free(3).
177 void mem_free(void* mem
);
180 * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if
181 * no callback should be called. The callback takes the same arguments as
182 * realloc(3) and may try to fulfill the request. The return value of the
183 * callback function will be returned from the allocation function and must be
184 * a valid pointer to an allocated block of memory. The callback function
185 * should not call mem_alloc or mem_realloc and must not return if a block of
186 * memory could not be allocated.
188 void mem_set_fn(void* (*fn
)(void*, size_t));
191 * Get the number of blocks currently allocated with either mem_alloc or
192 * mem_realloc. This number should be zero at the end of a process running
199 * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
200 * The result needs to be freed with mem_free.
203 char* mem_strdup(const char* str
)
205 size_t size
= strlen(str
) + 1;
206 return (char*)memcpy(mem_alloc(size
), str
, size
);
210 * Get an allocated string made up of two strings concatenated together.
211 * The result needs to be freed with mem_free.
214 char* mem_strcat(const char* str1
, const char* str2
)
216 size_t size1
= strlen(str1
);
217 size_t size2
= strlen(str2
);
218 char* str
= (char*)mem_alloc(size1
+ size2
+ 1);
219 memcpy(str
+ size1
, str2
, size2
+ 1);
220 return memcpy(str
, str1
, size1
);
225 * Cut a string short at the last matching character. The string will be
226 * modified if the character was matched. Either way, str is returned.
229 char* strcut(char* str
, int c
)
231 char* ptr
= strrchr(str
, c
);
239 * Trim white space off of the right side of a string.
241 void rtrim(char *str
);
244 * Trim white space off of the left side of a string.
246 void ltrim(char *str
);
249 * Trim white space off of both sides of a string.
252 char* trim(char *str
)
261 * Initialize the timer.
266 * Stop the timer and return how many microseconds passed after
This page took 0.048818 seconds and 3 git commands to generate.