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