1 /*
2  * Copyright (C) 2010-2011 Marcin Kościelnicki <koriakin@0x04.net>
3  * Copyright (C) 2010 Francisco Jerez <currojerez@riseup.net>
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef UTIL_H
27 #define UTIL_H
28 
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <inttypes.h>
32 
33 #define ADDARRAY(a, e) \
34 	do { \
35 	if ((a ## num) >= (a ## max)) { \
36 		if (!(a ## max)) \
37 			(a ## max) = 16; \
38 		else \
39 			(a ## max) *= 2; \
40 		(a) = realloc((a), (a ## max)*sizeof(*(a))); \
41 	} \
42 	(a)[(a ## num)++] = (e); \
43 	} while(0)
44 
45 #define FINDARRAY(a, tmp, pred)				\
46 	({							\
47 		int __i;					\
48 								\
49 		for (__i = 0; __i < (a ## num); __i++) {	\
50 			tmp = (a)[__i];				\
51 			if (pred)				\
52 				break;				\
53 		}						\
54 								\
55 		tmp = ((pred) ? tmp : NULL);			\
56 	})
57 
58 /* ceil(log2(x)) */
clog2(uint64_t x)59 static inline int clog2(uint64_t x) {
60 	if (!x)
61 		return x;
62 	int r = 0;
63 	while (x - 1 > (1ull << r) - 1)
64 		r++;
65 	return r;
66 }
67 
68 #define ARRAY_SIZE(a) (sizeof (a) / sizeof *(a))
69 
70 #define min(a,b)				\
71 	({					\
72 		typeof (a) _a = (a);		\
73 		typeof (b) _b = (b);		\
74 		_a < _b ? _a : _b;		\
75 	})
76 
77 #define max(a,b)				\
78 	({					\
79 		typeof (a) _a = (a);		\
80 		typeof (b) _b = (b);		\
81 		_a > _b ? _a : _b;		\
82 	})
83 
84 #define CEILDIV(a, b) (((a) + (b) - 1)/(b))
85 
86 #define extr(a, b, c) ((uint64_t)(a) << (64 - (b) - (c)) >> (64 - (c)))
87 #define extrs(a, b, c) ((int64_t)(a) << (64 - (b) - (c)) >> (64 - (c)))
88 #define sext(a, b) extrs(a, 0, b+1)
89 #define bflmask(a) ((2ull << ((a)-1)) - 1)
90 #define insrt(a, b, c, d) ((a) = ((a) & ~(bflmask(c) << (b))) | ((d) & bflmask(c)) << (b))
91 
92 struct envy_loc {
93 	int lstart;
94 	int cstart;
95 	int lend;
96 	int cend;
97 	const char *file;
98 };
99 
100 #define LOC_FORMAT(loc, str) "%s:%d.%d-%d.%d: " str, (loc).file, (loc).lstart, (loc).cstart, (loc).lend, (loc).cend
101 
102 uint32_t elf_hash(const char *str);
103 
104 FILE *find_in_path(const char *name, const char *path, char **pfullname);
105 
106 struct astr {
107 	char *str;
108 	size_t len;
109 };
110 
111 void print_escaped_astr(FILE *out, struct astr *astr);
112 
113 char *aprintf(const char *format, ...);
114 
115 #endif
116