]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java
Support putting unreference DYNAMIC_EX PCD into Pcd runtime database.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / entity / MemoryDatabaseManager.java
CommitLineData
878ddf1f 1/** @file\r
2 MemoryDatabaseManager class.\r
3\r
4 Database hold all PCD information comes from SPD, MSA, FPD file in memory.\r
5 \r
6Copyright (c) 2006, Intel Corporation\r
7All rights reserved. This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11 \r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16package org.tianocore.build.pcd.entity;\r
17\r
878ddf1f 18import java.util.ArrayList;\r
19import java.util.HashMap;\r
20import java.util.List;\r
21import java.util.Map;\r
8840ad58 22import java.util.UUID;\r
878ddf1f 23\r
58f1099f 24import org.tianocore.build.pcd.exception.EntityException;\r
878ddf1f 25\r
26/** Database hold all PCD information comes from SPD, MSA, FPD file in memory.\r
27**/\r
28public class MemoryDatabaseManager {\r
29 ///\r
30 /// Memory database. The string "cName + SpaceNameGuid" is primary key.\r
31 /// memory database is in global scope, and it will be used for others PCD tools.\r
32 ///\r
8840ad58 33 private static Map<String, Token> memoryDatabase = null;\r
34\r
878ddf1f 35 ///\r
8840ad58 36 /// Before build a module, the used libary will be build firstly, the PCD of these\r
37 /// libarry is inheritted by the module, so stored module's PCD information as PCD\r
38 /// context of building libary.\r
39 /// \r
40 public static List<UsageInstance> UsageInstanceContext = null;\r
878ddf1f 41 ///\r
8840ad58 42 /// \r
43 /// \r
44 public static String CurrentModuleName = null;\r
99d2c3c4 45 public static String PcdPeimHString = "";\r
32648c62 46 public static String PcdPeimCString = "";\r
47 public static String PcdDxeHString = "";\r
48 public static String PcdDxeCString = "";\r
99d2c3c4 49\r
878ddf1f 50 /**\r
51 Constructure function\r
52 **/\r
53 public MemoryDatabaseManager() {\r
54 //\r
55 // Allocate memory for new database in global scope.\r
56 //\r
57 if (memoryDatabase == null) {\r
58 memoryDatabase = new HashMap<String, Token>();\r
59 }\r
60 }\r
61\r
878ddf1f 62 /**\r
63 Judege whether token exists in memory database\r
64 \r
65 @param primaryKey the primaryKey for searching token\r
66 \r
67 @retval TRUE - token already exist in database.\r
68 @retval FALSE - token does not exist in database.\r
69 **/\r
70 public boolean isTokenInDatabase(String primaryKey) {\r
71 return (memoryDatabase.get(primaryKey) != null);\r
72 }\r
73\r
74 /**\r
75 Add a pcd token into memory database.\r
76 \r
77 @param primaryKey the primary key for searching token\r
78 @param token token instance\r
79 **/\r
80 public void addTokenToDatabase(String primaryKey, Token token) {\r
81 memoryDatabase.put(primaryKey, token);\r
82 }\r
83\r
84 /**\r
85 Get a token instance from memory database with primary key.\r
86 \r
87 @param primaryKey the primary key for searching token\r
88 \r
89 @return token instance.\r
90 **/\r
91 public Token getTokenByKey(String primaryKey) {\r
92 return memoryDatabase.get(primaryKey);\r
93 }\r
94\r
95 /**\r
96 Get the number of PCD token record in memory database.\r
97 \r
98 @return the number of PCD token record in memory database.\r
99 **/\r
100 public int getDBSize() {\r
101 return memoryDatabase.size();\r
102 }\r
103\r
104 /**\r
105 Get the token record array contained all PCD token in memory database.\r
106 \r
107 @return the token record array contained all PCD token in memory database.\r
108 **/\r
109 public Token[] getRecordArray() {\r
110 Token[] tokenArray = null;\r
111 Object[] dataArray = null;\r
112 Map.Entry entry = null;\r
113 int index = 0;\r
114\r
115 if (memoryDatabase == null) {\r
116 return null;\r
117 }\r
118\r
119 dataArray = memoryDatabase.entrySet().toArray();\r
120 tokenArray = new Token[memoryDatabase.size()];\r
121 for (index = 0; index < memoryDatabase.size(); index ++) {\r
122 entry =(Map.Entry) dataArray [index];\r
123 tokenArray[index] =(Token) entry.getValue();\r
124 }\r
125\r
126 return tokenArray;\r
127 }\r
128\r
8840ad58 129 /**\r
130 Get record array only contains DYNAMIC or DYNAMIC_EX type PCD.\r
131 \r
132 @return ArrayList\r
133 */\r
134 private ArrayList getDynamicRecordArray() {\r
99d2c3c4 135 Token[] tokenArray = getRecordArray();\r
136 int index = 0;\r
6a4cae58 137 ArrayList<Token> al = new ArrayList<Token>();\r
99d2c3c4 138\r
139 for (index = 0; index < tokenArray.length; index++) {\r
6ff7a41c 140 if (tokenArray[index].isDynamicPCD) {\r
99d2c3c4 141 al.add(tokenArray[index]);\r
142 }\r
143 }\r
144\r
145 return al;\r
146 }\r
147\r
148\r
878ddf1f 149 /**\r
99d2c3c4 150 Get the token record array contained all PCD token referenced by PEI phase.\r
151 The output array is sorted based on descending order of the size of alignment for each feilds.\r
152\r
153 @return the token record array contained all PCD token referenced in PEI phase.\r
58f1099f 154 * @throws EntityException \r
99d2c3c4 155 **/\r
58f1099f 156 public void getTwoPhaseDynamicRecordArray(ArrayList<Token> pei, ArrayList<Token> dxe) \r
157 throws EntityException {\r
99d2c3c4 158 int usageInstanceIndex = 0;\r
159 int index = 0;\r
8840ad58 160 ArrayList tokenArrayList = getDynamicRecordArray();\r
161 Object[] usageInstanceArray = null;\r
99d2c3c4 162 UsageInstance usageInstance = null;\r
163\r
8840ad58 164 //pei = new ArrayList<Token>();\r
165 //dxe = new ArrayList<Token>();\r
166\r
99d2c3c4 167 for (index = 0; index < tokenArrayList.size(); index++) {\r
168 boolean found = false;\r
169 Token token = (Token) tokenArrayList.get(index);\r
8840ad58 170 if (token.consumers != null) {\r
171 usageInstanceArray = token.consumers.entrySet().toArray();\r
172 for (usageInstanceIndex = 0; usageInstanceIndex < token.consumers.size(); usageInstanceIndex ++) {\r
173 usageInstance =(UsageInstance) (((Map.Entry)usageInstanceArray[usageInstanceIndex]).getValue());\r
174 if (usageInstance.isPeiPhaseComponent()) {\r
99d2c3c4 175 pei.add(token);\r
176 found = true;\r
177 break;\r
178 }\r
179 }\r
99d2c3c4 180 }\r
181\r
58f1099f 182 //\r
183 // If no PEI components reference the PCD entry, \r
184 // we check if it is referenced in DXE driver. \r
32648c62 185 //\r
186 if (!found) {\r
58f1099f 187 if (token.consumers != null) {\r
188 usageInstanceArray = token.consumers.entrySet().toArray();\r
189 for (usageInstanceIndex = 0; usageInstanceIndex < token.consumers.size(); usageInstanceIndex ++) {\r
190 usageInstance =(UsageInstance) (((Map.Entry)usageInstanceArray[usageInstanceIndex]).getValue());\r
191 if (usageInstance.isDxePhaseComponent()) {\r
192 dxe.add(token);\r
193 found = true;\r
194 break;\r
195 }\r
196 }\r
197 }\r
198 \r
199 if (!found) {\r
8d82d611 200 if (token.isDynamicPCD && token.consumers.size() == 0) {\r
201 dxe.add(token);\r
202 } else {\r
203 //\r
204 // We only support Dynamice(EX) type for PEI and DXE phase.\r
205 // If it is not referenced in either PEI or DXE, throw exception now.\r
206 //\r
207 throw new EntityException("Dynamic(EX) PCD Entries are referenced in module that is not in PEI phase nor in DXE phase.");\r
208 }\r
58f1099f 209 }\r
32648c62 210 }\r
99d2c3c4 211 }\r
212\r
32648c62 213 return;\r
99d2c3c4 214 }\r
215\r
32648c62 216 /**\r
8840ad58 217 Get all PCD record for a module according to module's name, module's GUID,\r
218 package name, package GUID, arch, version information.\r
878ddf1f 219 \r
220 @param moduleName the name of module.\r
221 \r
222 @return all usage instance for this module in memory database.\r
223 **/\r
8840ad58 224 public List<UsageInstance> getUsageInstanceArrayByModuleName(String moduleName,\r
225 UUID moduleGuid,\r
226 String packageName,\r
227 UUID packageGuid,\r
228 String arch,\r
229 String version) {\r
230\r
231 String primaryKey = UsageInstance.getPrimaryKey(moduleName, \r
232 moduleGuid,\r
233 packageName,\r
234 packageGuid,\r
235 arch,\r
236 version);\r
237\r
238 return getUsageInstanceArrayByKeyString(primaryKey);\r
239 }\r
240\r
241 /**\r
242 Get all PCD token for a usage instance according to primary key.\r
243 \r
244 @param primaryKey the primary key of usage instance.\r
245 \r
246 @return List<UsageInstance>\r
247 */\r
248 public List<UsageInstance> getUsageInstanceArrayByKeyString(String primaryKey) {\r
878ddf1f 249 Token[] tokenArray = null;\r
250 int recordIndex = 0; \r
878ddf1f 251 UsageInstance usageInstance = null;\r
252 List<UsageInstance> returnArray = new ArrayList<UsageInstance>();\r
253\r
254 tokenArray = getRecordArray();\r
255\r
256 //\r
257 // Loop to find all PCD record related to current module\r
258 //\r
259 for (recordIndex = 0; recordIndex < getDBSize(); recordIndex ++) {\r
8840ad58 260 if (tokenArray[recordIndex].consumers.size() != 0) {\r
261 usageInstance = tokenArray[recordIndex].consumers.get(primaryKey);\r
262 if (usageInstance != null) {\r
263 returnArray.add(usageInstance);\r
878ddf1f 264 }\r
265 }\r
266 }\r
267\r
878ddf1f 268 return returnArray;\r
269 }\r
270\r
271 /**\r
272 Get all modules name who contains PCD information\r
273 \r
274 @return Array for module name\r
275 **/\r
276 public List<String> getAllModuleArray()\r
277 {\r
278 int indexToken = 0;\r
279 int usageIndex = 0;\r
280 int moduleIndex = 0;\r
281 Token[] tokenArray = null;\r
8840ad58 282 Object[] usageInstanceArray = null;\r
878ddf1f 283 List<String> moduleNames = new ArrayList<String>();\r
284 UsageInstance usageInstance = null;\r
285 boolean bFound = false;\r
286\r
8840ad58 287 tokenArray = getRecordArray();\r
878ddf1f 288 //\r
289 // Find all consumer usage instance for retrieving module's name\r
290 //\r
291 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {\r
8840ad58 292 usageInstanceArray = tokenArray[indexToken].consumers.entrySet().toArray();\r
878ddf1f 293 for (usageIndex = 0; usageIndex < tokenArray[indexToken].consumers.size(); usageIndex ++) {\r
8840ad58 294 usageInstance = (UsageInstance)((Map.Entry)usageInstanceArray[usageIndex]).getValue();\r
878ddf1f 295 bFound = false;\r
296 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {\r
8840ad58 297 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.getPrimaryKey())) {\r
878ddf1f 298 bFound = true;\r
299 break;\r
300 }\r
301 }\r
302 if (!bFound) {\r
8840ad58 303 moduleNames.add(usageInstance.getPrimaryKey());\r
878ddf1f 304 }\r
305 }\r
306 }\r
307 return moduleNames;\r
308 }\r
878ddf1f 309}\r