headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / storage / disk_device / PartitioningInfo.cpp
blobef15e9673809dfbbffa790881e34f8d44c32744f
1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
3 // by the MIT License.
4 //---------------------------------------------------------------------
6 #include <stdio.h>
7 #include <string.h>
9 #include <new>
11 #include <disk_device_manager.h>
12 #include <PartitioningInfo.h>
14 #include <syscalls.h>
15 #include <ddm_userland_interface_defs.h>
18 using namespace std;
21 //#define TRACING 1
22 #if TRACING
23 # define TRACE(x) printf x
24 #else
25 # define TRACE(x)
26 #endif
29 // constructor
30 BPartitioningInfo::BPartitioningInfo()
32 fPartitionID(-1),
33 fSpaces(NULL),
34 fCount(0),
35 fCapacity(0)
40 // destructor
41 BPartitioningInfo::~BPartitioningInfo()
43 Unset();
47 // SetTo
48 status_t
49 BPartitioningInfo::SetTo(off_t offset, off_t size)
51 TRACE(("%p - BPartitioningInfo::SetTo(offset = %lld, size = %lld)\n", this, offset, size));
53 fPartitionID = -1;
54 delete[] fSpaces;
56 if (size > 0) {
57 fSpaces = new(nothrow) partitionable_space_data[1];
58 if (!fSpaces)
59 return B_NO_MEMORY;
61 fCount = 1;
62 fSpaces[0].offset = offset;
63 fSpaces[0].size = size;
64 } else {
65 fSpaces = NULL;
66 fCount = 0;
69 fCapacity = fCount;
71 return B_OK;
75 // Unset
76 void
77 BPartitioningInfo::Unset()
79 delete[] fSpaces;
80 fPartitionID = -1;
81 fSpaces = NULL;
82 fCount = 0;
83 fCapacity = 0;
87 // ExcludeOccupiedSpace
88 status_t
89 BPartitioningInfo::ExcludeOccupiedSpace(off_t offset, off_t size)
91 if (size <= 0)
92 return B_OK;
94 TRACE(("%p - BPartitioningInfo::ExcludeOccupiedSpace(offset = %lld, "
95 "size = %lld)\n", this, offset, size));
97 int32 leftIndex = -1;
98 int32 rightIndex = -1;
99 for (int32 i = 0; i < fCount; i++) {
100 if (leftIndex == -1
101 && offset < fSpaces[i].offset + fSpaces[i].size) {
102 leftIndex = i;
105 if (fSpaces[i].offset < offset + size)
106 rightIndex = i;
109 TRACE((" leftIndex = %ld, rightIndex = %ld\n", leftIndex, rightIndex));
111 // If there's no intersection with any free space, we're done.
112 if (leftIndex == -1 || rightIndex == -1 || leftIndex > rightIndex)
113 return B_OK;
115 partitionable_space_data& leftSpace = fSpaces[leftIndex];
116 partitionable_space_data& rightSpace = fSpaces[rightIndex];
118 off_t rightSpaceEnd = rightSpace.offset + rightSpace.size;
120 // special case: split a single space in two
121 if (leftIndex == rightIndex && leftSpace.offset < offset
122 && rightSpaceEnd > offset + size) {
124 TRACE((" splitting space at %ld\n", leftIndex));
126 // add a space after this one
127 status_t error = _InsertSpaces(leftIndex + 1, 1);
128 if (error != B_OK)
129 return error;
131 // IMPORTANT: after calling _InsertSpace(), one can not
132 // use the partitionable_space_data references "leftSpace"
133 // and "rightSpace" anymore (declared above)!
135 partitionable_space_data& space = fSpaces[leftIndex];
136 partitionable_space_data& newSpace = fSpaces[leftIndex + 1];
138 space.size = offset - space.offset;
140 newSpace.offset = offset + size;
141 newSpace.size = rightSpaceEnd - newSpace.offset;
143 #ifdef TRACING
144 PrintToStream();
145 #endif
146 return B_OK;
149 // check whether the first affected space is eaten completely
150 int32 deleteFirst = leftIndex;
151 if (leftSpace.offset < offset) {
152 leftSpace.size = offset - leftSpace.offset;
154 TRACE((" left space remains, new size is %lld\n", leftSpace.size));
156 deleteFirst++;
159 // check whether the last affected space is eaten completely
160 int32 deleteLast = rightIndex;
161 if (rightSpaceEnd > offset + size) {
162 rightSpace.offset = offset + size;
163 rightSpace.size = rightSpaceEnd - rightSpace.offset;
165 TRACE((" right space remains, new offset = %lld, size = %lld\n",
166 rightSpace.offset, rightSpace.size));
168 deleteLast--;
171 // remove all spaces that are completely eaten
172 if (deleteFirst <= deleteLast)
173 _RemoveSpaces(deleteFirst, deleteLast - deleteFirst + 1);
175 #ifdef TRACING
176 PrintToStream();
177 #endif
178 return B_OK;
182 // PartitionID
183 partition_id
184 BPartitioningInfo::PartitionID() const
186 return fPartitionID;
190 // GetPartitionableSpaceAt
191 status_t
192 BPartitioningInfo::GetPartitionableSpaceAt(int32 index, off_t* offset,
193 off_t *size) const
195 if (!fSpaces)
196 return B_NO_INIT;
197 if (!offset || !size)
198 return B_BAD_VALUE;
199 if (index < 0 || index >= fCount)
200 return B_BAD_INDEX;
201 *offset = fSpaces[index].offset;
202 *size = fSpaces[index].size;
203 return B_OK;
207 // CountPartitionableSpaces
208 int32
209 BPartitioningInfo::CountPartitionableSpaces() const
211 return fCount;
215 // PrintToStream
216 void
217 BPartitioningInfo::PrintToStream() const
219 if (!fSpaces) {
220 printf("BPartitioningInfo is not initialized\n");
221 return;
223 printf("BPartitioningInfo has %" B_PRId32 " spaces:\n", fCount);
224 for (int32 i = 0; i < fCount; i++) {
225 printf(" space at %" B_PRId32 ": offset = %" B_PRId64 ", size = %"
226 B_PRId64 "\n", i, fSpaces[i].offset, fSpaces[i].size);
231 // #pragma mark -
234 // _InsertSpaces
235 status_t
236 BPartitioningInfo::_InsertSpaces(int32 index, int32 count)
238 if (index <= 0 || index > fCount || count <= 0)
239 return B_BAD_VALUE;
241 // If the capacity is sufficient, we just need to copy the spaces to create
242 // a gap.
243 if (fCount + count <= fCapacity) {
244 memmove(fSpaces + index + count, fSpaces + index,
245 (fCount - index) * sizeof(partitionable_space_data));
246 fCount += count;
247 return B_OK;
250 // alloc new array
251 int32 capacity = (fCount + count) * 2;
252 // add a bit room for further resizing
254 partitionable_space_data* spaces
255 = new(nothrow) partitionable_space_data[capacity];
256 if (!spaces)
257 return B_NO_MEMORY;
259 // copy items
260 memcpy(spaces, fSpaces, index * sizeof(partitionable_space_data));
261 memcpy(spaces + index + count, fSpaces + index,
262 (fCount - index) * sizeof(partitionable_space_data));
264 delete[] fSpaces;
265 fSpaces = spaces;
266 fCapacity = capacity;
267 fCount += count;
269 return B_OK;
273 // _RemoveSpaces
274 void
275 BPartitioningInfo::_RemoveSpaces(int32 index, int32 count)
277 if (index < 0 || count <= 0 || index + count > fCount)
278 return;
280 if (count < fCount) {
281 int32 endIndex = index + count;
282 memmove(fSpaces + index, fSpaces + endIndex,
283 (fCount - endIndex) * sizeof(partitionable_space_data));
286 fCount -= count;