Solution for Bluetooth SIG hands-on training course
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw_starter by
RefCounted.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 /** 00027 * Base class for payload for ref-counted objects. Used by ManagedString and MicroBitImage. 00028 * There is no constructor, as this struct is typically malloc()ed. 00029 */ 00030 #include "mbed.h" 00031 #include "MicroBitConfig.h" 00032 #include "RefCounted.h" 00033 #include "MicroBitDisplay.h" 00034 00035 /** 00036 * Initializes for one outstanding reference. 00037 */ 00038 void RefCounted::init() 00039 { 00040 // Initialize to one reference (lowest bit set to 1) 00041 refCount = 3; 00042 } 00043 00044 /** 00045 * Checks if the object resides in flash memory. 00046 * 00047 * @param t the object to check. 00048 * 00049 * @return true if the object resides in flash memory, false otherwise. 00050 */ 00051 static inline bool isReadOnlyInline(RefCounted *t) 00052 { 00053 uint32_t refCount = t->refCount; 00054 00055 if (refCount == 0xffff) 00056 return true; // object in flash 00057 00058 // Do some sanity checking while we're here 00059 if (refCount == 1 || // object should have been deleted 00060 (refCount & 1) == 0) // refCount doesn't look right 00061 microbit_panic(MICROBIT_HEAP_ERROR); 00062 00063 // Not read only 00064 return false; 00065 } 00066 00067 /** 00068 * Checks if the object resides in flash memory. 00069 * 00070 * @return true if the object resides in flash memory, false otherwise. 00071 */ 00072 bool RefCounted::isReadOnly() 00073 { 00074 return isReadOnlyInline(this); 00075 } 00076 00077 /** 00078 * Increment reference count. 00079 */ 00080 void RefCounted::incr() 00081 { 00082 if (!isReadOnlyInline(this)) 00083 refCount += 2; 00084 } 00085 00086 /** 00087 * Decrement reference count. 00088 */ 00089 void RefCounted::decr() 00090 { 00091 if (isReadOnlyInline(this)) 00092 return; 00093 00094 refCount -= 2; 00095 if (refCount == 1) { 00096 free(this); 00097 } 00098 }
Generated on Tue Jul 12 2022 20:39:13 by 1.7.2