вторник, 24 января 2012 г.

CleanCF: CoreFoundation refactored


As I mentioned earlier, Itoa uses Apple's CoreFoundation to implement corresponding Foundation classes.

However, I found CoreFoundation library hard to work with. It was big and messy, and contained things I didn't need. So I decided to refactor it.

Welcome CleanCF, a refactored CoreFoundation!


CleanCF is based on version 417-17 of CoreFoundation. This is the version that was used in iOS 2.0, which is Itoa's current target. I also used couple of patches from OpenCFLite project.

All my changes fall into several categories:
  1. Cleanup and formatting
    1. All deployment targets were dropped (DEPLOYMENT_TARGET_XXX macros). There is single POSIX deployment target and CFPlatform abstraction. Windows version can be built using a tiny emulation layer (to be released).
    2. GC support dropped. Itoa doesn't support GC and won't support it in the near future (ARC support is far more likely).
    3. All unused code dropped. That include things like CFUniCharPrecompose that are not used by CoreFoundation itself and are not exposed in public API.
    4. Core reformatted according to Coding Style (almost done).
  2. Enhancements
    1. Reference counter moved separate field of CFRuntimeBase (as Apple do in LP64). That simplified code but also introduced incompatibilities.
    2. OSAtomics (see itoa-macemu) are used to perform atomic operations.
    3. CFAllocator is bridged to malloc_zone_t (see itoa-macemu for minimal implementation). I.e. allocators can be passed to zone functions and vise versa.
    4. CFRunLoop refactored (there will be specific post on that).
    5. CFSet/CFBag/CFDictionary are implemented using single file, THash.inl (I have thoughts to revert that).
  3. Foundation-specific changes
    1. Bridging enhanced: every CF class can be bridged to 2 NS classes: mutable and immutable.
    2. Validation mechanism was redone to support callbacks. That allows throwing NS exceptions directly from CoreFoundation, getting rid of validation in NS classes (and poses some leak risks and change in behavior - more on that in next posts).
As you can see CleanCF became incompatible with CoreFoundation. That was the price. On the other side, changes in CoreFoundation are incremental and can pretty easily be ported.

I must note that CleanCF is not finished -- there are many things to do. To name a few:
  • Android integration. CFBundle is the first candidate, it should work with resources in Android .apk packages.
  • Then there is ICU dependency. Currently on Android Itoa relies on a fact that icu is always loaded, but this is wrong, so wrong. One way to solve this is to implement all affected classes around Java's counterparts (e.g. CFDateFormatter on java.text.DateFormat).
  • CFRunLoop needs finishing. CFRunLoopPort implementation from itoa-main should be integrated in CF and simple callback for run loop integration should be provided.
  • Tests. There must be tests. Tests will allow for even deeper refactoring -- for example to extract code for reading unichar surrogates from string buffer, which is now copy-pasted in dozen of places.

Next posts will cover some of the issues above.

Комментариев нет:

Отправить комментарий