Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions be/src/common/object_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,42 @@ namespace doris {
// Thread-safe.
class ObjectPool {
public:
ObjectPool() : _objects() {}
ObjectPool() = default;

~ObjectPool() { clear(); }

template <class T>
T* add(T* t) {
// Create the object to be pushed to the shared vector outside the critical section.
// TODO: Consider using a lock-free structure.
SpecificElement<T>* obj = new SpecificElement<T>(t);
DCHECK(obj != NULL);
std::lock_guard<SpinLock> l(_lock);
_objects.push_back(obj);
_objects.emplace_back(Element{t, [](void* obj) { delete reinterpret_cast<T*>(obj); }});
return t;
}

void clear() {
std::lock_guard<SpinLock> l(_lock);
for (auto i = _objects.rbegin(); i != _objects.rend(); ++i) {
delete *i;
}
for (Element& elem : _objects) elem.delete_fn(elem.obj);
_objects.clear();
}

// Absorb all objects from src pool
// Note: This method is not thread safe
void acquire_data(ObjectPool* src) {
_objects.insert(_objects.end(), src->_objects.begin(), src->_objects.end());
src->_objects.clear();
}

private:
struct GenericElement {
virtual ~GenericElement() {}
};
DISALLOW_COPY_AND_ASSIGN(ObjectPool);

template <class T>
struct SpecificElement : GenericElement {
SpecificElement(T* t) : t(t) {}
~SpecificElement() { delete t; }
/// A generic deletion function pointer. Deletes its first argument.
using DeleteFn = void (*)(void*);

T* t;
/// For each object, a pointer to the object and a function that deletes it.
struct Element {
void* obj;
DeleteFn delete_fn;
};

typedef std::vector<GenericElement*> ElementVector;
ElementVector _objects;

std::vector<Element> _objects;
SpinLock _lock;
};

Expand Down