vfs: check userland buffers before reading them.
[haiku.git] / src / system / kernel / util / Bitmap.cpp
blob8f47c95e19f8481a2726b151ac1408f25989ac21
1 /*
2 * Copyright 2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Paweł Dziepak, pdziepak@quarnos.org
7 */
10 #include <util/Bitmap.h>
12 #include <new>
14 #include <string.h>
16 #include <util/BitUtils.h>
19 const int Bitmap::kBitsPerElement = sizeof(addr_t) * 8;
22 Bitmap::Bitmap(int bitCount)
24 fInitStatus(B_OK),
25 fElementsCount(0),
26 fSize(bitCount)
28 int count = fSize + kBitsPerElement - 1;
29 count /= kBitsPerElement;
31 fBits = new(std::nothrow) addr_t[count];
32 if (fBits == NULL) {
33 fSize = 0;
34 fInitStatus = B_NO_MEMORY;
37 fElementsCount = count;
38 memset(fBits, 0, sizeof(addr_t) * count);
42 Bitmap::~Bitmap()
44 delete[] fBits;
48 int
49 Bitmap::GetHighestSet() const
51 int i = fElementsCount - 1;
52 while (i >= 0 && fBits[i] == 0)
53 i--;
55 if (i < 0)
56 return -1;
58 STATIC_ASSERT(sizeof(addr_t) == sizeof(uint64)
59 || sizeof(addr_t) == sizeof(uint32));
60 if (sizeof(addr_t) == sizeof(uint32))
61 return log2(fBits[i]) + i * kBitsPerElement;
63 uint32 v = (uint64)fBits[i] >> 32;
64 if (v != 0)
65 return log2(v) + sizeof(uint32) * 8 + i * kBitsPerElement;
66 return log2(fBits[i]) + i * kBitsPerElement;