• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()21 void 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()32 void 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() const44 bool LinkStack::IsEmpty() const
45 {
46     if (head_ == top_) {
47         return true;
48     }
49     return false;
50 }
51 
IsFull() const52 bool LinkStack::IsFull() const
53 {
54     if (maxSize_ > 0 && count_ >= maxSize_) {
55         return true;
56     }
57     return false;
58 }
59 
Push(const char * value)60 bool LinkStack::Push(const char *value)
61 {
62     if (value == nullptr || top_ == nullptr) {
63         return false;
64     }
65     if (IsFull()) {
66         HILOG_ERROR(HILOG_MODULE_ACE, "stack is full");
67         return false;
68     }
69 
70     StackNode *node = new StackNode(value);
71     if (node == nullptr) {
72         return false;
73     }
74 
75     top_->SetNodeNext(node);
76     node->SetNodePrev(top_);
77     count_++;
78     top_ = top_->GetNodeNext();
79     return true;
80 }
81 
Pop(const char ** value)82 bool LinkStack::Pop(const char **value)
83 {
84     if (top_ == nullptr || IsEmpty()) {
85         return false;
86     }
87     StackNode *tmp = top_;
88     if (value != nullptr) {
89         *value = top_->GetNodeData();
90     }
91     top_->SetNodeData(nullptr);
92     top_ = top_->GetNodePrev();
93     delete tmp;
94     tmp = nullptr;
95     top_->SetNodeNext(nullptr);
96     count_--;
97     return true;
98 }
99 } // namespace ACELite
100 } // namespace OHOS
101