|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept id="GUID-E7C41361-C0B9-5341-A864-B59770FB7C9B" xml:lang="en"><title>Supporting |
|
13 Writable Static Data in DLLs </title><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
14 <section><title>Introduction</title> <p>Many UNIX programs rely |
|
15 on Writable Static Data (WSD) for data storage. Native Symbian platform applications |
|
16 have been developed without any support for WSD on an Emulator for dynamic |
|
17 link libraries. P.I.P.S. supports WSD and a complete implementation is available |
|
18 for target phones. The implementation of WSD on an Emulator, however, poses |
|
19 certain restrictions for dynamic link libraries. </p><p>A Symbian project's |
|
20 MMP file can specify the <codeph>EPOCALLOWDLLDATA</codeph> directive to enforce |
|
21 a rule that WSD should not be shared between processes. The P.I.P.S. <codeph>STDEXE</codeph>y |
|
22 target type sets this directive automatically. The rule imposes the restriction |
|
23 that only one process can load a DLL with WSD. P.I.P.S. provides a mechanism |
|
24 to avoid these restrictions. </p> </section> |
|
25 <section><title>A UNIX DLL with WSD</title><codeblock xml:space="preserve">int refCount = 0; |
|
26 int IncRefCount() |
|
27 { |
|
28 //Increment the reference count |
|
29 return ++refCount; |
|
30 } |
|
31 |
|
32 int DecRefCount() |
|
33 { |
|
34 //Decrement the reference count |
|
35 return --refCount; |
|
36 } |
|
37 </codeblock><p/></section> |
|
38 <section><title>A Symbian DLL with WSD</title><p>The UNIX code above will |
|
39 provide indeterminate results on a Symbian emulator. If the DLL had <codeph>EPOCALLOWDLLDATA</codeph> in |
|
40 its MMP file, then only one process would be able to use the reference counting |
|
41 functions. Without <codeph>EPOCALLOWDLLDATA</codeph>, the reference count |
|
42 would be global and not process specific. </p><p>The following code demonstrates |
|
43 the use of the <xref href="GUID-807C676B-5C46-328C-879C-A4FCB638A5B6.dita"><apiname>Pls()</apiname></xref> function (Process Local Storage), |
|
44 which is the P.I.P.S. WSD solution to the problem. </p><codeblock xml:space="preserve">#include <pls.h> |
|
45 |
|
46 #ifdef __WINSCW__ |
|
47 //The next code will run only on the emulator |
|
48 |
|
49 //Put the global count into a structure |
|
50 struct DLLData |
|
51 { |
|
52 int refCount; |
|
53 }; |
|
54 |
|
55 //Define a way to access the structure |
|
56 //On the first call to this function, memory will be allocated with the specified |
|
57 //UID as an identifier and the Initialization function will be called |
|
58 //Subsequent calls to this function return the allocated memory |
|
59 struct DLLData* GetGlobals() |
|
60 { |
|
61 return Pls<DLLData>(KDLLUid3, InitializeGlobals); |
|
62 } |
|
63 |
|
64 //Initialization function |
|
65 TInt InitializeGlobals(DLLData* aData) |
|
66 { |
|
67 aData->refCount = 0; |
|
68 return KErrNone; |
|
69 } |
|
70 |
|
71 //Access the refCount variable |
|
72 int& get_refCount() |
|
73 { |
|
74 return GetClobals()->refCount; |
|
75 } |
|
76 |
|
77 //Macro to replace the variable with our new method |
|
78 #define refCount get_refCount() |
|
79 |
|
80 #else |
|
81 //Target device code |
|
82 int refCount; |
|
83 |
|
84 #endif |
|
85 |
|
86 int IncRefCount() |
|
87 { |
|
88 return ++refCount; |
|
89 } |
|
90 |
|
91 int DecRefCount() |
|
92 { |
|
93 return --refCount; |
|
94 } |
|
95 </codeblock></section> |
|
96 </conbody></concept> |