1 //===-- sanitizer_list.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains implementation of a list class to be used by
11 // ThreadSanitizer, etc run-times.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_LIST_H
15 #define SANITIZER_LIST_H
17 #include "sanitizer_internal_defs.h"
19 namespace __sanitizer {
21 // Intrusive singly-linked list with size(), push_back(), push_front()
22 // pop_front(), append_front() and append_back().
23 // This class should be a POD (so that it can be put into TLS)
24 // and an object with all zero fields should represent a valid empty list.
25 // This class does not have a CTOR, so clear() should be called on all
26 // non-zero-initialized objects before using.
28 struct IntrusiveList {
29 friend class Iterator;
36 bool empty() const { return size_ == 0; }
37 uptr size() const { return size_; }
39 void push_back(Item *x) {
52 void push_front(Item *x) {
66 first_ = first_->next;
72 Item *front() { return first_; }
73 Item *back() { return last_; }
75 void append_front(IntrusiveList<Item> *l) {
81 } else if (!l->empty()) {
82 l->last_->next = first_;
89 void append_back(IntrusiveList<Item> *l) {
96 last_->next = l->first_;
103 void CheckConsistency() {
109 for (Item *i = first_; ; i = i->next) {
111 if (i == last_) break;
113 CHECK_EQ(size(), count);
114 CHECK_EQ(last_->next, 0);
118 template<class ListTy, class ItemTy>
121 explicit IteratorBase(ListTy *list)
122 : list_(list), current_(list->first_) { }
124 ItemTy *ret = current_;
125 if (current_) current_ = current_->next;
128 bool hasNext() const { return current_ != 0; }
134 typedef IteratorBase<IntrusiveList<Item>, Item> Iterator;
135 typedef IteratorBase<const IntrusiveList<Item>, const Item> ConstIterator;
137 // private, don't use directly.
143 } // namespace __sanitizer
145 #endif // SANITIZER_LIST_H