home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / BITOPS.H < prev    next >
C/C++ Source or Header  |  1995-05-10  |  5KB  |  196 lines

  1. /*
  2.  * bitops.h --- Bitmap frobbing code.
  3.  * 
  4.  * Copyright (C) 1993, 1994 Theodore Ts'o.  This file may be
  5.  * redistributed under the terms of the GNU Public License.
  6.  * 
  7.  * Taken from <asm/bitops.h>, Copyright 1992, Linus Torvalds.
  8.  */
  9.  
  10.  
  11. extern int set_bit(int nr,void * addr);
  12. extern int clear_bit(int nr, void * addr);
  13. extern int test_bit(int nr, const void * addr);
  14.  
  15. /*
  16.  * EXT2FS bitmap manipulation routines.
  17.  */
  18.  
  19. /* Support for sending warning messages from the inline subroutines */
  20. extern const char *ext2fs_block_string;
  21. extern const char *ext2fs_inode_string;
  22. extern const char *ext2fs_mark_string;
  23. extern const char *ext2fs_unmark_string;
  24. extern const char *ext2fs_test_string;
  25. extern void ext2fs_warn_bitmap(errcode_t errcode, unsigned long arg,
  26.                    const char *description);
  27.  
  28. extern void ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
  29. extern void ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
  30.                        blk_t block);
  31. extern int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap, blk_t block);
  32.  
  33. extern void ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, ino_t inode);
  34. extern void ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
  35.                        ino_t inode);
  36. extern int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap, ino_t inode);
  37.  
  38. /*
  39.  * The inline routines themselves...
  40.  * 
  41.  * If NO_INLINE_FUNCS is defined, then we won't try to do inline
  42.  * functions at all!
  43.  */
  44. #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
  45. #ifdef INCLUDE_INLINE_FUNCS
  46. #define _INLINE_ extern
  47. #else
  48. #define _INLINE_ extern __inline__
  49. #endif
  50.  
  51. #if (defined(__i386__) || defined(__i486__) || defined(__i586__))
  52.  
  53. #define _EXT2_HAVE_ASM_BITOPS_
  54.     
  55. /*
  56.  * These are done by inline assembly for speed reasons.....
  57.  *
  58.  * All bitoperations return 0 if the bit was cleared before the
  59.  * operation and != 0 if it was not.  Bit 0 is the LSB of addr; bit 32
  60.  * is the LSB of (addr+1).
  61.  */
  62.  
  63. /*
  64.  * Some hacks to defeat gcc over-optimizations..
  65.  */
  66. struct __dummy_h { unsigned long a[100]; };
  67. #define ADDR (*(struct __dummy_h *) addr)
  68. #define CONST_ADDR (*(const struct __dummy_h *) addr)    
  69.  
  70. _INLINE_ int set_bit(int nr, void * addr)
  71. {
  72.     int oldbit;
  73.  
  74.     __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
  75.         :"=r" (oldbit),"=m" (ADDR)
  76.         :"r" (nr));
  77.     return oldbit;
  78. }
  79.  
  80. _INLINE_ int clear_bit(int nr, void * addr)
  81. {
  82.     int oldbit;
  83.  
  84.     __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
  85.         :"=r" (oldbit),"=m" (ADDR)
  86.         :"r" (nr));
  87.     return oldbit;
  88. }
  89.  
  90. _INLINE_ int test_bit(int nr, const void * addr)
  91. {
  92.     int oldbit;
  93.  
  94.     __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
  95.         :"=r" (oldbit)
  96.         :"m" (CONST_ADDR),"r" (nr));
  97.     return oldbit;
  98. }
  99.  
  100. #undef ADDR
  101.  
  102. #endif    /* i386 */
  103.  
  104. #ifdef __mc68000__
  105.  
  106. #define _EXT2_HAVE_ASM_BITOPS_
  107.  
  108. _INLINE_ int set_bit(int nr,void * addr)
  109. {
  110.     char retval;
  111.  
  112.     __asm__ __volatile__ ("bfset %2@{%1:#1}; sne %0"
  113.          : "=d" (retval) : "d" (nr), "a" (addr));
  114.  
  115.     return retval;
  116. }
  117.  
  118. _INLINE_ int clear_bit(int nr, void * addr)
  119. {
  120.     char retval;
  121.  
  122.     __asm__ __volatile__ ("bfclr %2@{%1:#1}; sne %0"
  123.          : "=d" (retval) : "d" (nr), "a" (addr));
  124.  
  125.     return retval;
  126. }
  127.  
  128. _INLINE_ int test_bit(int nr, const void * addr)
  129. {
  130.     char retval;
  131.  
  132.     __asm__ __volatile__ ("bftst %2@{%1:#1}; sne %0"
  133.          : "=d" (retval) : "d" (nr), "a" (addr));
  134.  
  135.     return retval;
  136. }
  137.  
  138. #endif /* __mc68000__ */
  139.  
  140. _INLINE_ void ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
  141.                        blk_t block)
  142. {
  143.     if ((block < bitmap->start) || (block > bitmap->end))
  144.         ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
  145.                    bitmap->description);
  146.     set_bit(block - bitmap->start, bitmap->bitmap);
  147. }
  148.  
  149. _INLINE_ void ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
  150.                      blk_t block)
  151. {
  152.     if ((block < bitmap->start) || (block > bitmap->end))
  153.         ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK,
  154.                    block, bitmap->description);
  155.     clear_bit(block - bitmap->start, bitmap->bitmap);
  156. }
  157.  
  158. _INLINE_ int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
  159.                        blk_t block)
  160. {
  161.     if ((block < bitmap->start) || (block > bitmap->end))
  162.         ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
  163.                    block, bitmap->description);
  164.     return test_bit(block - bitmap->start, bitmap->bitmap);
  165. }
  166.  
  167. _INLINE_ void ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
  168.                        ino_t inode)
  169. {
  170.     if ((inode < bitmap->start) || (inode > bitmap->end))
  171.         ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_MARK,
  172.                    inode, bitmap->description);
  173.     set_bit(inode - bitmap->start, bitmap->bitmap);
  174. }
  175.  
  176. _INLINE_ void ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
  177.                      ino_t inode)
  178. {
  179.     if ((inode < bitmap->start) || (inode > bitmap->end))
  180.         ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_UNMARK,
  181.                    inode, bitmap->description);
  182.     clear_bit(inode - bitmap->start, bitmap->bitmap);
  183. }
  184.  
  185. _INLINE_ int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
  186.                        ino_t inode)
  187. {
  188.     if ((inode < bitmap->start) || (inode > bitmap->end))
  189.         ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
  190.                    inode, bitmap->description);
  191.     return test_bit(inode - bitmap->start, bitmap->bitmap);
  192. }
  193.  
  194. #undef _INLINE_
  195. #endif
  196.