Skip to content

Commit

Permalink
Refactored RID/RID_Owner to always use O(1) allocation.
Browse files Browse the repository at this point in the history
* Implements a growing chunked allocator
* Removed redudant methods get and getptr, only getornull is supported now.
  • Loading branch information
reduz committed Feb 11, 2020
1 parent 1522d8c commit 4f16397
Show file tree
Hide file tree
Showing 47 changed files with 1,209 additions and 1,097 deletions.
41 changes: 0 additions & 41 deletions core/rid.cpp

This file was deleted.

151 changes: 12 additions & 139 deletions core/rid.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,172 +32,45 @@
#define RID_H

#include "core/list.h"
#include "core/oa_hash_map.h"
#include "core/os/memory.h"
#include "core/safe_refcount.h"
#include "core/set.h"
#include "core/typedefs.h"

class RID_OwnerBase;

class RID_Data {

friend class RID_OwnerBase;

#ifndef DEBUG_ENABLED
RID_OwnerBase *_owner;
#endif
uint32_t _id;

public:
_FORCE_INLINE_ uint32_t get_id() const { return _id; }

virtual ~RID_Data();
};
class RID_AllocBase;

class RID {
friend class RID_OwnerBase;

mutable RID_Data *_data;
friend class RID_AllocBase;
uint64_t _id;

public:
_FORCE_INLINE_ RID_Data *get_data() const { return _data; }

_FORCE_INLINE_ bool operator==(const RID &p_rid) const {

return _data == p_rid._data;
return _id == p_rid._id;
}
_FORCE_INLINE_ bool operator<(const RID &p_rid) const {

return _data < p_rid._data;
return _id < p_rid._id;
}
_FORCE_INLINE_ bool operator<=(const RID &p_rid) const {

return _data <= p_rid._data;
return _id <= p_rid._id;
}
_FORCE_INLINE_ bool operator>(const RID &p_rid) const {

return _data > p_rid._data;
return _id > p_rid._id;
}
_FORCE_INLINE_ bool operator!=(const RID &p_rid) const {

return _data != p_rid._data;
return _id != p_rid._id;
}
_FORCE_INLINE_ bool is_valid() const { return _data != NULL; }
_FORCE_INLINE_ bool is_valid() const { return _id != 0; }

_FORCE_INLINE_ uint32_t get_id() const { return _data ? _data->get_id() : 0; }
_FORCE_INLINE_ uint64_t get_id() const { return _id; }

_FORCE_INLINE_ RID() {
_data = NULL;
}
};

class RID_OwnerBase {
protected:
static SafeRefCount refcount;
_FORCE_INLINE_ void _set_data(RID &p_rid, RID_Data *p_data) {
p_rid._data = p_data;
refcount.ref();
p_data->_id = refcount.get();
#ifndef DEBUG_ENABLED
p_data->_owner = this;
#endif
}

#ifndef DEBUG_ENABLED

_FORCE_INLINE_ bool _is_owner(const RID &p_rid) const {

return this == p_rid._data->_owner;
}

_FORCE_INLINE_ void _remove_owner(RID &p_rid) {

p_rid._data->_owner = NULL;
}
#endif

public:
virtual void get_owned_list(List<RID> *p_owned) = 0;

static void init_rid();
virtual ~RID_OwnerBase() {}
};

template <class T>
class RID_Owner : public RID_OwnerBase {
public:
#ifdef DEBUG_ENABLED
mutable Set<RID_Data *> id_map;
#endif
public:
_FORCE_INLINE_ RID make_rid(T *p_data) {

RID rid;
_set_data(rid, p_data);

#ifdef DEBUG_ENABLED
id_map.insert(p_data);
#endif

return rid;
}

_FORCE_INLINE_ T *get(const RID &p_rid) {

#ifdef DEBUG_ENABLED

ERR_FAIL_COND_V(!p_rid.is_valid(), NULL);
ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), NULL);
#endif
return static_cast<T *>(p_rid.get_data());
}

_FORCE_INLINE_ T *getornull(const RID &p_rid) {

#ifdef DEBUG_ENABLED

if (p_rid.get_data()) {
ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), NULL);
}
#endif
return static_cast<T *>(p_rid.get_data());
}

_FORCE_INLINE_ T *getptr(const RID &p_rid) {

return static_cast<T *>(p_rid.get_data());
}

_FORCE_INLINE_ bool owns(const RID &p_rid) const {

if (p_rid.get_data() == NULL)
return false;
#ifdef DEBUG_ENABLED
return id_map.has(p_rid.get_data());
#else
return _is_owner(p_rid);
#endif
}

void free(RID p_rid) {

#ifdef DEBUG_ENABLED
id_map.erase(p_rid.get_data());
#else
_remove_owner(p_rid);
#endif
}

void get_owned_list(List<RID> *p_owned) {

#ifdef DEBUG_ENABLED

for (typename Set<RID_Data *>::Element *E = id_map.front(); E; E = E->next()) {
RID r;
_set_data(r, static_cast<T *>(E->get()));
p_owned->push_back(r);
}
#endif
_id = 0;
}
};

Expand Down
3 changes: 3 additions & 0 deletions core/rid_owner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "rid_owner.h"

volatile uint64_t RID_AllocBase::base_id = 1;
Loading

0 comments on commit 4f16397

Please sign in to comment.