PageRenderTime 48ms CodeModel.GetById 15ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

/filesystems/unixfs/common/linux/linux.h

http://macfuse.googlecode.com/
C++ Header | 563 lines | 447 code | 105 blank | 11 comment | 23 complexity | 64e052fd4b8c4599215d355284e84431 MD5 | raw file
  1#ifndef _LINUX_TYPES_H_
  2#define _LINUX_TYPES_H_
  3
  4#if defined(__APPLE__)
  5
  6#include <errno.h>
  7#include <sys/types.h>
  8#include <stdint.h>
  9#include <stdlib.h>
 10#include <stdio.h>
 11#include <string.h>
 12#include <unistd.h>
 13#include <ctype.h>
 14#include <time.h>
 15#include <libkern/OSByteOrder.h>
 16
 17#define __force
 18#define __bitwise
 19#define __packed2__ __attribute__((packed, aligned(2)))
 20
 21#define __u64 uint64_t
 22#define __u32 uint32_t
 23#define __u16 uint16_t
 24#define __u8  uint8_t
 25#define __s64 int64_t
 26#define __s32 int32_t
 27#define __s16 int16_t
 28#define __s8  int8_t
 29
 30typedef __u64 u64;
 31typedef __u32 u32;
 32typedef __u16 u16;
 33typedef __u8  u8;
 34typedef __s64 s64;
 35typedef __s32 s32;
 36typedef __s16 s16;
 37typedef __s8  s8;
 38
 39typedef __u16 __bitwise __le16;
 40typedef __u16 __bitwise __be16;
 41typedef __u32 __bitwise __le32;
 42typedef __u32 __bitwise __be32;
 43typedef __u64 __bitwise __le64;
 44typedef __u64 __bitwise __be64;
 45
 46#ifndef __fs16
 47#define __fs16 __u16
 48#endif
 49#ifndef __fs32
 50#define __fs32 __u32
 51#endif
 52
 53typedef unsigned gfp_t;
 54typedef off_t    loff_t;
 55typedef long     pgoff_t;
 56typedef uint64_t sector_t;
 57typedef uint16_t umode_t;
 58
 59/* dummy structures */
 60
 61struct dentry;
 62struct file;
 63struct super_block;
 64struct kstatfs {
 65    __u64 f_type;
 66    __u64 f_blocks;
 67    __u64 f_bfree;
 68    __u64 f_bavail;
 69    __u64 f_files;
 70    __u64 f_ffree;
 71    __u32 f_bsize;
 72    __u32 f_namelen;
 73    __u32 f_frsize;
 74    __u32 padding;
 75    __u32 f_spare[6];
 76};
 77
 78/****** grab-bag stuff and defines ******/
 79
 80#define MS_RDONLY        1
 81#define EXPORT_SYMBOL(s) /**/
 82
 83#define BLOCK_SIZE_BITS 10
 84#define BLOCK_SIZE      (1 << BLOCK_SIZE_BITS)
 85
 86/* file types */
 87#define DT_UNKNOWN 0
 88#define DT_FIFO    1
 89#define DT_CHR     2
 90#define DT_DIR     4
 91#define DT_BLK     6
 92#define DT_REG     8
 93#define DT_LNK     10
 94#define DT_SOCK    12
 95#define DT_WHT     14
 96
 97#define get_seconds()    time(0)
 98#define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
 99
