Carbon is Apple Inc.'s procedural API for the Macintosh operating system, which permits a good degree of forward and backward compatibility between source code written to run on the older and now dated Classic Mac OS (Version 8.1 and later), and the newer Mac OS X. It is one of five APIs natively available for Mac OS X; the others are Cocoa, POSIX, Toolbox (for the obsolete Classic environment), and Java. Carbon is not fully compatible with 64-bit programs under Mac OS 10.5.
The Carbon APIs are published and accessed in the form of C header files and dynamically linkable libraries. In Mac OS X, much functionality is contained in ApplicationServices.framework. In Classic Mac OS, most functions are in a single library called CarbonLib. These different implementations of the APIs are interchangeable from the perspective of the executable. This permits a program that conforms to the Carbon specification to run natively on both operating systems. However, if an application uses a single function not in Carbon, compatibility with Mac OS X requires the Classic environment.
The Carbon APIs were designed to include as many of the older Toolbox APIs as possible, to permit easy porting of most legacy code to Mac OS X. Such porting was known as Carbonization. Carbon also added new functionality and new abstractions. Previously, many data structures of the OS were exposed and manipulated directly by the program. In Carbon, most such structures became fully opaque, and many new APIs were added to access them. This encouraged cleaner and less error-prone code, and made it easier for Apple to modify the operating system. Carbon removed some functions that were specifically attached to the older Mac OS, and removed some obsolete technologies altogether. Backward compatibility remained a focus as long as Mac OS 9 was developed, as later updates such as 9.2.2 were largely to improve support for newer software. However, little Carbon software today remains compatible with Mac OS 9, as the interface has continued to evolve. Carbon was not intended to guarantee backward compatibility. If a programmer desires compatibility with Mac OS 9.1, they must test and debug it with Mac OS 9.1 specifically. Between Mac OS 8.6 and Mac OS 9.2.2, CarbonLib gradually evolved from an adaptation of the QuickTime for Windows user interface API into the basis for much of the later Classic Mac OS development.
Carbon is sometimes seen as a transitional or legacy technology. This is incorrect, and it is misleading to describe it as a technology per se. Carbon is a set of application-level Mac OS X APIs for the C programming language. They are the most efficient alternative when the underlying operating system functionality is also implemented in C. They are also the most versatile, accessible from C, C++, Pascal, Ada, or any other language with suitable interface headers. A higher level approach may be taken with Carbon by using an application framework built on it, for example MacApp, Metrowerks PowerPlant or MacZoop. Many parts of the Cocoa API likewise implement Carbon for Objective-C. Also, many Carbon APIs provide C language access to functionality implemented in Objective-C. In general, it is inefficient for a programmer to be overly concerned with the underlying operating system implementation.
At WWDC 2007, Apple revealed that it will not be possible to compile Carbon apps as 64-bit code for Leopard, contrary to previous statements. Some lower-level parts of Carbon, such as the File Manager, are expected to be available in 64 bit.
Newer parts of Carbon tend to be much more object-oriented in their conception, most of them based on Core Foundation. Some Managers, such as the HIView Manager (a superset of the Control Manager), are implemented in C++, but Carbon remains a C API.
Some examples of Carbon Managers:
With the advent of System 7.0 and the ability to run more than one application simultaneously came a new Event Manager call, WaitNextEvent, which allows an application to specify a sleep interval. One easy trick for legacy code to adopt a more efficient model without major changes to its source code is simply to set the sleep parameter passed to WaitNextEvent to a very large value—on OS X, this puts the thread to sleep whenever there is nothing to do, and only returns an event when there is one to process. In this way, the polling model is quickly inverted to become equivalent to the callback model, with the application performing its own event dispatching in the original manner. There are loopholes, though. For one, the legacy toolbox call ModalDialog, for example, calls the older GetNextEvent function internally, resulting in polling in a tight loop without blocking.
Carbon introduces a replacement system, called the Carbon Event Manager. The original Event Manager still exists for compatibility with legacy applications). Carbon Event Manager provides the event loop for the developer (based on Core Foundation's
CFRunLoop in the current implementation); the developer sets up event handlers and enters the event loop in the main function, and waits for Carbon Event Manager to dispatch events to the application.
In the classic Mac OS, there was no operating system support for application level timers (the lower level Time Manager was available, but executed timer callbacks at interrupt time, during which you could not safely make calls to most Toolbox routines). Timers were usually left to application developers to implement, and this was usually done by counting elapsed time during the idle event - that is, an event that was returned by WaitNextEvent when any other event wasn't available. In order for such timers to have reasonable resolution, developers could not afford WNE to delay too long, and so low "sleep" parameters were usually set. This results in highly inefficient scheduling behaviour, since the thread will not sleep for very long, instead repeatedly waking to return these idle events. Apple added timer support to Carbon to address this problem -- the system can schedule timers with great efficiency.