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_stack.h" 17 #include "ace_log.h" 18 19 namespace OHOS { 20 namespace ACELite { InitStack()21void LinkStack::InitStack() 22 { 23 head_ = new StackNode(nullptr); 24 if (head_ == nullptr) { 25 HILOG_ERROR(HILOG_MODULE_ACE, "create head failed"); 26 return; 27 } 28 top_ = head_; 29 count_ = 0; 30 } 31 FreeNode()32void LinkStack::FreeNode() 33 { 34 StackNode *tmp = head_; 35 count_ = 0; 36 while (tmp != nullptr) { 37 head_ = head_->GetNodeNext(); 38 delete tmp; 39 tmp = head_; 40 } 41 top_ = nullptr; 42 } 43 IsEmpty() const44bool LinkStack::IsEmpty() const 45 { 46 return head_ == top_; 47 } 48 IsFull() const49bool LinkStack::IsFull() const 50 { 51 return maxSize_ > 0 && count_ >= maxSize_; 52 } 53 Push(const char * value)54bool LinkStack::Push(const char *value) 55 { 56 if (value == nullptr || top_ == nullptr) { 57 return false; 58 } 59 if (IsFull()) { 60 HILOG_ERROR(HILOG_MODULE_ACE, "stack is full"); 61 return false; 62 } 63 64 StackNode *node = new StackNode(value); 65 if (node == nullptr) { 66 return false; 67 } 68 69 top_->SetNodeNext(node); 70 node->SetNodePrev(top_); 71 count_++; 72 top_ = top_->GetNodeNext(); 73 return true; 74 } 75 Pop(const char ** value)76bool LinkStack::Pop(const char **value) 77 { 78 if (top_ == nullptr || IsEmpty()) { 79 return false; 80 } 81 StackNode *tmp = top_; 82 if (value != nullptr) { 83 *value = top_->GetNodeData(); 84 } 85 top_->SetNodeData(nullptr); 86 top_ = top_->GetNodePrev(); 87 delete tmp; 88 tmp = nullptr; 89 top_->SetNodeNext(nullptr); 90 count_--; 91 return true; 92 } 93 } // namespace ACELite 94 } // namespace OHOS 95