🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Marshalling structures out to APIs

Published August 31, 2008
Advertisement
Structures are basically finished, aside from one critical piece: it is not yet possible to pass a structure to an external API. Since this is one of the main goals of structures in the first place (for Win32 support) I obviously have a ways to go before R4 is ready.

My first approach to passing structures was to just pass a pointer to the raw Epoch memory block where the structure was held. This works great when all members of the structure are integers or floats. Problems arise when it comes to handling strings.

In Epoch, a string variable holds a 32-bit handle, which is used to look up the actual string text in a centralized pool when necessary. This is done to facilitate easy garbage collection as well as make it trivial to pass strings around on the stack.


A problem arises then when we want to store a string in a structure. Suppose we have the following simple structure in Epoch:

structure demo : (string(foo), integer(bar))


In memory, this structure uses 64 bits: 32 for the string handle, and 32 for the integer member. The actual string data lives someplace else.

Now, Win32 expects string members to come in the form of LPCTSTR (usually), which is a pointer to character data. Suppose our string has the handle 0x00000001; if we pass this structure to a Win32 API, it will try to find the corresponding string at address 0x00000001.

Boom! Instant crash.


This means we need to add a marshalling layer which converts an Epoch structure in memory into a structure that external APIs can understand. Once a structure is converted, using a temporary buffer, the pointer to that buffer can be passed to the API.

By way of summary, we basically have to dereference our string handles into pointers that external APIs can understand.


This marshalling isn't exactly complicated, but I also wasn't planning on having to do it tonight. So chances are it will have to wait until later this week, work-schedule permitting. Unfortunately, then, R4 is pushed back a bit [sad]



So there's your daily Epoch, for all you RSS-addicts out there who just can't stop pressing F5 and waiting for news.


Update - 0124 hours
I got bit by the insomnia/must-finish-project bug and decided to go ahead and implement marshalling tonight. It'll cost me a bit of coherency at work tomorrow, but such is life [grin]

Structures are now safely converted into API-friendly form before being passed on to external functions. Of course, for the moment they aren't marshalled back into Epoch form, but that's OK - it just means that any API that writes to a struct is useless.

Considering that any API that relies on a callback is also useless, this isn't such a big deal, because the vast majority of APIs out there are therefore currently incompatible with Epoch [wink]


The important thing is that the groundwork is laid, and getting the rest of the stuff implemented on top of it will be easy enough.

So... with all that hackery done, look for R4 sometime tomorrow, probably later on in the day/early evening. I still have to make sure all the loose ends are tied up, but aside from that, it should be ready to go.
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement