1 /*
2  * Cogl
3  *
4  * A Low Level GPU Graphics and Utilities API
5  *
6  * Copyright (C) 2011 Intel Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  * Authors:
29  *   Neil Roberts <neil@linux.intel.com>
30  */
31 
32 #ifndef __COGL_FLAGS_H
33 #define __COGL_FLAGS_H
34 
35 #include <glib.h>
36 
37 #include "cogl-util.h"
38 
39 G_BEGIN_DECLS
40 
41 /* These are macros used to implement a fixed-size array of bits. This
42    should be used instead of CoglBitmask when the maximum bit number
43    that will be set is known at compile time, for example when setting
44    for recording a set of known available features */
45 
46 /* The bits are stored in an array of unsigned longs. To use these
47    macros, you would typically have an enum defining the available
48    bits with an extra last enum to define the maximum value. Then to
49    store the flags you would declare an array of unsigned longs sized
50    using COGL_FLAGS_N_LONGS_FOR_SIZE, eg:
51 
52    typedef enum { FEATURE_A, FEATURE_B, FEATURE_C, N_FEATURES } Features;
53 
54    unsigned long feature_flags[COGL_FLAGS_N_LONGS_FOR_SIZE (N_FEATURES)];
55 */
56 
57 #define COGL_FLAGS_N_LONGS_FOR_SIZE(size)       \
58   (((size) +                                    \
59     (sizeof (unsigned long) * 8 - 1))           \
60    / (sizeof (unsigned long) * 8))
61 
62 /* @flag is expected to be constant so these should result in a
63    constant expression. This means that setting a flag is equivalent
64    to just setting in a bit in a global variable at a known
65    location */
66 #define COGL_FLAGS_GET_INDEX(flag)              \
67   ((flag) / (sizeof (unsigned long) * 8))
68 #define COGL_FLAGS_GET_MASK(flag)               \
69   (1UL << ((unsigned long) (flag) &             \
70            (sizeof (unsigned long) * 8 - 1)))
71 
72 #define COGL_FLAGS_GET(array, flag)             \
73   (!!((array)[COGL_FLAGS_GET_INDEX (flag)] &    \
74       COGL_FLAGS_GET_MASK (flag)))
75 
76 /* The expectation here is that @value will be constant so the if
77    statement will be optimised out */
78 #define COGL_FLAGS_SET(array, flag, value)      \
79   G_STMT_START {                                \
80     if (value)                                  \
81       ((array)[COGL_FLAGS_GET_INDEX (flag)] |=  \
82        COGL_FLAGS_GET_MASK (flag));             \
83     else                                        \
84       ((array)[COGL_FLAGS_GET_INDEX (flag)] &=  \
85        ~COGL_FLAGS_GET_MASK (flag));            \
86   } G_STMT_END
87 
88 /* Macros to help iterate an array of flags. It should be used like
89  * this:
90  *
91  * int n_longs = COGL_FLAGS_N_LONGS_FOR_SIZE (...);
92  * unsigned long flags[n_longs];
93  * int bit_num;
94  *
95  * COGL_FLAGS_FOREACH_START (flags, n_longs, bit_num)
96  *   {
97  *     do_something_with_the_bit (bit_num);
98  *   }
99  * COGL_FLAGS_FOREACH_END;
100  */
101 #define COGL_FLAGS_FOREACH_START(array, n_longs, bit)   \
102   G_STMT_START {                                        \
103   const unsigned long *_p = (array);                    \
104   int _n_longs = (n_longs);                             \
105   int _i;                                               \
106                                                         \
107   for (_i = 0; _i < _n_longs; _i++)                     \
108     {                                                   \
109       unsigned long _mask = *(_p++);                    \
110                                                         \
111       (bit) = _i * sizeof (unsigned long) * 8 - 1;      \
112                                                         \
113       while (_mask)                                     \
114         {                                               \
115           int _next_bit = _cogl_util_ffsl (_mask);      \
116           (bit) += _next_bit;                           \
117           /* This odd two-part shift is to avoid */     \
118           /* shifting by sizeof (long)*8 which has */   \
119           /* undefined results according to the */      \
120           /* C spec (and seems to be a no-op in */      \
121           /* practice) */                               \
122           _mask = (_mask >> (_next_bit - 1)) >> 1;      \
123 
124 #define COGL_FLAGS_FOREACH_END \
125   } } } G_STMT_END
126 
127 G_END_DECLS
128 
129 #endif /* __COGL_FLAGS_H */
130 
131