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
.pcd
.entity
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
23 import org
.tianocore
.pcd
.entity
.UsageIdentification
;
24 import org
.tianocore
.pcd
.exception
.EntityException
;
27 Database hold all PCD information comes from SPD, MSA, FPD file in memory.
29 public class MemoryDatabaseManager
{
31 /// Memory database. The string "cName + SpaceNameGuid" is primary key.
32 /// memory database is in global scope, and it will be used for others PCD tools.
34 private static Map
<String
, Token
> memoryDatabase
= null;
37 /// Before build a module, the used libary will be build firstly, the PCD of these
38 /// library is inheritted by the module, so stored module's PCD information as PCD
39 /// context of building libary.
41 public static List
<UsageInstance
> UsageInstanceContext
= null;
44 /// Current module name, if now is buiding library, this value indicate this library
45 /// is for building what module.
47 public static String CurrentModuleName
= null;
50 /// String for PCD PEIM and DXE autogen file
52 public static String PcdPeimHString
= "";
53 public static String PcdPeimCString
= "";
54 public static String PcdDxeHString
= "";
55 public static String PcdDxeCString
= "";
60 public MemoryDatabaseManager() {
62 // Allocate memory for new database in global scope.
64 if (memoryDatabase
== null) {
65 memoryDatabase
= new HashMap
<String
, Token
>();
70 Judege whether token exists in memory database
72 @param primaryKey the primaryKey for searching token
74 @retval TRUE - token already exist in database.
75 @retval FALSE - token does not exist in database.
77 public boolean isTokenInDatabase(String primaryKey
) {
78 return (memoryDatabase
.get(primaryKey
) != null);
82 Add a pcd token into memory database.
84 @param primaryKey the primary key for searching token
85 @param token token instance
87 public void addTokenToDatabase(String primaryKey
, Token token
) {
88 memoryDatabase
.put(primaryKey
, token
);
92 Get a token instance from memory database with primary key.
94 @param primaryKey the primary key for searching token
96 @return token instance.
98 public Token
getTokenByKey(String primaryKey
) {
99 return memoryDatabase
.get(primaryKey
);
103 Get the number of PCD token record in memory database.
105 @return the number of PCD token record in memory database.
107 public int getDBSize() {
108 return memoryDatabase
.size();
112 Get the token record array contained all PCD token in memory database.
114 @return the token record array contained all PCD token in memory database.
116 public Token
[] getRecordArray() {
117 Token
[] tokenArray
= null;
118 Object
[] dataArray
= null;
119 Map
.Entry entry
= null;
123 if (memoryDatabase
== null) {
126 dataArray
= memoryDatabase
.entrySet().toArray();
127 size
= memoryDatabase
.size();
128 tokenArray
= new Token
[memoryDatabase
.size()];
129 for (index
= 0; index
< size
; index
++) {
130 entry
=(Map
.Entry
) dataArray
[index
];
131 tokenArray
[index
] =(Token
) entry
.getValue();
137 Get record array only contains DYNAMIC or DYNAMIC_EX type PCD.
139 @return ArrayList the array list contains all dynamic type PCD.
141 private ArrayList
getDynamicRecordArray() {
142 Token
[] tokenArray
= getRecordArray();
144 ArrayList
<Token
> al
= new ArrayList
<Token
>();
146 for (index
= 0; index
< tokenArray
.length
; index
++) {
147 if (tokenArray
[index
].isDynamicPCD
) {
148 al
.add(tokenArray
[index
]);
157 Get the token record array contained all PCD token referenced by PEI phase.
158 The output array is sorted based on descending order of the size of alignment for each feilds.
160 @return the token record array contained all PCD token referenced in PEI phase.
161 @throws EntityException
163 public void getTwoPhaseDynamicRecordArray(ArrayList
<Token
> pei
, ArrayList
<Token
> dxe
)
164 throws EntityException
{
165 int usageInstanceIndex
= 0;
167 ArrayList tokenArrayList
= getDynamicRecordArray();
168 Object
[] usageInstanceArray
= null;
169 UsageInstance usageInstance
= null;
171 int consumerSize
= 0;
173 size
= tokenArrayList
.size();
174 for (index
= 0; index
< size
; index
++) {
175 boolean found
= false;
176 Token token
= (Token
) tokenArrayList
.get(index
);
177 if (token
.consumers
!= null) {
178 usageInstanceArray
= token
.consumers
.entrySet().toArray();
179 consumerSize
= token
.consumers
.size();
180 for (usageInstanceIndex
= 0; usageInstanceIndex
< consumerSize
; usageInstanceIndex
++) {
181 usageInstance
=(UsageInstance
) (((Map
.Entry
)usageInstanceArray
[usageInstanceIndex
]).getValue());
182 if (usageInstance
.isPeiPhaseComponent()) {
191 // If no PEI components reference the PCD entry,
192 // we check if it is referenced in DXE driver.
195 if (token
.consumers
!= null) {
196 usageInstanceArray
= token
.consumers
.entrySet().toArray();
197 consumerSize
= token
.consumers
.size();
198 for (usageInstanceIndex
= 0; usageInstanceIndex
< consumerSize
; usageInstanceIndex
++) {
199 usageInstance
=(UsageInstance
) (((Map
.Entry
)usageInstanceArray
[usageInstanceIndex
]).getValue());
200 if (usageInstance
.isDxePhaseComponent()) {
209 if (token
.isDynamicPCD
&& token
.consumers
.size() == 0) {
213 // We only support Dynamice(EX) type for PEI and DXE phase.
214 // If it is not referenced in either PEI or DXE, throw exception now.
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.");
223 public void clearDatabase() {
224 memoryDatabase
.clear();
228 Get all PCD token for a usage instance according to primary key.
230 @param primaryKey the primary key of usage instance.
232 @return List<UsageInstance> the list contains all usage instances.
234 public List
<UsageInstance
> getUsageInstanceArrayById(UsageIdentification usageId
) {
235 Token
[] tokenArray
= null;
237 UsageInstance usageInstance
= null;
238 List
<UsageInstance
> returnArray
= new ArrayList
<UsageInstance
>();
239 String primaryKey
= usageId
.toString();
241 tokenArray
= getRecordArray();
244 // Loop to find all PCD record related to current module
246 for (recordIndex
= 0; recordIndex
< getDBSize(); recordIndex
++) {
247 if (tokenArray
[recordIndex
].consumers
.size() != 0) {
248 usageInstance
= tokenArray
[recordIndex
].consumers
.get(primaryKey
);
249 if (usageInstance
!= null) {
250 returnArray
.add(usageInstance
);
258 public List
<Token
> getPcdTokenListForModule(UsageIdentification usageId
) {
259 List
<UsageInstance
> usageList
= getUsageInstanceArrayById(usageId
);
260 List
<Token
> tokenList
= new ArrayList
<Token
>();
262 if (usageList
== null) {
266 for (int usageIndex
= 0; usageIndex
< usageList
.size(); usageIndex
++) {
267 tokenList
.add(usageList
.get(usageIndex
).parentToken
);
274 Get all modules name who contains PCD information
276 @return Array for usage's identification
278 public List
<UsageIdentification
> getAllModuleArray()
283 Token
[] tokenArray
= null;
284 Object
[] usageInstanceArray
= null;
285 List
<UsageIdentification
> usageArray
= new ArrayList
<UsageIdentification
>();
286 UsageInstance usageInstance
= null;
287 boolean bFound
= false;
288 String primaryKey
= null;
290 tokenArray
= getRecordArray();
292 // Find all consumer usage instance for retrieving module's name
294 for (tokenIndex
= 0; tokenIndex
< getDBSize(); tokenIndex
++) {
295 usageInstanceArray
= tokenArray
[tokenIndex
].consumers
.entrySet().toArray();
296 for (usageIndex
= 0; usageIndex
< tokenArray
[tokenIndex
].consumers
.size(); usageIndex
++) {
297 usageInstance
= (UsageInstance
)((Map
.Entry
)usageInstanceArray
[usageIndex
]).getValue();
298 primaryKey
= usageInstance
.getPrimaryKey();
300 for (moduleIndex
= 0; moduleIndex
< usageArray
.size(); moduleIndex
++) {
301 if (usageArray
.get(moduleIndex
).toString().equalsIgnoreCase(primaryKey
)) {
307 usageArray
.add(usageInstance
.usageId
);