kernel: Move GPL'd kernel files to sys/gnu to have them all in one place.
[dragonfly.git] / sys / gnu / vfs / ext2fs / ext2_bitops.h
1 /*-
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/gnu/fs/ext2fs/ext2_bitops.h,v 1.3 2006/04/13 19:37:32 cracauer Exp $
27  */
28
29 #ifndef _SYS_GNU_EXT2FS_EXT2_BITOPS_H_
30 #define _SYS_GNU_EXT2FS_EXT2_BITOPS_H_
31
32 #define find_first_zero_bit(data, sz)           find_next_zero_bit(data, sz, 0)
33
34 static __inline int
35 clear_bit(int no, void *data)
36 {
37         uint32_t *p;
38         uint32_t mask, new, old;
39
40         p = (uint32_t*)data + (no >> 5);
41         mask = (1U << (no & 31));
42         do {
43                 old = *p;
44                 new = old & ~mask;
45         } while (!atomic_cmpset_32(p, old, new));
46         return (old & mask);
47 }
48
49 static __inline int
50 set_bit(int no, void *data)
51 {
52         uint32_t *p;
53         uint32_t mask, new, old;
54
55         p = (uint32_t*)data + (no >> 5);
56         mask = (1U << (no & 31));
57         do {
58                 old = *p;
59                 new = old | mask;
60         } while (!atomic_cmpset_32(p, old, new));
61         return (old & mask);
62 }
63
64 static __inline int
65 test_bit(int no, void *data)
66 {
67         uint32_t *p;
68         uint32_t mask;
69
70         p = (uint32_t*)data + (no >> 5);
71         mask = (1U << (no & 31));
72         return (*p & mask);
73 }
74
75 static __inline size_t
76 find_next_zero_bit(void *data, size_t sz, size_t ofs)
77 {
78         uint32_t *p;
79         uint32_t mask;
80         int bit;
81
82         p = (uint32_t*)data + (ofs >> 5);
83         if (ofs & 31) {
84                 mask = ~0U << (ofs & 31);
85                 bit = *p | ~mask;
86                 if (bit != ~0U)
87                         return (ffs(~bit) + ofs - 1);
88                 p++;
89                 ofs = (ofs + 31U) & ~31U;
90         }
91         while(ofs < sz && *p == ~0U) {
92                 p++;
93                 ofs += 32;
94         }
95         if (ofs == sz)
96                 return (ofs);
97         bit = *p;
98         return (ffs(~bit) + ofs - 1);
99 }
100
101 static __inline void *
102 memscan(void *data, int c, size_t sz)
103 {
104         uint8_t *p;
105
106         p = data;
107         while (sz && *p != c) {
108                 p++;
109                 sz--;
110         }
111         return (p);
112 }
113
114 #endif /* _SYS_GNU_EXT2FS_EXT2_BITOPS_H_ */