2011-03-23 14:44:19 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2011-03-23 14:44:19 +00:00
|
|
|
|
|
|
|
#ifndef V8_SMALL_POINTER_LIST_H_
|
|
|
|
#define V8_SMALL_POINTER_LIST_H_
|
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/logging.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/globals.h"
|
|
|
|
#include "src/zone.h"
|
2011-03-23 14:44:19 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// SmallPointerList is a list optimized for storing no or just a
|
|
|
|
// single value. When more values are given it falls back to ZoneList.
|
|
|
|
//
|
|
|
|
// The interface tries to be as close to List from list.h as possible.
|
|
|
|
template <typename T>
|
|
|
|
class SmallPointerList {
|
|
|
|
public:
|
|
|
|
SmallPointerList() : data_(kEmptyTag) {}
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
SmallPointerList(int capacity, Zone* zone) : data_(kEmptyTag) {
|
|
|
|
Reserve(capacity, zone);
|
2011-08-22 14:23:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
void Reserve(int capacity, Zone* zone) {
|
2011-08-22 14:23:37 +00:00
|
|
|
if (capacity < 2) return;
|
|
|
|
if ((data_ & kTagMask) == kListTag) {
|
|
|
|
if (list()->capacity() >= capacity) return;
|
|
|
|
int old_length = list()->length();
|
2012-06-11 12:42:31 +00:00
|
|
|
list()->AddBlock(NULL, capacity - list()->capacity(), zone);
|
2011-08-22 14:23:37 +00:00
|
|
|
list()->Rewind(old_length);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
PointerList* list = new(zone) PointerList(capacity, zone);
|
2011-08-22 14:23:37 +00:00
|
|
|
if ((data_ & kTagMask) == kSingletonTag) {
|
2012-06-11 12:42:31 +00:00
|
|
|
list->Add(single_value(), zone);
|
2011-08-22 14:23:37 +00:00
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsAligned(reinterpret_cast<intptr_t>(list), kPointerAlignment));
|
2011-08-22 14:23:37 +00:00
|
|
|
data_ = reinterpret_cast<intptr_t>(list) | kListTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
data_ = kEmptyTag;
|
|
|
|
}
|
|
|
|
|
2012-03-23 16:37:54 +00:00
|
|
|
void Sort() {
|
|
|
|
if ((data_ & kTagMask) == kListTag) {
|
|
|
|
list()->Sort(compare_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 14:44:19 +00:00
|
|
|
bool is_empty() const { return length() == 0; }
|
|
|
|
|
|
|
|
int length() const {
|
|
|
|
if ((data_ & kTagMask) == kEmptyTag) return 0;
|
|
|
|
if ((data_ & kTagMask) == kSingletonTag) return 1;
|
|
|
|
return list()->length();
|
|
|
|
}
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
void Add(T* pointer, Zone* zone) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsAligned(reinterpret_cast<intptr_t>(pointer), kPointerAlignment));
|
2011-03-23 14:44:19 +00:00
|
|
|
if ((data_ & kTagMask) == kEmptyTag) {
|
|
|
|
data_ = reinterpret_cast<intptr_t>(pointer) | kSingletonTag;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((data_ & kTagMask) == kSingletonTag) {
|
2012-06-11 12:42:31 +00:00
|
|
|
PointerList* list = new(zone) PointerList(2, zone);
|
|
|
|
list->Add(single_value(), zone);
|
|
|
|
list->Add(pointer, zone);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsAligned(reinterpret_cast<intptr_t>(list), kPointerAlignment));
|
2011-03-23 14:44:19 +00:00
|
|
|
data_ = reinterpret_cast<intptr_t>(list) | kListTag;
|
|
|
|
return;
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
list()->Add(pointer, zone);
|
2011-03-23 14:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: returns T* and not T*& (unlike List from list.h).
|
|
|
|
// This makes the implementation simpler and more const correct.
|
|
|
|
T* at(int i) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK((data_ & kTagMask) != kEmptyTag);
|
2011-03-23 14:44:19 +00:00
|
|
|
if ((data_ & kTagMask) == kSingletonTag) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(i == 0);
|
2011-03-23 14:44:19 +00:00
|
|
|
return single_value();
|
|
|
|
}
|
|
|
|
return list()->at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See the note above.
|
|
|
|
T* operator[](int i) const { return at(i); }
|
|
|
|
|
|
|
|
// Remove the given element from the list (if present).
|
|
|
|
void RemoveElement(T* pointer) {
|
|
|
|
if ((data_ & kTagMask) == kEmptyTag) return;
|
|
|
|
if ((data_ & kTagMask) == kSingletonTag) {
|
|
|
|
if (pointer == single_value()) {
|
|
|
|
data_ = kEmptyTag;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list()->RemoveElement(pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
T* RemoveLast() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK((data_ & kTagMask) != kEmptyTag);
|
2011-03-23 14:44:19 +00:00
|
|
|
if ((data_ & kTagMask) == kSingletonTag) {
|
|
|
|
T* result = single_value();
|
|
|
|
data_ = kEmptyTag;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return list()->RemoveLast();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rewind(int pos) {
|
|
|
|
if ((data_ & kTagMask) == kEmptyTag) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(pos == 0);
|
2011-03-23 14:44:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((data_ & kTagMask) == kSingletonTag) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(pos == 0 || pos == 1);
|
2011-03-23 14:44:19 +00:00
|
|
|
if (pos == 0) {
|
|
|
|
data_ = kEmptyTag;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list()->Rewind(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CountOccurrences(T* pointer, int start, int end) const {
|
|
|
|
if ((data_ & kTagMask) == kEmptyTag) return 0;
|
|
|
|
if ((data_ & kTagMask) == kSingletonTag) {
|
|
|
|
if (start == 0 && end >= 0) {
|
|
|
|
return (single_value() == pointer) ? 1 : 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return list()->CountOccurrences(pointer, start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef ZoneList<T*> PointerList;
|
|
|
|
|
2012-03-23 16:37:54 +00:00
|
|
|
static int compare_value(T* const* a, T* const* b) {
|
|
|
|
return Compare<T>(**a, **b);
|
|
|
|
}
|
|
|
|
|
2011-03-23 14:44:19 +00:00
|
|
|
static const intptr_t kEmptyTag = 1;
|
|
|
|
static const intptr_t kSingletonTag = 0;
|
|
|
|
static const intptr_t kListTag = 2;
|
|
|
|
static const intptr_t kTagMask = 3;
|
|
|
|
static const intptr_t kValueMask = ~kTagMask;
|
|
|
|
|
|
|
|
STATIC_ASSERT(kTagMask + 1 <= kPointerAlignment);
|
|
|
|
|
|
|
|
T* single_value() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK((data_ & kTagMask) == kSingletonTag);
|
2011-03-23 14:44:19 +00:00
|
|
|
STATIC_ASSERT(kSingletonTag == 0);
|
|
|
|
return reinterpret_cast<T*>(data_);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerList* list() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK((data_ & kTagMask) == kListTag);
|
2011-03-23 14:44:19 +00:00
|
|
|
return reinterpret_cast<PointerList*>(data_ & kValueMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t data_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SmallPointerList);
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_SMALL_POINTER_LIST_H_
|