From 9330b401d521be67876977e73f0e83d9970aca57 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Sat, 24 Oct 2015 20:30:15 -0700 Subject: [PATCH] Move RefCounted class to a separate file --- .gitignore | 6 +++++ inc/ManagedString.h | 19 +-------------- inc/RefCounted.h | 51 ++++++++++++++++++++++++++++++++++++++++ source/CMakeLists.txt | 1 + source/ManagedString.cpp | 30 ----------------------- source/RefCounted.cpp | 31 ++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 .gitignore create mode 100644 inc/RefCounted.h create mode 100644 source/RefCounted.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efc51aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.yotta.json +build +*.swp +yotta_modules +yotta_targets +Makefile diff --git a/inc/ManagedString.h b/inc/ManagedString.h index d2432ea..5cf9c07 100644 --- a/inc/ManagedString.h +++ b/inc/ManagedString.h @@ -1,24 +1,7 @@ #ifndef MANAGED_STRING_H #define MANAGED_STRING_H -#include "mbed.h" - -struct RefCounted -{ -public: - uint16_t refcnt; - uint16_t size; - - void incr(); - void decr(); - -}; - -class VirtualRefCounted : RefCounted -{ -public: - virtual ~VirtualRefCounted(); -}; +#include "RefCounted.h" struct StringData : RefCounted { diff --git a/inc/RefCounted.h b/inc/RefCounted.h new file mode 100644 index 0000000..0136888 --- /dev/null +++ b/inc/RefCounted.h @@ -0,0 +1,51 @@ +#ifndef REF_COUNTED_H +#define REF_COUNTED_H + +#include "mbed.h" + +/** + * Base class for payload for ref-counted objects. Used by ManagedString and MicroBitImage. + * There is no constructor, as this struct is typically malloc()ed. + */ +struct RefCounted +{ +public: + /** + * Number of outstanding references. Should never be zero (object should be deleted then). + * When it's set to 0xffff, it means the object sits in flash and should not be counted. + */ + uint16_t refcnt; + + /** + * For strings this is length. For images this is both width and length (8 bit each). + * A value of 0xffff indicates that this is in fact an instance of VirtualRefCounted + * and therefore when the reference count reaches zero, the virtual destructor + * should be called. + */ + union { + uint16_t size; + struct { + uint8_t width; + uint8_t height; + }; + }; + + /** Increment reference count. */ + void incr(); + + /** Decrement reference count. */ + void decr(); + +}; + +/** + * Base class for ref-counted objects. Used in native compiler in TD for Collections, + * user-defined records, closures etc. + */ +class VirtualRefCounted : RefCounted +{ +public: + virtual ~VirtualRefCounted(); +}; + +#endif diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 2d9806c..83b74a8 100755 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -28,6 +28,7 @@ set(YOTTA_AUTO_MICROBIT-DAL_CPP_FILES "MicroBitSerial.cpp" "MicroBitHeapAllocator.cpp" "MicroBitListener.cpp" + "RefCounted.cpp" "MemberFunctionCallback.cpp" "ble-services/MicroBitDFUService.cpp" "ble-services/MicroBitEventService.cpp" diff --git a/source/ManagedString.cpp b/source/ManagedString.cpp index 23e8bf3..5d79f95 100644 --- a/source/ManagedString.cpp +++ b/source/ManagedString.cpp @@ -366,33 +366,3 @@ char ManagedString::charAt(int16_t index) * Empty string constant literal */ ManagedString ManagedString::EmptyString((StringData*)(void*)empty); - - -void RefCounted::incr() -{ - if (refcnt == 0xffff) - return; - if (refcnt == 0) - uBit.panic(30); - refcnt++; -} - -void RefCounted::decr() -{ - if (refcnt == 0xffff) - return; - if (refcnt == 0) - uBit.panic(31); - refcnt--; - if (refcnt == 0) { - // size of 0xffff indicates a class with a virtual destructor - if (size == 0xffff) - delete (VirtualRefCounted*)this; - else - free(this); - } -} - -VirtualRefCounted::~VirtualRefCounted() -{ -} diff --git a/source/RefCounted.cpp b/source/RefCounted.cpp new file mode 100644 index 0000000..31763d5 --- /dev/null +++ b/source/RefCounted.cpp @@ -0,0 +1,31 @@ +#include "mbed.h" +#include "MicroBit.h" + +void RefCounted::incr() +{ + if (refcnt == 0xffff) + return; + if (refcnt == 0) + uBit.panic(30); + refcnt++; +} + +void RefCounted::decr() +{ + if (refcnt == 0xffff) + return; + if (refcnt == 0) + uBit.panic(31); + refcnt--; + if (refcnt == 0) { + // size of 0xffff indicates a class with a virtual destructor + if (size == 0xffff) + delete (VirtualRefCounted*)this; + else + free(this); + } +} + +VirtualRefCounted::~VirtualRefCounted() +{ +}