diff --git a/experiments/placer/Makefile b/experiments/placer/Makefile index d39cf009995..ade84628fb3 100644 --- a/experiments/placer/Makefile +++ b/experiments/placer/Makefile @@ -8,6 +8,6 @@ run: placer clean: $(RM) *.o placer -placer:placer.o +placer: placer.o .PHONY: all run clean diff --git a/experiments/placer/placer.cc b/experiments/placer/placer.cc index 80136d7a27c..a7139eebfc2 100644 --- a/experiments/placer/placer.cc +++ b/experiments/placer/placer.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -58,7 +59,7 @@ class Container { } }; -// Raw reference to data, meaning T*, only for cleaness of intentions. +// Raw reference to data, meaning T*, invented only for cleaness of intentions. template class RawRef { private: @@ -69,8 +70,7 @@ class RawRef { void set(const T& value) { *ptr_ = value; } }; -// Object reference, adds reference counting in container. In real implementation -// we may want to differentiate between +// Object reference, adds reference counting in container. template class ObjRef { private: @@ -107,6 +107,12 @@ class ObjRef { return *reinterpret_cast(ptr_); } + template + RawRef at() const { + return RawRef( + reinterpret_cast(reinterpret_cast(ref()) + offset)); + } + void Assign(const ObjRef& other) { // TODO: optimize for an important case where containers match. if (ptr_) { @@ -118,10 +124,11 @@ class ObjRef { } } - template - RawRef at() const { - return RawRef( - reinterpret_cast(reinterpret_cast(ref()) + offset)); + ObjRef Clone(Container* container) { + ObjRef result = Alloc(container); + // TODO: take into account object references in T! + memcpy(result.ref(), ref(), sizeof(T)); + return result; } bool null() const { return ptr_ == nullptr; } @@ -136,8 +143,17 @@ struct List { int data_; }; +void UpdateElement(ObjRef element) { + element.at().set + (element.at().get() + 10); +} + +void DoNotUpdateElement(ObjRef element) { + element.at().set + (element.at().get() + 100); +} + void test_placer() { - printf("Start placement\n"); Container heap(1024); constexpr int next_offset = offsetof(struct List, next_); constexpr int data_offset = offsetof(struct List, data_); @@ -151,6 +167,18 @@ void test_placer() { cur.at().set(i + 2); } cur = head; + // Pass by reference. + while (!cur.null()) { + UpdateElement(cur); + cur = cur.at, next_offset>().get(); + } + // Pass by value. + while (!cur.null()) { + // We could place clone on stack as well. + DoNotUpdateElement(cur.Clone(&heap)); + cur = cur.at, next_offset>().get(); + } + cur = head; while (!cur.null()) { printf("next is %d\n", cur.at().get()); cur = cur.at, next_offset>().get();