/* * Copyright (C) 2024 Amlogic Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "BitsMap.h" #include #define BYTE_IDX(idx) (idx >> 3) #define BIT_OFFSET(idx) (idx & 0x7) BitsMap::BitsMap(int bits) { mLen = BYTE_IDX(bits) + 1; mBits = new char[mLen]; if (mBits != NULL) memset(mBits, 0, mLen); } BitsMap::~BitsMap() { if (mBits != NULL) delete [] mBits; } int BitsMap::getZeroBit() { int i = 0; if (mBits == NULL) return -1; for (; i < mLen; i++) { if (mBits[i] == 0xFF) { continue; } char byteval = mBits[i]; int idx = 0; if ((byteval & 0xF) == 0xF) { byteval >>= 4; idx += 4; } if ((byteval & 0x3) == 0x3) { byteval >>= 2; idx += 2; } if ((byteval & 0x1) == 0x1) { byteval >>= 1; idx += 1; } idx += i * 8; return idx; } return -1; } int BitsMap::setBit(int idx) { if (mBits != NULL) mBits[BYTE_IDX(idx)] |= 1 << BIT_OFFSET(idx); return 0; } int BitsMap::clearBit(int idx) { if (mBits != NULL) mBits[BYTE_IDX(idx)] &= ~(1 << BIT_OFFSET(idx)); return 0; }