inital commit
[c11concurrency-benchmarks.git] / mabain / src / util / mb_lsq.cpp
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #include "mb_lsq.h"
20 #include "error.h"
21
22 namespace mabain {
23
24 MBlsq::MBlsq(void (*free_fn)(void *)) : FreeFunc(free_fn)
25 {
26     head = NULL;
27     tail = NULL;
28     count = 0;
29 }
30
31 void MBlsq::Clear()
32 {
33     LSQ_Node *node;
34     while(head != NULL)
35     {
36         node = head;
37         head = head->next;
38         if(FreeFunc)
39             FreeFunc(node->data.data_ptr);
40         free(node);
41     }
42
43     head = NULL;
44     tail = NULL;
45     count = 0;
46 }
47
48 MBlsq::~MBlsq()
49 {
50     Clear();
51 }
52
53 uint64_t MBlsq::Count() const
54 {
55     return count;
56 }
57
58 int MBlsq::AddToHead(void *ptr)
59 {
60     LSQ_Node *node = (LSQ_Node *) malloc(sizeof(*node));
61     if(node == NULL)
62         return MBError::NO_MEMORY;
63
64     node->data.data_ptr = ptr;
65     node->next = head;
66     head = node;
67     if(count == 0)
68         tail = node;
69
70     count++;
71     return MBError::SUCCESS;
72 }
73
74 int MBlsq::AddIntToHead(int64_t value)
75 {
76     LSQ_Node *node = (LSQ_Node *) malloc(sizeof(*node));
77     if(node == NULL)
78         return MBError::NO_MEMORY;
79
80     node->data.value = value;
81     node->next = head;
82     head = node;
83     if(count == 0)
84         tail = node;
85
86     count++;
87     return MBError::SUCCESS;
88 }
89
90 int MBlsq::AddIntToTail(int64_t value)
91 {
92     LSQ_Node *node = (LSQ_Node *) malloc(sizeof(*node));
93     if(node == NULL)
94         return MBError::NO_MEMORY;
95
96     node->data.value = value;
97     node->next = NULL;
98     if(tail)
99     {
100         tail->next = node;
101         tail = node;
102     }
103     else
104     {
105         head = node;
106         tail = node;
107     }
108
109     count++;
110     return MBError::SUCCESS;
111 }
112
113 int MBlsq::AddToTail(void *ptr)
114 {
115     LSQ_Node *node = (LSQ_Node *) malloc(sizeof(*node));
116     if(node == NULL)
117         return MBError::NO_MEMORY;
118
119     node->data.data_ptr = ptr;
120     node->next = NULL;
121     if(tail != NULL)
122     {
123         tail->next = node;
124     }
125     else
126     {
127         head = node;
128     }
129
130     tail = node;
131     count++;
132     return MBError::SUCCESS;
133 }
134
135 void* MBlsq::RemoveFromHead()
136 {
137     void *data;
138
139     if(count > 1)
140     {
141         LSQ_Node *node = head;
142         data = head->data.data_ptr;
143         head = head->next;
144         count--;
145         free(node);
146     }
147     else if(count == 1)
148     {
149         LSQ_Node *node = head;
150         data = head->data.data_ptr;
151         head = NULL;
152         tail = NULL;
153         count = 0;
154         free(node);
155     }
156     else
157     {
158         data = NULL;
159     }
160
161     return data;
162 }
163
164 int64_t MBlsq::RemoveIntFromHead()
165 {
166     int64_t value;
167
168     if(count > 1)
169     {
170         LSQ_Node *node = head;
171         value = head->data.value;
172         head = head->next;
173         count--;
174         free(node);
175     }
176     else if(count == 1)
177     {
178         LSQ_Node *node = head;
179         value = head->data.value;
180         head = NULL;
181         tail = NULL;
182         count = 0;
183         free(node);
184     }
185     else
186     {
187         value = 0;
188     }
189
190     return value;
191 }
192
193 }