/* * CS5600 University of Utah * Charles McGarvey * mcgarvey@eng.utah.edu */ #ifndef __COMMON_H__ #define __COMMON_H__ #include #include #include #include #include /* * Define a type for scalar values, either float or double. */ #ifdef USE_DOUBLE typedef double scal_t; #define S(K) K #define scal_sin sin #define scal_cos cos #else typedef float scal_t; #define S(K) K##f #define scal_sin sinf #define scal_cos cosf #endif #define S_ZERO S(0.0) /* * Define a keyword for use while defining small and fast functions. */ #define __fast__ static inline /* * Define some macros for packing and unpacking bytes to and from larger ints. */ #define PACK(W,N,B) (((B) << (8 * (N))) | ((W) & ~(0xff << (8 * (N))))) #define UNPACK(W,N) ((uint8_t)((W) >> (8 * (N))) & 0xff) typedef void (*dtor_t)(void*); #define DTOR(A) (dtor_t)(A) /* * Allocate a block of memory of a certain size. This follows the semantics * of malloc(3), except it will never return NULL and will abort(3) if the * memory could not be allocated. */ void* mem_alloc(size_t size); /* * Change the size of a block of memory. This follows the semantics of * realloc(3), except it will never return NULL and will abort(3) if the * memory could not be allocated. */ void* mem_realloc(void* mem, size_t size); /* * Deallocate a block of memory previously allocated by mem_alloc or malloc(3) * and friends. This is essentially just a call to free(3). */ void mem_free(void* mem); /* * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if * no callback should be called. The callback takes the same arguments as * realloc(3) and may try to fulfill the request. The return value of the * callback function will be returned from the allocation function and must be * a valid pointer to an allocated block of memory. The callback function * should not call mem_alloc or mem_realloc and must not return if a block of * memory could not be allocated. */ void mem_set_fn(void* (*fn)(void*, size_t)); /* * Get the number of blocks currently allocated with either mem_alloc or * mem_realloc. This number should be zero at the end of a process running * this program. */ int mem_blocks(); /* * Trim white space off of the right side of a string. */ void rtrim(char *str); /* * Trim white space off of the left side of a string. */ void ltrim(char *str); /* * Trim white space off of both sides of a string. */ __fast__ void trim(char *str) { rtrim(str); ltrim(str); } #endif // __COMMON_H__