Writing portable, adaptable code using CompLib
There may be no such thing as completely portable code, but with the release of a new Linux version of a component library to developers, writing portable, adaptable code just got easier. More on CompLib, operating system independent programming and OSI

by Ravi Murty, senior network software engineer, Corporate Technology Group, and Ashok Raj, senior staff software engineer, Intel Corp.

Writing code just got easier
There may be no such thing as completely portable code, but with the release of a new Linux version of a component library to developers, writing portable, adaptable code just got easier. A noncommercial product, the Intel® operating system independent (OSI) library implementation called CompLib has been used by Intel developers to port their applications from one platform to another. Now this rigorously tested and effective tool is available to any software engineer wanting access to a critical resource for creating more flexible, robust applications.

With today's large and complex applications, there are strong incentives to reuse and port code to new platforms. A lack of readily available OSI libraries has hindered efforts to generate applications that can easily port from one system to another. But by using CompLib, a developer can do more than write code that is just adapted to new operating systems.

What is CompLib?
CompLib is an operating system independent (OSI) library of functions that abstracts the OS services and performs some porting tasks for you. It includes abstractions for common OS primitives, such as memory allocation, deallocation, spinlocks, semaphores, timers, events, and so on. In addition, CompLib also abstracts compiler directives to hide differences between different compilers and tools. One example of this is the directive that lets the writer pack a structure.

For example, Figure 1 below shows a segment of code that allocates memory and checks the value of the returned pointer. In Figure 1, the code is implemented using #ifdef’s to make it portable across several different OSes. Figure 2 shows the same code written using CompLib. Notice that cl_malloc() is implemented for each OS. It is evident from looking at the code that the code is not only portable and extendible to a new OS—by implementing a flavor of CompLib for that new OS—but it will also be easy to read and maintain.


... 
/* Allocate memory in an OS dependent manner */ 
#if defined(LINUX) 
pMemory = kmalloc(...); 
#endif /* LINUX */ 
#if defined(EFI) 
BS->AllocatePool(..., &pMemory) 
#endif /* EFI */ 
#if defined(VXWORKS) 
... 
#endif /* VXWORKS */ 
if (pMemory == NULL) 
 
... 
 

Figure 1. Code that tries to allocate a block of memory.



... 
pMemory = cl_malloc(...); 
if (pMemory == NULL) 
 
... 

Figure 2. Code written with an OSI library.

With an OSI library such as CompLib, developers can now write most or all of their drivers and applications independent of the operating system. Once they implement CompLib functions in the base code, they can move between platforms fairly quickly.

The Intel OSI reference implementation, CompLib, currently supports four OSes: Windows, Linux, EFI (Extensible Firmware Interface), and VxWorks. The Linux version of CompLib is now available (free) online for developers to use.

Advantages to porting code
Although portable code is not appropriate for all projects, it does have its advantages, including:

    Subscribers who liked this article also read:
    J2EE performance optimization, part 1: laying the foundation
    by Kingsum Chow, PhD, senior performance architect with the Managed ...
    J2EE performance optimization, part 3 - design of experiments for performance tuning
    by Kingsum Chow, Ph.D., senior performance architect, Managed Runtime ...

    If you're interested in this topic, these articles may be helpful:

    Dalmaker XE source-code generation tool
    from TLWallace.NET Dalmaker, a source-code generation toolset, will...
    Source code for 'Network Security with OpenSSL: Cryptography for Secure Communications'
    by John Viega, Matt Messier, and Pravir Chandra. O'Reilly Media Ve...
    Maximum FPS: three tips for faster code
    by Dean Macri, Solutions Enabling Group, Intel Corp. Welcome back t...
    Web Code Optimization: Google does it. Yahoo! does it. Why don't you do it?
    by Tad Fleshman. Port80 Software Inc. Google and Yahoo! know that s...
    Model with the Eclipse Modeling Framework, part 2: generate code with Eclipse's Java Emitter Templates
    by Adrian Powell, senior software developer, IBM Corp. First published...

    Related Jobs:

    Lead Software Development Engineer #147270 - WA - Redmond - Microsoft Corporation
    Crazy about movies and TV? Ga-ga about your digital music? Love to...
    Engineer II/III #CMM134149 - VA - Mclean - Science Applications International Corporation
    NGA Library (NGL) Field Support Engineer will be responsible for opera...
    Software Development Engineer #138815 - WA - Redmond - Microsoft Corporation
    The Media2Go team builds media experiences for Windows Mobile devices....
    Software Development Engineer #143761 - WA - Redmond - Microsoft Corporation
    Just how fast can you make mathematical code run? Multimedia compre...
    Software Development Engineer #149993 - WA - Redmond - Microsoft Corporation
    Crazy about movies and TV? Ga-ga about your digital music? Love to...
    Software Development Engineer in Test #147147 - WA - Redmond - Microsoft Corporation
    How would you like to work on a device that plays high quality music, ...
    Software Development Engineer in Test #131958 - WA - Redmond - Microsoft Corporation
    Interested in assuring quality concerning some of the most exciting, f...
    Software Engineer #261501 - WA - Seattle - Qpass
    This is a software engineering position in a world-class software orga...
    Software Development Engineer #144608 - WA - Redmond - Microsoft Corporation
    The Windows Media Player team is working on the future of music, video...
    Software Development Engineer in Test #146775 - WA - Redmond - Microsoft Corporation
    Do you want to help deliver the next generation of Media on the Smart ...