Ritchie Swann

RSS

Another little story about GetType()

Gosh, somebody's linked to something I wrote. Not only that, it's something I forgot I wrote about anyway. Here's the original article by Brandon Kelly, which talks about "is GetType() using reflection"?

Of course it is. What you've got to think about here is seeing your program at different levels of abstraction (which is another article for another time, but maybe not now). Let's put aside the "Manual memory allocation? We don't need no stinkin' manual memory allocation!" world of .NET for a minute and go back to good old C :


struct test
{
  int foo;
  char bar;
  void* baz;
};

Now, when this bit of code gets compiled into something the computer can run, a lot of information gets thrown by the wayside. Your executable code won't know you've got a structure called test, or members called "foo", "bar" and "baz". All it knows about are what you've got in memory. (This isn't quite true if you compile with debug information turned on, but that's another conversation for another time).

What does this look like in memory? Well, it might something like this:

0x12ac0 = foo, 0x12ac4 = bar, 0x12ac8 = baz

The reason I say might is because the size of types in C is completely undefined and up to the compiler to choose, normally based upon the hardware architecture of its target platform. Anyway, it's not important to this conversation - for now, let's just agree that this is what the structure looks like internally.

So far, so good. Now, let's consider another structure:


struct test2
{
  int foo;
  char* bar;
  int baz;
};

What does this look like in memory? Well, it might something like this:

0x12ac0 = foo, 0x12ac4 = bar, 0x12ac8 = baz

Now, if you were at address 0x12ac0, how do you know that what you've got ahead of you is a "test" or a "test2" at runtime? You can't. All you've got is some stuff in memory. This, incidentally, is why templates on C++ take so flippin' long to compile, as they have to back to the original header files again and again and recompile stuff. Everything has got to be reduced down to a layout in memory at the end of the day.

Now, let's imagine that we pass a decree that says "From now on, all structures will start with a description of their name". So our test structure above becomes :


struct test
{
  const char* name = "test";
  int foo;
  char bar;
  void* baz;
};

Now, when you arrive at address 0x12ac0, you'll have something like this that allows you to work out what you've got by looking at its name :

0x12ac0 -> 'test' 0x12ac4 = foo, 0x12ac8 = bar, 0x12acb = baz

This, in a simplified nutshell is what Reflection is doing under the hood for you. When you compile up your class in C#, you get a lot of additional information such as the class name, as per this example - plus a whole load of more stuff. This is the class metadata and layed out in a standard format that the .NET runtime understands, so it can reach in and grab the data you need. So when you call GetType, it has go and hunt through the metadata looking for the information you want.

For the two readers of this who haven't fallen asleep yet, this, incidentally, is why things like Activator.CreateObject() are slow - the .NET runtime always has to search through the metadata and find out what you're talking about. You can work around this by dynamically compiling constructors on the fly, so you only need to look up the metadata once (which is another conversation for another time).

0 comments
Friday, 25 June 2010

iPhone woes

Sorry, I'm a bit of a loose end at the moment. I was trying to browse something on my iPhone, but Safari hung and dropped me back to the main menu screen. I guess it must be time to give it a reboot. Whoever thought you'd ever need to reboot a phone?

I've had an iPhone for a while now, actually. It seemed like a good idea at the time. Then again, so did leaving a keyboard in the back seat of my car overnight once after a gig because I couldn't be bothered to lug it up to the house. (You can guess what happened .... and needless to say I've never done it since).

It's amazing how much goodwill and karma is held by Apple. There seems to be a particular class of devotee who will refuse to believe that there is ever anything wrong with anything they ship, at a level that would make the most feverent GNU supporter sit in awe.

But sadly, things just don't seem to stack up sometimes. And don't get me started on the dross in the iTunes store. (Actually, I don't need to, somebody's already done it for me....

0 comments
Tuesday, 22 June 2010

Don't OpenSource developers believe in regression testing?

Sorry, it's Monday morning and I fancy a bit of a rant. I feel like I should be contributing to the Linux Hater's Blog than here, but what the hell.

I recently helped a site upgrade from phpBB 3.0.5 to 3.0.6 to fix a specific bug with polls that the users were having. Now, I would assume, given the difference in version numbers, that this would be a minor upgrade containing bugfixes. But no, there's a whole slew of new features, none of which I need, and a number of features simply don't work anymore. Didn't anybody test this?

To quote directly from the phpBB site:

"Active topics doesn't work anymore after updating!
Actually, it is now working even better than before. "

Now, I don't know about you, but a rash of users complaining that "I click on the 'View Active Topics' button but I can't see anything - waaah!" doesn't really qualify in my mind as "even better than before". Yes, it's a FAQ, it's easy to fix and I didn't waste too much time - but if it was that easy to fix, surely it would have been preferable to defer to the previous release's behaviour as a default rather than just break it.

Custom templates is a similar story - the design of them has changed between 3.0.5 and 3.0.6 meaning that none of my old templates work anymore. There doesn't seem to be a satisfactory answer to this on the phpBB forums either, just a bunch of people saying "well you might need to do 'x' and 'y'". Or maybe just rewrite them from scratch, which seems to sometimes be the open source way of doing things.

I'd prefer those who say a system is "release quality" to actually mean they, like, make sure the upgrade path is smooth and doesn't break stuff that used to work. Or do I expect too much?

1 comment
Monday, 25 January 2010

JavaScript stopwatch

Today I needed a nice stopwatch utility. There wasn't one that quite did what I wanted, so I wrote one based on something similar here.

You can view the source for this here

0 comments
Tuesday, 28 July 2009

PHPBB authentication for DokuWiki

Last month, I wrote a phpBB authentication module for MediaWiki. However, that's not the only wiki software out there, and another one I've come across is DokuWiki. Unlike MediaWiki, it uses file storage by default instead of requiring a database, which might make it an alternative for small, portable stuff.

Anyway, to install it, you'll need to do the following:

  • Download the source Here
  • Unpack this into the directory you've installed DokuWiki
  • Edit your conf/local.php to include the following:
    
    $conf['authtype'] = 'phpbb';
    
    // Change this to where you've installed phpBB
    $conf['phpbb']['path'] = '../forum/'; 
    
    // Important setting - this will disable
    // dokuwiki's UTF8 functions and use phpbb's instead
    define('NO_UTF8',true);
    

    The standard phpBB group names are available for ACL authentication. For an example conf/acl.auth.php...

    
    # Moderators have full access
    *    @GLOBAL_MODERATORS  8
    # Registered users have read access
    *    @REGISTERED         1
    

2 comments
Thursday, 23 July 2009

Archives: