1 /*
   2  * support.h - Various useful things. Part of the Linux-NTFS project.
   3  *
   4  * Copyright (c) 2000-2004 Anton Altaparmakov
   5  * Copyright (c)      2006 Szabolcs Szakacsits
   6  * Copyright (c)      2006 Yura Pakhuchiy
   7  *
   8  * This program/include file is free software; you can redistribute it and/or
   9  * modify it under the terms of the GNU General Public License as published
  10  * by the Free Software Foundation; either version 2 of the License, or
  11  * (at your option) any later version.
  12  *
  13  * This program/include file is distributed in the hope that it will be
  14  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  * GNU General Public License for more details.
  17  *
  18  * You should have received a copy of the GNU General Public License
  19  * along with this program (in the main directory of the Linux-NTFS
  20  * distribution in the file COPYING); if not, write to the Free Software
  21  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22  */
  23 
  24 #ifndef _NTFS_SUPPORT_H
  25 #define _NTFS_SUPPORT_H
  26 
  27 #ifdef HAVE_CONFIG_H
  28 #include "config.h"
  29 #endif
  30 
  31 #ifdef HAVE_STDDEF_H
  32 #include <stddef.h>
  33 #endif
  34 
  35 #ifdef HAVE_STDLIB_H
  36 #include <stdlib.h>
  37 #endif
  38 
  39 #include "logging.h"
  40 
  41 /*
  42  * Our mailing list. Use this define to prevent typos in email address.
  43  */
  44 #define NTFS_DEV_LIST   "linux-ntfs-dev@lists.sf.net"
  45 
  46 /*
  47  * Generic macro to convert pointers to values for comparison purposes.
  48  */
  49 #ifndef p2n
  50 #define p2n(p)          ((ptrdiff_t)((ptrdiff_t*)(p)))
  51 #endif
  52 
  53 /*
  54  * The classic min and max macros.
  55  */
  56 #ifndef min
  57 #define min(a,b)        ((a) <= (b) ? (a) : (b))
  58 #endif
  59 
  60 #ifndef max
  61 #define max(a,b)        ((a) >= (b) ? (a) : (b))
  62 #endif
  63 
  64 /*
  65  * Useful macro for determining the offset of a struct member.
  66  */
  67 #ifndef offsetof
  68 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  69 #endif
  70 
  71 /*
  72  * Round up and down @num to 2 in power of @order.
  73  */
  74 #define ROUND_UP(num,order)     (((num) + ((1 << (order)) - 1)) & \
  75                                 ~((1 << (order)) - 1))
  76 #define ROUND_DOWN(num,order)   ((num) & ~((1 << (order)) - 1))
  77 
  78 /*
  79  * Simple bit operation macros. NOTE: These are NOT atomic.
  80  */
  81 #define test_bit(bit, var)            ((var) & (1 << (bit)))
  82 #define set_bit(bit, var)             (var) |= 1 << (bit)
  83 #define clear_bit(bit, var)           (var) &= ~(1 << (bit))
  84 
  85 #ifdef __sun
  86 #define test_and_set_bit(bit, var)      _test_and_set_bit(bit, &var)
  87 static __inline__ BOOL _test_and_set_bit(unsigned long bit, unsigned long *var)
  88 {
  89         const BOOL old_state = test_bit(bit, *var);
  90         set_bit(bit, *var);
  91         return old_state;
  92 }
  93 
  94 #define test_and_clear_bit(bit, var)    _test_and_clear_bit(bit, &var)
  95 static __inline__ BOOL _test_and_clear_bit(unsigned long bit, unsigned long *var)
  96 {
  97         const BOOL old_state = test_bit(bit, *var);
  98         clear_bit(bit, *var);
  99         return old_state;
 100 }
 101 #else /* !__sun */
 102 #define test_and_set_bit(bit, var)                      \
 103 ({                                                      \
 104         const BOOL old_state = test_bit(bit, var);      \
 105         set_bit(bit, var);                              \
 106         old_state;                                      \
 107 })
 108 
 109 #define test_and_clear_bit(bit, var)                    \
 110 ({                                                      \
 111         const BOOL old_state = test_bit(bit, var);      \
 112         clear_bit(bit, var);                            \
 113         old_state;                                      \
 114 })
 115 #endif /* __sun */
 116 
 117 /* Memory allocation with logging. */
 118 extern void *ntfs_calloc(size_t size);
 119 extern void *ntfs_malloc(size_t size);
 120 
 121 #endif /* defined _NTFS_SUPPORT_H */