In my compilation I have two stages: Precompilation and Compilation. In precompilation, everything is done in a non-write test mode and the script's length is retrieved. Then I search for X amount of successing FF's (or 00's for Emerald, not sure if I implemented that). The program goes as follows:
* Scan with while loop from start address for 0xFF
* Once FF is found, loop for X amount of bytes
* If number of bytes is free, enable boolean to true to end upper while loop and then break, retrieving freespace location.
* If not, break and continue while loop. Rinse and repeat.
It's fairly simple, but may or may not be prone to bugs. If you want a good example of freespace finding you could also take a look at the FSF source, since it's available on Google Code.