Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lnet / include / libcfs / bitmap.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2007 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #ifndef _LIBCFS_BITMAP_H_
23 #define _LIBCFS_BITMAP_H_
24
25
26 typedef struct {
27         int             size;
28         unsigned long   data[0];
29 } bitmap_t;
30
31 #define CFS_BITMAP_SIZE(nbits) \
32      (((nbits/BITS_PER_LONG)+1)*sizeof(long)+sizeof(bitmap_t))
33
34 static inline
35 bitmap_t *ALLOCATE_BITMAP(int size)
36 {
37         bitmap_t *ptr;
38
39         OBD_ALLOC(ptr, CFS_BITMAP_SIZE(size));
40         if (ptr == NULL)
41                 RETURN(ptr);
42
43         ptr->size = size;
44
45         RETURN (ptr);
46 }
47
48 #define FREE_BITMAP(ptr)        OBD_FREE(ptr, CFS_BITMAP_SIZE(ptr->size))
49
50 static inline
51 void cfs_bitmap_set(bitmap_t *bitmap, int nbit)
52 {
53         set_bit(nbit, bitmap->data);
54 }
55
56 static inline
57 void cfs_bitmap_clear(bitmap_t *bitmap, int nbit)
58 {
59         clear_bit(nbit, bitmap->data);
60 }
61
62 static inline
63 int cfs_bitmap_check(bitmap_t *bitmap, int nbit)
64 {
65         return test_bit(nbit, bitmap->data);
66 }
67
68 /* return 0 is bitmap has none set bits */
69 static inline
70 int cfs_bitmap_check_empty(bitmap_t *bitmap)
71 {
72         return find_first_bit(bitmap->data, bitmap->size) == bitmap->size;
73 }
74
75 #define cfs_foreach_bit(bitmap, pos) \
76         for((pos)=find_first_bit((bitmap)->data, bitmap->size);   \
77             (pos) < (bitmap)->size;                               \
78             (pos) = find_next_bit((bitmap)->data, (bitmap)->size, (pos)))
79
80 #endif