100#define simple_strtoul strtoul
101#define simple_strtol  strtol
102#define printk         printf
103
104#define lock_kernel()   /**/
105#define unlock_kernel() /**/
106#define KERN_ERR        /**/
107#define KERN_INFO       /**/
108
109#define __GFP_DMA     ((__force gfp_t)0x01u)
110#define __GFP_HIGHMEM ((__force gfp_t)0x02u)
111#define __GFP_DMA32   ((__force gfp_t)0x04u)
112#define __GFP_WAIT    ((__force gfp_t)0x10u)
113#define __GFP_HIGH    ((__force gfp_t)0x20u)
114#define __GFP_IO      ((__force gfp_t)0x40u)
115#define __GFP_FS      ((__force gfp_t)0x80u)
116#define GFP_KERNEL    (__GFP_WAIT | __GFP_IO | __GFP_FS)
117
118#ifdef __compiler_offsetof
119#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
120#else
121#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
122#endif
123
124#define container_of(ptr, type, member) ({                    \
125        const typeof( ((type *)0)->member ) *__mptr = (ptr);  \
126        (type *)( (char *)__mptr - offsetof(type,member) );})
127
128#define likely(x)               __builtin_expect(!!(x), 1)
129#define unlikely(x)             __builtin_expect(!!(x), 0)
130
131#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
132#define BUILD_BUG_ON_ZERO(e)    (sizeof(char[1 - 2 * !!(e)]) - 1)
133#define BUG_ON(condition)       /**/
134#define MAX_ERRNO               4095
135#define IS_ERR_VALUE(x)          unlikely((x) >= (unsigned long)-MAX_ERRNO)
136#define __must_be_array(a)      \
137    BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
138#define ARRAY_SIZE(arr)         \
139    (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
140
141static inline long PTR_ERR(const void* ptr)
142{
143    return (long)ptr;
144}
145
146static inline void* ERR_PTR(long error)
147{
148    return (void *)error;
149}
150
151static inline long IS_ERR(const void *ptr)
152{
153    return IS_ERR_VALUE((unsigned long)ptr);
154}
155
156#define min_t(type, x, y) ({            \
157    type __min1 = (x);                  \
158    type __min2 = (y);                  \
159    __min1 < __min2 ? __min1: __min2; })
160
161#define max_t(type, x, y) ({            \
162    type __max1 = (x);                  \
163    type __max2 = (y);                  \
164    __max1 > __max2 ? __max1: __max2; })
165
166/****** page and memory stuff ******/
167
168#define PAGE_SIZE        4096
169#define PAGE_CACHE_SIZE  PAGE_SIZE
170#define PAGE_CACHE_SHIFT 12
171#define PAGE_CACHE_MASK  (~(PAGE_CACHE_SIZE-1))
172
173struct address_space {
174    struct inode* host;
175};
176
177struct page {
178    union {
179        unsigned long private;
180        struct address_space* mapping;
181    };
182};
183
184static inline struct page* find_lock_page(struct address_space* mapping,
185                                          pgoff_t offset)
186{
187    return (struct page*)0;
188}
189
190static inline void page_noop(struct page* page)
191{
192    /* nothing */
193}
194
195#define lock_page          page_noop
196#define unlock_page        page_noop
197#define put_page           page_noop
198#define page_cache_release page_noop
199#define PageUptodate(x)    1
200#define PageError(x)       0
201
202static inline struct page* read_mapping_page(struct address_space* mapping,
203                                             pgoff_t index, void* data)
204{
205    return (struct page*)0;
206}
207
208static inline void* kmalloc(size_t size, gfp_t flags)
209{
210    return malloc(size);
211}
212
213static inline void* kzalloc(size_t size, gfp_t flags)
214{
215    return calloc(1, size);
216}
217
218static inline void kfree(const void* p)
219{
220    free((void*)p);
221}
222
223/****** buffer stuff *****/
224
225struct block_device {
226    int fd;
227    unsigned bd_block_size;
228};
229
230struct buffer_head {
231    sector_t b_blocknr;
232    size_t   b_size;
233    struct   b_flags {
234        uint32_t dynamic : 1;
235    } b_flags;
236    unsigned char b_data[PAGE_SIZE];
237};
238
239int sb_bread_intobh(struct super_block* sb, off_t block,
240                    struct buffer_head* bh);
241struct buffer_head* sb_bread(struct super_block* sb, off_t block);
242struct buffer_head* sb_getblk(struct super_block* sb, sector_t block);
243void brelse(struct buffer_head* bh);
244#define bforget brelse
245
246static inline void buffer_noop(struct buffer_head *bh)
247{
248    /* nothing */
249}
250
251static inline void ll_rw_block(int rw, int nr, struct buffer_head* bhs[])
252{
253    /* nothing */
254}
255
256#define mark_buffer_dirty     buffer_noop
257#define set_buffer_uptodate   buffer_noop
258#define clear_buffer_uptodate buffer_noop
259#define wait_on_buffer        buffer_noop
260#define buffer_dirty(x)       0
261#define set_buffer_mapped     buffer_noop
262
263/****** bit and math stuff ******/
264
265#define BITOP_WORD(nr)   ((nr) / BITS_PER_LONG)
266#define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
267
268#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
269#define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
270#else
271#define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
272#endif
273#define ADDR  BITOP_ADDR(addr)
274
275#if defined(__i386__) || defined(__x86_64__)
276static inline void __set_bit(int nr, volatile unsigned long *addr)
277{
278    asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
279}
280#elif defined(__ppc__) || defined(__ppc64__)
281static inline void __set_bit(int nr, volatile unsigned long *addr)
282{
283    *addr |= (1L << nr);
284}
285#endif
286
287#if defined(__LITTLE_ENDIAN__)
288
289#define generic_test_le_bit(nr, addr)   test_bit(nr, addr)
290#define generic___set_le_bit(nr, addr)   __set_bit(nr, addr)
291#define generic___clear_le_bit(nr, addr) __clear_bit(nr, addr)
292
293#define generic_test_and_set_le_bit(nr, addr)   test_and_set_bit(nr, addr)
294#define generic_test_and_clear_le_bit(nr, addr) test_and_clear_bit(nr, addr)
295
296#define generic___test_and_set_le_bit(nr, addr)   __test_and_set_bit(nr, addr)
297#define generic___test_and_clear_le_bit(nr, addr) __test_and_clear_bit(nr, addr)
298
299#define generic_find_next_zero_le_bit(addr, size, offset) \
300            find_next_zero_bit(addr, size, offset)
301#define generic_find_next_le_bit(addr, size, offset)      \
302            find_next_bit(addr, size, offset)
303
304#elif defined(__BIG_ENDIAN__)
305
306#define generic_test_le_bit(nr, addr)             \
307    test_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
308#define generic___set_le_bit(nr, addr)            \
309    __set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
310#define generic___clear_le_bit(nr, addr)          \
311    __clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
312
313#define generic_test_and_set_le_bit(nr, addr)     \
314    test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
315#define generic_test_and_clear_le_bit(nr, addr)   \
316    test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
317
318#define generic___test_and_set_le_bit(nr, addr)   \
319    __test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
320#define generic___test_and_clear_le_bit(nr, addr) \
321    __test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
322
323extern unsigned long generic_find_next_zero_le_bit(
324                         const unsigned long *addr, unsigned long size,
325                         unsigned long offset);
326extern unsigned long generic_find_next_le_bit(const unsigned long *addr,
327                         unsigned long size, unsigned long offset);
328
329#endif
330
331#define ext2_set_bit(nr,addr)   \
332    generic___test_and_set_le_bit((nr),(unsigned long *)(addr))
333#define ext2_clear_bit(nr,addr) \
334    generic___test_and_clear_le_bit((nr),(unsigned long *)(addr))
335
336#define ext2_test_bit(nr,addr)  \
337    generic_test_le_bit((nr),(unsigned long *)(addr))
338#define ext2_find_first_zero_bit(addr, size) \
339    generic_find_first_zero_le_bit((unsigned long *)(addr), (size))
340#define ext2_find_next_zero_bit(addr, size, off) \
341    generic_find_next_zero_le_bit((unsigned long *)(addr), (size), (off))
342#define ext2_find_next_bit(addr, size, off) \
343    generic_find_next_le_bit((unsigned long *)(addr), (size), (off))
344
345#if defined(__i386__) || defined(__ppc__)
346#define BITS_PER_LONG 32
347#elif defined(__x86_64__) || defined(__ppc64__)
348#define BITS_PER_LONG 64
349#endif
350
351static inline unsigned int blksize_bits(unsigned int size)
352{
353    unsigned int bits = 8;
354    do {
355        bits++;
356        size >>= 1;
357    } while (size > 256);
358    return bits;
359}
360
361static inline __attribute__((const)) int is_power_of_2(unsigned long n)
362{
363    return (n != 0 && ((n & (n - 1)) == 0));
364}
365
366static inline unsigned long ffz(unsigned long word) /* find first zero bit */
367{
368    asm("bsf %1,%0"
369        : "=r" (word)
370        : "r" (~word));
371    return word;
372}
373
374static inline unsigned long find_next_zero_bit(const unsigned long *addr,
375                                unsigned long size, unsigned long offset)
376{
377    const unsigned long* p = addr + BITOP_WORD(offset);
378    unsigned long result = offset & ~(BITS_PER_LONG-1);
379    unsigned long tmp;
380
381    if (offset >= size)
382        return size;
383
384    size -= result;
385    offset %= BITS_PER_LONG;
386
387    if (offset) {
388        tmp = *(p++);
389        tmp |= ~0UL >> (BITS_PER_LONG - offset);
390        if (size < BITS_PER_LONG)
391            goto found_first;
392        if (~tmp)
393            goto found_middle;
394        size -= BITS_PER_LONG;
395        result += BITS_PER_LONG;
396    }
397
398    while (size & ~(BITS_PER_LONG-1)) {
399        if (~(tmp = *(p++)))
400            goto found_middle;
401        result += BITS_PER_LONG;
402        size -= BITS_PER_LONG;
403    }
404
405    if (!size)
406        return result;
407
408    tmp = *p;
409
410found_first:
411
412    tmp |= ~0UL << size;
413    if (tmp == ~0UL)          /* Are any bits zero? */
414        return result + size; /* Nope. */
415
416found_middle:
417
418    return result + ffz(tmp);
419}
420
421#define do_div(n, base)                     \
422({                                  \
423    unsigned long __upper, __low, __high, __mod, __base;    \
424    __base = (base);                    \
425    asm("":"=a" (__low), "=d" (__high) : "A" (n));      \
426    __upper = __high;                       \
427    if (__high) {                       \
428        __upper = __high % (__base);            \
429        __high = __high / (__base);             \
430    }                               \
431    asm("divl %2":"=a" (__low), "=d" (__mod)        \
432        : "rm" (__base), "0" (__low), "1" (__upper));       \
433    asm("":"=A" (n) : "a" (__low), "d" (__high));       \
434    __mod;                          \
435})
436
437/****** endian stuff ******/
438
439#define le64_to_cpu(x) OSSwapLittleToHostInt64(x)
440#define be64_to_cpu(x) OSSwapBigToHostInt64(x)
441#define cpu_to_le64(x) OSSwapHostToLittleInt64(x)
442#define cpu_to_be64(x) OSSwapHostToBigInt64(x)
443
444#define le32_to_cpu(x) OSSwapLittleToHostInt32(x)
445#define be32_to_cpu(x) OSSwapBigToHostInt32(x)
446#define cpu_to_le32(x) OSSwapHostToLittleInt32(x)
447#define cpu_to_be32(x) OSSwapHostToBigInt32(x)
448
449#define le16_to_cpu(x) OSSwapLittleToHostInt16(x)
450#define be16_to_cpu(x) OSSwapBigToHostInt16(x)
451#define cpu_to_le16(x) OSSwapHostToLittleInt16(x)
452#define cpu_to_be16(x) OSSwapHostToBigInt16(x)
453
454static inline void le16_add_cpu(__le16* var, u16 val)
455{
456    *var = cpu_to_le16(le16_to_cpu(*var) + val);
457}
458
459static inline void le32_add_cpu(__le32* var, u32 val)
460{
461    *var = cpu_to_le32(le32_to_cpu(*var) + val);
462}
463
464static inline void le64_add_cpu(__le64* var, u64 val)
465{
466    *var = cpu_to_le64(le64_to_cpu(*var) + val);
467}
468
469static inline void be16_add_cpu(__be16* var, u16 val)
470{
471    *var = cpu_to_be16(be16_to_cpu(*var) + val);
472}
473
474static inline void be32_add_cpu(__be32* var, u32 val)
475{
476    *var = cpu_to_be32(be32_to_cpu(*var) + val);
477}
478
479static inline void be64_add_cpu(__be64* var, u64 val)
480{
481    *var = cpu_to_be64(be64_to_cpu(*var) + val);
482}
483
484/****** device stuff ******/
485
486#define MINORBITS    20
487#define MINORMASK    ((1U << MINORBITS) - 1)
488#define MAJOR(dev)   ((unsigned int) ((dev) >> MINORBITS))
489#define MINOR(dev)   ((unsigned int) ((dev) & MINORMASK))
490#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
491
492static inline int old_valid_dev(dev_t dev)
493{
494    return MAJOR(dev) < 256 && MINOR(dev) < 256;
495}
496
497static inline u16 old_encode_dev(dev_t dev)
498{
499    return (MAJOR(dev) << 8) | MINOR(dev);
500}
501
502static inline dev_t old_decode_dev(u16 val)
503{
504    return MKDEV((val >> 8) & 255, val & 255);
505}
506
507static inline int new_valid_dev(dev_t dev)
508{
509    return 1;
510}
511
512static inline u32 new_encode_dev(dev_t dev)
513{
514    unsigned major = MAJOR(dev);
515    unsigned minor = MINOR(dev);
516    return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
517}
518
519static inline dev_t new_decode_dev(u32 dev)
520{
521    unsigned major = (dev & 0xfff00) >> 8;
522    unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
523    return MKDEV(major, minor);
524}
525
526static inline int huge_valid_dev(dev_t dev)
527{
528    return 1;
529}
530
531static inline u64 huge_encode_dev(dev_t dev)
532{
533    return new_encode_dev(dev);
534}
535
536static inline dev_t huge_decode_dev(u64 dev)
537{
538    return new_decode_dev(dev);
539}
540
541static inline int sysv_valid_dev(dev_t dev)
542{
543    return MAJOR(dev) < (1<<14) && MINOR(dev) < (1<<18);
544}
545
546static inline u32 sysv_encode_dev(dev_t dev)
547{
548    return MINOR(dev) | (MAJOR(dev) << 18);
549}
550
551static inline unsigned sysv_major(u32 dev)
552{
553    return (dev >> 18) & 0x3fff;
554}
555
556static inline unsigned sysv_minor(u32 dev)
557{
558    return dev & 0x3ffff;
559}
560
561#endif /* __APPLE__ */
562
563#endif /* _LINUX_TYPES_H_ */