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); ...@@ -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. // 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. // incref/decref must be safe to use from multiple threads simultaneously.
template<typename T> template<typename T>
class refptr { class refptr {
...@@ -585,17 +585,20 @@ inline refptr<T> newref(T *_obj) { ...@@ -585,17 +585,20 @@ inline refptr<T> newref(T *_obj) {
return p; 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. // (*) this way we don't require destructor to be virtual.
class refobj { class object {
std::atomic<int> _refcnt; // reference counter for the object std::atomic<int> _refcnt; // reference counter for the object
protected: protected:
LIBGOLANG_API refobj(); LIBGOLANG_API object();
LIBGOLANG_API ~refobj(); LIBGOLANG_API ~object();
LIBGOLANG_API bool __decref(); LIBGOLANG_API bool __decref();
public: public:
......
...@@ -202,23 +202,23 @@ struct _with_lock_guard { ...@@ -202,23 +202,23 @@ struct _with_lock_guard {
// ---- reference-counted objects ---- // ---- reference-counted objects ----
refobj::refobj() : _refcnt(1) {} object::object() : _refcnt(1) {}
refobj::~refobj() { object::~object() {
refobj *obj = this; object *obj = this;
if (obj->_refcnt != 0) if (obj->_refcnt != 0)
panic("~refobj: refcnt != 0"); panic("~object: refcnt != 0");
} }
void refobj::incref() { void object::incref() {
refobj *obj = this; object *obj = this;
int refcnt_was = obj->_refcnt.fetch_add(+1); int refcnt_was = obj->_refcnt.fetch_add(+1);
if (refcnt_was < 1) if (refcnt_was < 1)
panic("incref: refcnt was < 1"); panic("incref: refcnt was < 1");
} }
bool refobj::__decref() { bool object::__decref() {
refobj *obj = this; object *obj = this;
int refcnt_was = obj->_refcnt.fetch_add(-1); int refcnt_was = obj->_refcnt.fetch_add(-1);
if (refcnt_was < 1) if (refcnt_was < 1)
...@@ -229,8 +229,8 @@ bool refobj::__decref() { ...@@ -229,8 +229,8 @@ bool refobj::__decref() {
return true; // should destroy return true; // should destroy
} }
int refobj::refcnt() const { int object::refcnt() const {
const refobj *obj = this; const object *obj = this;
return obj->_refcnt; return obj->_refcnt;
} }
...@@ -251,7 +251,7 @@ struct _RecvSendWaiting; ...@@ -251,7 +251,7 @@ struct _RecvSendWaiting;
// //
// (*) for example "thread" runtime works without GIL, while "gevent" runtime // (*) for example "thread" runtime works without GIL, while "gevent" runtime
// acquires GIL on every semaphore acquire. // acquires GIL on every semaphore acquire.
struct _chan : refobj { struct _chan : object {
unsigned _cap; // channel capacity (in elements) unsigned _cap; // channel capacity (in elements)
unsigned _elemsize; // size of element unsigned _elemsize; // size of element
...@@ -288,7 +288,7 @@ private: ...@@ -288,7 +288,7 @@ private:
bool __recv2_(void *, _WaitGroup*, _RecvSendWaiting*); bool __recv2_(void *, _WaitGroup*, _RecvSendWaiting*);
friend _chan *_makechan(unsigned elemsize, unsigned size); 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. // _RecvSendWaiting represents a receiver/sender waiting on a chan.
...@@ -439,7 +439,7 @@ _chan *_makechan(unsigned elemsize, unsigned size) { ...@@ -439,7 +439,7 @@ _chan *_makechan(unsigned elemsize, unsigned size) {
ch = (_chan *)zalloc(sizeof(_chan) + size*elemsize); ch = (_chan *)zalloc(sizeof(_chan) + size*elemsize);
if (ch == NULL) if (ch == NULL)
panic("makechan: alloc failed"); panic("makechan: alloc failed");
new (ch) _chan(); // init .refobj, ._mu, ... new (ch) _chan(); // init .object, ._mu, ...
ch->_cap = size; ch->_cap = size;
ch->_elemsize = elemsize; ch->_elemsize = elemsize;
......
...@@ -480,8 +480,8 @@ void _test_defer() { ...@@ -480,8 +480,8 @@ void _test_defer() {
} }
// verify refptr/refobj // verify refptr/object
class MyObj : public refobj { class MyObj : public object {
public: public:
void decref() { void decref() {
if (__decref()) if (__decref())
......
...@@ -104,7 +104,7 @@ LIBGOLANG_API Ticker new_ticker(double dt); ...@@ -104,7 +104,7 @@ LIBGOLANG_API Ticker new_ticker(double dt);
// //
// If the receiver is slow, Ticker does not queue events and skips them. // If the receiver is slow, Ticker does not queue events and skips them.
// Ticking can be canceled via .stop() . // Ticking can be canceled via .stop() .
struct _Ticker : refobj { struct _Ticker : object {
chan<double> c; chan<double> c;
private: private:
...@@ -137,7 +137,7 @@ LIBGOLANG_API Timer new_timer(double dt); ...@@ -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. // 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). // The timer can be stopped (.stop), or reinitialized to another time (.reset).
struct _Timer : refobj { struct _Timer : object {
chan<double> c; chan<double> c;
private: 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