parent
5df59c2f7e
commit
11c99d0b84
@ -1,27 +1,48 @@
|
||||
#include "mbed.h"
|
||||
#include "MicroBit.h"
|
||||
|
||||
void RefCounted::init()
|
||||
{
|
||||
// Initialize to one reference (lowest bit set to 1)
|
||||
refCount = 3;
|
||||
}
|
||||
|
||||
static inline bool isReadOnlyInline(RefCounted *t)
|
||||
{
|
||||
uint32_t refCount = t->refCount;
|
||||
|
||||
if (refCount == 0xffff)
|
||||
return true; // object in flash
|
||||
|
||||
// Do some sanity checking while we're here
|
||||
if (refCount == 1)
|
||||
uBit.panic(30); // object should have been deleted
|
||||
|
||||
if ((refCount & 1) == 0)
|
||||
uBit.panic(31); // refCount doesn't look right
|
||||
|
||||
// Not read only
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RefCounted::isReadOnly()
|
||||
{
|
||||
return isReadOnlyInline(this);
|
||||
}
|
||||
|
||||
void RefCounted::incr()
|
||||
{
|
||||
if (refcnt == 0xffff)
|
||||
return;
|
||||
if (refcnt == 0)
|
||||
uBit.panic(30);
|
||||
refcnt++;
|
||||
if (!isReadOnlyInline(this))
|
||||
refCount += 2;
|
||||
}
|
||||
|
||||
void RefCounted::decr()
|
||||
{
|
||||
if (refcnt == 0xffff)
|
||||
if (isReadOnlyInline(this))
|
||||
return;
|
||||
if (refcnt == 0)
|
||||
uBit.panic(31);
|
||||
refcnt--;
|
||||
if (refcnt == 0) {
|
||||
// size of 0xffff indicates a class with a virtual destructor
|
||||
if (size == 0xffff)
|
||||
uBit.panic(32);
|
||||
else
|
||||
free(this);
|
||||
|
||||
refCount -= 2;
|
||||
if (refCount == 2) {
|
||||
free(this);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue