Add LPC1768
Dependencies: mbed-rtos mbed Xbus
Fork of MTi-1_example by
Revision 31:ce1ea9ae861e, committed 2015-05-21
- Comitter:
- Alex Young
- Date:
- Thu May 21 11:43:11 2015 +0200
- Parent:
- 30:27ff4335edec
- Child:
- 32:fafe0f42d82b
- Commit message:
- Use a simple object to handle xbus memory cleanup
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed May 20 17:04:45 2015 +0200
+++ b/main.cpp Thu May 21 11:43:11 2015 +0200
@@ -46,12 +46,33 @@
return ev.status == osEventMessage ? (XbusMessage*)ev.value.p : NULL;
}
-static void freeMessage(XbusMessage const* m)
+/*!
+ * \brief RAII object to manage message memory deallocation.
+ *
+ * Will automatically free the memory used by a XbusMessage when going out
+ * of scope.
+ */
+class XbusMessageMemoryManager
{
- if (m->data)
- deallocateMessageData(m->data);
- g_messagePool.free((XbusMessage*)m);
-}
+ public:
+ XbusMessageMemoryManager(XbusMessage const* message)
+ : m_message(message)
+ {
+ }
+
+ ~XbusMessageMemoryManager()
+ {
+ if (m_message)
+ {
+ if (m_message->data)
+ deallocateMessageData(m_message->data);
+ g_messagePool.free(const_cast<XbusMessage*>(m_message));
+ }
+ }
+
+ private:
+ XbusMessage const* m_message;
+};
static void dumpResponse(XbusMessage const* response)
{
@@ -91,11 +112,11 @@
{
XbusMessage m = {cmdId};
XbusMessage const* response = doTransaction(&m);
+ XbusMessageMemoryManager janitor(response);
if (response)
{
dumpResponse(response);
- freeMessage(response);
}
else
{
@@ -176,6 +197,7 @@
{
XbusMessage reqDid = {XMID_ReqDid};
XbusMessage const* didRsp = doTransaction(&reqDid);
+ XbusMessageMemoryManager janitor(didRsp);
uint32_t deviceId = 0;
if (didRsp)
{
@@ -183,7 +205,6 @@
{
deviceId = *(uint32_t*)didRsp->data;
}
- freeMessage(didRsp);
}
return deviceId;
}
@@ -220,6 +241,7 @@
XbusMessage outputConfMsg = {XMID_SetOutputConfig, 5, &conf};
XbusMessage const* outputConfRsp = doTransaction(&outputConfMsg);
+ XbusMessageMemoryManager janitor(outputConfRsp);
if (outputConfRsp)
{
if (outputConfRsp->mid == XMID_OutputConfig)
@@ -236,7 +258,6 @@
{
dumpResponse(outputConfRsp);
}
- freeMessage(outputConfRsp);
}
else
{
David Khosravi
