1 /*
2  * Copyright (C) 2004-2010 NXP Software
3  * Copyright (C) 2010 The Android Open Source Project
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 #include "InstAlloc.h"
19 
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
26  *  Remarks     :
27  ****************************************************************************************/
28 
InstAlloc_Init(INST_ALLOC * pms,void * StartAddr)29 void    InstAlloc_Init( INST_ALLOC      *pms,
30                         void            *StartAddr )
31 {
32     pms->TotalSize = 3;
33     pms->pNextMember = (((uintptr_t)StartAddr + 3) & (uintptr_t)~3);
34 }
35 
36 /****************************************************************************************
37  *  Name        : InstAlloc_AddMember()
38  *  Input       : pms  - Pointer to the INST_ALLOC instance
39                   Size - The size in bytes of the new added member
40  *  Returns     : A pointer to the new added member
41  *  Description : Allocates space for a new member in the instance memory and returns
42                   a pointer to this new member.  The start address of all members will
43                   be 32 bit alligned.
44  *  Remarks     :
45  ****************************************************************************************/
46 
InstAlloc_AddMember(INST_ALLOC * pms,LVM_UINT32 Size)47 void*   InstAlloc_AddMember( INST_ALLOC         *pms,
48                              LVM_UINT32           Size )
49 {
50     void *NewMemberAddress; /* Variable to temporarily store the return value */
51     NewMemberAddress = (void*)pms->pNextMember;
52 
53     Size = ((Size + 3) & (LVM_UINT32)~3); /* Ceil the size to a multiple of four */
54 
55     pms->TotalSize += Size;
56     pms->pNextMember += Size;
57 
58     return(NewMemberAddress);
59 }
60 
61 /****************************************************************************************
62  *  Name        : InstAlloc_GetTotal()
63  *  Input       : pms  - Pointer to the INST_ALLOC instance
64  *  Returns     : The instance memory size
65  *  Description : This functions returns the calculated instance memory size
66  *  Remarks     :
67  ****************************************************************************************/
68 
InstAlloc_GetTotal(INST_ALLOC * pms)69 LVM_UINT32 InstAlloc_GetTotal( INST_ALLOC *pms)
70 {
71     if (pms->TotalSize > 3)
72     {
73         return(pms->TotalSize);
74     }
75     else
76     {
77         return 0;           /* No memory added */
78     }
79 }
80 
InstAlloc_InitAll(INST_ALLOC * pms,LVM_MemoryTable_st * pMemoryTable)81 void    InstAlloc_InitAll( INST_ALLOC                      *pms,
82                            LVM_MemoryTable_st             *pMemoryTable)
83 {
84     uintptr_t StartAddr;
85 
86     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress;
87 
88     pms[0].TotalSize = 3;
89     pms[0].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
90 
91     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress;
92 
93     pms[1].TotalSize = 3;
94     pms[1].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
95 
96     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress;
97 
98     pms[2].TotalSize = 3;
99     pms[2].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
100 
101     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress;
102 
103     pms[3].TotalSize = 3;
104     pms[3].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
105 
106 }
107 
108 /****************************************************************************************
109  *  Name        : InstAlloc_InitAll_NULL()
110  *  Input       : pms  - Pointer to array of four INST_ALLOC instances
111  *  Returns     : Nothing
112  *  Description : This function reserves Size of 3 bytes for all memory regions and
113  *                intializes pNextMember for all regions to 0
114  *  Remarks     :
115  ****************************************************************************************/
116 
InstAlloc_InitAll_NULL(INST_ALLOC * pms)117 void    InstAlloc_InitAll_NULL( INST_ALLOC  *pms)
118 {
119     pms[0].TotalSize = 3;
120     pms[0].pNextMember = 0;
121 
122     pms[1].TotalSize = 3;
123     pms[1].pNextMember = 0;
124 
125     pms[2].TotalSize = 3;
126     pms[2].pNextMember = 0;
127 
128     pms[3].TotalSize = 3;
129     pms[3].pNextMember = 0;
130 
131 }
132 
InstAlloc_AddMemberAll(INST_ALLOC * pms,LVM_UINT32 Size[],LVM_MemoryTable_st * pMemoryTable)133 void*   InstAlloc_AddMemberAll( INST_ALLOC                     *pms,
134                                  LVM_UINT32                   Size[],
135                                  LVM_MemoryTable_st           *pMemoryTable)
136 {
137     void *NewMemberAddress; /* Variable to temporarily store the return value */
138 
139     /* coverity[returned_pointer] Ignore coverity warning that ptr is not used */
140     NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_SLOW_DATA], Size[LVM_PERSISTENT_SLOW_DATA]);
141 
142     pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].Size         = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_SLOW_DATA]);
143     pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].Type         = LVM_PERSISTENT_SLOW_DATA;
144     pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress = LVM_NULL;
145 
146     NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_DATA], Size[LVM_PERSISTENT_FAST_DATA]);
147 
148     pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].Size         = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_FAST_DATA]);
149     pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].Type         = LVM_PERSISTENT_FAST_DATA;
150     pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress = LVM_NULL;
151 
152     NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_COEF], Size[LVM_PERSISTENT_FAST_COEF]);
153 
154     pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].Size         = InstAlloc_GetTotal(&pms[LVM_PERSISTENT_FAST_COEF]);
155     pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].Type         = LVM_PERSISTENT_FAST_COEF;
156     pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress = LVM_NULL;
157 
158     NewMemberAddress = InstAlloc_AddMember(&pms[LVM_TEMPORARY_FAST], Size[LVM_TEMPORARY_FAST]);
159 
160     pMemoryTable->Region[LVM_TEMPORARY_FAST].Size                 = InstAlloc_GetTotal(&pms[LVM_TEMPORARY_FAST]);
161     pMemoryTable->Region[LVM_TEMPORARY_FAST].Type                 = LVM_TEMPORARY_FAST;
162     pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress         = LVM_NULL;
163 
164     return(NewMemberAddress);
165 }
166 
InstAlloc_AddMemberAllRet(INST_ALLOC * pms,LVM_UINT32 Size[],void ** ptr)167 void*   InstAlloc_AddMemberAllRet(     INST_ALLOC                 *pms,
168                                      LVM_UINT32               Size[],
169                                      void                    **ptr)
170 {
171     ptr[0] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_SLOW_DATA], Size[LVM_PERSISTENT_SLOW_DATA]);
172     ptr[1] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_DATA], Size[LVM_PERSISTENT_FAST_DATA]);
173     ptr[2] = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_COEF], Size[LVM_PERSISTENT_FAST_COEF]);
174     ptr[3] = InstAlloc_AddMember(&pms[LVM_TEMPORARY_FAST], Size[LVM_TEMPORARY_FAST]);
175 
176     return (ptr[0]);
177 }
178