Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / bitops.c
1 /*
2  * bitops.c --- Bitmap frobbing code.  See bitops.h for the inlined
3  *      routines.
4  * 
5  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
13 #include <stdio.h>
14 #if HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif
17
18 #if EXT2_FLAT_INCLUDES
19 #include "ext2_fs.h"
20 #else
21 #include <linux/ext2_fs.h>
22 #endif
23
24 #include "ext2fs.h"
25
26 #ifndef _EXT2_HAVE_ASM_BITOPS_
27
28 /*
29  * For the benefit of those who are trying to port Linux to another
30  * architecture, here are some C-language equivalents.  You should
31  * recode these in the native assmebly language, if at all possible.
32  *
33  * C language equivalents written by Theodore Ts'o, 9/26/92.
34  * Modified by Pete A. Zaitcev 7/14/95 to be portable to big endian
35  * systems, as well as non-32 bit systems.
36  */
37
38 int ext2fs_set_bit(int nr,void * addr)
39 {
40         int             mask, retval;
41         unsigned char   *ADDR = (unsigned char *) addr;
42
43         ADDR += nr >> 3;
44         mask = 1 << (nr & 0x07);
45         retval = (mask & *ADDR) != 0;
46         *ADDR |= mask;
47         return retval;
48 }
49
50 int ext2fs_clear_bit(int nr, void * addr)
51 {
52         int             mask, retval;
53         unsigned char   *ADDR = (unsigned char *) addr;
54
55         ADDR += nr >> 3;
56         mask = 1 << (nr & 0x07);
57         retval = (mask & *ADDR) != 0;
58         *ADDR &= ~mask;
59         return retval;
60 }
61
62 int ext2fs_test_bit(int nr, const void * addr)
63 {
64         int                     mask;
65         const unsigned char     *ADDR = (const unsigned char *) addr;
66
67         ADDR += nr >> 3;
68         mask = 1 << (nr & 0x07);
69         return ((mask & *ADDR) != 0);
70 }
71
72 #endif  /* !_EXT2_HAVE_ASM_BITOPS_ */
73
74 void ext2fs_warn_bitmap(errcode_t errcode, unsigned long arg,
75                         const char *description)
76 {
77 #ifndef OMIT_COM_ERR
78         if (description)
79                 com_err(0, errcode, "#%u for %s", arg, description);
80         else
81                 com_err(0, errcode, "#%u", arg);
82 #endif
83 }
84
85 void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
86                             int code, unsigned long arg)
87 {
88 #ifndef OMIT_COM_ERR
89         if (bitmap->description)
90                 com_err(0, bitmap->base_error_code+code,
91                         "#%u for %s", arg, bitmap->description);
92         else
93                 com_err(0, bitmap->base_error_code + code, "#%u", arg);
94 #endif
95 }
96