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