]>
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
41 #define scal_sqrt sqrt
45 #define scal_asin asin
46 #define scal_acos acos
47 #define scal_atan atan
52 #define scal_floor floorf
53 #define scal_ceil ceilf
54 #define scal_min fminf
55 #define scal_max fmaxf
57 #define scal_sqrt sqrtf
61 #define scal_asin asinf
62 #define scal_acos acosf
63 #define scal_atan atanf
66 #define scal_min2(A,B,C) scal_min(scal_min(A,B),C)
67 #define scal_max2(A,B,C) scal_max(scal_max(A,B),C)
70 * Clamp a scalar between two values.
73 scal_t
scal_clamp(scal_t s
, scal_t min
, scal_t max
)
86 * Define min and max functions for integers.
89 int imin(int a
, int b
)
94 int imax(int a
, int b
)
101 * Define some macros for packing and unpacking bytes to and from larger ints.
103 #define PACK(W,N,B) (((B) << (8 * (N))) | ((W) & ~(0xff << (8 * (N)))))
104 #define UNPACK(W,N) ((uint8_t)((W) >> (8 * (N))) & 0xff)
108 * Try to execute a statement and print a status message. If the statement
109 * resolves to 0, it is considered to have succeeded; any other evaluation is
110 * an error condition and an abort(3) occurs.
113 #define TRY_DO(L, K, ARGS...) \
114 printf("* " L "... ", ##ARGS); \
116 if ((K) == 0) printf("done!\n"); \
121 * Declare a type of destructor functions.
123 typedef void (*dtor_t
)(void*);
124 #define DTOR(A) (dtor_t)(A)
128 * Allocate a block of memory of a certain size. This follows the semantics
129 * of malloc(3), except it will never return NULL and will abort(3) if the
130 * memory could not be allocated.
132 void* mem_alloc(size_t size
);
135 * Change the size of a block of memory. This follows the semantics of
136 * realloc(3), except it will never return NULL and will abort(3) if the
137 * memory could not be allocated.
139 void* mem_realloc(void* mem
, size_t size
);
142 * Deallocate a block of memory previously allocated by mem_alloc or malloc(3)
143 * and friends. This is essentially just a call to free(3).
145 void mem_free(void* mem
);
148 * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if
149 * no callback should be called. The callback takes the same arguments as
150 * realloc(3) and may try to fulfill the request. The return value of the
151 * callback function will be returned from the allocation function and must be
152 * a valid pointer to an allocated block of memory. The callback function
153 * should not call mem_alloc or mem_realloc and must not return if a block of
154 * memory could not be allocated.
156 void mem_set_fn(void* (*fn
)(void*, size_t));
159 * Get the number of blocks currently allocated with either mem_alloc or
160 * mem_realloc. This number should be zero at the end of a process running
167 * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
168 * The result needs to be freed with mem_free.
171 char* mem_strdup(const char* str
)
173 size_t size
= strlen(str
) + 1;
174 return (char*)memcpy(mem_alloc(size
), str
, size
);
178 * Get an allocated string made up of two strings concatenated together.
179 * The result needs to be freed with mem_free.
182 char* mem_strcat(const char* str1
, const char* str2
)
184 size_t size1
= strlen(str1
);
185 size_t size2
= strlen(str2
);
186 char* str
= (char*)mem_alloc(size1
+ size2
+ 1);
187 memcpy(str
+ size1
, str2
, size2
+ 1);
188 return memcpy(str
, str1
, size1
);
193 * Cut a string short at the last matching character. The string will be
194 * modified if the character was matched. Either way, str is returned.
197 char* strcut(char* str
, int c
)
199 char* ptr
= strrchr(str
, c
);
207 * Trim white space off of the right side of a string.
209 void rtrim(char *str
);
212 * Trim white space off of the left side of a string.
214 void ltrim(char *str
);
217 * Trim white space off of both sides of a string.
228 * Initialize the timer.
233 * Stop the timer and return how many microseconds passed after
This page took 0.047455 seconds and 5 git commands to generate.