source: mainline/uspace/lib/ext4/libext4_bitmap.c@ 662bd71

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 662bd71 was 662bd71, checked in by Frantisek Princ <frantisek.princ@…>, 14 years ago

added support for releasing more blocks in one operation

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2011 Frantisek Princ
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libext4
30 * @{
31 */
32
33/**
34 * @file libext4_bitmap.c
35 * @brief Ext4 bitmap operations.
36 */
37
38#include <errno.h>
39#include <libblock.h>
40#include <sys/types.h>
41#include "libext4.h"
42
43void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
44{
45 uint32_t byte_index = index / 8;
46 uint32_t bit_index = index % 8;
47
48 uint8_t *target = bitmap + byte_index;
49
50 *target &= ~ (1 << bit_index);
51}
52
53void ext4_bitmap_free_bits(uint8_t *bitmap, uint32_t index, uint32_t count)
54{
55 uint8_t *target;
56 uint32_t idx = index;
57 uint32_t remaining = count;
58 uint32_t byte_index;
59
60 while (((idx % 8) != 0) && (remaining > 0)) {
61
62 byte_index = idx / 8;
63 uint32_t bit_index = idx % 8;
64
65 target = bitmap + byte_index;
66
67 *target &= ~ (1 << bit_index);
68
69 idx++;
70 remaining--;
71 }
72
73 assert((idx % 8) == 0);
74
75 byte_index = idx / 8;
76 target = bitmap + byte_index;
77
78 while (remaining >= 8) {
79 *target = 0;
80
81 idx += 8;
82 remaining -= 8;
83 target++;
84 }
85
86 assert(remaining < 8);
87
88 while (remaining != 0) {
89
90 byte_index = idx / 8;
91 uint32_t bit_index = idx % 8;
92
93 target = bitmap + byte_index;
94
95 *target &= ~ (1 << bit_index);
96
97 idx++;
98 remaining--;
99 }
100}
101
102void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
103{
104 uint32_t byte_index = index / 8;
105 uint32_t bit_index = index % 8;
106
107 uint8_t *target = bitmap + byte_index;
108
109 *target |= 1 << bit_index;
110}
111
112bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
113{
114 uint32_t byte_index = index / 8;
115 uint32_t bit_index = index % 8;
116
117 uint8_t *target = bitmap + byte_index;
118
119 if (*target & (1 << bit_index)) {
120 return false;
121 } else {
122 return true;
123 }
124
125}
126
127int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t max)
128{
129 uint32_t idx;
130 if (start % 8) {
131 idx = start + (8 - (start % 8));
132 } else {
133 idx = start;
134 }
135
136 uint8_t *pos = bitmap + (idx / 8);
137
138 while (idx < max) {
139
140 if (*pos == 0) {
141 *pos |= 1;
142
143 *index = idx;
144 return EOK;
145 }
146
147 idx += 8;
148 ++pos;
149 }
150
151 return ENOSPC;
152}
153
154int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx,
155 uint32_t *index, uint32_t max)
156{
157 uint8_t *pos = bitmap + (start_idx / 8);
158 uint32_t idx = start_idx;
159 bool byte_part = false;
160
161 // Check the rest of byte
162 while ((idx % 8) != 0) {
163 byte_part = true;
164
165 if ((*pos & (1 << (idx % 8))) == 0) {
166 *pos |= (1 << (idx % 8));
167 *index = idx;
168 return EOK;
169 }
170
171 ++idx;
172 }
173
174 if (byte_part) {
175 ++pos;
176 }
177
178 while (idx < max) {
179
180 if ((*pos & 255) != 255) {
181 // free bit found
182 break;
183 }
184
185 idx += 8;
186 ++pos;
187 }
188
189
190 if (idx < max) {
191 for (uint8_t i = 0; i < 8; ++i) {
192 if ((*pos & (1 << i)) == 0) {
193 // free bit found
194 *pos |= (1 << i);
195 *index = idx;
196 return EOK;
197 }
198 idx++;
199 }
200 }
201
202 return ENOSPC;
203}
204
205/**
206 * @}
207 */
Note: See TracBrowser for help on using the repository browser.