Using Legacy C APIs with Swift

Swift’s type system is designed to make our lives easier by enforcing strict rules around what we can and cannot do in our code. This is undoubtedly a great thing and it encourages programmers to write better, more correct code. However, it can seem extremely prohibitive when interacting with legacy code bases, particularly C-based libraries. It is a reality that many C libraries abuse types in such a way that does not play nicely with the Swift compiler. It’s true that the Swift team at Apple has gone out of their way to support some of the more basic features of C, such as C strings, but there are still many issues when working with a legacy C library in Swift. Here’s how to deal with them.

Before we begin, I would like to note that many of the operations in this article are inherently unsafe as they bypass the Swift compiler’s type system altogether. I encourage you to read carefully and refrain from copy and pasting code from this article. This is not Stack Overflow, these snippets have a real chance of corrupting memory, causing memory leaks, or simply crashing your application if used improperly.

The Basics

Most of the time, C pointers are imported into Swift in one of two ways:

UnsafePointer<T>

or

UnsafeMutablePointer<T>

Where T is the equivalent Swift type for the original C type. Pointers declared const in C are imported as UnsafePointer and pointers that are not declared const are imported as UnsafeMutablePointer.

When using the inout operator, variables declared with var and constants declared with let are transformed into UnsafePoiner and UnsafeMutablePointer respectively. This is very easy to miss if you do not pay close attention to the original type in the code. Trying to pass an UnsafePointer where an UnsafeMutablePointer is expected results in a seemingly cryptic compiler error so be wary.

This operator only works in the context of passing Swift values and references as function arguments that expect an UnsafePointer or UnsafeMutablePointer. You cannot get the pointer in any other context. For example, this is invalid and will result in a compiler error:

Swift:
let x = 42
let y = &x

From time to time, you will need to interoperate with an API that takes or returns a void pointer in place of an explicit type. This is unfortunately common in C, where there isn’t a way to specify a generic type.

C:
void takesAnObject(void *theObject);

If you know the expected type taken by the function, you can coerce an object into a void pointer using withUnsafePointer and unsafeBitCast. For example, let’s say takesAnObject is actually expecting a pointer to an int.

Let’s break this down. First, we make a call to withUnsafeMutablePointer. This generic function takes two arguments.

The first is an inout of type T, and the second is a closure of type (UnsafePointer) -> ResultType. This function calls the closure by taking a pointer to the first argument of the function, and passing it as the sole argument of the closure. The function then returns the result of the closure. In the example above, the closure is typed to return Void, and therefore doesn’t return anything. We could just as easily do something like this:

About unsafeBitCast

unsafeBitCast is an extremely dangerous operation. The documentation describes it as a “brutal bit-cast of something to anything of the same size.” The reason we are able to use it safely above is because we’re simply casting between pointers of different types, and all pointers are the same size on any given platform. This is why we must call withUnsafePointer to obtain a typed UnsafePointerfirst before casting that to an UnsafePointer as the C API is defined.

This can be confusing at first, especially when working with a type that is the same size as a pointer, such as an Int in Swift (on all currently available platforms anyway, where the size of a pointer is 1 Word, and 1 Word is the size of an Int).

It is easy to make a mistake like this:

var x: Int = 7
let xPtr = unsafeBitCast(x, UnsafePointer<Void>.self)

With the intention of obtaining a pointer to x. This is incredibly misleading because it will compile and run, but lead to unexpected errors because instead of a pointer to x, the C APIs will receive a pointer with location 0x7, or garbage.

Because unsafeBitCast requires that the size of the types be equal, it is less insidious when attempting to cast something other than an Int, such as Int8, or a one byte integer.

var x: Int8 = 7
let xPtr = unsafeBitCast(x, UnsafePointer<Void>.self)

This will simply cause unsafeBitCast to throw an exception and crash your program!

Interacting With C Structs

Let’s tackle this one with a concrete example. You want to retrieve information about the system your computer is running on. There’s a C API for that, uname(2), which takes a pointer to a structure and fills out the information in the supplied object with the systems information, such as OS name and version or its hardware identifier. There’s a catch though, the struct is imported into Swift as:

Oh no! Swift imports C array literals as tuples! On top of that, the default initializer requires values for each and every field. So if you were to do this the normal Swift way, it would be like this:

That’s not a good idea! But there’s another problem. The machine field of utsname is a tuple so that println is going to print out 256 Int8’s, with only the first few representing the ASCII values of the characters in the string we actually want.

So, how can we fix this?

Swift’s UnsafeMutablePointer supplies two methods, alloc(Int) and dealloc(Int), for manually allocating and deallocating respectively. The argument the amount of T’s to allocate or deallocate. We can use these APIs to simplify our code.

The first step is making our call to withUnsafePointer, passing it the machine tuple and telling it that our closure is going to return an optional String.

Inside the closure we take the provided pointer and cast it to UnsafePointer, a mostly equivalent representation of the same value. Except that Swift’s String has a class method for initializing from UnsafePointer where CChar is a typealias for Int8! So we can pass our new pointer to the initializer and return the value.

After capturing the result of withUnsafePointer (which, remember, forwards the return value of the supplied closure) we can test to see if we obtained a value using a conditional-let statement, and print the result. For me, this yields “x86_64” as expected.

Conclusion

A bit of a disclaimer. Using unsafe APIs in Swift should be a last resort because, they’re inherently not safe! As we transition from legacy C and Objective-C code to Swift, it’s likely that we’ll continue to need these APIs to be compatible with our existing tools. However, one should always be skeptical when their first resort is to break out withUnsafePointer and unsafeBitCast.

New code should strive to be as idiomatic as possible, which explicitly prohibits the use of Swift’s unsafe APIs. As software developers, it’s just as important to know how to use your tools as it is to know when and where not to use them. Swift brings with it the benefit of modernizing a large subset of software development and we must respect its ideologies in order for it to really make an impact.