2 * Copyright (C) 2004-2010 NXP Software
3 * Copyright (C) 2010 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "InstAlloc.h"
20 /****************************************************************************************
21 * Name : InstAlloc_Init()
22 * Input : pms - Pointer to the INST_ALLOC instance
23 StartAddr - Base address of the instance memory
24 * Returns : Error code
25 * Description : Initializes the instance distribution and memory size calculation function
27 ****************************************************************************************/
29 void InstAlloc_Init( INST_ALLOC *pms,
33 pms->pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);/* This code will fail if the platform address space is more than 32-bits*/
37 /****************************************************************************************
38 * Name : InstAlloc_AddMember()
39 * Input : pms - Pointer to the INST_ALLOC instance
40 Size - The size in bytes of the new added member
41 * Returns : A pointer to the new added member
42 * Description : Allocates space for a new member in the instance memory and returns
43 a pointer to this new member. The start address of all members will
46 ****************************************************************************************/
48 void* InstAlloc_AddMember( INST_ALLOC *pms,
51 void *NewMemberAddress; /* Variable to temporarily store the return value */
52 NewMemberAddress = (void*)pms->pNextMember;
54 Size = ((Size + 3) & 0xFFFFFFFC); /* Ceil the size to a multiple of four */
56 pms->TotalSize += Size;
57 pms->pNextMember += Size;
59 return(NewMemberAddress);
63 /****************************************************************************************
64 * Name : InstAlloc_GetTotal()
65 * Input : pms - Pointer to the INST_ALLOC instance
66 * Returns : The instance memory size
67 * Description : This functions returns the calculated instance memory size
69 ****************************************************************************************/
71 LVM_UINT32 InstAlloc_GetTotal( INST_ALLOC *pms)
73 if (pms->TotalSize > 3)
75 return(pms->TotalSize);
79 return 0; /* No memory added */
84 void InstAlloc_InitAll( INST_ALLOC *pms,
85 LVM_MemoryTable_st *pMemoryTable)
89 StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress;
92 pms[0].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
95 StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress;
98 pms[1].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
101 StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress;
103 pms[2].TotalSize = 3;
104 pms[2].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
107 StartAddr = (LVM_UINT32)pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress;
109 pms[3].TotalSize = 3;
110 pms[3].pNextMember = (LVM_UINT32)(((LVM_UINT32)StartAddr + 3) & 0xFFFFFFFC);
114 /****************************************************************************************
115 * Name : InstAlloc_InitAll_NULL()
116 * Input : pms - Pointer to array of four INST_ALLOC instances
118 * Description : This function reserves Size of 3 bytes for all memory regions and
119 * intializes pNextMember for all regions to 0
121 ****************************************************************************************/
123 void InstAlloc_InitAll_NULL( INST_ALLOC *pms)
125 pms[0].TotalSize = 3;
126 pms[0].pNextMember = 0;
129 pms[1].TotalSize = 3;
130 pms[1].pNextMember = 0;
132 pms[2].TotalSize = 3;
133 pms[2].pNextMember = 0;
135 pms[3].TotalSize = 3;
136 pms[3].pNextMember = 0;
141 void* InstAlloc_AddMemberAll( INST_ALLOC *pms,
143 LVM_MemoryTable_st *pMemoryTable)
145 void *NewMemberAddress; /* Variable to temporarily store the return value */
147 /* coverity[returned_pointer] Ignore coverity warning that ptr is not used */
148 NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_SLOW_DATA], Size[LVM_PERSISTENT_SLOW_DATA]);
150 pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].Size = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_SLOW_DATA]);
151 pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].Type = LVM_PERSISTENT_SLOW_DATA;
152 pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress = LVM_NULL;
154 NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_DATA], Size[LVM_PERSISTENT_FAST_DATA]);
156 pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].Size = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_FAST_DATA]);
157 pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].Type = LVM_PERSISTENT_FAST_DATA;
158 pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress = LVM_NULL;
160 NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_COEF], Size[LVM_PERSISTENT_FAST_COEF]);
162 pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].Size = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_FAST_COEF]);
163 pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].Type = LVM_PERSISTENT_FAST_COEF;
164 pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress = LVM_NULL;
166 NewMemberAddress = InstAlloc_AddMember(&pms[LVM_TEMPORARY_FAST], Size[LVM_TEMPORARY_FAST]);
168 pMemoryTable->Region[LVM_TEMPORARY_FAST].Size = InstAlloc_GetTotal(&pms[LVM_TEMPORARY_FAST]);
169 pMemoryTable->Region[LVM_TEMPORARY_FAST].Type = LVM_TEMPORARY_FAST;
170 pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress = LVM_NULL;
172 return(NewMemberAddress);
176 void* InstAlloc_AddMemberAllRet( INST_ALLOC *pms,
180 ptr[0] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_SLOW_DATA], Size[LVM_PERSISTENT_SLOW_DATA]);
181 ptr[1] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_DATA], Size[LVM_PERSISTENT_FAST_DATA]);
182 ptr[2] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_COEF], Size[LVM_PERSISTENT_FAST_COEF]);
183 ptr[3] = InstAlloc_AddMember(&pms[LVM_TEMPORARY_FAST], Size[LVM_TEMPORARY_FAST]);