Supporting Writable Static Data in DLLs

Introduction

Many UNIX programs rely on Writable Static Data (WSD) for data storage. Native Symbian platform applications have been developed without any support for WSD on an Emulator for dynamic link libraries. P.I.P.S. supports WSD and a complete implementation is available for target phones. The implementation of WSD on an Emulator, however, poses certain restrictions for dynamic link libraries.

A Symbian project's MMP file can specify the EPOCALLOWDLLDATA directive to enforce a rule that WSD should not be shared between processes. The P.I.P.S. STDEXEy target type sets this directive automatically. The rule imposes the restriction that only one process can load a DLL with WSD. P.I.P.S. provides a mechanism to avoid these restrictions.

A UNIX DLL with WSD

int refCount = 0;
int IncRefCount()
{
  //Increment the reference count
  return ++refCount;
}

int DecRefCount()
{
  //Decrement the reference count
  return --refCount;
}

A Symbian DLL with WSD

The UNIX code above will provide indeterminate results on a Symbian emulator. If the DLL had EPOCALLOWDLLDATA in its MMP file, then only one process would be able to use the reference counting functions. Without EPOCALLOWDLLDATA, the reference count would be global and not process specific.

The following code demonstrates the use of the Pls() function (Process Local Storage), which is the P.I.P.S. WSD solution to the problem.

#include <pls.h>

#ifdef __WINSCW__
//The next code will run only on the emulator

//Put the global count into a structure
struct DLLData
{
   int refCount;
};

//Define a way to access the structure
//On the first call to this function, memory will be allocated with the specified
//UID as an identifier and the Initialization function will be called
//Subsequent calls to this function return the allocated memory
struct DLLData* GetGlobals()
{
   return Pls<DLLData>(KDLLUid3, InitializeGlobals);
}

//Initialization function
TInt InitializeGlobals(DLLData* aData)
{
   aData->refCount = 0;
   return KErrNone;
}

//Access the refCount variable
int& get_refCount()
{
   return GetClobals()->refCount;
}

//Macro to replace the variable with our new method
#define refCount get_refCount()

#else
//Target device code
int refCount;

#endif

int IncRefCount()
{
  return ++refCount;
}

int DecRefCount()
{
  return --refCount;
}