提交 adf12bd3 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

c++ wrapper for marked pointers

上级 5fd96933
...@@ -17,6 +17,12 @@ operator new(unsigned long nbytes, void *buf) ...@@ -17,6 +17,12 @@ operator new(unsigned long nbytes, void *buf)
return buf; return buf;
} }
void *
operator new[](unsigned long nbytes)
{
return kmalloc(nbytes);
}
void void
operator delete(void *p) operator delete(void *p)
{ {
......
差异被折叠。
...@@ -5,6 +5,82 @@ ...@@ -5,6 +5,82 @@
using std::atomic; using std::atomic;
struct crange; struct crange;
struct range;
template<class T>
class markptr_ptr;
template<class T>
class markptr_mark;
template<class T>
class markptr {
protected:
atomic<uptr> _p;
public:
markptr() : _p(0) {}
markptr(T* v) : _p((uptr) v) {}
markptr(const markptr<T> &v) : _p(v._p.load()) {}
void operator=(T* v) { _p = (uptr) v; }
void operator=(const markptr<T> &v) { _p = v._p.load(); }
bool operator!=(const markptr<T> &v) const { return _p != v._p; }
bool operator==(const markptr<T> &v) const { return _p == v._p; }
markptr_ptr<T>& ptr() {
return *(markptr_ptr<T>*) this;
}
markptr_mark<T>& mark() {
return *(markptr_mark<T>*) this;
}
// Convenience operator to avoid having to write out xx.ptr()->...
T* operator->() { return ptr(); }
bool cmpxch(markptr<T> expected, markptr<T> desired) {
uptr ee = expected._p.load();
return _p.compare_exchange_weak(ee, desired._p.load());
}
};
template<class T>
class markptr_ptr : private markptr<T> {
public:
void operator=(T *ptr) {
uptr p0, p1;
do {
p0 = markptr<T>::_p.load();
p1 = (p0 & 1) | (uptr) ptr;
} while (!markptr<T>::_p.compare_exchange_weak(p0, p1));
}
T* load() const {
return (T*) (markptr<T>::_p.load() & ~1);
}
operator T*() const { return load(); }
};
template<class T>
class markptr_mark : public markptr<T> {
public:
void operator=(bool mark) {
uptr p0, p1;
do {
p0 = markptr<T>::_p.load();
p1 = (p0 & ~1) | !!mark;
} while (!markptr<T>::_p.compare_exchange_weak(p0, p1));
}
bool load() const {
return markptr<T>::_p.load() & 1;
}
operator bool() const { return load(); }
};
struct range : public rcu_freed { struct range : public rcu_freed {
public: public:
...@@ -14,9 +90,9 @@ public: ...@@ -14,9 +90,9 @@ public:
atomic<int> curlevel; // the current levels it appears on atomic<int> curlevel; // the current levels it appears on
int nlevel; // the number of levels this range should appear int nlevel; // the number of levels this range should appear
crange *cr; // the crange this range is part of crange *cr; // the crange this range is part of
struct range** next; // one next pointer per level markptr<range>* next; // one next pointer per level
struct spinlock *lock; // on separate cache line? spinlock *lock; // on separate cache line?
range(crange *cr, u64 k, u64 sz, void *v, struct range *n, int nlevel); range(crange *cr, u64 k, u64 sz, void *v, markptr<range> n, int nlevel = 0);
~range(); ~range();
virtual void do_gc() { virtual void do_gc() {
delete this; delete this;
...@@ -24,7 +100,7 @@ public: ...@@ -24,7 +100,7 @@ public:
void print(int l); void print(int l);
void free_delayed(); void free_delayed();
void dec_ref(void); void dec_ref(void);
int lockif(range *e); int lockif(markptr<range> e);
} __mpalign__; } __mpalign__;
struct crange { struct crange {
...@@ -41,7 +117,7 @@ public: ...@@ -41,7 +117,7 @@ public:
void print(int); void print(int);
void check(struct range *absent); void check(struct range *absent);
int del_index(range *p0, range **e, int l); int del_index(range *p0, range **e, int l);
void add_index(int l, range *e, range *p1, range *s1); void add_index(int l, range *e, range *p1, markptr<range> s1);
int lock_range(u64 k, u64 sz, int l, range **er, range **pr, range **fr, range **lr, range **sr); int lock_range(u64 k, u64 sz, int l, range **er, range **pr, range **fr, range **lr, range **sr);
int find_and_lock(u64 k, u64 sz, range **p0, range **f0, range **l0, range **s0); int find_and_lock(u64 k, u64 sz, range **p0, range **f0, range **l0, range **s0);
}; };
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论