<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Effective C Tip #8 &#8211; Structure Comparison</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Sun, 06 May 2012 10:34:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/comment-page-1/#comment-350</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Tue, 02 Feb 2010 13:26:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/12/04/effective-c-tip-8-structure-comparison/#comment-350</guid>
		<description>I did. See point 3. Granted I didn&#039;t exactly sky write it!</description>
		<content:encoded><![CDATA[<p>I did. See point 3. Granted I didn&#39;t exactly sky write it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/comment-page-1/#comment-349</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 02 Feb 2010 10:24:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/12/04/effective-c-tip-8-structure-comparison/#comment-349</guid>
		<description>Why didn&#039;t you mention the obvious and oft-used approach of using memset?  E.g. memset(&amp;p1, 0, sizeof(COORD));</description>
		<content:encoded><![CDATA[<p>Why didn&#39;t you mention the obvious and oft-used approach of using memset?  E.g. memset(&amp;p1, 0, sizeof(COORD));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/comment-page-1/#comment-348</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 16 Dec 2009 23:13:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/12/04/effective-c-tip-8-structure-comparison/#comment-348</guid>
		<description>(2nd try, didn&#039;t go through)Thanks for your reply. Yes I was using the term handler to mean ISR (and ta for the ref to the RMA algorithm; not my usual line of work but the more I know the better).Your reply is shows some intimidating issues I&#039;d not appreciated and I feel my perspective of this line of work tends much towards the nightmare end. I&#039;ll stick to databases &amp; data processing, thanks. But you have answered my core questions, thanks.Going back to the M4 question, I&#039;ll elaborate slightly. There&#039;s no need to use M4; use whatever&#039;s appropriate. If you need a domain specific language, go for it. I happen to use M4 as that because it suits me at the mo but M4 isn&#039;t the answer, it&#039;s /an/ answer. The underlying technique is the importance, not the particular tool (maybe you can use the c preprocessor? Maybe too inflexible).Regarding the &#039;more problems than it&#039;s worth&#039;, my experience (I&#039;ve a fair bit with M4 but NOT in C) is that it introduces problems but they are emphatically worth it. It&#039;s frustrating when a macro decides to play up suddenly - there&#039;s never a shortage of aggro with M4, sadly - but when it works it *really* works. Things fly. I&#039;ve used it to generate test cases for past projects, and a present one - clear one line macros become comprehensive 10-line tests - and I can reuse some of the larger test macros to generate much larger test case lumps - great! I actually use it as an IDL to generate javascript for a library, the idea being that later I can modify the macros and spit out another lang. Not got that far so we&#039;ll see, but even ignoring that I can spit out fully asserted code (with dirty detail nicely concealed), or no assertions, or in future partial assertions, or tracing code (normally you&#039;d rely on the debugger but MS&#039; JS debugger stinks), and hide one of MS&#039; rather nasty JS bugs, and more.Summary: my experience says that once you&#039;re familiar with this technique and use it creatively and intelligently and appropriately and not to excess, it&#039;s painful but significantly less painful than not using it. I am sure this experience will extend from javascript + various test case generations to C.</description>
		<content:encoded><![CDATA[<p>(2nd try, didn&#39;t go through)Thanks for your reply. Yes I was using the term handler to mean ISR (and ta for the ref to the RMA algorithm; not my usual line of work but the more I know the better).Your reply is shows some intimidating issues I&#39;d not appreciated and I feel my perspective of this line of work tends much towards the nightmare end. I&#39;ll stick to databases &amp; data processing, thanks. But you have answered my core questions, thanks.Going back to the M4 question, I&#39;ll elaborate slightly. There&#39;s no need to use M4; use whatever&#39;s appropriate. If you need a domain specific language, go for it. I happen to use M4 as that because it suits me at the mo but M4 isn&#39;t the answer, it&#39;s /an/ answer. The underlying technique is the importance, not the particular tool (maybe you can use the c preprocessor? Maybe too inflexible).Regarding the &#39;more problems than it&#39;s worth&#39;, my experience (I&#39;ve a fair bit with M4 but NOT in C) is that it introduces problems but they are emphatically worth it. It&#39;s frustrating when a macro decides to play up suddenly &#8211; there&#39;s never a shortage of aggro with M4, sadly &#8211; but when it works it *really* works. Things fly. I&#39;ve used it to generate test cases for past projects, and a present one &#8211; clear one line macros become comprehensive 10-line tests &#8211; and I can reuse some of the larger test macros to generate much larger test case lumps &#8211; great! I actually use it as an IDL to generate javascript for a library, the idea being that later I can modify the macros and spit out another lang. Not got that far so we&#39;ll see, but even ignoring that I can spit out fully asserted code (with dirty detail nicely concealed), or no assertions, or in future partial assertions, or tracing code (normally you&#39;d rely on the debugger but MS&#39; JS debugger stinks), and hide one of MS&#39; rather nasty JS bugs, and more.Summary: my experience says that once you&#39;re familiar with this technique and use it creatively and intelligently and appropriately and not to excess, it&#39;s painful but significantly less painful than not using it. I am sure this experience will extend from javascript + various test case generations to C.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/comment-page-1/#comment-347</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 16 Dec 2009 22:59:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/12/04/effective-c-tip-8-structure-comparison/#comment-347</guid>
		<description>test</description>
		<content:encoded><![CDATA[<p>test</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/comment-page-1/#comment-346</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Sun, 13 Dec 2009 13:04:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/12/04/effective-c-tip-8-structure-comparison/#comment-346</guid>
		<description>Let&#039;s get the answer to your question out of the way first. You&#039;ve used the term &#039;handler&#039;. It&#039;s not clear whether you mean a &#039;task&#039; or an &#039;interrupt service routine&#039;. For tasks, one can use Rate Monotonic Analysis (see http://en.wikipedia.org/wiki/Rate-monotonic_scheduling) to ensure that a task always meets its deadline.For interrupt service routines (which is what I think you are asking about), then the job gets a lot harder. The response time to an ISR is usually dominated by two things:1. How long interrupts get disabled by the operating system.2. The execution time of higher priority interrupts  In the first case, an operating system needs to disable interrupts in order to do certain things such as task switching. All good real time operating systems will specify the worst case interrupt disable time. However if you are running on a non real time OS (such as Windows) then I suspect you are going to find it very hard to obtain the interrupt disable time.In the second case, most CPUs impose an interrupt priority structure. It is common (but not universal) for a higher priority interrupt to be be able to interrupt a lower priority interrupt. Thus the worst case response time for an interrupt of a given priority based upon this criterion is the execution time of all the interrupt service routines that are of a higher priority. Note that in some extreme cases, it&#039;s actually possible for a very high priority routine to trigger multiple times while a lower priority routine is waiting.Once you get past these two roadblocks, then you are into the realm of things such as how long it takes a CPU to recognize an interrupt, the number of registers it has to stack (I wrote an article on this - you&#039;ll find it at http://www.embedded.com/columns/technicalinsights/196800005?_requestid=1093456 ), and then the actual execution time of the instructions, since almost by definition, the ISR will not be in the instruction cache, and so the cache will have to flushed and loaded with the ISR code.As you can see, writing an interrupt service routine such as to guarantee a certain response can be very challenging indeed. It&#039;s part of what makes embedded systems either fun, or a nightmare, depending upon your perspective.As to whether there is a limit to the number of handlers. Well clearly there is. It&#039;s surprisingly easy to make a CPU &#039;interrupt bound&#039;. That is it spends its entire time getting in and out of ISRs and as a result never gets any other work done. Inexperienced embedded folks often find themselves in this unhappy situation.Finally, to your point about real time handlers being simple. As a general rule - yes. The name of the game is usually to get in, do the least work possible, and push the remainder of the work off onto non real time code. The division of work between the interrupt handler and foreground tasks is one of the bigger challenges in embedded systems.OK, with that out of the way, let&#039;s turn to your comments on structure comparison. I don&#039;t disagree with you. C just doesn&#039;t have these sorts of capabilities. Now you can get close to what you want to do with C++, but it still isn&#039;t automated. Now for me, using a macro processing language such as M4 in order to generate / maintain embedded code brings in more problems than it&#039;s worth. However, it&#039;s an issue of personal preference without a great argument to back it up. Anyone else have an opinion on this?</description>
		<content:encoded><![CDATA[<p>Let&#39;s get the answer to your question out of the way first. You&#39;ve used the term &#39;handler&#39;. It&#39;s not clear whether you mean a &#39;task&#39; or an &#39;interrupt service routine&#39;. For tasks, one can use Rate Monotonic Analysis (see <a href="http://en.wikipedia.org/wiki/Rate-monotonic_scheduling" rel="nofollow">http://en.wikipedia.org/wiki/Rate-monotonic_scheduling</a>) to ensure that a task always meets its deadline.For interrupt service routines (which is what I think you are asking about), then the job gets a lot harder. The response time to an ISR is usually dominated by two things:1. How long interrupts get disabled by the operating system.2. The execution time of higher priority interrupts  In the first case, an operating system needs to disable interrupts in order to do certain things such as task switching. All good real time operating systems will specify the worst case interrupt disable time. However if you are running on a non real time OS (such as Windows) then I suspect you are going to find it very hard to obtain the interrupt disable time.In the second case, most CPUs impose an interrupt priority structure. It is common (but not universal) for a higher priority interrupt to be be able to interrupt a lower priority interrupt. Thus the worst case response time for an interrupt of a given priority based upon this criterion is the execution time of all the interrupt service routines that are of a higher priority. Note that in some extreme cases, it&#39;s actually possible for a very high priority routine to trigger multiple times while a lower priority routine is waiting.Once you get past these two roadblocks, then you are into the realm of things such as how long it takes a CPU to recognize an interrupt, the number of registers it has to stack (I wrote an article on this &#8211; you&#39;ll find it at <a href="http://www.embedded.com/columns/technicalinsights/196800005?_requestid=1093456" rel="nofollow">http://www.embedded.com/columns/technicalinsights/196800005?_requestid=1093456</a> ), and then the actual execution time of the instructions, since almost by definition, the ISR will not be in the instruction cache, and so the cache will have to flushed and loaded with the ISR code.As you can see, writing an interrupt service routine such as to guarantee a certain response can be very challenging indeed. It&#39;s part of what makes embedded systems either fun, or a nightmare, depending upon your perspective.As to whether there is a limit to the number of handlers. Well clearly there is. It&#39;s surprisingly easy to make a CPU &#39;interrupt bound&#39;. That is it spends its entire time getting in and out of ISRs and as a result never gets any other work done. Inexperienced embedded folks often find themselves in this unhappy situation.Finally, to your point about real time handlers being simple. As a general rule &#8211; yes. The name of the game is usually to get in, do the least work possible, and push the remainder of the work off onto non real time code. The division of work between the interrupt handler and foreground tasks is one of the bigger challenges in embedded systems.OK, with that out of the way, let&#39;s turn to your comments on structure comparison. I don&#39;t disagree with you. C just doesn&#39;t have these sorts of capabilities. Now you can get close to what you want to do with C++, but it still isn&#39;t automated. Now for me, using a macro processing language such as M4 in order to generate / maintain embedded code brings in more problems than it&#39;s worth. However, it&#39;s an issue of personal preference without a great argument to back it up. Anyone else have an opinion on this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/comment-page-1/#comment-345</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Sun, 13 Dec 2009 02:57:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/12/04/effective-c-tip-8-structure-comparison/#comment-345</guid>
		<description>dammit, when I meant struct + function or similar, I meant the comparison function you required. Unclear, sorry.</description>
		<content:encoded><![CDATA[<p>dammit, when I meant struct + function or similar, I meant the comparison function you required. Unclear, sorry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/12/effective-c-tip-8-structure-comparison/comment-page-1/#comment-344</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Sun, 13 Dec 2009 02:53:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/12/04/effective-c-tip-8-structure-comparison/#comment-344</guid>
		<description>I came here after the answer to a different question, but your structure comparison question is interesting so I&#039;ll comment on that first.First up, I haven&#039;t used C for a long time and have no experience with embedded stuff (more on that later) and have never worried about compiler padding. That out of the way, I&#039;d probably get the pooter to define the structure and comparison functions, my personal preference being with something like M4 at first.So, instead of defining struct + function, perhaps you&#039;d use something like this (my idiosyncratic M4; it doesn&#039;t have to look like this. Also, entirely off top of my head so not tested. Also comments here follow m4_dnl, don&#039;t ask why)m4_defstruct([:COORD:], m4_dnl structure name[: m4_dnl start pairs/triples of components[:uint8_t, x:],[:uint8_t, y:]:] m4_dnl end pairs/triples of components:]) m4_dnl end macrowith suitable definitions i will get this to unfold into a structure + a suitable function. Add another pair/triple and it all gets updated.I mentioned triples above because it&#039;s possible that structure components won&#039;t always be == comparable and would need a suitable comparison function, in which case you&#039;d write something like:[:fancystruct, fncy, fancystructs_equals_func:]Note that a macro can also expand into extra stuff like a structure copier and/or cloner function if required, and a whole load of other stuff.Have used this technique to generate compiler unit tests + plenty of other stuf.If M4 gets too painful, write a proper DSL. Actually I really don&#039;t understand while this approach isn&#039;t used more often. People do so much by hand which is the job of the machine. Note that this is all off the top of my head, can try to do it properly if you want to contact me (will provide email if asked).Right, about my 2nd question, which I can&#039;t find any answer to: real-time handlers. I&#039;ve always wondered how one writes such a handler to have 1) a guarantee of response within a given time, and 2) guessing there must be a limit to the number of these handlers that an OS can support.On 1) I&#039;m guessing that handlers must be non-virtual-memory-pageable (well duh) but beyond that you assume that everywhere possible you get the worst case eg. perhaps one instruction in the pipeline + rest bubbles, branch misprediction always, L0/1/2 cache misses always so ram read penalty every time, etc. Then you count this cost down all paths and pick the worst. This is the guaranteed worst case.Is this how it&#039;s done? Does the OS have to be given this worst case info, because to address point 2) the os cannot support and infinite number of these (duh again) so it must budget for some and reject the rest if they try to install themselves. I&#039;m also guessing as point 3) that RT handlers are almost always very simple - true?</description>
		<content:encoded><![CDATA[<p>I came here after the answer to a different question, but your structure comparison question is interesting so I&#39;ll comment on that first.First up, I haven&#39;t used C for a long time and have no experience with embedded stuff (more on that later) and have never worried about compiler padding. That out of the way, I&#39;d probably get the pooter to define the structure and comparison functions, my personal preference being with something like M4 at first.So, instead of defining struct + function, perhaps you&#39;d use something like this (my idiosyncratic M4; it doesn&#39;t have to look like this. Also, entirely off top of my head so not tested. Also comments here follow m4_dnl, don&#39;t ask why)m4_defstruct([:COORD:], m4_dnl structure name[: m4_dnl start pairs/triples of components[:uint8_t, x:],[:uint8_t, y:]:] m4_dnl end pairs/triples of components:]) m4_dnl end macrowith suitable definitions i will get this to unfold into a structure + a suitable function. Add another pair/triple and it all gets updated.I mentioned triples above because it&#39;s possible that structure components won&#39;t always be == comparable and would need a suitable comparison function, in which case you&#39;d write something like:[:fancystruct, fncy, fancystructs_equals_func:]Note that a macro can also expand into extra stuff like a structure copier and/or cloner function if required, and a whole load of other stuff.Have used this technique to generate compiler unit tests + plenty of other stuf.If M4 gets too painful, write a proper DSL. Actually I really don&#39;t understand while this approach isn&#39;t used more often. People do so much by hand which is the job of the machine. Note that this is all off the top of my head, can try to do it properly if you want to contact me (will provide email if asked).Right, about my 2nd question, which I can&#39;t find any answer to: real-time handlers. I&#39;ve always wondered how one writes such a handler to have 1) a guarantee of response within a given time, and 2) guessing there must be a limit to the number of these handlers that an OS can support.On 1) I&#39;m guessing that handlers must be non-virtual-memory-pageable (well duh) but beyond that you assume that everywhere possible you get the worst case eg. perhaps one instruction in the pipeline + rest bubbles, branch misprediction always, L0/1/2 cache misses always so ram read penalty every time, etc. Then you count this cost down all paths and pick the worst. This is the guaranteed worst case.Is this how it&#39;s done? Does the OS have to be given this worst case info, because to address point 2) the os cannot support and infinite number of these (duh again) so it must budget for some and reject the rest if they try to install themselves. I&#39;m also guessing as point 3) that RT handlers are almost always very simple &#8211; true?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

