From 7b82f3e8eb63e73fa8b6d7dc80e7f06b9d5af2a8 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Sun, 25 Oct 2015 13:09:24 -0700 Subject: [PATCH] Improve comments; cleanup --- inc/ManagedString.h | 12 ++++++++---- source/ManagedString.cpp | 2 -- source/MicroBitImage.cpp | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/inc/ManagedString.h b/inc/ManagedString.h index 653e96c..fefc271 100644 --- a/inc/ManagedString.h +++ b/inc/ManagedString.h @@ -21,11 +21,15 @@ struct StringData : RefCounted * 1) std::shared_ptr is not yet availiable on the ARMCC compiler * 2) to reduce memory footprint - we don't need many of the other features in the std library * 3) it makes an interestin case study for anyone interested in seeing how it works! + * 4) we need explicit reference counting to inter-op with low-level application langauge runtimes + * 5) the reference counting needs to also work for read-only, flash-resident strings */ class ManagedString { - // Internally we record the string as a char *, but control access to this to proide immutability - // and reference counting. + // StringData contains the reference count, the length, follwed by char[] data, all in one block. + // When referece count is 0xffff, then it's read only and should not be counted. + // Otherwise the block was malloc()ed. + // We control access to this to proide immutability and reference counting. StringData *ptr; public: @@ -140,7 +144,7 @@ class ManagedString * * Free this ManagedString, and decrement the reference count to the * internal character buffer. If we're holding the last reference, - * also free the character buffer and reference counter. + * also free the character buffer. */ ~ManagedString(); @@ -282,7 +286,7 @@ class ManagedString */ const char *toCharArray() const { - ptr->isReadOnly(); // this performs sanity checks on refCount + // ptr->isReadOnly(); // this performs sanity checks on refCount return ptr->data; } diff --git a/source/ManagedString.cpp b/source/ManagedString.cpp index 66af6fe..d2265ed 100644 --- a/source/ManagedString.cpp +++ b/source/ManagedString.cpp @@ -3,8 +3,6 @@ #include "mbed.h" #include "MicroBit.h" -#define printf(...) uBit.serial.printf(__VA_ARGS__) - static const char empty[] __attribute__ ((aligned (4))) = "\xff\xff\0\0\0"; /** diff --git a/source/MicroBitImage.cpp b/source/MicroBitImage.cpp index ca63a05..d67eacc 100644 --- a/source/MicroBitImage.cpp +++ b/source/MicroBitImage.cpp @@ -7,11 +7,11 @@ #include "MicroBit.h" -static const uint8_t empty[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 1, 1, 0, 0 }; /* * The null image. We actally create a small one byte buffer here, just to keep NULL pointers out of the equation. */ +static const uint8_t empty[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 1, 1, 0, 0 }; MicroBitImage MicroBitImage::EmptyImage((ImageData*)(void*)empty); /**