]>
Commit | Line | Data |
---|---|---|
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 | |
6 | Copyright (c) 2006, Intel Corporation\r | |
7 | All rights reserved. This program and the accompanying materials\r | |
8 | are licensed and made available under the terms and conditions of the BSD License\r | |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | package org.tianocore.build.pcd.entity;\r | |
17 | \r | |
18 | import java.io.BufferedWriter;\r | |
19 | import java.io.File;\r | |
20 | import java.io.FileWriter;\r | |
21 | import java.io.IOException;\r | |
22 | import java.util.ArrayList;\r | |
99d2c3c4 | 23 | import java.util.Collections;\r |
24 | import java.util.Comparator;\r | |
878ddf1f | 25 | import java.util.HashMap;\r |
99d2c3c4 | 26 | import java.util.Hashtable;\r |
878ddf1f | 27 | import java.util.List;\r |
28 | import java.util.Map;\r | |
8840ad58 | 29 | import java.util.UUID;\r |
878ddf1f | 30 | \r |
878ddf1f | 31 | import org.tianocore.build.pcd.action.ActionMessage;\r |
32 | \r | |
33 | /** Database hold all PCD information comes from SPD, MSA, FPD file in memory.\r | |
34 | **/\r | |
35 | public class MemoryDatabaseManager {\r | |
36 | ///\r | |
37 | /// Memory database. The string "cName + SpaceNameGuid" is primary key.\r | |
38 | /// memory database is in global scope, and it will be used for others PCD tools.\r | |
39 | ///\r | |
8840ad58 | 40 | private static Map<String, Token> memoryDatabase = null;\r |
41 | \r | |
878ddf1f | 42 | ///\r |
8840ad58 | 43 | /// Before build a module, the used libary will be build firstly, the PCD of these\r |
44 | /// libarry is inheritted by the module, so stored module's PCD information as PCD\r | |
45 | /// context of building libary.\r | |
46 | /// \r | |
47 | public static List<UsageInstance> UsageInstanceContext = null;\r | |
878ddf1f | 48 | ///\r |
8840ad58 | 49 | /// \r |
50 | /// \r | |
51 | public static String CurrentModuleName = null;\r | |
99d2c3c4 | 52 | public static String PcdPeimHString = "";\r |
32648c62 | 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 | |
71 | \r | |
72 | @param primaryKey the primaryKey for searching token\r | |
73 | \r | |
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 | |
83 | \r | |
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 | |
93 | \r | |
94 | @param primaryKey the primary key for searching token\r | |
95 | \r | |
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 | |
104 | \r | |
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 | |
113 | \r | |
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 | |
121 | \r | |
122 | if (memoryDatabase == null) {\r | |
123 | return null;\r | |
124 | }\r | |
125 | \r | |
126 | dataArray = memoryDatabase.entrySet().toArray();\r | |
127 | tokenArray = new Token[memoryDatabase.size()];\r | |
128 | for (index = 0; index < memoryDatabase.size(); index ++) {\r | |
129 | entry =(Map.Entry) dataArray [index];\r | |
130 | tokenArray[index] =(Token) entry.getValue();\r | |
131 | }\r | |
132 | \r | |
133 | return tokenArray;\r | |
134 | }\r | |
135 | \r | |
8840ad58 | 136 | /**\r |
137 | Get record array only contains DYNAMIC or DYNAMIC_EX type PCD.\r | |
138 | \r | |
139 | @return ArrayList\r | |
140 | */\r | |
141 | private ArrayList getDynamicRecordArray() {\r | |
99d2c3c4 | 142 | Token[] tokenArray = getRecordArray();\r |
143 | int index = 0;\r | |
144 | int count = 0;\r | |
6a4cae58 | 145 | ArrayList<Token> al = new ArrayList<Token>();\r |
99d2c3c4 | 146 | \r |
147 | for (index = 0; index < tokenArray.length; index++) {\r | |
148 | if (tokenArray[index].pcdType == Token.PCD_TYPE.DYNAMIC ||\r | |
149 | tokenArray[index].pcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r | |
150 | al.add(tokenArray[index]);\r | |
151 | }\r | |
152 | }\r | |
153 | \r | |
154 | return al;\r | |
155 | }\r | |
156 | \r | |
157 | \r | |
878ddf1f | 158 | /**\r |
99d2c3c4 | 159 | Get the token record array contained all PCD token referenced by PEI phase.\r |
160 | The output array is sorted based on descending order of the size of alignment for each feilds.\r | |
161 | \r | |
162 | @return the token record array contained all PCD token referenced in PEI phase.\r | |
163 | **/\r | |
164 | public void getTwoPhaseDynamicRecordArray(ArrayList<Token> pei, ArrayList<Token> dxe) {\r | |
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 |
170 | \r | |
8840ad58 | 171 | //pei = new ArrayList<Token>();\r |
172 | //dxe = new ArrayList<Token>();\r | |
173 | \r | |
99d2c3c4 | 174 | for (index = 0; index < tokenArrayList.size(); index++) {\r |
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 | |
179 | for (usageInstanceIndex = 0; usageInstanceIndex < token.consumers.size(); usageInstanceIndex ++) {\r | |
180 | usageInstance =(UsageInstance) (((Map.Entry)usageInstanceArray[usageInstanceIndex]).getValue());\r | |
181 | if (usageInstance.isPeiPhaseComponent()) {\r | |
99d2c3c4 | 182 | pei.add(token);\r |
183 | found = true;\r | |
184 | break;\r | |
185 | }\r | |
186 | }\r | |
99d2c3c4 | 187 | }\r |
188 | \r | |
32648c62 | 189 | // If no PEI components reference the PCD entry, we insert it to DXE list\r |
190 | //\r | |
191 | if (!found) {\r | |
192 | dxe.add(token);\r | |
193 | }\r | |
99d2c3c4 | 194 | }\r |
195 | \r | |
32648c62 | 196 | return;\r |
99d2c3c4 | 197 | }\r |
198 | \r | |
32648c62 | 199 | /**\r |
8840ad58 | 200 | Get all PCD record for a module according to module's name, module's GUID,\r |
201 | package name, package GUID, arch, version information.\r | |
878ddf1f | 202 | \r |
203 | @param moduleName the name of module.\r | |
204 | \r | |
205 | @return all usage instance for this module in memory database.\r | |
206 | **/\r | |
8840ad58 | 207 | public List<UsageInstance> getUsageInstanceArrayByModuleName(String moduleName,\r |
208 | UUID moduleGuid,\r | |
209 | String packageName,\r | |
210 | UUID packageGuid,\r | |
211 | String arch,\r | |
212 | String version) {\r | |
213 | \r | |
214 | String primaryKey = UsageInstance.getPrimaryKey(moduleName, \r | |
215 | moduleGuid,\r | |
216 | packageName,\r | |
217 | packageGuid,\r | |
218 | arch,\r | |
219 | version);\r | |
220 | \r | |
221 | return getUsageInstanceArrayByKeyString(primaryKey);\r | |
222 | }\r | |
223 | \r | |
224 | /**\r | |
225 | Get all PCD token for a usage instance according to primary key.\r | |
226 | \r | |
227 | @param primaryKey the primary key of usage instance.\r | |
228 | \r | |
229 | @return List<UsageInstance>\r | |
230 | */\r | |
231 | public List<UsageInstance> getUsageInstanceArrayByKeyString(String primaryKey) {\r | |
878ddf1f | 232 | Token[] tokenArray = null;\r |
233 | int recordIndex = 0; \r | |
878ddf1f | 234 | UsageInstance usageInstance = null;\r |
235 | List<UsageInstance> returnArray = new ArrayList<UsageInstance>();\r | |
236 | \r | |
237 | tokenArray = getRecordArray();\r | |
238 | \r | |
239 | //\r | |
240 | // Loop to find all PCD record related to current module\r | |
241 | //\r | |
242 | for (recordIndex = 0; recordIndex < getDBSize(); recordIndex ++) {\r | |
8840ad58 | 243 | if (tokenArray[recordIndex].consumers.size() != 0) {\r |
244 | usageInstance = tokenArray[recordIndex].consumers.get(primaryKey);\r | |
245 | if (usageInstance != null) {\r | |
246 | returnArray.add(usageInstance);\r | |
878ddf1f | 247 | }\r |
248 | }\r | |
249 | }\r | |
250 | \r | |
878ddf1f | 251 | return returnArray;\r |
252 | }\r | |
253 | \r | |
254 | /**\r | |
255 | Get all modules name who contains PCD information\r | |
256 | \r | |
257 | @return Array for module name\r | |
258 | **/\r | |
259 | public List<String> getAllModuleArray()\r | |
260 | {\r | |
261 | int indexToken = 0;\r | |
262 | int usageIndex = 0;\r | |
263 | int moduleIndex = 0;\r | |
264 | Token[] tokenArray = null;\r | |
8840ad58 | 265 | Object[] usageInstanceArray = null;\r |
878ddf1f | 266 | List<String> moduleNames = new ArrayList<String>();\r |
267 | UsageInstance usageInstance = null;\r | |
268 | boolean bFound = false;\r | |
269 | \r | |
8840ad58 | 270 | tokenArray = getRecordArray();\r |
878ddf1f | 271 | //\r |
272 | // Find all consumer usage instance for retrieving module's name\r | |
273 | //\r | |
274 | for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {\r | |
8840ad58 | 275 | usageInstanceArray = tokenArray[indexToken].consumers.entrySet().toArray();\r |
878ddf1f | 276 | for (usageIndex = 0; usageIndex < tokenArray[indexToken].consumers.size(); usageIndex ++) {\r |
8840ad58 | 277 | usageInstance = (UsageInstance)((Map.Entry)usageInstanceArray[usageIndex]).getValue();\r |
878ddf1f | 278 | bFound = false;\r |
279 | for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {\r | |
8840ad58 | 280 | if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.getPrimaryKey())) {\r |
878ddf1f | 281 | bFound = true;\r |
282 | break;\r | |
283 | }\r | |
284 | }\r | |
285 | if (!bFound) {\r | |
8840ad58 | 286 | moduleNames.add(usageInstance.getPrimaryKey());\r |
878ddf1f | 287 | }\r |
288 | }\r | |
289 | }\r | |
290 | return moduleNames;\r | |
291 | }\r | |
878ddf1f | 292 | }\r |