<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matt Galloway&#039;s iPhone Apps</title>
	<atom:link href="http://iphone.galloway.me.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://iphone.galloway.me.uk</link>
	<description>iPhone Applications</description>
	<lastBuildDate>Thu, 02 Feb 2012 09:22:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A look under ARC’s hood – Episode 3</title>
		<link>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/</link>
		<comments>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 10:46:38 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=509</guid>
		<description><![CDATA[This instalment of &#8220;A look under ARC&#8217;s hood&#8221; is all about the new @autoreleasepool directive. LLVM tells us that the semantics of autorelease pools has changed with LLVM 3.0 and in particular, I thought it might be interesting to see what ARC is doing when it comes to these. So consider the following method: This [...]]]></description>
			<content:encoded><![CDATA[<p>This instalment of &#8220;A look under ARC&#8217;s hood&#8221; is all about the new <code>@autoreleasepool</code> directive. <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool">LLVM tells us</a> that the semantics of autorelease pools has changed with LLVM 3.0 and in particular, I thought it might be interesting to see what ARC is doing when it comes to these.</p>
<p><span id="more-509"></span></p>
<p>So consider the following method:</p>
<pre class="brush: objc; title: ; notranslate">
void foo() {
    @autoreleasepool {
        NSNumber *number = [NSNumber numberWithInt:0];
        NSLog(@&quot;number = %p&quot;, number);
    }
}
</pre>
<p>This is entirely contrived, of course, but it should let us see what&#8217;s going on. In non-ARC land we would assume here that <code>number</code> would be allocated inside <code>numberWithInt:</code> and returned autoreleased. So when the autorelease pool is next drained, it will be released. So let&#8217;s see if that&#8217;s what happened (as usual, this is ARMv7 instructions):</p>
<pre class="brush: cpp; highlight: [8,21,29]; title: ; notranslate">
        .globl  _foo
        .align  2
        .code   16
        .thumb_func     _foo
_foo:
        push    {r4, r7, lr}
        add     r7, sp, #4
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        mov     r1, r0
        movw    r0, :lower16:(L__unnamed_cfstring_-(LPC0_2+4))
        movt    r0, :upper16:(L__unnamed_cfstring_-(LPC0_2+4))
LPC0_2:
        add     r0, pc
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        pop     {r4, r7, pc}
</pre>
<p>Well, yes. That&#8217;s exactly what&#8217;s happening. We can see the call to push an autorelease pool then a call to <code>numberWithInt:</code> then a call to pop an autorelease pool. Exactly what we&#8217;d expect. Now let&#8217;s look at the exact same code compiled under ARC:</p>
<pre class="brush: cpp; highlight: [25,34]; title: ; notranslate">
        .globl  _foo
        .align  2
        .code   16
        .thumb_func     _foo
_foo:
        push    {r4, r5, r7, lr}
        add     r7, sp, #8
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        mov     r5, r0
        movw    r0, :lower16:(L__unnamed_cfstring_-(LPC0_2+4))
        movt    r0, :upper16:(L__unnamed_cfstring_-(LPC0_2+4))
        mov     r1, r5
LPC0_2:
        add     r0, pc
        blx     _NSLog
        mov     r0, r5
        blx     _objc_release
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        pop     {r4, r5, r7, pc}
</pre>
<p>Notice the calls to <code>objc_retainAutoreleasedReturnValue</code> and <code>objc_release</code>. What&#8217;s happening there is that ARC has determined for us that it doesn&#8217;t really need to worry about the autorelease pool that&#8217;s in place, because it can simply tell the autorelease to not happen (with the call to <code>objc_retainAutoreleasedReturnValue</code>) and then release the object later itself. This is desirable as it means the autorelease logic doesn&#8217;t have to happen.</p>
<p>Note that the autorelease pool is still required to be pushed and popped because ARC can&#8217;t know what&#8217;s going on in the calls to <code>numberWithInt:</code> and <code>NSLog</code> to know if objects will be put into the pool there. If it did know that they didn&#8217;t autorelease anything then it could actually get rid of the push and pop. Perhaps that kind of logic will come in future versions although I&#8217;m not quite sure how the semantics of that would work though.</p>
<p>Now let&#8217;s consider another example which is where we want to use <code>number</code> outside of the scope of the autorelease pool block. This should show us why ARC is a wonder to work with. Consider the following code:</p>
<pre class="brush: objc; title: ; notranslate">
void bar() {
    NSNumber *number;
    @autoreleasepool {
        number = [NSNumber numberWithInt:0];
        NSLog(@&quot;number = %p&quot;, number);
    }
    NSLog(@&quot;number = %p&quot;, number);
}
</pre>
<p>You might be (correctly) thinking that this is going to cause problems even though it looks perfectly innocuous. It&#8217;s a problem because <code>number</code> will be allocated inside the autorelease pool block, will be deallocated when the autorelease pool pops but is then used after it&#8217;s been deallocated. Uh oh! Let&#8217;s see if we&#8217;re right by compiling it without ARC enabled:</p>
<pre class="brush: cpp; title: ; notranslate">
        .globl  _bar
        .align  2
        .code   16
        .thumb_func     _bar
_bar:
        push    {r4, r5, r6, r7, lr}
        add     r7, sp, #12
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r6, :lower16:(L__unnamed_cfstring_-(LPC1_2+4))
        movt    r6, :upper16:(L__unnamed_cfstring_-(LPC1_2+4))
LPC1_2:
        add     r6, pc
        mov     r5, r0
        mov     r1, r5
        mov     r0, r6
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        mov     r0, r6
        mov     r1, r5
        blx     _NSLog
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Obviously no calls to retain, release or autorelease as we&#8217;d expect since we haven&#8217;t made any explicitly and we&#8217;re not using ARC. We can see here that it&#8217;s been compiled exactly as we&#8217;d expect from our reasoning before. So let&#8217;s see what it looks like when ARC gives us a helping hand:</p>
<pre class="brush: cpp; highlight: [25,40]; title: ; notranslate">
        .globl  _bar
        .align  2
        .code   16
        .thumb_func     _bar
_bar:
        push    {r4, r5, r6, r7, lr}
        add     r7, sp, #12
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        movw    r6, :lower16:(L__unnamed_cfstring_-(LPC1_2+4))
        movt    r6, :upper16:(L__unnamed_cfstring_-(LPC1_2+4))
LPC1_2:
        add     r6, pc
        mov     r5, r0
        mov     r1, r5
        mov     r0, r6
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        mov     r0, r6
        mov     r1, r5
        blx     _NSLog
        mov     r0, r5
        blx     _objc_release
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Round of applause for ARC please! Notice that it&#8217;s realised we&#8217;re using <code>number</code> outside of the scope of the autorelease pool block so it&#8217;s retained the return value from <code>numberWithInt:</code> just as it did before, but this time it&#8217;s placed the release at the end of the <code>bar</code> function rather than before the autorelease pool is popped. That will have saved us a crash in some code that we might have thought was correct but actually had a subtle memory management bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UIImageOrientation / EXIF orientation sample images</title>
		<link>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 18:18:22 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=491</guid>
		<description><![CDATA[Whilst I was doing some work with images recently I was in desperate need for some sample images which were tagged with the EXIF orientation flag for each of the 8 orientations which are supported by UIImage in its UIImageOrientation metadata. I couldn&#8217;t find any already out there, so I made my own. And now [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst I was doing some work with images recently I was in desperate need for some sample images which were tagged with the EXIF orientation flag for each of the 8 orientations which are supported by UIImage in its <code>UIImageOrientation</code> metadata. I couldn&#8217;t find any already out there, so I made my own. And now I&#8217;m posting them here for anyone who might also find this useful. I now have these saved on my iPhone and use them in apps as test images.</p>
<p><a href='http://iphone.galloway.me.uk/wp-content/uploads/2012/01/EXIF_Orientation_Samples.zip'>Download All</a></p>
<p><span id="more-491"></span></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-225x300.jpg" alt="" title="up" width="225" height="300" class="aligncenter size-medium wp-image-499" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-mirrored-225x300.jpg" alt="" title="up-mirrored" width="225" height="300" class="aligncenter size-medium wp-image-498" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-225x300.jpg" alt="" title="down" width="225" height="300" class="aligncenter size-medium wp-image-493" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-mirrored-225x300.jpg" alt="" title="down-mirrored" width="225" height="300" class="aligncenter size-medium wp-image-492" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-300x225.jpg" alt="" title="left" width="300" height="225" class="aligncenter size-medium wp-image-495" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-mirrored-300x225.jpg" alt="" title="left-mirrored" width="300" height="225" class="aligncenter size-medium wp-image-494" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-300x225.jpg" alt="" title="right" width="300" height="225" class="aligncenter size-medium wp-image-497" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-mirrored-300x225.jpg" alt="" title="right-mirrored" width="300" height="225" class="aligncenter size-medium wp-image-496" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to visualise affine transforms when drawing with Quartz 2D.</title>
		<link>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 18:09:00 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=484</guid>
		<description><![CDATA[I had been struggling with some code that I had to rotate an image whilst drawing it so I decided to sit down and work out a nice way to visualise it as I hadn&#8217;t seen anything out there really that explained it very well. This is what I came up with&#8230; In order to [...]]]></description>
			<content:encoded><![CDATA[<p>I had been <a href="http://stackoverflow.com/questions/9062363/cgcontext-drawing-rotated-around-arbitrary-point">struggling with some code</a> that I had to rotate an image whilst drawing it so I decided to sit down and work out a nice way to visualise it as I hadn&#8217;t seen anything out there really that explained it very well. This is what I came up with&#8230;</p>
<p><span id="more-484"></span></p>
<p>In order to visualise it I decided to create a card with an &#8216;F&#8217; on the front and then trace through the &#8216;F&#8217; onto the back to that effectively it is mirrored. The purpose of the mirror is to enable me to visualise the case when dealing with images that are mirrored (i.e. <code>UIImageOrientationUpMirrored</code>, <code>UIImageOrientationDownMirrored</code>, etc). Here is an example of my card:</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Sample-Card.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Sample-Card.png" alt="" title="Sample-Card" width="400" height="354" class="aligncenter size-full wp-image-487" /></a></p>
<p>Now we can get to work using that to work out how we might want to translate &#038; rotate a context whilst drawing into. Consider first of all an image that is in the <code>UIImageOrientationDown</code> orientation. So hold your card with the &#8216;F&#8217; on it with the front facing you and rotate it through 180 degrees. That is the orientation we want to determine the correct transformation matrix for in this case. Now since Quartz draws with the origin in the bottom right, you need to imagine a coordinate system where the y-axis is going up from the bottom to the top of your card and the x-axis is going across from the left to the right. I like to imagine little arrows going up &#038; left from the bottom left corner of my card.</p>
<p>So to get this to draw into our canvas correctly it&#8217;s pretty clear that we need to rotate by 180 degrees. A rotation will rotate about the origin of our coordinate system so if we purely rotate by 180 degrees then the image will be drawn the right way up but will lie down and left from the origin. To see why this is, hold your card with your fingers at the bottom left. Now imagine the canvas going up and right from here, the size of the card. Now push the card round from the top right corner by 180 degrees, pivoting about the bottom left. You can see that nothing would be drawn into the canvas! So what we need to do is translate up by the height and right by the width. That then brings the image into the right place in the canvas.</p>
<p>I like to draw out this like so on paper:</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Down-Example.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Down-Example.png" alt="" title="Down-Example" width="640" height="452" class="aligncenter size-full wp-image-485" /></a></p>
<p>I think this helps explain it because if at all times you keep your little coordinate arrows on your card lined up with the ones in the diagram, you can visualise what&#8217;s going on.</p>
<p>Now let&#8217;s consider a much more complicated example, the case of <code>UIImageOrientationLeftMirrored</code>. Start with your card on its front and on its side with the long side of the &#8216;F&#8217; at the bottom. Then flip it over. This is now in the desired orientation.</p>
<p>It&#8217;s hard to work out from here what needs to be done but if you follow through the diagram below and at all times keeping your little coordinate arrows on your card aligned with the arrows on the diagram then you should be able to convince yourself that it&#8217;s right.</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Left-Mirrored-Example.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Left-Mirrored-Example.png" alt="" title="Left-Mirrored-Example" width="640" height="452" class="aligncenter size-full wp-image-486" /></a></p>
<p>So to follow this you would hold your card with the &#8216;F&#8217; in the position described above with its bottom left aligned with the bottom left of the canvas &#8217;1&#8242; on the diagram. Then to get to position 2 you would move the card horizontally across the width of the canvas where your little arrows should line up with the arrows in the diagram on canvas &#8217;2&#8242;. To get to position 3 you would rotate 90 degrees counter-clockwise (i.e. positive rotation &#8211; which is rotating from the positive x-axis towards the positive y-axis). Then to get to position 4 you need to move the card vertically, which is a translation in positive x if you look at it carefully. Then finally to get to position 5 you need to flip the card along the y-axis which brings the image into the canvas fully and you&#8217;ll notice the &#8216;F&#8217; is the right way up!</p>
<p>Note that if we want to then draw the image we would need to define the rectangle to draw into with a width equal to the canvas height and a height equal to the canvas width because our x axis is now vertical and our y axis is horizontal. Alternatively think of it that the height of the image is the width of the canvas and the width of the image is the height of the canvas.</p>
<p>I used this method to work out how to draw a <code>UIImage</code> rotated correctly to its upright position. Here is that code:</p>
<pre class="brush: objc; title: ; notranslate">
UIImage *image = &lt;THE_IMAGE&gt;;
CGSize imageSize = image.size;

CGRect imageRect = CGRectIntegral(CGRectMake(0.0f, 0.0f, imageSize.width, imageSize.height));
CGRect imageTransposedRect = CGRectMake(0.0f, 0.0f, imageSize.height, imageSize.width);

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect drawRect = imageRect;

switch (image.imageOrientation) {
    case UIImageOrientationUp:
        break;
    case UIImageOrientationUpMirrored:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        break;
    case UIImageOrientationDown:
        transform = CGAffineTransformTranslate(transform, rect.size.width, rect.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, 0.0f, rect.size.height);
        transform = CGAffineTransformScale(transform, 1.0f, -1.0f);
        break;
    case UIImageOrientationLeft:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        transform = CGAffineTransformTranslate(transform, rect.size.height, 0.0f);
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationRight:
        transform = CGAffineTransformTranslate(transform, 0.0f, rect.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        drawRect = imageTransposedRect;
        break;
}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            drawRect.size.width,
                                            drawRect.size.height,
                                            8,
                                            0,
                                            colorSpace,
                                            kCGImageAlphaPremultipliedLast);

CGContextConcatCTM(bitmap, transform);
CGContextDrawImage(bitmap, drawRect, image);

CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGContextRelease(bitmap);
CGImageRelease(newImageRef);
CGColorSpaceRelease(colorSpace);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look under ARC&#8217;s hood &#8211; Episode 2</title>
		<link>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-2/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-2/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 15:30:31 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=456</guid>
		<description><![CDATA[Following on from my first post about looking at how ARC works under the hood I thought I would share another little snippet that I found interesting. This time I was wondering what happened when you pulled an object out of an array and returned it from a method. Pre-ARC, you would retain the object [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from my first post about looking at <a href="http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/">how ARC works under the hood</a> I thought I would share another little snippet that I found interesting. This time I was wondering what happened when you pulled an object out of an array and returned it from a method. Pre-ARC, you would retain the object then return it autoreleased. With ARC we can get rid of those memory management calls but it just feels wrong. So I decided to check ARC was doing the right thing.</p>
<p><span id="more-456"></span></p>
<p>Consider this class:</p>
<pre class="brush: objc; title: ; notranslate">
#import &lt;Foundation/Foundation.h&gt;

@interface ClassA : NSObject

@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation ClassA

@synthesize array;

- (id)popObject {
    id lastObject = [array lastObject];
    if (lastObject) {
        [array removeLastObject];
    }
    return lastObject;
}

@end
</pre>
<p>In non-ARC land, the call to <code>removeLastObject</code> would release the object that was in the array and then if that was the last reference to it, it would <code>dealloc</code> the object meaning that we return a dead object. So we would retain <code>lastObject</code> and then return it with an autorelease.</p>
<p>But this scared me not doing this, even though I knew full well that ARC should be doing its job. I think I thought this because naively I thought that ARC would parse the method line by line. If it did then I thought it might not necessarily add in a retain because when we get a reference to the last object, ARC doesn&#8217;t know it needs to add a retain because well, why would it necessarily have to?</p>
<p>That&#8217;s where I was wrong. Obviously what ARC does is it will add in a retain once we get a reference to that object and then when that variable goes out of scope it adds a release or in our case, because we are returning it and the method name does not start with <code>new</code> or <code>copy</code>, it autoreleases it.</p>
<p>Let&#8217;s see what that code compiled to:</p>
<pre class="brush: cpp; highlight: [21,34]; title: ; notranslate">
        .thumb_func     &quot;-[ClassA popObject]&quot;
&quot;-[ClassA popObject]&quot;:
        push    {r4, r5, r6, r7, lr}
        movw    r6, :lower16:(_OBJC_IVAR_$_ClassA.array-(LPC0_0+4))
        mov     r4, r0
        movt    r6, :upper16:(_OBJC_IVAR_$_ClassA.array-(LPC0_0+4))
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4))
LPC0_0:
        add     r6, pc
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4))
LPC0_1:
        add     r1, pc
        add     r7, sp, #12
        ldr     r0, [r6]
        ldr     r1, [r1]
        ldr     r0, [r4, r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        mov     r5, r0
        cbz     r5, LBB0_2
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC0_2+4))
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC0_2+4))
        ldr     r0, [r6]
LPC0_2:
        add     r1, pc
        ldr     r1, [r1]
        ldr     r0, [r4, r0]
        blx     _objc_msgSend
LBB0_2:
        mov     r0, r5
        blx     _objc_autoreleaseReturnValue
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Well, there we go. ARC has done a fine job for us. What it&#8217;s actually done is added a call to <code>objc_retainAutoreleaseReturnValue</code> which means it has noticed it needs to retain a value that was returned autoreleased which must be an ARC optimisation that will just pull the object once from the autorelease pool rather than actually performing a retain. Then at the end of the method it calls <code>objc_autoreleaseReturnValue</code> which must do the work of autoreleasing the value we&#8217;re returning.</p>
<p>This is just another example of how to look at what ARC is doing under the hood. The more I use ARC, the more I realise how useful it is. It makes code less prone to memory management errors and allows for optimisations such as the one shown here of retaining an autoreleased return value.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look under ARC&#8217;s hood &#8211; Episode 1</title>
		<link>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 11:12:58 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=443</guid>
		<description><![CDATA[After a conversation on Twitter with @jacobrelkin I decided to write a little post about how ARC works under the hood and how you can go about seeing what it&#8217;s doing. In this post I&#8217;ll explain about how ARC adds in retain, release and autorelease calls accordingly. We shall start by defining a class like [...]]]></description>
			<content:encoded><![CDATA[<p>After a <a href="http://twitter.com/#!/mattjgalloway/status/154478264537194496">conversation on Twitter</a> with <a href="http://twitter.com/jacobrelkin">@jacobrelkin</a> I decided to write a little post about how ARC works under the hood and how you can go about seeing what it&#8217;s doing. In this post I&#8217;ll explain about how ARC adds in <code>retain</code>, <code>release</code> and <code>autorelease</code> calls accordingly. </p>
<p><span id="more-443"></span></p>
<p>We shall start by defining a class like so:</p>
<pre class="brush: objc; title: ; notranslate">
#import &lt;Foundation/Foundation.h&gt;

@interface ClassA : NSObject 

@property (nonatomic, retain) NSNumber *foo;

@end

@implementation ClassA

@synthesize foo;

- (void)changeFooDirect:(NSNumber*)inFoo {
    foo = inFoo;
}

- (void)changeFooSetter:(NSNumber*)inFoo {
    self.foo = inFoo;
}

- (NSNumber*)newNumber {
    return [[NSNumber alloc] initWithInt:10];
}

- (NSNumber*)getNumber {
    return [[NSNumber alloc] initWithInt:10];
}

@end
</pre>
<p>This outlines a few important aspects of ARC including direct access to ivars versus using a setter and how ARC will add autorelease calls when returning an object from a method based on the name of the method.</p>
<p>Let&#8217;s first look at the direct access to ivars versus using a setter. If we compile that code and look at the assembly then we&#8217;ll get an insight into what&#8217;s going on. I decided to use ARMv7 because it&#8217;s easier to understand what&#8217;s going on than x86 (in my opinion anyway!). We can turn ARC on and off with the <code>-fobjc-arc</code> and <code>-fno-objc-arc</code> compiler options. In these examples I&#8217;ve used optimisation level 3 which will mean the compiler will also remove redundant code which we&#8217;re not really interested in and would clog up the understanding (an exercise for the reader is to try without any optimisations and see what it looks like).</p>
<p>So to compile without ARC I used the following command:</p>
<pre class="brush: bash; title: ; notranslate">$ /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk -arch armv7 -fno-objc-arc -O3 -S -o - test-arc.m</pre>
<p>So, let&#8217;s look at <code>changeFooDirect:</code> and <code>changeFooSetter:</code>:</p>
<pre class="brush: cpp; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooDirect:]&quot;
&quot;-[ClassA changeFooDirect:]&quot;:
        movw    r1, :lower16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
        movt    r1, :upper16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
LPC0_0:
        add     r1, pc
        ldr     r1, [r1]
        str     r2, [r0, r1]
        bx      lr

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooSetter:]&quot;
&quot;-[ClassA changeFooSetter:]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
LPC1_0:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}
</pre>
<p>And following straight on, let&#8217;s look at what it looks like with ARC enabled. To do this I used the following command:</p>
<pre class="brush: bash; title: ; notranslate">$ /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk -arch armv7 -fobjc-arc -O3 -S -o - test-arc.m</pre>
<p>Again, we&#8217;re interested at the moment in <code>changeFooDirect:</code> and <code>changeFooSetter:</code>:</p>
<pre class="brush: cpp; highlight: [14]; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooDirect:]&quot;
&quot;-[ClassA changeFooDirect:]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
        mov     r7, sp
        movt    r1, :upper16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
LPC0_0:
        add     r1, pc
        ldr     r1, [r1]
        add     r0, r1
        mov     r1, r2
        blx     _objc_storeStrong
        pop     {r7, pc}

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooSetter:]&quot;
&quot;-[ClassA changeFooSetter:]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
LPC1_0:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}
</pre>
<p>We can instantly see the difference here. The <code>changeFooSetter:</code> is exactly the same whereas <code>changeFooDirect:</code> has changed with a single call to <code>objc_storeStrong</code>. That&#8217;s the interesting bit. If we looks at the <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime.objc_storeStrong">LLVM documentation for this</a> then we see that it&#8217;s doing a standard swap of the variable by releasing the old value and retain the new value. Whereas in the non-ARC version the ivar is just swapped without any retain or release. That&#8217;s just what we&#8217;d expect! Thanks ARC!</p>
<p>Now for the more interesting bit, the <code>newNumber</code> versus <code>getNumber</code>. Those methods in non-ARC land are both returning <code>NSNumber</code> objects which have a retain count of 1, i.e. the caller owns them. That sounds right for <code>newNumber</code> but not for <code>getNumber</code> according to Cocoa&#8217;s naming conventions. We&#8217;d expect to see an <code>autorelease</code> call when returning from <code>getNumber</code>. So let&#8217;s see what the code looks like without ARC:</p>
<pre class="brush: cpp; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA newNumber]&quot;
&quot;-[ClassA newNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
LPC2_0:
        add     r1, pc
LPC2_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
LPC2_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA getNumber]&quot;
&quot;-[ClassA getNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
LPC3_0:
        add     r1, pc
LPC3_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
LPC3_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}
</pre>
<p>And now with ARC:</p>
<pre class="brush: cpp; highlight: [51]; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA newNumber]&quot;
&quot;-[ClassA newNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
LPC2_0:
        add     r1, pc
LPC2_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
LPC2_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA getNumber]&quot;
&quot;-[ClassA getNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
LPC3_0:
        add     r1, pc
LPC3_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
LPC3_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        blx     _objc_autorelease
        pop     {r7, pc}
</pre>
<p>And look at the single difference &#8211; a <code>blx</code> (this is a method call) to <code>objc_autorelease</code> in <code>getNumber:</code>. That&#8217;s just what we&#8217;d expect from ARC because it&#8217;s noticed that the method name doesn&#8217;t start with <code>new</code> or <code>copy</code> and it knows that at the point of return the <code>NSNumber</code> has a retain count of 1 so it adds in an <code>autorelease</code> call. Excellent!</p>
<p>This has just shown a little insight into how ARC works in two circumstances and I hope it inspires the reader to go away and look for themselves into how ARC works rather than just taking it for granted. It&#8217;s important as a programmer to understand how your tools work.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is it with people and free apps?</title>
		<link>http://iphone.galloway.me.uk/2011/11/what-is-it-with-people-and-free-apps/</link>
		<comments>http://iphone.galloway.me.uk/2011/11/what-is-it-with-people-and-free-apps/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 09:24:20 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[iPhone Apps]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=435</guid>
		<description><![CDATA[So we have just released a new version of BeerMap which is an iPhone app that I created with a couple of friends. It&#8217;s had a long life and has changed significantly in this latest update because, to be honest, it was quite confusing to use. We didn&#8217;t really have a plan for it before [...]]]></description>
			<content:encoded><![CDATA[<p>So we have just released a new version of <a href="http://www.beermap.co">BeerMap</a> which is an <a href="http://itunes.apple.com/gb/app/beermap/id313367328?mt=8">iPhone app</a> that I created with a couple of friends. It&#8217;s had a long life and has changed significantly in this latest update because, to be honest, it was quite confusing to use. We didn&#8217;t really have a plan for it before but now we do and we&#8217;re executing it step by step.</p>
<p>Since 2.2 went live we&#8217;ve seen a lot more uptake of the app with more people reviewing beers and even some of the social media integration being used in the app which is exactly what we wanted. We wanted to push the app in a more social direction rather than just reviewing beers and pubs. If you&#8217;re simply reviewing beers and pubs then you need critical mass of users before any of that data becomes usable. So opting for the more Twitter-like style of a review now being a &#8220;Taste&#8221; and having a timeline of realtime &#8220;Tastes&#8221; coming in, the idea becomes like a way to tell the world what you think about the beer you&#8217;re drinking right now. We kept the idea of pubs (or places as we call them, because it might be a pub, a bar, a beer festival, etc) but you cannot actually review a pub. This serves the purpose of making the app simpler to understand because there&#8217;s just one first class citizen and that&#8217;s the &#8220;Taste&#8221;.</p>
<p>But, I&#8217;m sad now. Why is it that people decide for themselves what an app should do? Why is it that people writing reviews for apps cannot be constructive? Why is it that people think that free apps should be 100% perfect straight away? Here&#8217;s a review we got a day after 2.2 came out:</p>
<blockquote><p>[1 star] &#8211; Why hasn&#8217;t beerintheevening got an app? This is not a satisfactory substitute &#8211; it neither adequately provides user reviews of pubs nor does it review beers. FAIL.</p></blockquote>
<p>This was coupled with a support email request which goes further to complain about the app and how they failed to read the instructions for adding a new pub (it&#8217;s really not that complicated).</p>
<p>We&#8217;ll see what the future holds for BeerMap. I sincerely hope that it&#8217;s good things because I think that there&#8217;s a definite use for an app like this, especially in the UK where beer can vary between parts of the country so you want a way to find a pub that&#8217;s great.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/11/what-is-it-with-people-and-free-apps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GiffGaff: Great for iPad!</title>
		<link>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/</link>
		<comments>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 14:32:51 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=432</guid>
		<description><![CDATA[I&#8217;ve heard about GiffGaff from a lot of people but it was only when I realised how good it would be for my iPad that I decided to give it a go. So I got a SIM, topped up £10 and now I can spend just 20p each day and get 20MB of data! It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve heard about <a href="http://giffgaff.com/orders/affiliate/mattjgalloway_ipad">GiffGaff</a> from a lot of people but it was only when I realised how good it would be for my iPad that I decided to give it a go. So I got a SIM, topped up £10 and now I can spend just 20p each day and get 20MB of data! It&#8217;s great for my iPad because I can use it whilst out and about and just pay for each day that I want mobile data, which is quite rare for me really.</p>
<p>So if you&#8217;ve got an iPad and want cheap data for it then go <a href="http://giffgaff.com/orders/affiliate/mattjgalloway_ipad">grab a GiffGaff SIM</a> and away you go!</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[UPDATED - Not a bug!] Another interesting compiler bug (in Apple&#8217;s LLVM this time)</title>
		<link>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/</link>
		<comments>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 13:57:09 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=419</guid>
		<description><![CDATA[I came across another interesting compiler bug today. I&#8217;m not going to go into too much detail about it, but the problem code is this: To break that down a bit, the function is doing this: Initialise a couple of variables and set them to 0. Then perform some inline assembly using the variables. This [...]]]></description>
			<content:encoded><![CDATA[<p>I came across another interesting compiler bug today. I&#8217;m not going to go into too much detail about it, but the problem code is this:</p>
<pre class="brush: cpp; title: ; notranslate">
void a() {
    int a = 0;
    int b = 0;
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=r&quot;(a), &quot;+m&quot;(b)
           );
}
</pre>
<p>To break that down a bit, the function is doing this:</p>
<ol>
<li>
<pre class="brush: cpp; title: ; notranslate">
    int a = 0;
    int b = 0;
</pre>
<p>Initialise a couple of variables and set them to 0.
	</li>
<li>
<pre class="brush: cpp; title: ; notranslate">
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=r&quot;(a), &quot;+m&quot;(b)
           );
</pre>
<p>Then perform some inline assembly using the variables. This just puts 0 into the register that will hold our &#8216;a&#8217; variable, then loads the value of &#8216;b&#8217; into &#8216;a&#8217;, then sets &#8216;b&#8217; to 0.
	</li>
</ol>
<p>This is of course a contrived example, but it illustrates the bug. The output assembly from LLVM-GCC or clang is (for ARM architecture):</p>
<pre class="brush: plain; title: ; notranslate">
        .globl  _a
        .align  2
        .code   16
        .thumb_func     _a
_a:
        sub     sp, #8
        movs    r0, #0
        movt    r0, #0
        str     r0, [sp, #4]
        str     r0, [sp]
        mov     r0, sp
        @ InlineAsm Start

        mov r0, 0
        ldr r0, [r0]
        mov [r0], 0

        @ InlineAsm End
        str     r0, [sp, #4]
        add     sp, #8
        bx      lr
</pre>
<p>The interesting bit is the inline assembly. You&#8217;ll notice that it&#8217;s doing something very stupid. It&#8217;s choosing the same register for both operands (it&#8217;s choosing r0). This is completely wrong, and will lead to a runtime crash in this case due to the dereference of 0.</p>
<p>I did a bit of hunting and it appears to be a problem in generating the LLVM bytecode as the problem manifests itself before the LLVM bytecode is compiled down into instructions, like so:</p>
<pre class="brush: plain; title: ; notranslate">
define void @a() nounwind ssp {
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4
  store i32 0, i32* %b, align 4
  %1 = call i32 asm &quot;&#92;&#48;A&#92;&#48;9mov $0, 0&#92;&#48;A&#92;&#48;9ldr $0, $1&#92;&#48;A&#92;&#48;9mov $1, 0&#92;&#48;A&quot;, &quot;=r,=*m,*m&quot;(i32* %b, i32* %b) nounwind, !srcloc !0
  store i32 %1, i32* %a, align 4
  ret void
}
</pre>
<p>You can see here that the &#8216;%a&#8217; (i.e. variable &#8216;a&#8217;) is never referenced, only &#8216;%b&#8217; (i.e. variable &#8216;a&#8217;). This is not what we&#8217;d expect at all given we&#8217;re referencing both variables in the code.</p>
<p>I found this quite interesting <img src='http://iphone.galloway.me.uk/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p><strong>[Update]</strong></p>
<p>I&#8217;ve actually found out that this isn&#8217;t a bug! That&#8217;s good news, right? It&#8217;s quite a subtle thing, but the heart of the problem can be explained after understanding the <a href="http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Modifiers.html#Modifiers">modifiers to operands</a> on inline assembly. The problem is that we&#8217;re not specifying that &#8216;a&#8217; is <em>clobbered early</em>. In the assembly, we&#8217;re writing to it before reading we&#8217;ve finished using all input operands (only &#8216;b&#8217; in this case) so we&#8217;re meant to mark it like that. It&#8217;s just luck that GCC does the right thing &#8211; Apple&#8217;s LLVM is doing the right thing and using less registers!</p>
<p>So the correct code is this:</p>
<pre class="brush: cpp; title: ; notranslate">
void a() {
    int a = 0;
    int b = 0;
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=&amp;r&quot;(a), &quot;+m&quot;(b)
           );
}
</pre>
<p>Which results in the following assembly:</p>
<pre class="brush: plain; title: ; notranslate">
        .globl  _a
        .align  2
        .code   16
        .thumb_func     _a
_a:
        sub     sp, #8
        movs    r0, #0
        movt    r0, #0
        str     r0, [sp, #4]
        str     r0, [sp]
        mov     r0, sp
        @ InlineAsm Start

        mov r1, 0
        ldr r1, [r0]
        mov [r0], 0

        @ InlineAsm End
        str     r1, [sp, #4]
        add     sp, #8
        bx      lr
</pre>
<p>And the following LLVM:</p>
<pre class="brush: plain; title: ; notranslate">
define void @a() nounwind ssp {
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4
  store i32 0, i32* %b, align 4
  %1 = call i32 asm &quot;&#92;&#48;A&#92;&#48;9mov $0, 0&#92;&#48;A&#92;&#48;9ldr $0, $1&#92;&#48;A&#92;&#48;9mov $1, 0&#92;&#48;A&quot;, &quot;=&amp;r,=*m,*m&quot;(i32* %b, i32* %b) nounwind, !srcloc !0
  store i32 %1, i32* %a, align 4
  ret void
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Review: Sensible TableView</title>
		<link>http://iphone.galloway.me.uk/2011/04/review-sensible-tableview/</link>
		<comments>http://iphone.galloway.me.uk/2011/04/review-sensible-tableview/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 20:30:54 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=402</guid>
		<description><![CDATA[Every iOS developer should be very familiar with the UITableView. It&#8217;s the main building block of most applications (excluding OpenGL based games, of course). So back when I started developing applications when iOS was on version 2.0, I created a tutorial for creating custom UITableViewCell objects. Since then application developers are finding more and more [...]]]></description>
			<content:encoded><![CDATA[<p>Every iOS developer should be very familiar with the UITableView. It&#8217;s the main building block of most applications (excluding OpenGL based games, of course). So back when I started developing applications when iOS was on version 2.0, I created a <a href="http://iphone.galloway.me.uk/iphone-sdktutorials/custom-uitableviewcell/">tutorial</a> for creating custom UITableViewCell objects. Since then application developers are finding more and more that they need to push the boundaries of what they can do with a simple UITableViewCell. In iOS 3.0 Apple introduced a style attribute for cells, which certainly helped with a lot of situations but there is still the tedious process of creating custom cells with anything more than the standard 1 or 2 text labels.<br />
<span id="more-402"></span></p>
<p>I usually end up hand rolling a UITableViewCell subclass, or if I only ever want 1 of that type of cell in a given view (for instance a single username and single password cell) I might use Interface Builder to create the cell and put it in the XIB for the view controller. But when you end up with 10+ projects each with a handful of custom cells, you soon go quite mad trying to remember each detail about each cell.</p>

<p>So I was pleasantly surprised when I was recently given the opportunity to take a look at <a href="http://sensiblecocoa.com/?amigosid=2">Sensible Cocoa</a>&#8216;s <a href="http://sensiblecocoa.com/features.html?amigosid=2">Sensible TableView (STV)</a> project, which is a collection of Objective-C classes for easily creating custom UITableViewCells.</p>
<h1>The Review</h1>
<p>Having had a play with STV I have managed to get to grips with how it works and why you might want to use it. Hopefully by reading this you can get an insight into why you might want to use STV.</p>
<h2>Features</h2>
<p>STV at it&#8217;s core is a very simple way of creating custom cells (see the code samples below) but also there&#8217;s a lot of very impressive extra features such as binding to a data model so that you can create very complex interfaces to interact with your data. They also appear to be adding features all the time which is a good sign.</p>
<h2>Examples</h2>
<p>The best examples are on <a href="http://sensiblecocoa.com/video-tutorials.html?amigosid=2">Sensible Cocoa&#8217;s website</a> where there are videos showing how to use it.</p>
<h2>Cost</h2>
<p>STV is <a href="http://sensiblecocoa.com/?amigosid=2">currently $99</a> for a license which includes full source code &#038; limited time support. I think this is pretty decent value for money given what you can do with it.</p>
<h2>Summary</h2>
<p>If you&#8217;re someone who uses UITableView everyday, I can see that STV will definitely make your life a lot easier. I&#8217;d suggest that it might be suited more to contractors who are constantly churning out apps rather than developers working on long term projects where rolling your own cells is probably the right way to go still, given that you still get much more control over the cells if you do it yourself. For instance, in my <a href="http://itunes.apple.com/gb/app/subnet-calc/id295652242?mt=8&#038;uo=4">Subnet Calc</a> <a href="http://itunes.apple.com/gb/app/subnet-calc-pro/id303555213?mt=8&#038;uo=4">range</a> of <a href="http://itunes.apple.com/gb/app/subnet-calc-hd/id383209264?mt=8&#038;uo=4">applications</a> I have created a custom cell that is far too custom for STV to ever implement &#8211; it&#8217;s too specific to the application.</p>
<p>Overall, go and give STV a look. Watch the <a href="http://sensiblecocoa.com/video-tutorials.html?amigosid=2">tutorial videos</a> and see if it can do something for you.</p>
<p>In the coming weeks I aim to write a few more posts aimed at showing a few of the features of STV.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/04/review-sensible-tableview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assembly &#8211; beware local label names with &#8220;-dead_strip&#8221; option!</title>
		<link>http://iphone.galloway.me.uk/2011/03/assembly-beware-local-label-names-with-dead_strip-option/</link>
		<comments>http://iphone.galloway.me.uk/2011/03/assembly-beware-local-label-names-with-dead_strip-option/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 13:08:47 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=393</guid>
		<description><![CDATA[I came across a very strange bug whilst developing an iOS application whereby the application would seg fault and whilst stepping through the code I found it was going all over the place. This lead me to run the application through otool, and I discovered that half the code for a function was missing! Here [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a very strange bug whilst developing an iOS application whereby the application would seg fault and whilst stepping through the code I found it was going all over the place. This lead me to run the application through otool, and I discovered that half the code for a function was missing!</p>
<p>Here is an example of what happened&#8230;</p>
<p>Consider the following C file. It&#8217;s just a very simple function that has some inline assembly (to count down from 10 to 0) and a very simple function that does absolutely nothing.</p>
<pre class="brush: cpp; title: ; notranslate">
void func() {
    int tmp;
    __asm__ __volatile__ (
        &quot;\tmov %0, #10\n&quot;

        &quot;.loop:\n&quot;
        &quot;\tsubs %0, %0, #1\n&quot;
        &quot;\tbne .loop\n&quot;

        : &quot;=r&quot; (tmp)
        : &quot;r&quot; (tmp)
    );
}

void funcB() {
}
</pre>
<p>Let&#8217;s see what happens when we compile it for iOS&#8230;</p>
<pre class="brush: plain; title: ; notranslate">
        .section __TEXT,__text,regular
        .section __TEXT,__textcoal_nt,coalesced
        .section __TEXT,__const_coal,coalesced
        .section __TEXT,__picsymbolstub4,symbol_stubs,none,16
        .text
        .align 2
        .globl _func
_func:
        @ args = 0, pretend = 0, frame = 4
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        sub     sp, sp, #4
        ldr     r3, [sp]
                mov r3, #10
.loop:
        subs r3, r3, #1
        bne .loop

        str     r3, [sp]
        sub     sp, r7, #0
        ldmfd   sp!, {r7, pc}
        .align 2
        .globl _funcB
_funcB:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        ldmfd   sp!, {r7, pc}
        .subsections_via_symbols
</pre>
<p>That all looks fairly normal and we could quite happily believe that was going to work just fine. So, let&#8217;s use this function in a test application. The code below is just a very simple application that calls the &#8216;func()&#8217; function and then returns.</p>
<pre class="brush: cpp; title: ; notranslate">
void func();
int main() {
    func();
    return 0;
}
</pre>
<p>So now let&#8217;s see what happens when we link this with the same options that you would have on by default in an iOS application (hint: -dead_strip is active).</p>
<p>Output from &#8216;otool -vV -t&#8217; on the linked application:</p>
<pre class="brush: plain; title: ; notranslate">
_main:
00002fa0        e92d4080        push    {r7, lr}
00002fa4        e28d7000        add     r7, sp, #0      @ 0x0
00002fa8        eb000002        bl      _func
00002fac        e3a03000        mov     r3, #0  @ 0x0
00002fb0        e1a00003        mov     r0, r3
00002fb4        e8bd8080        pop     {r7, pc}
_func:
00002fb8        e92d4080        push    {r7, lr}
00002fbc        e28d7000        add     r7, sp, #0      @ 0x0
00002fc0        e24dd004        sub     sp, sp, #4      @ 0x4
00002fc4        e59d3000        ldr     r3, [sp]
00002fc8        e3a0300a        mov     r3, #10 @ 0xa
</pre>
<p>What?! Where&#8217;s the rest of the &#8216;func()&#8217; code?! Not only is it missing, but it would appear that &#8216;func()&#8217; just simply stops without ever returning?! That looks very suspicious&#8230; So, let&#8217;s compile it without the &#8216;-dead_strip&#8217; option:</p>
<p>Output from &#8216;otool -vV -t&#8217; on the linked application:</p>
<pre class="brush: plain; title: ; notranslate">
_main:
00002f80        e92d4080        push    {r7, lr}
00002f84        e28d7000        add     r7, sp, #0      @ 0x0
00002f88        eb000002        bl      _func
00002f8c        e3a03000        mov     r3, #0  @ 0x0
00002f90        e1a00003        mov     r0, r3
00002f94        e8bd8080        pop     {r7, pc}
_func:
00002f98        e92d4080        push    {r7, lr}
00002f9c        e28d7000        add     r7, sp, #0      @ 0x0
00002fa0        e24dd004        sub     sp, sp, #4      @ 0x4
00002fa4        e59d3000        ldr     r3, [sp]
00002fa8        e3a0300a        mov     r3, #10 @ 0xa
.loop:
00002fac        e2533001        subs    r3, r3, #1      @ 0x1
00002fb0        1afffffd        bne     .loop
00002fb4        e58d3000        str     r3, [sp]
00002fb8        e247d000        sub     sp, r7, #0      @ 0x0
00002fbc        e8bd8080        pop     {r7, pc}
_funcB:
00002fc0        e92d4080        push    {r7, lr}
00002fc4        e28d7000        add     r7, sp, #0      @ 0x0
00002fc8        e8bd8080        pop     {r7, pc}
</pre>
<p>Ah, that&#8217;s better! The loop is back and so is the return. Also, &#8216;funcB()&#8217; is still in there. So, what has happened you may ask. Well, -dead_strip is designed to remove symbols from a binary that are not required. So we&#8217;d expect &#8216;funcB&#8217; to be removed, but not .loop as it&#8217;s part of &#8216;func&#8217;. However, if we look closer, what has happened is that &#8216;.loop&#8217; has become a top level symbol rather than a symbol local to the &#8216;func&#8217; symbol. So -dead_strip assumed that it was a symbol not used anywhere (as it&#8217;s only accessed from within .loop itself) and so it removed it, resulting in a completely mangled application binary.</p>
<p>To stop this happening you must always prefix your local symbols with &#8216;L&#8217; (as per the GCC documentation!). But I think this is an excellent example of what can go wrong if you just do 1 tiny thing wrong with inline assembly.</p>
<p>As a side note, I decided to try compiling/assembling/linking all of the above for Android as well. Interestingly the results were different. First I&#8217;ll show the outputs of the various stages and then explain the results.</p>
<p>The assembly generated for Android:</p>
<pre class="brush: plain; title: ; notranslate">
        .arch armv6
        .fpu softvfp
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 2
        .eabi_attribute 30, 6
        .eabi_attribute 18, 4
        .file   &quot;test.c&quot;
        .text
        .align  2
        .global func
        .type   func, %function
func:
        @ args = 0, pretend = 0, frame = 8
        @ frame_needed = 1, uses_anonymous_args = 0
        @ link register save eliminated.
        str     fp, [sp, #-4]!
        add     fp, sp, #0
        sub     sp, sp, #12
        ldr     r3, [fp, #-8]
#APP
@ 3 &quot;test.c&quot; 1
                mov r3, #10
.loop:
        subs r3, r3, #1
        bne .loop

@ 0 &quot;&quot; 2
        str     r3, [fp, #-8]
        add     sp, fp, #0
        ldmfd   sp!, {fp}
        bx      lr
        .size   func, .-func
        .align  2
        .global funcB
        .type   funcB, %function
funcB:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        @ link register save eliminated.
        str     fp, [sp, #-4]!
        add     fp, sp, #0
        add     sp, fp, #0
        ldmfd   sp!, {fp}
        bx      lr
        .size   funcB, .-funcB
        .ident  &quot;GCC: (GNU) 4.4.3&quot;
        .section        .note.GNU-stack,&quot;&quot;,%progbits
</pre>
<p>Disassembly of linked app for Android (without stripping):</p>
<pre class="brush: plain; title: ; notranslate">
000082e0 &lt;main&gt;:
    82e0:       e92d4800        push    {fp, lr}
    82e4:       e28db004        add     fp, sp, #4      ; 0x4
    82e8:       eb000002        bl      82f8 &lt;func&gt;
    82ec:       e3a03000        mov     r3, #0  ; 0x0
    82f0:       e1a00003        mov     r0, r3
    82f4:       e8bd8800        pop     {fp, pc}

000082f8 &lt;func&gt;:
    82f8:       e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
    82fc:       e28db000        add     fp, sp, #0      ; 0x0
    8300:       e24dd00c        sub     sp, sp, #12     ; 0xc
    8304:       e51b3008        ldr     r3, [fp, #-8]
    8308:       e3a0300a        mov     r3, #10 ; 0xa

0000830c &lt;.loop&gt;:
    830c:       e2533001        subs    r3, r3, #1      ; 0x1
    8310:       1afffffd        bne     830c &lt;.loop&gt;
    8314:       e50b3008        str     r3, [fp, #-8]
    8318:       e28bd000        add     sp, fp, #0      ; 0x0
    831c:       e8bd0800        pop     {fp}
    8320:       e12fff1e        bx      lr

00008324 &lt;funcB&gt;:
    8324:       e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
    8328:       e28db000        add     fp, sp, #0      ; 0x0
    832c:       e28bd000        add     sp, fp, #0      ; 0x0
    8330:       e8bd0800        pop     {fp}
    8334:       e12fff1e        bx      lr
</pre>
<p>Disassembly of linked app for Android (after using strip command):</p>
<pre class="brush: plain; title: ; notranslate">
000082e0 &lt;main&gt;:
    82e0:       e92d4800        push    {fp, lr}
    82e4:       e28db004        add     fp, sp, #4      ; 0x4
    82e8:       eb000002        bl      82f8 &lt;func&gt;
    82ec:       e3a03000        mov     r3, #0  ; 0x0
    82f0:       e1a00003        mov     r0, r3
    82f4:       e8bd8800        pop     {fp, pc}

000082f8 &lt;func&gt;:
    82f8:       e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
    82fc:       e28db000        add     fp, sp, #0      ; 0x0
    8300:       e24dd00c        sub     sp, sp, #12     ; 0xc
    8304:       e51b3008        ldr     r3, [fp, #-8]
    8308:       e3a0300a        mov     r3, #10 ; 0xa
    830c:       e2533001        subs    r3, r3, #1      ; 0x1
    8310:       1afffffd        bne     830c &lt;func+0x14&gt;
    8314:       e50b3008        str     r3, [fp, #-8]
    8318:       e28bd000        add     sp, fp, #0      ; 0x0
    831c:       e8bd0800        pop     {fp}
    8320:       e12fff1e        bx      lr
</pre>
<p>You can see that the Android GCC has done a much better job at stripping out the symbols. This is because of a subtle &#8220;.size&#8221; attribute given to functions in the assembly for Linux (and therefore Android) which tells the assembler how big the function is. However, this doesn&#8217;t exist on Mac and the size is calculated by taking the distance between the start of a symbol and the next symbol.</p>
<p>This actually helps us to understand a bit more what <strong>actually</strong> went wrong. If you look back up at the assembly generated for iOS and Android you&#8217;ll see that the &#8216;.loop&#8217; appears as a top level symbol, which is confirmed by running &#8216;nm&#8217; on the resulting object file. This sounds all wrong, since the .loop symbol should really be local to the &#8216;func()&#8217; function. The reason being because you need to prefix local symbols with &#8216;L&#8217;. If we change &#8216;.loop&#8217; for &#8216;Lloop&#8217; in the sample file, then these are the resulting outputs showing the iOS linker doing the right thing this time even with -dead_strip enabled.</p>
<p>Compiled output:</p>
<pre class="brush: plain; title: ; notranslate">
        .section __TEXT,__text,regular
        .section __TEXT,__textcoal_nt,coalesced
        .section __TEXT,__const_coal,coalesced
        .section __TEXT,__picsymbolstub4,symbol_stubs,none,16
        .text
        .align 2
        .globl _func
_func:
        @ args = 0, pretend = 0, frame = 4
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        sub     sp, sp, #4
        ldr     r3, [sp]
                mov r3, #10
Lloop:
        subs r3, r3, #1
        bne Lloop

        str     r3, [sp]
        sub     sp, r7, #0
        ldmfd   sp!, {r7, pc}
        .align 2
        .globl _funcB
_funcB:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        ldmfd   sp!, {r7, pc}
        .subsections_via_symbols
</pre>
<p>Disassembly of linked application:</p>
<pre class="brush: plain; title: ; notranslate">
_main:
00002f8c        e92d4080        push    {r7, lr}
00002f90        e28d7000        add     r7, sp, #0      @ 0x0
00002f94        eb000002        bl      _func
00002f98        e3a03000        mov     r3, #0  @ 0x0
00002f9c        e1a00003        mov     r0, r3
00002fa0        e8bd8080        pop     {r7, pc}
_func:
00002fa4        e92d4080        push    {r7, lr}
00002fa8        e28d7000        add     r7, sp, #0      @ 0x0
00002fac        e24dd004        sub     sp, sp, #4      @ 0x4
00002fb0        e59d3000        ldr     r3, [sp]
00002fb4        e3a0300a        mov     r3, #10 @ 0xa
00002fb8        e2533001        subs    r3, r3, #1      @ 0x1
00002fbc        1afffffd        bne     0x2fb8
00002fc0        e58d3000        str     r3, [sp]
00002fc4        e247d000        sub     sp, r7, #0      @ 0x0
00002fc8        e8bd8080        pop     {r7, pc}
</pre>
<p>This all serves to illustrate the point that it&#8217;s worth knowing about the options of your compiler, assembler &#038; linker and understanding how everything fits together.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/03/assembly-beware-local-label-names-with-dead_strip-option/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

