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 /****************************************************************************************
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
44                   be 32 bit alligned.
45  *  Remarks     :
46  ****************************************************************************************/
47 
InstAlloc_AddMember(INST_ALLOC * pms,LVM_UINT32 Size)48 void*   InstAlloc_AddMember( INST_ALLOC         *pms,
49                              LVM_UINT32           Size )
50 {
51     void *NewMemberAddress; /* Variable to temporarily store the return value */
52     NewMemberAddress = (void*)pms->pNextMember;
53 
54     Size = ((Size + 3) & (LVM_UINT32)~3); /* Ceil the size to a multiple of four */
55 
56     pms->TotalSize += Size;
57     pms->pNextMember += Size;
58 
59     return(NewMemberAddress);
60 }
61 
62 
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
68  *  Remarks     :
69  ****************************************************************************************/
70 
InstAlloc_GetTotal(INST_ALLOC * pms)71 LVM_UINT32 InstAlloc_GetTotal( INST_ALLOC *pms)
72 {
73     if (pms->TotalSize > 3)
74     {
75         return(pms->TotalSize);
76     }
77     else
78     {
79         return 0;           /* No memory added */
80     }
81 }
82 
83 
InstAlloc_InitAll(INST_ALLOC * pms,LVM_MemoryTable_st * pMemoryTable)84 void    InstAlloc_InitAll( INST_ALLOC                      *pms,
85                            LVM_MemoryTable_st             *pMemoryTable)
86 {
87     uintptr_t StartAddr;
88 
89     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_PERSISTENT_SLOW_DATA].pBaseAddress;
90 
91     pms[0].TotalSize = 3;
92     pms[0].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
93 
94 
95     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_PERSISTENT_FAST_DATA].pBaseAddress;
96 
97     pms[1].TotalSize = 3;
98     pms[1].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
99 
100 
101     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_PERSISTENT_FAST_COEF].pBaseAddress;
102 
103     pms[2].TotalSize = 3;
104     pms[2].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
105 
106 
107     StartAddr = (uintptr_t)pMemoryTable->Region[LVM_TEMPORARY_FAST].pBaseAddress;
108 
109     pms[3].TotalSize = 3;
110     pms[3].pNextMember = ((StartAddr + 3) & (uintptr_t)~3);
111 
112 }
113 
114 /****************************************************************************************
115  *  Name        : InstAlloc_InitAll_NULL()
116  *  Input       : pms  - Pointer to array of four INST_ALLOC instances
117  *  Returns     : Nothing
118  *  Description : This function reserves Size of 3 bytes for all memory regions and
119  *                intializes pNextMember for all regions to 0
120  *  Remarks     :
121  ****************************************************************************************/
122 
InstAlloc_InitAll_NULL(INST_ALLOC * pms)123 void    InstAlloc_InitAll_NULL( INST_ALLOC  *pms)
124 {
125     pms[0].TotalSize = 3;
126     pms[0].pNextMember = 0;
127 
128 
129     pms[1].TotalSize = 3;
130     pms[1].pNextMember = 0;
131 
132     pms[2].TotalSize = 3;
133     pms[2].pNextMember = 0;
134 
135     pms[3].TotalSize = 3;
136     pms[3].pNextMember = 0;
137 
138 }
139 
140 
InstAlloc_AddMemberAll(INST_ALLOC * pms,LVM_UINT32 Size[],LVM_MemoryTable_st * pMemoryTable)141 void*   InstAlloc_AddMemberAll( INST_ALLOC                     *pms,
142                                  LVM_UINT32                   Size[],
143                                  LVM_MemoryTable_st           *pMemoryTable)
144 {
145     void *NewMemberAddress; /* Variable to temporarily store the return value */
146 
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]);
149 
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;
153 
154     NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_DATA], Size[LVM_PERSISTENT_FAST_DATA]);
155 
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;
159 
160     NewMemberAddress = InstAlloc_AddMember(&pms[LVM_PERSISTENT_FAST_COEF], Size[LVM_PERSISTENT_FAST_COEF]);
161 
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;
165 
166     NewMemberAddress = InstAlloc_AddMember(&pms[LVM_TEMPORARY_FAST], Size[LVM_TEMPORARY_FAST]);
167 
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;
171 
172     return(NewMemberAddress);
173 }
174 
175 
InstAlloc_AddMemberAllRet(INST_ALLOC * pms,LVM_UINT32 Size[],void ** ptr)176 void*   InstAlloc_AddMemberAllRet(     INST_ALLOC                 *pms,
177                                      LVM_UINT32               Size[],
178                                      void                    **ptr)
179 {
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]);
184 
185     return (ptr[0]);
186 }
187