initial commit
[chaz/rasterize] / common.h
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef __COMMON_H__
9 #define __COMMON_H__
10
11 #include <math.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17
18 /*
19 * Define a type for scalar values, either float or double.
20 */
21 #ifdef USE_DOUBLE
22 typedef double scal_t;
23 #define S(K) K
24 #define scal_sin sin
25 #define scal_cos cos
26 #else
27 typedef float scal_t;
28 #define S(K) K##f
29 #define scal_sin sinf
30 #define scal_cos cosf
31 #endif
32
33 #define S_ZERO S(0.0)
34
35
36 /*
37 * Define a keyword for use while defining small and fast functions.
38 */
39 #define __fast__ static inline
40
41
42 /*
43 * Define some macros for packing and unpacking bytes to and from larger ints.
44 */
45 #define PACK(W,N,B) (((B) << (8 * (N))) | ((W) & ~(0xff << (8 * (N)))))
46 #define UNPACK(W,N) ((uint8_t)((W) >> (8 * (N))) & 0xff)
47
48
49 typedef void (*dtor_t)(void*);
50 #define DTOR(A) (dtor_t)(A)
51
52
53 /*
54 * Allocate a block of memory of a certain size. This follows the semantics
55 * of malloc(3), except it will never return NULL and will abort(3) if the
56 * memory could not be allocated.
57 */
58 void* mem_alloc(size_t size);
59
60 /*
61 * Change the size of a block of memory. This follows the semantics of
62 * realloc(3), except it will never return NULL and will abort(3) if the
63 * memory could not be allocated.
64 */
65 void* mem_realloc(void* mem, size_t size);
66
67 /*
68 * Deallocate a block of memory previously allocated by mem_alloc or malloc(3)
69 * and friends. This is essentially just a call to free(3).
70 */
71 void mem_free(void* mem);
72
73 /*
74 * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if
75 * no callback should be called. The callback takes the same arguments as
76 * realloc(3) and may try to fulfill the request. The return value of the
77 * callback function will be returned from the allocation function and must be
78 * a valid pointer to an allocated block of memory. The callback function
79 * should not call mem_alloc or mem_realloc and must not return if a block of
80 * memory could not be allocated.
81 */
82 void mem_set_fn(void* (*fn)(void*, size_t));
83
84 /*
85 * Get the number of blocks currently allocated with either mem_alloc or
86 * mem_realloc. This number should be zero at the end of a process running
87 * this program.
88 */
89 int mem_blocks();
90
91
92 /*
93 * Trim white space off of the right side of a string.
94 */
95 void rtrim(char *str);
96
97 /*
98 * Trim white space off of the left side of a string.
99 */
100 void ltrim(char *str);
101
102 /*
103 * Trim white space off of both sides of a string.
104 */
105 __fast__
106 void trim(char *str)
107 {
108 rtrim(str);
109 ltrim(str);
110 }
111
112
113 #endif // __COMMON_H__
114
This page took 0.042407 seconds and 4 git commands to generate.