blob: adbb97dc70dceda840bcf7672e0978f223653105 [file] [log] [blame]
Kostya Serebryany5a2327c2012-07-06 09:03:45 +00001//===-- sanitizer_list.h ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains implementation of a list class to be used by
11// ThreadSanitizer, etc run-times.
12//
13//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080014
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000015#ifndef SANITIZER_LIST_H
16#define SANITIZER_LIST_H
17
18#include "sanitizer_internal_defs.h"
19
20namespace __sanitizer {
21
22// Intrusive singly-linked list with size(), push_back(), push_front()
23// pop_front(), append_front() and append_back().
24// This class should be a POD (so that it can be put into TLS)
25// and an object with all zero fields should represent a valid empty list.
26// This class does not have a CTOR, so clear() should be called on all
27// non-zero-initialized objects before using.
28template<class Item>
29struct IntrusiveList {
Stephen Hines2d1fdb22014-05-28 23:58:16 -070030 friend class Iterator;
31
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000032 void clear() {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080033 first_ = last_ = nullptr;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000034 size_ = 0;
35 }
36
37 bool empty() const { return size_ == 0; }
38 uptr size() const { return size_; }
39
40 void push_back(Item *x) {
41 if (empty()) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080042 x->next = nullptr;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000043 first_ = last_ = x;
44 size_ = 1;
45 } else {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080046 x->next = nullptr;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000047 last_->next = x;
48 last_ = x;
49 size_++;
50 }
51 }
52
53 void push_front(Item *x) {
54 if (empty()) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080055 x->next = nullptr;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000056 first_ = last_ = x;
57 size_ = 1;
58 } else {
59 x->next = first_;
60 first_ = x;
61 size_++;
62 }
63 }
64
65 void pop_front() {
66 CHECK(!empty());
67 first_ = first_->next;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080068 if (!first_)
69 last_ = nullptr;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000070 size_--;
71 }
72
73 Item *front() { return first_; }
74 Item *back() { return last_; }
75
76 void append_front(IntrusiveList<Item> *l) {
77 CHECK_NE(this, l);
Dmitry Vyukov312de6a2013-01-11 10:15:13 +000078 if (l->empty())
79 return;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000080 if (empty()) {
81 *this = *l;
Kostya Serebryanydeafb6b2012-07-06 14:32:00 +000082 } else if (!l->empty()) {
83 l->last_->next = first_;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000084 first_ = l->first_;
85 size_ += l->size();
86 }
87 l->clear();
88 }
89
90 void append_back(IntrusiveList<Item> *l) {
91 CHECK_NE(this, l);
Dmitry Vyukov312de6a2013-01-11 10:15:13 +000092 if (l->empty())
93 return;
Kostya Serebryany5a2327c2012-07-06 09:03:45 +000094 if (empty()) {
95 *this = *l;
96 } else {
97 last_->next = l->first_;
98 last_ = l->last_;
99 size_ += l->size();
100 }
101 l->clear();
102 }
103
104 void CheckConsistency() {
105 if (size_ == 0) {
106 CHECK_EQ(first_, 0);
107 CHECK_EQ(last_, 0);
108 } else {
109 uptr count = 0;
110 for (Item *i = first_; ; i = i->next) {
111 count++;
112 if (i == last_) break;
113 }
114 CHECK_EQ(size(), count);
115 CHECK_EQ(last_->next, 0);
116 }
117 }
118
Stephen Hines86277eb2015-03-23 12:06:32 -0700119 template<class ListTy, class ItemTy>
120 class IteratorBase {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700121 public:
Stephen Hines86277eb2015-03-23 12:06:32 -0700122 explicit IteratorBase(ListTy *list)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700123 : list_(list), current_(list->first_) { }
Stephen Hines86277eb2015-03-23 12:06:32 -0700124 ItemTy *next() {
125 ItemTy *ret = current_;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700126 if (current_) current_ = current_->next;
127 return ret;
128 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800129 bool hasNext() const { return current_ != nullptr; }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700130 private:
Stephen Hines86277eb2015-03-23 12:06:32 -0700131 ListTy *list_;
132 ItemTy *current_;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700133 };
134
Stephen Hines86277eb2015-03-23 12:06:32 -0700135 typedef IteratorBase<IntrusiveList<Item>, Item> Iterator;
136 typedef IteratorBase<const IntrusiveList<Item>, const Item> ConstIterator;
137
Kostya Serebryany5a2327c2012-07-06 09:03:45 +0000138// private, don't use directly.
139 uptr size_;
140 Item *first_;
141 Item *last_;
142};
143
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800144} // namespace __sanitizer
Kostya Serebryany5a2327c2012-07-06 09:03:45 +0000145
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800146#endif // SANITIZER_LIST_H