Archive for the 'Software' Category

No, iTunes, I don’t want to listen to holiday music in February….

Monday, February 22nd, 2010

We have a ton of Christmas music — 400 or so tracks, with it growing by about 50-75 tracks each year — that ranges across all genres. It is some awesome stuff; blues, reggae, pop, traditional, you name it…

However, it is music that should only be in the rotation starting December 1st, heavy rotation by about the 15th-20th and then no more from December 26th to the next December 1st.

Of course, iTunes is agnostic and, thus, does not follow my holiday music filtration wishes.

Easy to fix.

  1. On all Christmas/Holiday music, add a grouping of “Holiday” or “Christmas” (or whatever you want).
  2. Create a smart playlist that finds all music in said grouping. Make sure the smart playlist matches unchecked items.
  3. On December 26th, select all, ctrl-click, and select uncheck selection
  4. On Decmber 1st of the next year, select the smart playlist, select all, ctrl-clik, and select check selection

Done. iTunes will not select unchecked songs when constructing genius playlists or when playing through the library on shuffle play. Smart playlists can optionally include checked songs.

Posted in Apple, Software | No Comments »

objc_msgSend() Tour Part 4: Method Lookup & Some Odds and Ends

Thursday, February 4th, 2010

Table of Contents

  1. objc_msgSend() Tour Part 1: The Road Map
  2. objc_msgSend() Tour Part 2: Setting the Stage
  3. objc_msgSend() Tour Part 3: The Fast Path
  4. objc_msgSend() Tour Part 4: Method Lookup & Some Odds and Ends

In the first three parts, I gave an overview, explained a bit of the ABI used by Objective-C, and took a near instruction by instruction tour of what happens on the fast path of Objective-C method dispatch. By fast path, I mean what happens 99.9% of the time; a very fast, no overhead, no function call, no locking, set of instructions that grabs the method implementation from the cache and does a tail-call jump to that implementation.

The slow path is used rarely. As little as once per unique selector invoked per class with a goal of filling the cache such that the slow path is never used again for that selector/class combination. A handful of operations will cause a class’s cache to be flushed; method swizzling, category loading, and the like.

Note that during +initialize, methods won’t always be cached. Yet another reason to not do any real work during +initialize! Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 3 Comments »

objc_msgSend() Tour Part 3: The Fast Path

Friday, December 18th, 2009

Table of Contents

  1. objc_msgSend() Tour Part 1: The Road Map
  2. objc_msgSend() Tour Part 2: Setting the Stage
  3. objc_msgSend() Tour Part 3: The Fast Path
  4. objc_msgSend() Tour Part 4: Method Lookup & Some Odds and Ends

In any case, with the foundation set — with the id of the object to be targeted in %rdi and the selector of the method to be invoked in %rsi — we can jump into objc_msgSend() and understand exactly what happens instruction by instruction. Or more specifically, the compiler issues a call into objc_msgSend() (which sets up a stackframe for objc_msgSend() which, through tail call optimization, turns into the stackframe for the called method) and the method implementation that objc_msgSend() jumps to will issue a ret instruction to unwind the stack back to the original caller’s frame.

It is pretty easy to correlate the disassembly with the comments and code in the original source file. However, if you ever need to step through the messenger (si steps by instruction in gdb), this will be easier to follow as this is closer to the reality during a debug session.

For almost all method dispatches, dispatch takes what is called the “fast path”. That is, objc_msgSend() finds the implementation in the method cache and passes control to the implementation. Since this is the most common path, it is a good opportunity to break the tour of objc_msgSend() into two parts; the fast path and the slow path (with administrivia).

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 4 Comments »

objc_msgSend() Tour Part 2: Setting the Stage

Friday, December 18th, 2009

Table of Contents

  1. objc_msgSend() Tour Part 1: The Road Map
  2. objc_msgSend() Tour Part 2: Setting the Stage
  3. objc_msgSend() Tour Part 3: The Fast Path
  4. objc_msgSend() Tour Part 4: Method Lookup & Some Odds and Ends

Objective-C is, ultimately, a simple set of extensions to C that provide an object oriented coding and runtime model. Objective-C fully embraces C and leverages the C calling ABI throughout. Every method call is really just a C function call with objc_msgSend() acting as the preamble that figures out exactly which C function — which method — to call!

Thus, it is helpful to understand how objc_msgSend() is called and how that relates to C. That is, how does the compiler translate [someObject doSomething: 0x2a] into a call.

What follows is a bit of code that makes a [totally bogus] simple method call followed by the assembly generated.

Code:
@interface NSObject(foo)
- (id) doSomething: (NSUInteger) index;
@end
...
    NSObject *b;
    NSArray *a;
    b = [a doSomething: 0x2a]; // line 11
Assembly:
    .loc 1 11 0
    movq	-16(%rbp), %rdi
    movq	L_OBJC_SELECTOR_REFERENCES_0(%rip), %rsi
    movl	$42, %edx
    call	_objc_msgSend
    movq	%rax, -8(%rbp)

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 3 Comments »

objc_msgSend() Tour Part 1: The Road Map

Friday, December 18th, 2009

What follows (across this and 3 more posts, maybe more) is a rather detailed tour of objc_msgSend() as implemented in Mac OS X 10.6.2. Rather detailed in that every instruction will be explained. Even though it is relatively few instructions, there is a considerable amount of background information that is helpful to understanding the objc_msgSend() instruction stream.

The motivation behind these posts is entirely selfish. I find the best way for me to learn something is to know it well enough to be able to explain any detail to a room full of folks in full-blown student mode.

Table of Contents

  1. objc_msgSend() Tour Part 1: The Road Map
  2. objc_msgSend() Tour Part 2: Setting the Stage
  3. objc_msgSend() Tour Part 3: The Fast Path
  4. objc_msgSend() Tour Part 4: Method Lookup & Some Odds and Ends

Read the rest of this entry »

Posted in Mac OS X, Objective-C, Software | 12 Comments »

Uncharted 2; Brilliant, Marred by Intermittent Lockup/Freezes

Sunday, November 29th, 2009

I have recently been playing through Uncharted 2: Among Thieves
after having played through the utterly brilliant Uncharted: Drake’s Fortune
.

Absolutely genius game. Personally, I have always hated tracked games; games that have a single story line that you follow along, with little to no bifurcation. Yet, Uncharted 1/2 both do such a good job of telling a compelling story that I’m having a total blast. The voice acting and character animation are top notch.

It is as if you are playing along an interactive movie. Where you do get to make all the decisions is in how you approach any given battle; …go stealth or go in with guns a-blazing? …AK-47 with hundreds of round, a sniper rifle that’ll one-shot-kill, but requires precise aim, or a shotgun that requires close range & is devastating? …sneak around and take out the gunner’s nest from behind or try and lob a grenade?

The one problem though, is that the game freezes about once every hour to hour and a half of play. It may be a thermal issue (though I’m playing in a relatively chill room and the PS3 has excellent ventilation), but the symptoms are slightly odd. The music keeps playing, but all graphics and controls are dead. Upon restart, you find yourself just beyond where the freeze occurred, as if the game detected the freeze and moved you to just beyond the point where it happened. If true, this is welcome, but it sometimes means that you are in a tight spot with no appropriate weapons.

And I’m not the only one. Looking at this google search, I see hundreds of reports of the same behavior.

Some see the freeze occur more often than others (and I suspect some are thermal issues, from the descriptions). The problems aren’t limited to any particular model of PS3, either (I’m on a 120GB Slim, being the cheap bastard that I am).

Others have reported no problems at all.

I wish there were some means of reporting bugs and/or tracking issues like these.

While annoying, this isn’t a catastrophic bug (at least not for me — it doesn’t happen that often) to the point of making me regret the purchase. Total a worthy and awesome game to play through!

Posted in Entertainment, Software | No Comments »

Calling Python from Objective-C

Saturday, November 21st, 2009

Every six months or so, I run across a question along the lines of how do I invoke some Python code from Objective-C?

Kinda like here for which I posted the same conceptually concrete but technically vague pattern that I have posted for the last decade+.

Which led to this question.

OK — enough is enough. Here is a working example. Read the rest of this entry »

Posted in Objective-C, PyObjC, Software | No Comments »

DPhyllotaxis

Sunday, October 25th, 2009
Screenalicious - DPhyllotaxis - 200910298 205041.539.png

Ultimately, the whole point of resurrecting my old screensaver code was to finally port DPhyllotaxis to Snow Leopard.

Beyond being, perhaps, the most over-engineered screen saver ever for what ultimately draws colored dots, I wrote this as a sort of virtual flower for my then-girlfriend, now-wife-of-more-than-a-decade, Christine.

The underlying algorithm on this one is based entirely on phyllotaxis and the phyllotactic pattern of growth seen across so much of the plant world. The most well known example being the layout of seeds in a sunflower and that particular form of phyllotaxis is exactly what this screensaver mimics.

The color calculation in this particular screen saver is, frankly, goofy. Every floret — every dot — is actually rendered. The brightness is determined by calculating a color once, grabbing the red component and then calculating the color again using a slightly different algorithm and using the previous red value as the new brightness. Rather silly, but the results are pleasant enough.

Screenalicious - DPhyllotaxis - 200910298 205114.991.png

When I originally wrote this in 1994-ish, it used Display PostScript to do all the drawing. Specifically:

/* this should not be done here */
PSarc(cp.x, cp.y, 15. + (11. * pp.r), 0, 360);
PSgsave();
PSfill();
PSgrestore();
NXSetColor(NX_COLORBLACK);
PSstroke();

I have no idea why, 15 years ago, I thought it important to note that “this should not be done here”. None. So, in the ported code, the comment is gone.

CGFloat floretDiameter = 10. + (11. * pp.r);
CGFloat floretRadius = floretDiameter / 2.;
NSRect floretRect =
    NSMakeRect(cp.x - floretRadius,
    cp.y - floretRadius,
    floretDiameter, floretDiameter);
NSBezierPath *floretPath = [NSBezierPath
    bezierPathWithOvalInRect: floretRect];

[floretPath fill];

[[NSColor blackColor] set];
[floretPath stroke];

There are, of course, many ways to make the above a ton faster. Save for reducing power usage by going a more efficient route, it just doesn’t matter for this particular use case as I already had to slow down the animation rate considerably.

Seems there has been a bit of performance jump between the 25MHZ 68040 this was originally written on and the 2GHZ Core 2 Duo machine I used for the porting work.

Code is in the same repository as the other screen savers. I also tossed a pre-built binary on the server. Only tested on 64-bit Snow Leopard, but it might should also work on 10.5 ppc/i386.

Posted in Entertainment, Mac OS X, Objective-C, Software | No Comments »

SpiroScales & LizardView; Source for Snow Leopard Available

Monday, October 19th, 2009
SolitaireInfoPanel

Hah! While the NeXT system in my garage didn’t have the original BackSpace.app on it, it did have a copy of the solitaire game that my friend and I worked on.

And that had an itty bitty SpiroScale screensaver embedded in the info panel!

Imagine that full screen — all dithered and textured in 2 bit grayscale. When color NeXT systems shipped a couple of years after I wrote the screensaver, I was quite delighted with the results (as seen below)!

Solitaire was an amusing little bit of indie development. Beyond having 4 different company logos in that about panel prior to finally signing with a distributor, the eventual distribution license was a flat monthly royalty for 25 months for Adam and I.

Flat royalties work out well when the company making the hardware your software runs on stops making said hardware….


Screenalicious - SpiroScales - 200910291 233622.054.png

Some months ago, I ported an ancient screensaver of mine to Mac OS X (then Leopard) and made the binary available.

I have since ported it to Snow Leopard and have made the source available via subversion. Pre-built binaries are also available.

There are actually two bits of source there; the first is which contains the Lizard Saver and a second screen saver called Spiro Scales. The screen savers should build for both Leopard and Snow Leopard, including support for Snow Leopard’s 64 bit GC-only screensaver requirement.

The second bit of source is called Screenalicious and is a simple/stupid application for displaying a screensaver in a window and allowing you to easily take scaled down screenshots. It was a hack when I originally wrote it in the ’90s sometime and it is still a hack.

SpiroScales was my first interactive screen saver. Interactive in that the pattern generated is entirely determined by mouse position (not movement).

Read the rest of this entry »

Posted in Hacks, Software | 6 Comments »

Class Extensions Explained

Friday, September 11th, 2009

One of the features added to Objective-C 2.0 is Class Extensions.

If you have feature requests or bugs, http://bugreport.apple.com/ is your friend!

There has been a bit of confusion about their functionality and purpose.

First, a bit of background. One of the great strengths of Objective-C is the very sharp division between interface and implementation.

Not only can you declare a public interface to a class — the set of methods that clients of the class can use — but you can also declare varying degrees of private interface ranging from “stuff for my framework” through to “stuff only to be used in the implementation of this subset of methods of the class”.

Class extensions were designed to solve two problems. The first was to enable to compiler to better validate the private interfaces a class might have and the second was to solve a subtle, but gnarly, problem with properties (another feature added to Objective-C 2.0).

Read the rest of this entry »

Posted in Objective-C, Software | 12 Comments »