blob: 572925bb50ac315280a81b31074fc4113b81318e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000011#include "webrtc/system_wrappers/source/map_no_stl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000013#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
14#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16namespace webrtc {
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000017
niklase@google.com470e71d2011-07-07 08:21:25 +000018MapNoStlItem::MapNoStlItem(int id, void* item)
19 : next_(0),
20 prev_(0),
21 item_id_(id),
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000022 item_ptr_(item) {
niklase@google.com470e71d2011-07-07 08:21:25 +000023}
24
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000025MapNoStlItem::~MapNoStlItem() {
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000028void* MapNoStlItem::GetItem() {
29 return item_ptr_;
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000032int MapNoStlItem::GetId() {
33 return item_id_;
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000036unsigned int MapNoStlItem::GetUnsignedId() {
37 return static_cast<unsigned int>(item_id_);
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000040void MapNoStlItem::SetItem(void* ptr) {
41 item_ptr_ = ptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
44MapNoStl::MapNoStl()
45 : critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
46 first_(0),
47 last_(0),
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000048 size_(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000049}
50
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000051MapNoStl::~MapNoStl() {
52 if (First()) {
53 WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
54 "Potential memory leak in MapNoStl");
55 while (Erase(First()) == 0)
56 {}
57 }
58 delete critical_section_;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000061int MapNoStl::Size() const {
62 return size_;
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000065int MapNoStl::Insert(int id, void* ptr) {
66 MapNoStlItem* new_item = new MapNoStlItem(id, ptr);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000068 CriticalSectionScoped lock(critical_section_);
69 MapNoStlItem* item = first_;
70 size_++;
71 if (!item) {
72 first_ = new_item;
niklase@google.com470e71d2011-07-07 08:21:25 +000073 last_ = new_item;
74 return 0;
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000075 }
76 while (item->next_) {
77 // Three scenarios
78 // 1. Item should be inserted first.
79 // 2. Item should be inserted between two items
80 // 3. Item should be inserted last
81 if (item->GetId() > id) {
82 new_item->next_ = item;
83 item->prev_ = new_item;
84 if (item == first_) {
85 first_ = new_item;
86 } else {
87 new_item->prev_ = item->prev_;
88 new_item->prev_->next_ = new_item;
89 }
90 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000091 }
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +000092 item = item->next_;
93 }
94 // 3
95 item->next_ = new_item;
96 new_item->prev_ = item;
97 last_ = new_item;
98 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000099}
100
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000101MapNoStlItem* MapNoStl::First() const {
102 return first_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103}
104
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000105MapNoStlItem* MapNoStl::Last() const {
106 return last_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000109MapNoStlItem* MapNoStl::Next(MapNoStlItem* item) const {
110 if (!item) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 return 0;
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000112 }
113 return item->next_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114}
115
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000116MapNoStlItem* MapNoStl::Previous(MapNoStlItem* item) const {
117 if (!item) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000118 return 0;
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000119 }
120 return item->prev_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121}
phoglund@webrtc.org6e0ce732012-12-18 17:18:35 +0000122
123MapNoStlItem* MapNoStl::Find(int id) const {
124 CriticalSectionScoped lock(critical_section_);
125 MapNoStlItem* item = Locate(id);
126 return item;
127}
128
129int MapNoStl::Erase(MapNoStlItem* item) {
130 if (!item) {
131 return -1;
132 }
133 CriticalSectionScoped lock(critical_section_);
134 return Remove(item);
135}
136
137int MapNoStl::Erase(const int id) {
138 CriticalSectionScoped lock(critical_section_);
139 MapNoStlItem* item = Locate(id);
140 if (!item) {
141 return -1;
142 }
143 return Remove(item);
144}
145
146MapNoStlItem* MapNoStl::Locate(int id) const {
147 MapNoStlItem* item = first_;
148 while (item) {
149 if (item->GetId() == id) {
150 return item;
151 }
152 item = item->next_;
153 }
154 return 0;
155}
156
157int MapNoStl::Remove(MapNoStlItem* item) {
158 if (!item) {
159 return -1;
160 }
161 size_--;
162 MapNoStlItem* previous_item = item->prev_;
163 MapNoStlItem* next_item = item->next_;
164 if (!previous_item) {
165 next_item->prev_ = 0;
166 first_ = next_item;
167 } else {
168 previous_item->next_ = next_item;
169 }
170 if (!next_item) {
171 previous_item->next_ = 0;
172 last_ = previous_item;
173 } else {
174 next_item->prev_ = previous_item;
175 }
176 delete item;
177 return 0;
178}
179
niklase@google.com470e71d2011-07-07 08:21:25 +0000180} // namespace webrtc