ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sparc / lib / bitext.c
1 /*
2  * bitext.c: kernel little helper (of bit shuffling variety).
3  *
4  * Copyright (C) 2002 Pete Zaitcev <zaitcev@yahoo.com>
5  *
6  * The algorithm to search a zero bit string is geared towards its application.
7  * We expect a couple of fixed sizes of requests, so a rotating counter, reset
8  * by align size, should provide fast enough search while maintaining low
9  * fragmentation.
10  */
11
12 #include <linux/smp_lock.h>
13
14 #include <asm/bitext.h>
15 #include <asm/bitops.h>
16
17 /**
18  * bit_map_string_get - find and set a bit string in bit map.
19  * @t: the bit map.
20  * @len: requested string length
21  * @align: requested alignment
22  *
23  * Returns offset in the map or -1 if out of space.
24  *
25  * Not safe to call from an interrupt (uses spin_lock).
26  */
27 int bit_map_string_get(struct bit_map *t, int len, int align)
28 {
29         int offset, count;      /* siamese twins */
30         int off_new;
31         int align1;
32         int i;
33
34         if (align == 0)
35                 align = 1;
36         align1 = align - 1;
37         if ((align & align1) != 0)
38                 BUG();
39         if (align < 0 || align >= t->size)
40                 BUG();
41         if (len <= 0 || len > t->size)
42                 BUG();
43
44         spin_lock(&t->lock);
45         if (len < t->last_size)
46                 offset = t->first_free;
47         else
48                 offset = t->last_off & ~align1;
49         count = 0;
50         for (;;) {
51                 off_new = find_next_zero_bit(t->map, t->size, offset);
52                 off_new = (off_new + align1) & ~align1;
53                 count += off_new - offset;
54                 offset = off_new;
55                 if (offset >= t->size)
56                         offset = 0;
57                 if (count + len > t->size) {
58                         spin_unlock(&t->lock);
59 /* P3 */ printk(KERN_ERR
60   "bitmap out: size %d used %d off %d len %d align %d count %d\n",
61   t->size, t->used, offset, len, align, count);
62                         return -1;
63                 }
64
65                 if (offset + len > t->size) {
66                         count += t->size - offset;
67                         offset = 0;
68                         continue;
69                 }
70
71                 i = 0;
72                 while (test_bit(offset + i, t->map) == 0) {
73                         i++;
74                         if (i == len) {
75                                 for (i = 0; i < len; i++)
76                                         __set_bit(offset + i, t->map);
77                                 if (offset == t->first_free)
78                                         t->first_free = find_next_zero_bit
79                                                         (t->map, t->size,
80                                                          t->first_free + len);
81                                 if ((t->last_off = offset + len) >= t->size)
82                                         t->last_off = 0;
83                                 t->used += len;
84                                 t->last_size = len;
85                                 spin_unlock(&t->lock);
86                                 return offset;
87                         }
88                 }
89                 count += i + 1;
90                 if ((offset += i + 1) >= t->size)
91                         offset = 0;
92         }
93 }
94
95 void bit_map_clear(struct bit_map *t, int offset, int len)
96 {
97         int i;
98
99         if (t->used < len)
100                 BUG();          /* Much too late to do any good, but alas... */
101         spin_lock(&t->lock);
102         for (i = 0; i < len; i++) {
103                 if (test_bit(offset + i, t->map) == 0)
104                         BUG();
105                 __clear_bit(offset + i, t->map);
106         }
107         if (offset < t->first_free)
108                 t->first_free = offset;
109         t->used -= len;
110         spin_unlock(&t->lock);
111 }
112
113 void bit_map_init(struct bit_map *t, unsigned long *map, int size)
114 {
115
116         if ((size & 07) != 0)
117                 BUG();
118         memset(map, 0, size>>3);
119
120         memset(t, 0, sizeof *t);
121         spin_lock_init(&t->lock);
122         t->map = map;
123         t->size = size;
124         t->last_size = 0;
125         t->first_free = 0;
126 }