]>
Dogcows Code - chaz/rasterize/blob - common.h
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
20 * Define a keyword for use while defining small and fast functions.
23 #define INLINE_MAYBE static inline
25 #define INLINE_MAYBE static
30 * Define a type for scalar values, either float or double.
33 typedef double scal_t
;
36 #define scal_floor floor
37 #define scal_ceil ceil
42 #define scal_sqrt sqrt
46 #define scal_asin asin
47 #define scal_acos acos
48 #define scal_atan atan
53 #define scal_floor floorf
54 #define scal_ceil ceilf
55 #define scal_min fminf
56 #define scal_max fmaxf
57 #define scal_abs fabsf
59 #define scal_sqrt sqrtf
63 #define scal_asin asinf
64 #define scal_acos acosf
65 #define scal_atan atanf
68 #define scal_min2(A,B,C) scal_min(scal_min(A,B),C)
69 #define scal_max2(A,B,C) scal_max(scal_max(A,B),C)
71 #define SCAL_EPSILON (S(0.000001))
74 * Check if two scalars are mostly equal, given a margin of error.
77 bool scal_isequal2(scal_t a
, scal_t b
, scal_t e
)
79 return scal_abs(a
-b
) < e
;
83 * Check if two scalars are mostly equal.
86 bool scal_isequal(scal_t a
, scal_t b
)
88 return scal_isequal2(a
, b
, SCAL_EPSILON
);
92 * Clamp a scalar between two values.
95 scal_t
scal_clamp(scal_t s
, scal_t min
, scal_t max
)
108 * Define min and max functions for integers.
111 int imin(int a
, int b
)
113 return a
< b
? a
: b
;
116 int imax(int a
, int b
)
118 return a
< b
? b
: a
;
123 * Define some macros for packing and unpacking bytes to and from larger ints.
125 #define PACK(W,N,B) (((B) << (8 * (N))) | ((W) & ~(0xff << (8 * (N)))))
126 #define UNPACK(W,N) ((uint8_t)((W) >> (8 * (N))) & 0xff)
130 * Try to execute a statement and print a status message. If the statement
131 * resolves to 0, it is considered to have succeeded; any other evaluation is
132 * an error condition and an abort(3) occurs.
135 #define TRY_DO(L, K, ARGS...) \
136 printf("* " L "... ", ##ARGS); \
138 if ((K) == 0) printf("done!\n"); \
143 * Declare a type of destructor functions.
145 typedef void (*dtor_t
)(void*);
146 #define DTOR(A) (dtor_t)(A)
150 * Allocate a block of memory of a certain size. This follows the semantics
151 * of malloc(3), except it will never return NULL and will abort(3) if the
152 * memory could not be allocated.
154 void* mem_alloc(size_t size
);
157 * Change the size of a block of memory. This follows the semantics of
158 * realloc(3), except it will never return NULL and will abort(3) if the
159 * memory could not be allocated.
161 void* mem_realloc(void* mem
, size_t size
);
164 * Deallocate a block of memory previously allocated by mem_alloc or malloc(3)
165 * and friends. This is essentially just a call to free(3).
167 void mem_free(void* mem
);
170 * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if
171 * no callback should be called. The callback takes the same arguments as
172 * realloc(3) and may try to fulfill the request. The return value of the
173 * callback function will be returned from the allocation function and must be
174 * a valid pointer to an allocated block of memory. The callback function
175 * should not call mem_alloc or mem_realloc and must not return if a block of
176 * memory could not be allocated.
178 void mem_set_fn(void* (*fn
)(void*, size_t));
181 * Get the number of blocks currently allocated with either mem_alloc or
182 * mem_realloc. This number should be zero at the end of a process running
189 * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
190 * The result needs to be freed with mem_free.
193 char* mem_strdup(const char* str
)
195 size_t size
= strlen(str
) + 1;
196 return (char*)memcpy(mem_alloc(size
), str
, size
);
200 * Get an allocated string made up of two strings concatenated together.
201 * The result needs to be freed with mem_free.
204 char* mem_strcat(const char* str1
, const char* str2
)
206 size_t size1
= strlen(str1
);
207 size_t size2
= strlen(str2
);
208 char* str
= (char*)mem_alloc(size1
+ size2
+ 1);
209 memcpy(str
+ size1
, str2
, size2
+ 1);
210 return memcpy(str
, str1
, size1
);
215 * Cut a string short at the last matching character. The string will be
216 * modified if the character was matched. Either way, str is returned.
219 char* strcut(char* str
, int c
)
221 char* ptr
= strrchr(str
, c
);
229 * Trim white space off of the right side of a string.
231 void rtrim(char *str
);
234 * Trim white space off of the left side of a string.
236 void ltrim(char *str
);
239 * Trim white space off of both sides of a string.
250 * Initialize the timer.
255 * Stop the timer and return how many microseconds passed after
This page took 0.043982 seconds and 5 git commands to generate.