|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #ifndef MADDRCONT_H |
|
17 #define MADDRCONT_H |
|
18 |
|
19 /** |
|
20 A container for storing objects keyed on a virtual address. |
|
21 */ |
|
22 class RAddressedContainer |
|
23 { |
|
24 public: |
|
25 /** |
|
26 @param aReadLock Fast mutex used to synchronise read operations, |
|
27 i.e. the Find functions. This may be the null pointer to |
|
28 indicate that extra locking is required. |
|
29 @param aWriteLock Reference to the mutex used to synchronise write operations, |
|
30 i.e. #Add and Remove functions. This mutex if not used by |
|
31 the RAddressedContainer class but it is used for asserting |
|
32 correct preconditions in debug builds |
|
33 */ |
|
34 RAddressedContainer(NFastMutex* aReadLock, DMutex*& aWriteLock); |
|
35 |
|
36 ~RAddressedContainer(); |
|
37 |
|
38 /** |
|
39 Add an object to the container. |
|
40 |
|
41 @param aAddress The address key for the object. |
|
42 @param aObject Pointer to the object to add. This may not be the null pointer. |
|
43 |
|
44 @pre The write lock must be held. |
|
45 */ |
|
46 TInt Add(TLinAddr aAddress, TAny* aObject); |
|
47 |
|
48 /** |
|
49 Remove an object from the container. |
|
50 |
|
51 @param aAddress The address key for the object. |
|
52 |
|
53 @return The pointer of the object removed, or the null pointer if there |
|
54 was none with the specified address key. |
|
55 |
|
56 @pre The write lock must be held. |
|
57 */ |
|
58 TAny* Remove(TLinAddr aAddress); |
|
59 |
|
60 /** |
|
61 Find an object in the container. |
|
62 |
|
63 @param aAddress The address key for the object. |
|
64 |
|
65 @return The pointer of the object found, or the null pointer if there |
|
66 was none with the specified address key. |
|
67 |
|
68 @pre The read lock must be held, or if there is no read lock, the write lock must be held. |
|
69 */ |
|
70 TAny* Find(TLinAddr aAddress); |
|
71 |
|
72 /** |
|
73 Find the object in the container which has the highest value |
|
74 address key less-than-or-equal to the specified address. |
|
75 |
|
76 @param aAddress The address key to search on. |
|
77 |
|
78 @param[out] aOffset Reference to an value which will be set to the difference |
|
79 between \a aAddress and the address key of the found object. |
|
80 |
|
81 @return The pointer of the object found, or the null pointer if there |
|
82 was none matching the search criteria. |
|
83 |
|
84 @pre The read lock must be held, or if there is no read lock, the write lock must be held. |
|
85 */ |
|
86 TAny* Find(TLinAddr aAddress, TUint& aOffset); |
|
87 |
|
88 /** Acquire the read lock. */ |
|
89 FORCE_INLINE void ReadLock() |
|
90 { |
|
91 if(iReadLock) |
|
92 NKern::FMWait(iReadLock); |
|
93 } |
|
94 |
|
95 /** Release the read lock. */ |
|
96 FORCE_INLINE void ReadUnlock() |
|
97 { |
|
98 if(iReadLock) |
|
99 NKern::FMSignal(iReadLock); |
|
100 } |
|
101 |
|
102 /** Flash (release and re-acquire) the read lock. */ |
|
103 FORCE_INLINE void ReadFlash() |
|
104 { |
|
105 if(iReadLock) |
|
106 NKern::FMFlash(iReadLock); |
|
107 } |
|
108 |
|
109 /** The number of objects in the container. */ |
|
110 FORCE_INLINE TUint Count() |
|
111 { |
|
112 return iCount; |
|
113 } |
|
114 private: |
|
115 TUint FindIndex(TLinAddr aAddress); |
|
116 TUint CalculateGrow(); |
|
117 TUint CalculateShrink(TUint aCount); |
|
118 TBool CheckWriteLock(); |
|
119 |
|
120 class TEntry |
|
121 { |
|
122 public: |
|
123 TLinAddr iAddress; |
|
124 TAny* iObject; |
|
125 }; |
|
126 private: |
|
127 TUint iMaxCount; |
|
128 TUint iCount; |
|
129 TEntry* iList; |
|
130 NFastMutex* iReadLock; |
|
131 DMutex*& iWriteLock; |
|
132 }; |
|
133 |
|
134 #endif // MADDRCONT_H |