Whamcloud - gitweb
Fixes some scability and access to not inited memory problems
[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 BITMAP_SIZE(nbits)      (((nbits/BITS_PER_LONG)+1)*sizeof(long)+sizeof(bitmap_t))
32
33 static inline
34 bitmap_t *ALLOCATE_BITMAP(int size)
35 {
36         bitmap_t *ptr;
37
38         OBD_ALLOC(ptr, BITMAP_SIZE(size));
39         if (ptr == NULL)
40                 RETURN(ptr);
41
42         ptr->size = size;
43         memset(ptr->data, 0, BITMAP_SIZE(size));
44
45         RETURN (ptr);
46 }
47
48 #define FREE_BITMAP(ptr)        OBD_FREE(ptr, BITMAP_SIZE(ptr->size))
49
50 static inline
51 void bitmap_set(bitmap_t *bitmap, int nbit)
52 {
53         set_bit(nbit, bitmap->data);
54 }
55
56 static inline
57 void bitmap_clear(bitmap_t *bitmap, int nbit)
58 {
59         clear_bit(nbit, bitmap->data);
60 }
61
62 static inline
63 int bitmap_check(bitmap_t *bitmap, int nbit)
64 {
65         int pos = nbit % BITS_PER_LONG;
66         return test_bit(pos, bitmap->data+(nbit/BITS_PER_LONG));
67 }
68
69 /* return 0 is bitmap has none set bits */
70 static inline
71 int bitmap_check_empty(bitmap_t *bitmap)
72 {
73         return find_first_bit(bitmap->data, bitmap->size) == bitmap->size;
74 }
75
76
77 #define foreach_bit(bitmap, pos) \
78         for((pos)=find_first_bit((bitmap)->data, bitmap->size);   \
79             (pos) < (bitmap)->size;                               \
80             (pos) = find_next_bit((bitmap)->data, (bitmap)->size, (pos)))
81
82 #endif