2 MemoryDatabaseManager class.
4 Database hold all PCD information comes from SPD, MSA, FPD file in memory.
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 package org
.tianocore
.build
.pcd
.entity
;
18 import java
.io
.BufferedWriter
;
20 import java
.io
.FileWriter
;
21 import java
.io
.IOException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collections
;
24 import java
.util
.Comparator
;
25 import java
.util
.HashMap
;
26 import java
.util
.Hashtable
;
27 import java
.util
.List
;
30 import org
.tianocore
.build
.autogen
.CommonDefinition
;
31 import org
.tianocore
.build
.pcd
.action
.ActionMessage
;
33 class AlignmentSizeComp
implements Comparator
{
34 public int compare (Object a
, Object b
) {
38 return Token
.getAutogendatumTypeAlignmentSize(tA
.datumType
)
39 - Token
.getAutogendatumTypeAlignmentSize(tB
.datumType
);
43 /** Database hold all PCD information comes from SPD, MSA, FPD file in memory.
45 public class MemoryDatabaseManager
{
47 /// Memory database. The string "cName + SpaceNameGuid" is primary key.
48 /// memory database is in global scope, and it will be used for others PCD tools.
50 private static Map
<String
, Token
> memoryDatabase
= null;
52 /// The log file name for dumping memory database.
54 private static String logFileName
= null;
56 public static String PcdPeimHString
= "";
57 public static String PcdPeimCString
= "";
58 public static String PcdDxeHString
= "";
59 public static String PcdDxeCString
= "";
64 public MemoryDatabaseManager() {
66 // Allocate memory for new database in global scope.
68 if (memoryDatabase
== null) {
69 memoryDatabase
= new HashMap
<String
, Token
>();
74 Get the log file name.
76 public String
getLogFileName() {
81 Set parameter log file name.
83 @param fileName log file name parameter.
85 public void setLogFileName(String fileName
) {
86 logFileName
= fileName
;
90 Judege whether token exists in memory database
92 @param primaryKey the primaryKey for searching token
94 @retval TRUE - token already exist in database.
95 @retval FALSE - token does not exist in database.
97 public boolean isTokenInDatabase(String primaryKey
) {
98 return (memoryDatabase
.get(primaryKey
) != null);
102 Add a pcd token into memory database.
104 @param primaryKey the primary key for searching token
105 @param token token instance
107 public void addTokenToDatabase(String primaryKey
, Token token
) {
108 memoryDatabase
.put(primaryKey
, token
);
112 Get a token instance from memory database with primary key.
114 @param primaryKey the primary key for searching token
116 @return token instance.
118 public Token
getTokenByKey(String primaryKey
) {
119 return memoryDatabase
.get(primaryKey
);
123 Get the number of PCD token record in memory database.
125 @return the number of PCD token record in memory database.
127 public int getDBSize() {
128 return memoryDatabase
.size();
132 Get the token record array contained all PCD token in memory database.
134 @return the token record array contained all PCD token in memory database.
136 public Token
[] getRecordArray() {
137 Token
[] tokenArray
= null;
138 Object
[] dataArray
= null;
139 Map
.Entry entry
= null;
142 if (memoryDatabase
== null) {
146 dataArray
= memoryDatabase
.entrySet().toArray();
147 tokenArray
= new Token
[memoryDatabase
.size()];
148 for (index
= 0; index
< memoryDatabase
.size(); index
++) {
149 entry
=(Map
.Entry
) dataArray
[index
];
150 tokenArray
[index
] =(Token
) entry
.getValue();
157 private ArrayList
getDynamicRecordArray() {
158 Token
[] tokenArray
= getRecordArray();
161 ArrayList al
= new ArrayList();
163 for (index
= 0; index
< tokenArray
.length
; index
++) {
164 if (tokenArray
[index
].pcdType
== Token
.PCD_TYPE
.DYNAMIC
||
165 tokenArray
[index
].pcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
166 al
.add(tokenArray
[index
]);
175 Get the token record array contained all PCD token referenced by PEI phase.
176 The output array is sorted based on descending order of the size of alignment for each feilds.
178 @return the token record array contained all PCD token referenced in PEI phase.
180 public void getTwoPhaseDynamicRecordArray(ArrayList
<Token
> pei
, ArrayList
<Token
> dxe
) {
181 int usageInstanceIndex
= 0;
183 ArrayList tokenArrayList
= getDynamicRecordArray();
184 List
<UsageInstance
> usageInstanceArray
= null;
185 UsageInstance usageInstance
= null;
187 //pei = new ArrayList<Token>();
188 //dxe = new ArrayList<Token>();
190 for (index
= 0; index
< tokenArrayList
.size(); index
++) {
191 boolean found
= false;
192 Token token
= (Token
) tokenArrayList
.get(index
);
193 if (token
.producers
!= null) {
194 usageInstanceArray
= token
.producers
;
195 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
196 usageInstance
= (UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
197 if (CommonDefinition
.isPeiPhaseComponent(usageInstance
.componentType
)) {
206 if (token
.consumers
!= null) {
207 usageInstanceArray
= token
.consumers
;
208 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
209 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
210 if (CommonDefinition
.isPeiPhaseComponent(usageInstance
.componentType
)) {
220 // If no PEI components reference the PCD entry, we insert it to DXE list
231 Get all PCD record for a module according to module's name.
233 @param moduleName the name of module.
235 @return all usage instance for this module in memory database.
237 public List
<UsageInstance
> getUsageInstanceArrayByModuleName(String moduleName
) {
238 Token
[] tokenArray
= null;
240 int usageInstanceIndex
= 0;
241 List
<UsageInstance
> usageInstanceArray
= null;
242 UsageInstance usageInstance
= null;
243 List
<UsageInstance
> returnArray
= new ArrayList
<UsageInstance
>();
245 tokenArray
= getRecordArray();
248 // Loop to find all PCD record related to current module
250 for (recordIndex
= 0; recordIndex
< getDBSize(); recordIndex
++) {
251 if (tokenArray
[recordIndex
].producers
!= null) {
252 usageInstanceArray
= tokenArray
[recordIndex
].producers
;
253 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
254 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
255 if (usageInstance
.moduleName
.equalsIgnoreCase(moduleName
)) {
256 returnArray
.add(usageInstance
);
261 if (tokenArray
[recordIndex
].consumers
!= null) {
262 usageInstanceArray
= tokenArray
[recordIndex
].consumers
;
263 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
264 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
265 if (usageInstance
.moduleName
.equalsIgnoreCase(moduleName
)) {
266 returnArray
.add(usageInstance
);
272 if (returnArray
.size() == 0) {
273 ActionMessage
.warning(this, "Can *not* find any usage instance for " + moduleName
+ " !");
280 Get all modules name who contains PCD information
282 @return Array for module name
284 public List
<String
> getAllModuleArray()
289 Token
[] tokenArray
= null;
290 List
<String
> moduleNames
= new ArrayList
<String
>();
291 UsageInstance usageInstance
= null;
292 boolean bFound
= false;
294 tokenArray
= this.getRecordArray();
296 // Find all producer usage instance for retrieving module's name
298 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
299 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].producers
.size(); usageIndex
++) {
300 usageInstance
= tokenArray
[indexToken
].producers
.get(usageIndex
);
302 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
303 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
309 moduleNames
.add(usageInstance
.moduleName
);
315 // Find all consumer usage instance for retrieving module's name
317 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
318 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].consumers
.size(); usageIndex
++) {
319 usageInstance
= tokenArray
[indexToken
].consumers
.get(usageIndex
);
321 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
322 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
328 moduleNames
.add(usageInstance
.moduleName
);
336 Dump all PCD record into file for reviewing.
338 public void DumpAllRecords() {
339 BufferedWriter bWriter
= null;
340 Object
[] tokenArray
= null;
341 Map
.Entry entry
= null;
345 UsageInstance usageInstance
= null;
346 String inheritString
= null;
347 String componentTypeName
= null;
350 bWriter
= new BufferedWriter(new FileWriter(new File(logFileName
)));
351 tokenArray
= memoryDatabase
.entrySet().toArray();
352 for (index
= 0; index
< memoryDatabase
.size(); index
++) {
353 entry
=(Map
.Entry
) tokenArray
[index
];
354 token
=(Token
) entry
.getValue();
355 bWriter
.write("****** token [" + Integer
.toString(index
) + "] ******\r\n");
356 bWriter
.write(" cName:" + token
.cName
+ "\r\n");
357 for (usageIndex
= 0; usageIndex
< token
.producers
.size(); usageIndex
++) {
358 usageInstance
=(UsageInstance
)token
.producers
.get(usageIndex
);
359 componentTypeName
= CommonDefinition
.getComponentTypeString(usageInstance
.componentType
);
361 if (usageInstance
.isInherit
) {
362 inheritString
= "Inherit";
366 bWriter
.write(String
.format(" (Producer)#%d: %s:%s Package:%s %s\r\n",
369 usageInstance
.moduleName
,
370 usageInstance
.packageName
,
375 for (usageIndex
= 0; usageIndex
< token
.consumers
.size(); usageIndex
++) {
376 usageInstance
=(UsageInstance
)token
.consumers
.get(usageIndex
);
377 componentTypeName
= CommonDefinition
.getComponentTypeString(usageInstance
.componentType
);
378 if (usageInstance
.isInherit
) {
379 inheritString
= "Inherit";
383 bWriter
.write(String
.format(" (Consumer)#%d: %s:%s Package:%s %s\r\n",
386 usageInstance
.moduleName
,
387 usageInstance
.packageName
,
394 } catch (IOException exp
) {
395 ActionMessage
.warning(this, "Failed to open database log file: " + logFileName
);