| Andy McFadden | 2867f0b | 2010-12-10 11:08:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Miscellaneous utility functions. |
| 19 | */ |
| 20 | #ifndef _DALVIK_BITVECTOR |
| 21 | #define _DALVIK_BITVECTOR |
| 22 | |
| 23 | /* |
| 24 | * Expanding bitmap, used for tracking resources. Bits are numbered starting |
| 25 | * from zero. |
| 26 | * |
| 27 | * All operations on a BitVector are unsynchronized. |
| 28 | */ |
| 29 | typedef struct BitVector { |
| 30 | bool expandable; /* expand bitmap if we run out? */ |
| 31 | int storageSize; /* current size, in 32-bit words */ |
| 32 | u4* storage; |
| 33 | } BitVector; |
| 34 | |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame^] | 35 | /* Handy iterator to walk through the bit positions set to 1 */ |
| 36 | typedef struct BitVectorIterator { |
| 37 | BitVector *pBits; |
| 38 | u4 idx; |
| 39 | u4 bitSize; |
| 40 | } BitVectorIterator; |
| 41 | |
| Andy McFadden | 2867f0b | 2010-12-10 11:08:22 -0800 | [diff] [blame] | 42 | /* allocate a bit vector with enough space to hold "startBits" bits */ |
| 43 | BitVector* dvmAllocBitVector(int startBits, bool expandable); |
| 44 | void dvmFreeBitVector(BitVector* pBits); |
| 45 | |
| 46 | /* |
| 47 | * dvmAllocBit always allocates the first possible bit. If we run out of |
| 48 | * space in the bitmap, and it's not marked expandable, dvmAllocBit |
| 49 | * returns -1. |
| 50 | * |
| 51 | * dvmSetBit sets the specified bit, expanding the vector if necessary |
| 52 | * (and possible). |
| 53 | * |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame^] | 54 | * dvmSetInitialBits sets all bits in [0..numBits-1]. Won't expand the vector. |
| 55 | * |
| Andy McFadden | 2867f0b | 2010-12-10 11:08:22 -0800 | [diff] [blame] | 56 | * dvmIsBitSet returns "true" if the bit is set. |
| 57 | */ |
| 58 | int dvmAllocBit(BitVector* pBits); |
| 59 | bool dvmSetBit(BitVector* pBits, int num); |
| 60 | void dvmClearBit(BitVector* pBits, int num); |
| 61 | void dvmClearAllBits(BitVector* pBits); |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame^] | 62 | void dvmSetInitialBits(BitVector* pBits, int numBits); |
| Andy McFadden | 2867f0b | 2010-12-10 11:08:22 -0800 | [diff] [blame] | 63 | bool dvmIsBitSet(const BitVector* pBits, int num); |
| 64 | |
| 65 | /* count the number of bits that have been set */ |
| 66 | int dvmCountSetBits(const BitVector* pBits); |
| 67 | |
| 68 | /* copy one vector to the other compatible one */ |
| 69 | bool dvmCopyBitVector(BitVector *dest, const BitVector *src); |
| 70 | |
| 71 | /* |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame^] | 72 | * Intersect two bit vectors and store the result to the dest vector. |
| Andy McFadden | 2867f0b | 2010-12-10 11:08:22 -0800 | [diff] [blame] | 73 | */ |
| 74 | bool dvmIntersectBitVectors(BitVector *dest, const BitVector *src1, |
| 75 | const BitVector *src2); |
| 76 | |
| Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame^] | 77 | /* |
| 78 | * Unify two bit vectors and store the result to the dest vector. |
| 79 | */ |
| 80 | bool dvmUnifyBitVectors(BitVector *dest, const BitVector *src1, |
| 81 | const BitVector *src2); |
| 82 | |
| 83 | /* |
| 84 | * Compare two bit vectors and return true if difference is seen. |
| 85 | */ |
| 86 | bool dvmCompareBitVectors(const BitVector *src1, const BitVector *src2); |
| 87 | |
| 88 | /* Initialize the iterator structure */ |
| 89 | void dvmBitVectorIteratorInit(BitVector* pBits, BitVectorIterator* iterator); |
| 90 | |
| 91 | /* Return the next position set to 1. -1 means end-of-vector reached */ |
| 92 | int dvmBitVectorIteratorNext(BitVectorIterator* iterator); |
| Andy McFadden | 2867f0b | 2010-12-10 11:08:22 -0800 | [diff] [blame] | 93 | |
| 94 | #endif /*_DALVIK_BITVECTOR*/ |