• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #ifndef _UTIL_H_
24 #define _UTIL_H_
25 
26 #include "config.h"
27 #include <stdbool.h>
28 #include <string.h>
29 
30 #define LONG_BITS (sizeof(long) * 8)
31 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
32 #define ARRAY_LENGTH(a) (sizeof(a) / (sizeof((a)[0])))
33 #define unlikely(x) (__builtin_expect(!!(x),0))
34 
35 #undef min
36 #undef max
37 #ifdef __GNUC__
38 #define min(a,b) \
39 		({ __typeof__ (a) _a = (a); \
40 	          __typeof__ (b) _b = (b); \
41 		_a > _b ? _b : _a; \
42 		})
43 #define max(a,b) \
44 		({ __typeof__ (a) _a = (a); \
45 	          __typeof__ (b) _b = (b); \
46 		_a > _b ? _a : _b; \
47 		})
48 #else
49 #define min(a,b) ((a) > (b) ? (b) : (a))
50 #define max(a,b) ((a) > (b) ? (a) : (b))
51 #endif
52 
53 static inline bool
startswith(const char * str,size_t len,const char * prefix,size_t plen)54 startswith(const char *str, size_t len, const char *prefix, size_t plen)
55 {
56 	return len >= plen && !strncmp(str, prefix, plen);
57 }
58 
59 static inline int
bit_is_set(const unsigned long * array,int bit)60 bit_is_set(const unsigned long *array, int bit)
61 {
62     return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
63 }
64 
65 static inline void
set_bit(unsigned long * array,int bit)66 set_bit(unsigned long *array, int bit)
67 {
68     array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
69 }
70 
71 static inline void
clear_bit(unsigned long * array,int bit)72 clear_bit(unsigned long *array, int bit)
73 {
74     array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
75 }
76 
77 static inline void
set_bit_state(unsigned long * array,int bit,int state)78 set_bit_state(unsigned long *array, int bit, int state)
79 {
80 	if (state)
81 		set_bit(array, bit);
82 	else
83 		clear_bit(array, bit);
84 }
85 
86 #endif
87