| 43 | } |
| 44 | |
| 45 | int BitMap::allocate(const int count) |
| 46 | { |
| 47 | if (count == 0) |
| 48 | return -1; |
| 49 | |
| 50 | int index, empty, start; |
| 51 | |
| 52 | index = 0; |
| 53 | while (index < length) |
| 54 | { |
| 55 | // 越过已经分配的资源 |
| 56 | while (index < length && get(index)) |
| 57 | ++index; |
| 58 | |
| 59 | // 不存在连续的count个资源 |
| 60 | if (index == length) |
| 61 | return -1; |
| 62 | |
| 63 | // 找到1个未分配的资源 |
| 64 | // 检查是否存在从index开始的连续count个资源 |
| 65 | empty = 0; |
| 66 | start = index; |
| 67 | while ((index < length) && (!get(index)) && (empty < count)) |
| 68 | { |
| 69 | ++empty; |
| 70 | ++index; |
| 71 | } |
| 72 | |
| 73 | // 存在连续的count个资源 |
| 74 | if (empty == count) |
| 75 | { |
| 76 | for (int i = 0; i < count; ++i) |
| 77 | { |
| 78 | set(start + i, true); |
| 79 | } |
| 80 | |
| 81 | return start; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | void BitMap::release(const int index, const int count) |
| 89 | { |
no outgoing calls
no test coverage detected