aboutsummaryrefslogtreecommitdiff
path: root/src/system_wrappers/source/list_no_stl.cc
blob: d45f27b310777018894261e8e27f293805115817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "list_wrapper.h"

#include "critical_section_wrapper.h"
#include "trace.h"

namespace webrtc {
ListItem::ListItem(const void* item)
    : next_(0),
      prev_(0),
      item_ptr_(item),
      item_(0)
{
}

ListItem::ListItem(const unsigned int item)
    : next_(0),
      prev_(0),
      item_ptr_(0),
      item_(item)
{
}

ListItem::~ListItem()
{
}

void* ListItem::GetItem() const
{
    return const_cast<void*>(item_ptr_);
}

unsigned int ListItem::GetUnsignedItem() const
{
    return item_;
}

ListWrapper::ListWrapper()
    : critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
      first_(0),
      last_(0),
      size_(0)
{
}

ListWrapper::~ListWrapper()
{
    if (!Empty())
    {
        // TODO (hellner) I'm not sure this loggin is useful.
        WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
                   "Potential memory leak in ListWrapper");
        // Remove all remaining list items.
        while (Erase(First()) == 0)
        {}
    }
    delete critical_section_;
}

bool ListWrapper::Empty() const
{
    return !first_ && !last_;
}

unsigned int ListWrapper::GetSize() const
{
    return size_;
}

int ListWrapper::PushBack(const void* ptr)
{
    ListItem* item = new ListItem(ptr);
    CriticalSectionScoped lock(*critical_section_);
    PushBackImpl(item);
    return 0;
}

int ListWrapper::PushBack(const unsigned int item_id)
{
    ListItem* item = new ListItem(item_id);
    CriticalSectionScoped lock(*critical_section_);
    PushBackImpl(item);
    return 0;
}

int ListWrapper::PushFront(const unsigned int item_id)
{
    ListItem* item = new ListItem(item_id);
    CriticalSectionScoped lock(*critical_section_);
    PushFrontImpl(item);
    return 0;
}

int ListWrapper::PushFront(const void* ptr)
{
    ListItem* item = new ListItem(ptr);
    CriticalSectionScoped lock(*critical_section_);
    PushFrontImpl(item);
    return 0;
}

int ListWrapper::PopFront()
{
    return Erase(first_);
}

int ListWrapper::PopBack()
{
    return Erase(last_);
}

ListItem* ListWrapper::First() const
{
    return first_;
}

ListItem* ListWrapper::Last() const
{
    return last_;
}

ListItem* ListWrapper::Next(ListItem* item) const
{
    if(!item)
    {
        return 0;
    }
    return item->next_;
}

ListItem* ListWrapper::Previous(ListItem* item) const
{
    if (!item)
    {
        return 0;
    }
    return item->prev_;
}

int ListWrapper::Insert(ListItem* existing_previous_item, ListItem* new_item)
{
    if (!new_item)
    {
        return -1;
    }
    // Allow existing_previous_item to be NULL if the list is empty.
    // TODO (hellner) why allow this? Keep it as is for now to avoid
    // breaking API contract.
    if (!existing_previous_item && !Empty())
    {
        return -1;
    }
    CriticalSectionScoped lock(*critical_section_);
    if (!existing_previous_item)
    {
        PushBackImpl(new_item);
        return 0;
    }
    ListItem* next_item = existing_previous_item->next_;
    new_item->next_ = existing_previous_item->next_;
    new_item->prev_ = existing_previous_item;
    existing_previous_item->next_ = new_item;
    if (next_item)
    {
        next_item->prev_ = new_item;
    }
    else
    {
        last_ = new_item;
    }
    size_++;
    return 0;
}

int ListWrapper::InsertBefore(ListItem* existing_next_item,
                              ListItem* new_item)
{
    if (!new_item)
    {
        return -1;
    }
    // Allow existing_next_item to be NULL if the list is empty.
    // Todo: why allow this? Keep it as is for now to avoid breaking API
    // contract.
    if (!existing_next_item && !Empty())
    {
        return -1;
    }
    CriticalSectionScoped lock(*critical_section_);
    if (!existing_next_item)
    {
        PushBackImpl(new_item);
        return 0;
    }

    ListItem* previous_item = existing_next_item->prev_;
    new_item->next_ = existing_next_item;
    new_item->prev_ = previous_item;
    existing_next_item->prev_ = new_item;
    if (previous_item)
    {
        previous_item->next_ = new_item;
    }
    else
    {
        first_ = new_item;
    }
    size_++;
    return 0;
}

int ListWrapper::Erase(ListItem* item)
{
    if (!item)
    {
        return -1;
    }
    size_--;
    ListItem* previous_item = item->prev_;
    ListItem* next_item = item->next_;
    if (!previous_item)
    {
        if(next_item)
        {
            next_item->prev_ = 0;
        }
        first_ = next_item;
    }
    else
    {
        previous_item->next_ = next_item;
    }
    if (!next_item)
    {
        if(previous_item)
        {
            previous_item->next_ = 0;
        }
        last_ = previous_item;
    }
    else
    {
        next_item->prev_ = previous_item;
    }
    delete item;
    return 0;
}

void ListWrapper::PushBackImpl(ListItem* item)
{
    if (Empty())
    {
        first_ = item;
        last_ = item;
        size_++;
        return;
    }

    item->prev_ = last_;
    last_->next_ = item;
    last_ = item;
    size_++;
}

void ListWrapper::PushFrontImpl(ListItem* item)
{
    if (Empty())
    {
        first_ = item;
        last_ = item;
        size_++;
        return;
    }

    item->next_ = first_;
    first_->prev_ = item;
    first_ = item;
    size_++;
}
} //namespace webrtc