]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@250 6f19259b...
[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
18import java.io.BufferedWriter;\r
19import java.io.File;\r
20import java.io.FileWriter;\r
21import java.io.IOException;\r
22import java.util.ArrayList;\r
99d2c3c4 23import java.util.Collections;\r
24import java.util.Comparator;\r
878ddf1f 25import java.util.HashMap;\r
99d2c3c4 26import java.util.Hashtable;\r
878ddf1f 27import java.util.List;\r
28import java.util.Map;\r
29\r
30import org.tianocore.build.autogen.CommonDefinition;\r
31import org.tianocore.build.pcd.action.ActionMessage;\r
32\r
99d2c3c4 33class AlignmentSizeComp implements Comparator {\r
34 public int compare (Object a, Object b) {\r
35 Token tA = (Token) a;\r
36 Token tB = (Token) b;\r
37\r
38 return Token.getAutogendatumTypeAlignmentSize(tA.datumType) \r
39 - Token.getAutogendatumTypeAlignmentSize(tB.datumType);\r
40 }\r
41}\r
42\r
878ddf1f 43/** Database hold all PCD information comes from SPD, MSA, FPD file in memory.\r
44**/\r
45public class MemoryDatabaseManager {\r
46 ///\r
47 /// Memory database. The string "cName + SpaceNameGuid" is primary key.\r
48 /// memory database is in global scope, and it will be used for others PCD tools.\r
49 ///\r
50 private static Map<String, Token> memoryDatabase = null;\r
51 ///\r
52 /// The log file name for dumping memory database.\r
53 ///\r
54 private static String logFileName = null;\r
55\r
99d2c3c4 56 public static String PcdPeimHString = "";\r
57 public static String PcdPeimCString = "";\r
58 public static String PcdDxeHString = "";\r
59 public static String PcdDxeCString = "";\r
60\r
878ddf1f 61 /**\r
62 Constructure function\r
63 **/\r
64 public MemoryDatabaseManager() {\r
65 //\r
66 // Allocate memory for new database in global scope.\r
67 //\r
68 if (memoryDatabase == null) {\r
69 memoryDatabase = new HashMap<String, Token>();\r
70 }\r
71 }\r
72\r
73 /**\r
74 Get the log file name.\r
75 **/\r
76 public String getLogFileName() {\r
77 return logFileName;\r
78 }\r
79\r
80 /**\r
81 Set parameter log file name.\r
82 \r
83 @param fileName log file name parameter.\r
84 **/\r
85 public void setLogFileName(String fileName) {\r
86 logFileName = fileName;\r
87 }\r
88\r
89 /**\r
90 Judege whether token exists in memory database\r
91 \r
92 @param primaryKey the primaryKey for searching token\r
93 \r
94 @retval TRUE - token already exist in database.\r
95 @retval FALSE - token does not exist in database.\r
96 **/\r
97 public boolean isTokenInDatabase(String primaryKey) {\r
98 return (memoryDatabase.get(primaryKey) != null);\r
99 }\r
100\r
101 /**\r
102 Add a pcd token into memory database.\r
103 \r
104 @param primaryKey the primary key for searching token\r
105 @param token token instance\r
106 **/\r
107 public void addTokenToDatabase(String primaryKey, Token token) {\r
108 memoryDatabase.put(primaryKey, token);\r
109 }\r
110\r
111 /**\r
112 Get a token instance from memory database with primary key.\r
113 \r
114 @param primaryKey the primary key for searching token\r
115 \r
116 @return token instance.\r
117 **/\r
118 public Token getTokenByKey(String primaryKey) {\r
119 return memoryDatabase.get(primaryKey);\r
120 }\r
121\r
122 /**\r
123 Get the number of PCD token record in memory database.\r
124 \r
125 @return the number of PCD token record in memory database.\r
126 **/\r
127 public int getDBSize() {\r
128 return memoryDatabase.size();\r
129 }\r
130\r
131 /**\r
132 Get the token record array contained all PCD token in memory database.\r
133 \r
134 @return the token record array contained all PCD token in memory database.\r
135 **/\r
136 public Token[] getRecordArray() {\r
137 Token[] tokenArray = null;\r
138 Object[] dataArray = null;\r
139 Map.Entry entry = null;\r
140 int index = 0;\r
141\r
142 if (memoryDatabase == null) {\r
143 return null;\r
144 }\r
145\r
146 dataArray = memoryDatabase.entrySet().toArray();\r
147 tokenArray = new Token[memoryDatabase.size()];\r
148 for (index = 0; index < memoryDatabase.size(); index ++) {\r
149 entry =(Map.Entry) dataArray [index];\r
150 tokenArray[index] =(Token) entry.getValue();\r
151 }\r
152\r
153 return tokenArray;\r
154 }\r
155\r
99d2c3c4 156\r
157 private ArrayList getDynamicRecordArray() {\r
158 Token[] tokenArray = getRecordArray();\r
159 int index = 0;\r
160 int count = 0;\r
161 ArrayList al = new ArrayList();\r
162\r
163 for (index = 0; index < tokenArray.length; index++) {\r
164 if (tokenArray[index].pcdType == Token.PCD_TYPE.DYNAMIC ||\r
165 tokenArray[index].pcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
166 al.add(tokenArray[index]);\r
167 }\r
168 }\r
169\r
170 return al;\r
171 }\r
172\r
173\r
878ddf1f 174 /**\r
99d2c3c4 175 Get the token record array contained all PCD token referenced by PEI phase.\r
176 The output array is sorted based on descending order of the size of alignment for each feilds.\r
177\r
178 @return the token record array contained all PCD token referenced in PEI phase.\r
179 **/\r
180 public void getTwoPhaseDynamicRecordArray(ArrayList<Token> pei, ArrayList<Token> dxe) {\r
181 int usageInstanceIndex = 0;\r
182 int index = 0;\r
183 ArrayList tokenArrayList = getDynamicRecordArray();\r
184 List<UsageInstance> usageInstanceArray = null;\r
185 UsageInstance usageInstance = null;\r
186\r
187 //pei = new ArrayList<Token>();\r
188 //dxe = new ArrayList<Token>();\r
189\r
190 for (index = 0; index < tokenArrayList.size(); index++) {\r
191 boolean found = false;\r
192 Token token = (Token) tokenArrayList.get(index);\r
193 if (token.producers != null) {\r
194 usageInstanceArray = token.producers;\r
195 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex++) {\r
196 usageInstance = (UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
197 if (CommonDefinition.isPeiPhaseComponent(usageInstance.componentType)) {\r
198 pei.add(token);\r
199 found = true;\r
200 break;\r
201 }\r
202 }\r
203\r
204 }\r
205 if (!found) {\r
206 if (token.consumers != null) {\r
207 usageInstanceArray = token.consumers;\r
208 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {\r
209 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
210 if (CommonDefinition.isPeiPhaseComponent(usageInstance.componentType)) {\r
211 pei.add(token);\r
212 found = true;\r
213 break;\r
214 }\r
215 }\r
216 }\r
217 }\r
218\r
219 //\r
220 // If no PEI components reference the PCD entry, we insert it to DXE list\r
221 //\r
222 if (!found) {\r
223 dxe.add(token);\r
224 }\r
225 }\r
226\r
227 return;\r
228 }\r
229\r
230 /**\r
878ddf1f 231 Get all PCD record for a module according to module's name.\r
232 \r
233 @param moduleName the name of module.\r
234 \r
235 @return all usage instance for this module in memory database.\r
236 **/\r
237 public List<UsageInstance> getUsageInstanceArrayByModuleName(String moduleName) {\r
238 Token[] tokenArray = null;\r
239 int recordIndex = 0; \r
240 int usageInstanceIndex = 0;\r
241 List<UsageInstance> usageInstanceArray = null;\r
242 UsageInstance usageInstance = null;\r
243 List<UsageInstance> returnArray = new ArrayList<UsageInstance>();\r
244\r
245 tokenArray = getRecordArray();\r
246\r
247 //\r
248 // Loop to find all PCD record related to current module\r
249 //\r
250 for (recordIndex = 0; recordIndex < getDBSize(); recordIndex ++) {\r
251 if (tokenArray[recordIndex].producers != null) {\r
252 usageInstanceArray = tokenArray[recordIndex].producers;\r
253 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {\r
254 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
255 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {\r
256 returnArray.add(usageInstance);\r
257 }\r
258 }\r
259 }\r
260\r
261 if (tokenArray[recordIndex].consumers != null) {\r
262 usageInstanceArray = tokenArray[recordIndex].consumers;\r
263 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {\r
264 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
265 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {\r
266 returnArray.add(usageInstance);\r
267 }\r
268 }\r
269 }\r
270 }\r
271\r
272 if (returnArray.size() == 0) {\r
273 ActionMessage.warning(this, "Can *not* find any usage instance for " + moduleName + " !");\r
274 }\r
275\r
276 return returnArray;\r
277 }\r
278\r
279 /**\r
280 Get all modules name who contains PCD information\r
281 \r
282 @return Array for module name\r
283 **/\r
284 public List<String> getAllModuleArray()\r
285 {\r
286 int indexToken = 0;\r
287 int usageIndex = 0;\r
288 int moduleIndex = 0;\r
289 Token[] tokenArray = null;\r
290 List<String> moduleNames = new ArrayList<String>();\r
291 UsageInstance usageInstance = null;\r
292 boolean bFound = false;\r
293\r
294 tokenArray = this.getRecordArray();\r
295 //\r
296 // Find all producer usage instance for retrieving module's name\r
297 //\r
298 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {\r
299 for (usageIndex = 0; usageIndex < tokenArray[indexToken].producers.size(); usageIndex ++) {\r
300 usageInstance = tokenArray[indexToken].producers.get(usageIndex);\r
301 bFound = false;\r
302 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {\r
303 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.moduleName)) {\r
304 bFound = true;\r
305 break;\r
306 }\r
307 }\r
308 if (!bFound) {\r
309 moduleNames.add(usageInstance.moduleName);\r
310 }\r
311 }\r
312 }\r
313\r
314 //\r
315 // Find all consumer usage instance for retrieving module's name\r
316 //\r
317 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {\r
318 for (usageIndex = 0; usageIndex < tokenArray[indexToken].consumers.size(); usageIndex ++) {\r
319 usageInstance = tokenArray[indexToken].consumers.get(usageIndex);\r
320 bFound = false;\r
321 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {\r
322 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.moduleName)) {\r
323 bFound = true;\r
324 break;\r
325 }\r
326 }\r
327 if (!bFound) {\r
328 moduleNames.add(usageInstance.moduleName);\r
329 }\r
330 }\r
331 }\r
332 return moduleNames;\r
333 }\r
334\r
335 /**\r
336 Dump all PCD record into file for reviewing.\r
337 **/\r
338 public void DumpAllRecords() {\r
339 BufferedWriter bWriter = null;\r
340 Object[] tokenArray = null;\r
341 Map.Entry entry = null;\r
342 Token token = null;\r
343 int index = 0;\r
344 int usageIndex = 0;\r
345 UsageInstance usageInstance = null;\r
346 String inheritString = null;\r
347 String componentTypeName = null;\r
348\r
349 try {\r
350 bWriter = new BufferedWriter(new FileWriter(new File(logFileName)));\r
351 tokenArray = memoryDatabase.entrySet().toArray();\r
352 for (index = 0; index < memoryDatabase.size(); index ++) {\r
353 entry =(Map.Entry) tokenArray [index];\r
354 token =(Token) entry.getValue();\r
355 bWriter.write("****** token [" + Integer.toString(index) + "] ******\r\n");\r
356 bWriter.write(" cName:" + token.cName + "\r\n");\r
357 for (usageIndex = 0; usageIndex < token.producers.size(); usageIndex ++) {\r
358 usageInstance =(UsageInstance)token.producers.get(usageIndex);\r
359 componentTypeName = CommonDefinition.getComponentTypeString(usageInstance.componentType);\r
360\r
361 if (usageInstance.isInherit) {\r
362 inheritString = "Inherit";\r
363 } else {\r
364 inheritString = "";\r
365 }\r
366 bWriter.write(String.format(" (Producer)#%d: %s:%s Package:%s %s\r\n",\r
367 usageIndex,\r
368 componentTypeName,\r
369 usageInstance.moduleName,\r
370 usageInstance.packageName,\r
371 inheritString\r
372 )\r
373 );\r
374 }\r
375 for (usageIndex = 0; usageIndex < token.consumers.size(); usageIndex ++) {\r
376 usageInstance =(UsageInstance)token.consumers.get(usageIndex);\r
377 componentTypeName = CommonDefinition.getComponentTypeString(usageInstance.componentType);\r
378 if (usageInstance.isInherit) {\r
379 inheritString = "Inherit";\r
380 } else {\r
381 inheritString = "";\r
382 }\r
383 bWriter.write(String.format(" (Consumer)#%d: %s:%s Package:%s %s\r\n",\r
384 usageIndex,\r
385 componentTypeName,\r
386 usageInstance.moduleName,\r
387 usageInstance.packageName,\r
388 inheritString\r
389 )\r
390 );\r
391 }\r
392 }\r
393 bWriter.close();\r
394 } catch (IOException exp) {\r
395 ActionMessage.warning(this, "Failed to open database log file: " + logFileName);\r
396 }\r
397 }\r
398}\r