blob: 0d233509102a748ecca38b6f152d56bd270ad677 [file] [log] [blame]
Andy McFadden2867f0b2010-12-10 11:08:22 -08001/*
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
Carl Shapiroae188c62011-04-08 13:11:58 -070023#ifdef __cplusplus
24extern "C" {
25#endif
26
Andy McFadden2867f0b2010-12-10 11:08:22 -080027/*
28 * Expanding bitmap, used for tracking resources. Bits are numbered starting
29 * from zero.
30 *
31 * All operations on a BitVector are unsynchronized.
32 */
33typedef struct BitVector {
34 bool expandable; /* expand bitmap if we run out? */
Andy McFadden9fd527f2010-12-10 15:34:00 -080035 u4 storageSize; /* current size, in 32-bit words */
Andy McFadden2867f0b2010-12-10 11:08:22 -080036 u4* storage;
37} BitVector;
38
Ben Cheng00603072010-10-28 11:13:58 -070039/* Handy iterator to walk through the bit positions set to 1 */
40typedef struct BitVectorIterator {
41 BitVector *pBits;
42 u4 idx;
43 u4 bitSize;
44} BitVectorIterator;
45
Andy McFadden2867f0b2010-12-10 11:08:22 -080046/* allocate a bit vector with enough space to hold "startBits" bits */
Andy McFadden9fd527f2010-12-10 15:34:00 -080047BitVector* dvmAllocBitVector(unsigned int startBits, bool expandable);
Andy McFadden2867f0b2010-12-10 11:08:22 -080048void dvmFreeBitVector(BitVector* pBits);
49
50/*
51 * dvmAllocBit always allocates the first possible bit. If we run out of
52 * space in the bitmap, and it's not marked expandable, dvmAllocBit
53 * returns -1.
54 *
55 * dvmSetBit sets the specified bit, expanding the vector if necessary
Andy McFadden9fd527f2010-12-10 15:34:00 -080056 * (and possible). Attempting to set a bit past the limit of a non-expandable
57 * bit vector will cause a fatal error.
Andy McFadden2867f0b2010-12-10 11:08:22 -080058 *
Ben Cheng00603072010-10-28 11:13:58 -070059 * dvmSetInitialBits sets all bits in [0..numBits-1]. Won't expand the vector.
60 *
Andy McFadden2867f0b2010-12-10 11:08:22 -080061 * dvmIsBitSet returns "true" if the bit is set.
62 */
63int dvmAllocBit(BitVector* pBits);
Andy McFadden9fd527f2010-12-10 15:34:00 -080064void dvmSetBit(BitVector* pBits, unsigned int num);
65void dvmClearBit(BitVector* pBits, unsigned int num);
Andy McFadden2867f0b2010-12-10 11:08:22 -080066void dvmClearAllBits(BitVector* pBits);
Andy McFadden9fd527f2010-12-10 15:34:00 -080067void dvmSetInitialBits(BitVector* pBits, unsigned int numBits);
68bool dvmIsBitSet(const BitVector* pBits, unsigned int num);
Andy McFadden2867f0b2010-12-10 11:08:22 -080069
70/* count the number of bits that have been set */
71int dvmCountSetBits(const BitVector* pBits);
72
Andy McFadden9fd527f2010-12-10 15:34:00 -080073/* copy one vector to another of equal size */
74void dvmCopyBitVector(BitVector *dest, const BitVector *src);
Andy McFadden2867f0b2010-12-10 11:08:22 -080075
76/*
Ben Cheng00603072010-10-28 11:13:58 -070077 * Intersect two bit vectors and store the result to the dest vector.
Andy McFadden2867f0b2010-12-10 11:08:22 -080078 */
79bool dvmIntersectBitVectors(BitVector *dest, const BitVector *src1,
80 const BitVector *src2);
81
Ben Cheng00603072010-10-28 11:13:58 -070082/*
83 * Unify two bit vectors and store the result to the dest vector.
84 */
85bool dvmUnifyBitVectors(BitVector *dest, const BitVector *src1,
86 const BitVector *src2);
87
88/*
Andy McFadden9fd527f2010-12-10 15:34:00 -080089 * Merge the contents of "src" into "dst", checking to see if this causes
90 * any changes to occur.
91 *
92 * Returns "true" if the contents of the destination vector were modified.
93 */
94bool dvmCheckMergeBitVectors(BitVector* dst, const BitVector* src);
95
96/*
Ben Cheng00603072010-10-28 11:13:58 -070097 * Compare two bit vectors and return true if difference is seen.
98 */
99bool dvmCompareBitVectors(const BitVector *src1, const BitVector *src2);
100
101/* Initialize the iterator structure */
102void dvmBitVectorIteratorInit(BitVector* pBits, BitVectorIterator* iterator);
103
104/* Return the next position set to 1. -1 means end-of-vector reached */
105int dvmBitVectorIteratorNext(BitVectorIterator* iterator);
Andy McFadden2867f0b2010-12-10 11:08:22 -0800106
Carl Shapiroae188c62011-04-08 13:11:58 -0700107#ifdef __cplusplus
108}
109#endif
110
Andy McFadden2867f0b2010-12-10 11:08:22 -0800111#endif /*_DALVIK_BITVECTOR*/