1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "id_allocator.h"
17 #include "intell_voice_log.h"
18 
19 using namespace std;
20 
21 #define LOG_TAG "IdAllocator"
22 
23 namespace OHOS {
24 namespace IntellVoiceUtils {
IdAllocator(int maxTimerNum)25 IdAllocator::IdAllocator(int maxTimerNum)
26 {
27     for (int i = 0; i < maxTimerNum; i++) {
28         idFlags.push_back(false);
29     }
30 }
31 
AllocId()32 int IdAllocator::AllocId()
33 {
34     for (unsigned int i = 0; i < idFlags.size(); i++) {
35         if (!idFlags[i]) {
36             idFlags[i] = true;
37             return i;
38         }
39     }
40 
41     return INVALID_ID;
42 }
43 
ReleaseId(unsigned int id)44 void IdAllocator::ReleaseId(unsigned int id)
45 {
46     if (id >= idFlags.size()) {
47         INTELL_VOICE_LOG_ERROR("try to release invalid id %d", id);
48         return;
49     }
50     INTELL_VOICE_LOG_INFO("release id %u", id);
51     idFlags[id] = false;
52 }
53 
ClearId()54 void IdAllocator::ClearId()
55 {
56     for (unsigned int i = 0; i < idFlags.size(); i++) {
57         idFlags[i] = false;
58     }
59 }
60 }
61 }