Commit b2253abf authored by Kirill Smelkov's avatar Kirill Smelkov

libgolang: Rename refobj -> object

Thinking a bit more after e82b4fab (libgolang: Objects refcounting
(initial draft)) and working on interfaces as part of context pyx -> C
move, I've came to conclusion that the better name for on-heap objects
managed by libgolang is just "object" without "ref" prefix.

-> Rename refobj -> object and amend its documentation correspondingly.
parent af4a8d80
......@@ -473,7 +473,7 @@ template<typename T> refptr<T> newref (T *_obj);
// refptr<T> is smart pointer to T which manages T lifetime via reference counting.
//
// T must provide incref/decref methods for example via inheriting from refobj.
// T must provide incref/decref methods for example via inheriting from object.
// incref/decref must be safe to use from multiple threads simultaneously.
template<typename T>
class refptr {
......@@ -585,17 +585,20 @@ inline refptr<T> newref(T *_obj) {
return p;
}
// refobj provides base-functionality for reference-counted objects.
// object is the base-class for on-heap objects.
//
// It provides incref & __decref - the user must implement decref(*).
// It provides reference-counting functionality, which, when used via refptr,
// provides automatic memory management for allocated objects.
//
// object provides incref & __decref - the user must implement decref(*).
//
// (*) this way we don't require destructor to be virtual.
class refobj {
class object {
std::atomic<int> _refcnt; // reference counter for the object
protected:
LIBGOLANG_API refobj();
LIBGOLANG_API ~refobj();
LIBGOLANG_API object();
LIBGOLANG_API ~object();
LIBGOLANG_API bool __decref();
public:
......
......@@ -202,23 +202,23 @@ struct _with_lock_guard {
// ---- reference-counted objects ----
refobj::refobj() : _refcnt(1) {}
refobj::~refobj() {
refobj *obj = this;
object::object() : _refcnt(1) {}
object::~object() {
object *obj = this;
if (obj->_refcnt != 0)
panic("~refobj: refcnt != 0");
panic("~object: refcnt != 0");
}
void refobj::incref() {
refobj *obj = this;
void object::incref() {
object *obj = this;
int refcnt_was = obj->_refcnt.fetch_add(+1);
if (refcnt_was < 1)
panic("incref: refcnt was < 1");
}
bool refobj::__decref() {
refobj *obj = this;
bool object::__decref() {
object *obj = this;
int refcnt_was = obj->_refcnt.fetch_add(-1);
if (refcnt_was < 1)
......@@ -229,8 +229,8 @@ bool refobj::__decref() {
return true; // should destroy
}
int refobj::refcnt() const {
const refobj *obj = this;
int object::refcnt() const {
const object *obj = this;
return obj->_refcnt;
}
......@@ -251,7 +251,7 @@ struct _RecvSendWaiting;
//
// (*) for example "thread" runtime works without GIL, while "gevent" runtime
// acquires GIL on every semaphore acquire.
struct _chan : refobj {
struct _chan : object {
unsigned _cap; // channel capacity (in elements)
unsigned _elemsize; // size of element
......@@ -288,7 +288,7 @@ private:
bool __recv2_(void *, _WaitGroup*, _RecvSendWaiting*);
friend _chan *_makechan(unsigned elemsize, unsigned size);
_chan() {}; // used by _makechan to init _mu, refobj, ...
_chan() {}; // used by _makechan to init _mu, object, ...
};
// _RecvSendWaiting represents a receiver/sender waiting on a chan.
......@@ -439,7 +439,7 @@ _chan *_makechan(unsigned elemsize, unsigned size) {
ch = (_chan *)zalloc(sizeof(_chan) + size*elemsize);
if (ch == NULL)
panic("makechan: alloc failed");
new (ch) _chan(); // init .refobj, ._mu, ...
new (ch) _chan(); // init .object, ._mu, ...
ch->_cap = size;
ch->_elemsize = elemsize;
......
......@@ -480,8 +480,8 @@ void _test_defer() {
}
// verify refptr/refobj
class MyObj : public refobj {
// verify refptr/object
class MyObj : public object {
public:
void decref() {
if (__decref())
......
......@@ -104,7 +104,7 @@ LIBGOLANG_API Ticker new_ticker(double dt);
//
// If the receiver is slow, Ticker does not queue events and skips them.
// Ticking can be canceled via .stop() .
struct _Ticker : refobj {
struct _Ticker : object {
chan<double> c;
private:
......@@ -137,7 +137,7 @@ LIBGOLANG_API Timer new_timer(double dt);
// Timer arranges for time event to be sent to .c channel after dt time.
//
// The timer can be stopped (.stop), or reinitialized to another time (.reset).
struct _Timer : refobj {
struct _Timer : object {
chan<double> c;
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment