Friday, January 26, 2007

A Wii bit of fun and games... Or what does milk have to do with a Wii??

My wife sometimes actually reads my blog.  Of course I get a some of grief about it and a few chuckles at home.  I also get the random threat when I do something bone-headed at home where she tells me that “I should post a comment on your blog and tell people what you're really like!”  I'm sure many of you can relate :-).  My wife likes to tell her friends things like, “he deals with all kinds of high tech stuff and helps lead a team of equally, if not more, talented engineers... yet he can't even remember what night is trash night!”  Suffice it to say, my wife is not at all impressed with what I do here at CodeGear, which is actually something I like.  It helps keep me a little humble (yeah, right! ;-) and a little more grounded.

Last year I made this post talking about how CodeGear needs to take a page from the Nintendo playbook surrounding their brand new gaming console called the Wii.  Ever since then, I've been keeping and eye on the Wii and it's availablity over the weeks following it's introduction.  Sales are still going strong for the Wii, and Nintendo is reporting strong sales and equally strong profits.  While the PS3 and XBox360 are getting more press coverage about how they're not doing as well as the Wii.  The more I read reviews about it and then talked with our very own David Lock who actually acquired his Wii console by paying a premium on eBay, the more I thought that this is actually a game console I'd by for myself.  Of course there are several PS2s, a GameCube, an XBox, PSP, NintendoDS at home, but they've all been purchased by and/or for my kids.  I've never really cared to have my own gaming console.  The Wii actually changed my mind.

So last weekend, I'd heard that some of the local purveyors of video game consoles were to get a fresh shipment of Wii consoles on Sunday morning.  So I got up and headed out to score my very own Wii console.  Didn't work out.  I was a little too late and the vouchers had all been handed out an hour before the store even opened!  Oh well...

So I come home a couple of days ago to find a couple of brown paper wrapped packages sitting on the counter. My wife and some of my children were in the kitchen when I came in.  Apparently these were some belated Christmas presents (since we ended up returning my main present due to it being defective had not replaced it).  So I opened them.  One was a huge bottle of Tums (an antacid)... gee thanks, I guess I could use those for all the stress here at CodeGear ;-).  The next package was a huge box of instant oatmeal packets that I keep in my office that I eat for breakfast when I get into work in the mornings.  Thanks, I can certainly use that too.  As you can imagine, I figured something was up...  So my wife then drags out another even larger package with the same brown-paper wrapping.  By this time, I was figuring it was another goofy gift as I've become used to being the butt of many jokes at home... I shaked it... nothing rattled too much.  I squeezed it... it was firm... must be another box.  Then I opened it.  I was shocked!  It was a Wii!  Apparently it was a very serendipidous event that she was even able to locate someplace that wasn't sold out.  We do a lot of shopping at the local warehouse club/big box/bulk store Costco, which is similar to the Sam's Club stores that dominate the mid-west US.  Anyway, she was grabbing some various items like food and other consumables, when as they were loading up to head back home, one of my sons who was helping noticed that the box of milk was leaking (only in America can you go to a store that sells milk and 60” HD plasma TVs!).  So they headed back into the exchange the milk, when they noticed one of the workers rolling out a brand new palette of Wii consoles!  SCORE!!

So it's been a couple of days that I've been able to play with this new toy, and so far it is everything and more.  I'm not one to sit down an play a game for hours, especially some Action Adventure, FPS, or MMORPGs since they really require a lot of time and investment.  However the Wii, with it's motion sensing wireless controllers, has enabled a new generation of “get off the couch and move” games.  So now I can go in and play a game of bowling, or tennis, baseball, golf or even boxing.  And that's the only investment I've made.  The whole family can get together and play a game.  They've even suggested that with this new generation of motion sensing controllers and more immersive and interactive gameplay, video games no longer have to be the pudge-inducing, lazy couch potato creating, bane of modern society.  Hm... maybe I can use the excuse that I need to shed a few pounds to wrestle control of the TV away from the kids and play some  games.

Saturday, January 13, 2007

How can you raise an "Out of Memory" exception when you don't have any?

Or, “How to take a simple problem and create a complex solution.“

In the early days of developing Delphi we pondered that very question.  How can you allocated an exception object on the heap when the memory manager has already just told you “Hey the memory's not here man.”?  The answer to that turned out to be remarkably easy and once we realized that, there was a big “DUH!” moment on the team.  Before I reveal the ultimate solution, let's talk about the stops along the way.  I want to highlight this early solution to demonstrate that simplicity is very often the right approach.  If you find yourself layering ever more complicated logic and architecture to solve some rare edge condition case, maybe you should first stop.  (the first step in getting yourself out of a hole is to stop digging ;-).

The original approach to this problem involved creating a separate heap that was reserved at application startup and would hold these “special case” exception object instances.  So when the memory manager would detect that it is unable to satisfy an allocation request, a runtime error would happen which is trapped by the SysUtils unit's exception hook and translate that error into an exception that it then raised.  The problem is how do you allocate a class instance on a different heap?  This is where the class function NewInstance was introduced.  If you override that virtual class method, you can control where and how the object's instance memory is allocated.  The corrollary method is not a class method but is an instance method called FreeInstance, which as you can see would return the memory to the heap.  That looks simple, right?  Just override those methods on the EOutOfMemory exception class and you're sure to always be able to allocate an instance of that object.  In the early days, this was how it was done and it worked fine.

The problem was that we'd set aside this memory at startup, but the question was how much memory?  Enough for one, or two instances?  Ten instances?  Another problem was that it meant having another memory manager that would only manage memory from this small heap off to the side.  It was becoming clear that this solution, while it worked and seemed very clever, it was just too complicated.  What is interesting is that buried within this original complicated solution, was a better solution just staring us in the face.  Can you guess what that solution is?

It turns out that it was as simple as just pre-allocating the exception class instance... DUH!!!  No need to create a separate heap with its own memory manager, no need to override NewInstance and FreeInstance.  We really had no further need for those two methods so we could remove then, right?  Not so fast.  What if someone wrote an application that actually handled the Out Of Memory exception and was able to relieve the memory pressure (say, by freeing some easily recreated cached objects) and continue executing.  The problem is that you don't want to let the EOutOfMemory exception to ever be freed.  How do you do that when you cannot control the exception processing logic which will always destroy the currently raised exception object once the the handling “except” block exits?  Remember the NewInstance and FreeInstance methods from our overly complicated solution?  What if you just overrode the FreeInstance method and it simply did... nothing?  So now the EOutOfMemory exception instance will remain for the life of the application and can be used over and over again.

So why didn't we remove the NewInstance method and just leave the FreeInstance method?  This is probably best characterized as a classic case of serendipity.  It turns out that being able to control how and where an object type or a whole class of object type instances are allocated and freed can be useful in solving a very wide variety of different problems.  Rather than have lopsided methods, we kept the NewInstance and FreeInstance methods so that our ever industrious, always clever, customers would be able to do some new an interesting things.  It would have also meant a compiler change to the codegenerator to remove the call to NewInstance.  So the general usefulness of the feature along with not wanting to potentially introduce a whole slew of new codegen bugs, were a couple of reasons to leave it alone.  We did remove the mini heap manager since we were not using that anymore.

So there you have it.  A little bit more history about the early development of Delphi.  Many times the simplest solution is the right solution.  Having a complicated “clever” solution can come back to haunt you later down the road.

Wednesday, January 3, 2007

The year ahead...

Welcome to the new year!  A new year is a convenient opportunity for one to shed some of the negative baggage from the past year(s) and concentrate on doing better (personally, professionally, family, etc...) throughout the coming year.  My pragmatic and skeptical side tends to eschew such patently “false” and “self-delusional” things like new years resolutions.  I far prefer to be retrospective in December to see how I did, rather than setup up some “pie-in-the-sky” aspiration formed late on December 31st only to stumble and become discouraged or outright forget by January 3rd.  This is why I posted my year in review last week.  To be blunt about it, on January 1st, 2006, I had nearly no clue about what 2006 would hold.  I had reasons to be cautiously optimistic.  However, as we all know, some pretty big changes came about in 2006.

That said, I'm not going to let that stop me from making a few bold predictions and set a few goals for 2007 ;-).

  • CodeGear officially launches in Q1.
  • CodeGear releases several new (as in not Delphi, C++ or JBuilder) products in 2007.
  • Newly revised Delphi and C++ roadmap published.
  • Overall CodeGear team grows throughout 2007.
  • Increase the overall “fun-factor“ for those that work at CodeGear (hmmm.... maybe we need Nerf guns for all the engineers??)

There's probably more that I haven't thought about right now, but that should get things started. 

On another topic, apparently I completely missed being “tagged” by Nick.  Now it has come to my attention that Chad Hower (of Indy fame) has also tagged me...

So here goes:  Five things you (probably) didn't know about me...

  • I have not piloted an F/A-18... But I do have a really cool picture of an F-15E on the wall in my office ;-).  I've had other engineers over the years offer real money for it!
  • I started to build a robot when I was 13 and actually have working arms, base with motorized wheels, and body. It kinda looks like R2D2.
  • Before CodeGear(and Borland), I used to design, prototype, build and program magnetic stripe encoders and access control equipment.  I had this job within 1 year of graduating high-school.
  • I have a patent that is currently pending.
  • My favorite SciFi book was Dune by Frank Herbert.  In fact all the machines I've had over the years are always named for something from those books.  The name of the machine I'm using right now is Atreides.  There is also Sardaukar, Feyd, and Shaddam in my office but many other names have been used over the years.

So that's it.  I guess I need to “tag” someone else now?  How about Marco Cantu and Dr. Bob Swart?