niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/map_no_stl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 13 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 14 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 17 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | MapNoStlItem::MapNoStlItem(int id, void* item) |
| 19 | : next_(0), |
| 20 | prev_(0), |
| 21 | item_id_(id), |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 22 | item_ptr_(item) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | } |
| 24 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 25 | MapNoStlItem::~MapNoStlItem() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | } |
| 27 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 28 | void* MapNoStlItem::GetItem() { |
| 29 | return item_ptr_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 32 | int MapNoStlItem::GetId() { |
| 33 | return item_id_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 36 | unsigned int MapNoStlItem::GetUnsignedId() { |
| 37 | return static_cast<unsigned int>(item_id_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 40 | void MapNoStlItem::SetItem(void* ptr) { |
| 41 | item_ptr_ = ptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | MapNoStl::MapNoStl() |
| 45 | : critical_section_(CriticalSectionWrapper::CreateCriticalSection()), |
| 46 | first_(0), |
| 47 | last_(0), |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 48 | size_(0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 51 | MapNoStl::~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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 61 | int MapNoStl::Size() const { |
| 62 | return size_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 65 | int MapNoStl::Insert(int id, void* ptr) { |
| 66 | MapNoStlItem* new_item = new MapNoStlItem(id, ptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 68 | CriticalSectionScoped lock(critical_section_); |
| 69 | MapNoStlItem* item = first_; |
| 70 | size_++; |
| 71 | if (!item) { |
| 72 | first_ = new_item; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | last_ = new_item; |
| 74 | return 0; |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 75 | } |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | } |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 92 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 101 | MapNoStlItem* MapNoStl::First() const { |
| 102 | return first_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 105 | MapNoStlItem* MapNoStl::Last() const { |
| 106 | return last_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 109 | MapNoStlItem* MapNoStl::Next(MapNoStlItem* item) const { |
| 110 | if (!item) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | return 0; |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 112 | } |
| 113 | return item->next_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 116 | MapNoStlItem* MapNoStl::Previous(MapNoStlItem* item) const { |
| 117 | if (!item) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | return 0; |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 119 | } |
| 120 | return item->prev_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
phoglund@webrtc.org | 6e0ce73 | 2012-12-18 17:18:35 +0000 | [diff] [blame] | 122 | |
| 123 | MapNoStlItem* MapNoStl::Find(int id) const { |
| 124 | CriticalSectionScoped lock(critical_section_); |
| 125 | MapNoStlItem* item = Locate(id); |
| 126 | return item; |
| 127 | } |
| 128 | |
| 129 | int MapNoStl::Erase(MapNoStlItem* item) { |
| 130 | if (!item) { |
| 131 | return -1; |
| 132 | } |
| 133 | CriticalSectionScoped lock(critical_section_); |
| 134 | return Remove(item); |
| 135 | } |
| 136 | |
| 137 | int 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 | |
| 146 | MapNoStlItem* 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 | |
| 157 | int 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | } // namespace webrtc |