1 /* 2 * Copyright (c) 2022 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 "link_queue.h" 17 #include "ace_log.h" 18 19 namespace OHOS { 20 namespace ACELite { InitQueue()21void LinkQueue::InitQueue() 22 { 23 front_ = new QueueNode(nullptr); 24 if (front_ == nullptr) { 25 HILOG_ERROR(HILOG_MODULE_ACE, "create front failed"); 26 return; 27 } 28 rear_ = front_; 29 length_ = 0; 30 } 31 IsEmpty() const32bool LinkQueue::IsEmpty() const 33 { 34 if (front_ == rear_ && length_ == 0) { 35 return true; 36 } 37 return false; 38 } 39 IsFull() const40bool LinkQueue::IsFull() const 41 { 42 if (maxSize_ > 0 && length_ >= maxSize_) { 43 return true; 44 } 45 return false; 46 } 47 LengthQueue() const48uint32_t LinkQueue::LengthQueue() const 49 { 50 return length_; 51 } 52 Enqueue(const char * value)53bool LinkQueue::Enqueue(const char *value) 54 { 55 if (value == nullptr || rear_ == nullptr) { 56 return false; 57 } 58 if (IsFull()) { 59 HILOG_ERROR(HILOG_MODULE_ACE, "queue is full"); 60 return false; 61 } 62 QueueNode* node = new QueueNode(value); 63 if (node == nullptr) { 64 HILOG_ERROR(HILOG_MODULE_ACE, "create queue node failed"); 65 return false; 66 } 67 rear_->SetNodeNext(node); 68 rear_ = node; 69 length_++; 70 return true; 71 } 72 Dequeue(const char ** value)73bool LinkQueue::Dequeue(const char **value) 74 { 75 if (front_ == nullptr || IsEmpty()) { 76 return false; 77 } 78 QueueNode *tmp = front_->GetNodeNext(); 79 if (value != nullptr) { 80 *value = tmp->GetNodeData(); 81 } 82 delete front_; 83 front_ = tmp; 84 length_--; 85 return true; 86 } 87 GetNext() const88QueueNode *LinkQueue::GetNext() const 89 { 90 return front_ ? front_->GetNodeNext() : nullptr; 91 } 92 FreeNode()93void LinkQueue::FreeNode() 94 { 95 QueueNode *tmp = front_; 96 length_ = 0; 97 while (tmp != nullptr) { 98 front_ = front_->GetNodeNext(); 99 delete tmp; 100 tmp = front_; 101 } 102 rear_ = nullptr; 103 } 104 } // namespace ACELite 105 } // namespace OHOS 106