1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* ---- includes ----------------------------------------------------------- */
18 
19 #include "b_BasicEm/Functions.h"
20 #include "b_BasicEm/Int16Arr.h"
21 
22 #ifndef bbs_TYPES_64_AVAILABLE
23 #include <stdint.h>
24 #endif
25 
26 /* ------------------------------------------------------------------------- */
27 
28 /* ========================================================================= */
29 /*                                                                           */
30 /* ---- \ghd{ auxiliary functions } ---------------------------------------- */
31 /*                                                                           */
32 /* ========================================================================= */
33 
34 /* ------------------------------------------------------------------------- */
35 
36 /* ========================================================================= */
37 /*                                                                           */
38 /* ---- \ghd{ constructor / destructor } ----------------------------------- */
39 /*                                                                           */
40 /* ========================================================================= */
41 
42 /* ------------------------------------------------------------------------- */
43 
bbs_Int16Arr_init(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA)44 void bbs_Int16Arr_init( struct bbs_Context* cpA,
45 					    struct bbs_Int16Arr* ptrA )
46 {
47 	ptrA->arrPtrE = NULL;
48 	ptrA->sizeE = 0;
49 	ptrA->allocatedSizeE = 0;
50 	ptrA->mspE = NULL;
51 }
52 
53 /* ------------------------------------------------------------------------- */
54 
bbs_Int16Arr_exit(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA)55 void bbs_Int16Arr_exit( struct bbs_Context* cpA,
56 					    struct bbs_Int16Arr* ptrA )
57 {
58 	bbs_MemSeg_free( cpA, ptrA->mspE, ptrA->arrPtrE );
59 	ptrA->arrPtrE = NULL;
60 	ptrA->mspE = NULL;
61 	ptrA->sizeE = 0;
62 	ptrA->allocatedSizeE = 0;
63 }
64 
65 /* ------------------------------------------------------------------------- */
66 
67 /* ========================================================================= */
68 /*                                                                           */
69 /* ---- \ghd{ operators } -------------------------------------------------- */
70 /*                                                                           */
71 /* ========================================================================= */
72 
73 /* ------------------------------------------------------------------------- */
74 
bbs_Int16Arr_copy(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA,const struct bbs_Int16Arr * srcPtrA)75 void bbs_Int16Arr_copy( struct bbs_Context* cpA,
76 					    struct bbs_Int16Arr* ptrA,
77 						const struct bbs_Int16Arr* srcPtrA )
78 {
79 #ifdef DEBUG1
80 	if( ptrA->allocatedSizeE < srcPtrA->sizeE )
81 	{
82 		bbs_ERROR0( "void bbs_Int16Arr_copy(...):\n"
83 				   "Insufficient allocated memory in destination array." );
84 		return;
85 	}
86 #endif
87 	bbs_Int16Arr_size( cpA, ptrA, srcPtrA->sizeE );
88 	bbs_memcpy16( ptrA->arrPtrE, srcPtrA->arrPtrE, srcPtrA->sizeE * bbs_SIZEOF16( int16 ) );
89 }
90 
91 /* ------------------------------------------------------------------------- */
92 
bbs_Int16Arr_equal(struct bbs_Context * cpA,const struct bbs_Int16Arr * ptrA,const struct bbs_Int16Arr * srcPtrA)93 flag bbs_Int16Arr_equal( struct bbs_Context* cpA,
94 						 const struct bbs_Int16Arr* ptrA,
95 						 const struct bbs_Int16Arr* srcPtrA )
96 {
97 	uint32 iL;
98 	const int16* ptr1L = ptrA->arrPtrE;
99 	const int16* ptr2L = srcPtrA->arrPtrE;
100 	if( ptrA->sizeE != srcPtrA->sizeE ) return FALSE;
101 	for( iL = ptrA->sizeE; iL > 0; iL-- )
102 	{
103 		if( *ptr1L++ != *ptr2L++ ) return FALSE;
104 	}
105 	return TRUE;
106 }
107 
108 /* ------------------------------------------------------------------------- */
109 
110 /* ========================================================================= */
111 /*                                                                           */
112 /* ---- \ghd{ query functions } -------------------------------------------- */
113 /*                                                                           */
114 /* ========================================================================= */
115 
116 /* ------------------------------------------------------------------------- */
117 
bbs_Int16Arr_heapSize(struct bbs_Context * cpA,const struct bbs_Int16Arr * ptrA,uint32 sizeA)118 uint32 bbs_Int16Arr_heapSize( struct bbs_Context* cpA,
119 							  const struct bbs_Int16Arr* ptrA,
120 							  uint32 sizeA )
121 {
122 	return sizeA * bbs_SIZEOF16( int16 ) + bbs_MEM_BLOCK_OVERHD;
123 }
124 
125 /* ------------------------------------------------------------------------- */
126 
127 /* ========================================================================= */
128 /*                                                                           */
129 /* ---- \ghd{ modify functions } ------------------------------------------- */
130 /*                                                                           */
131 /* ========================================================================= */
132 
133 /* ------------------------------------------------------------------------- */
134 
bbs_Int16Arr_create(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA,uint32 sizeA,struct bbs_MemSeg * mspA)135 void bbs_Int16Arr_create( struct bbs_Context* cpA,
136 						  struct bbs_Int16Arr* ptrA,
137 						  uint32 sizeA,
138 						  struct bbs_MemSeg* mspA )
139 {
140 	if( bbs_Context_error( cpA ) ) return;
141 	if( ptrA->sizeE == sizeA ) return;
142 	if( ptrA->arrPtrE != 0 )
143 	{
144 		bbs_Int16Arr_size( cpA, ptrA, sizeA );
145 	}
146 	else
147 	{
148 		ptrA->arrPtrE = bbs_MemSeg_alloc( cpA, mspA, sizeA * bbs_SIZEOF16( int16 ) );
149 		if( bbs_Context_error( cpA ) ) return;
150 		ptrA->allocatedSizeE = sizeA;
151 		ptrA->sizeE = sizeA;
152 		if( !mspA->sharedE ) ptrA->mspE = mspA;
153 	}
154 }
155 
156 /* ------------------------------------------------------------------------- */
157 
bbs_Int16Arr_createAligned(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA,uint32 sizeA,struct bbs_MemSeg * mspA,struct bbs_Int16Arr * allocPtrA,uint32 alignBytesA)158 void bbs_Int16Arr_createAligned( struct bbs_Context* cpA,
159 								 struct bbs_Int16Arr* ptrA,
160 								 uint32 sizeA,
161 								 struct bbs_MemSeg* mspA,
162 								 struct bbs_Int16Arr* allocPtrA,
163 								 uint32 alignBytesA )
164 {
165 	/* allocate extra memory for alignment */
166 	bbs_Int16Arr_create( cpA, allocPtrA, sizeA + ( ( alignBytesA - 1 ) >> 1 ), mspA );
167 
168 	/* set members of ptrA */
169 	ptrA->mspE = 0; /* no own allocated memory */
170 	ptrA->sizeE = sizeA;
171 	ptrA->allocatedSizeE = ptrA->sizeE;
172 	ptrA->arrPtrE = allocPtrA->arrPtrE;
173 
174 #if defined( WIN32 ) || defined( _WIN32_WCE )
175 	/* disable warning "pointer truncation...": */
176 	#pragma warning( disable : 4311 )
177 #endif
178 
179 
180 	/* align memory */
181 #ifdef bbs_TYPES_64_AVAILABLE
182 
183 	while( ( ( ( uint64 ) ptrA->arrPtrE ) & ( alignBytesA - 1 ) ) )
184 	{
185 		ptrA->arrPtrE++;
186 	}
187 #else
188 	while( ( ( ( uintptr_t ) ptrA->arrPtrE ) & ( alignBytesA - 1 ) ) )
189 	{
190 		ptrA->arrPtrE++;
191 	}
192 #endif
193 
194 }
195 
196 /* ------------------------------------------------------------------------- */
197 
bbs_Int16Arr_size(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA,uint32 sizeA)198 void bbs_Int16Arr_size( struct bbs_Context* cpA,
199 					    struct bbs_Int16Arr* ptrA,
200 						uint32 sizeA )
201 {
202 	if( ptrA->allocatedSizeE < sizeA )
203 	{
204 		bbs_ERROR1( "void bbs_Int16Arr_size( struct bbs_Int16Arr*, uint32 sizeA ):\n"
205 				   "Unsufficient allocated memory (allocatedSizeE = '%i')",
206 				   ptrA->allocatedSizeE );
207 		return;
208 	}
209 	ptrA->sizeE = sizeA;
210 }
211 
212 /* ------------------------------------------------------------------------- */
213 
214 /* ========================================================================= */
215 /*                                                                           */
216 /* ---- \ghd{ I/O } -------------------------------------------------------- */
217 /*                                                                           */
218 /* ========================================================================= */
219 
220 /* ------------------------------------------------------------------------- */
221 
bbs_Int16Arr_memSize(struct bbs_Context * cpA,const struct bbs_Int16Arr * ptrA)222 uint32 bbs_Int16Arr_memSize( struct bbs_Context* cpA,
223 							 const struct bbs_Int16Arr* ptrA )
224 {
225 	return bbs_SIZEOF16( uint32 ) + bbs_SIZEOF16( ptrA->sizeE ) +
226 										ptrA->sizeE * bbs_SIZEOF16( int16 );
227 }
228 
229 /* ------------------------------------------------------------------------- */
230 
bbs_Int16Arr_memWrite(struct bbs_Context * cpA,const struct bbs_Int16Arr * ptrA,uint16 * memPtrA)231 uint32 bbs_Int16Arr_memWrite( struct bbs_Context* cpA,
232 							  const struct bbs_Int16Arr* ptrA,
233 							  uint16* memPtrA )
234 {
235 	uint32 memSizeL = bbs_Int16Arr_memSize( cpA, ptrA );
236 	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
237 	memPtrA += bbs_memWrite32( &ptrA->sizeE, memPtrA );
238 	memPtrA += bbs_memWrite16Arr( cpA, ptrA->arrPtrE, ptrA->sizeE, memPtrA );
239 	return memSizeL;
240 }
241 
242 /* ------------------------------------------------------------------------- */
243 
bbs_Int16Arr_memRead(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA,const uint16 * memPtrA,struct bbs_MemSeg * mspA)244 uint32 bbs_Int16Arr_memRead( struct bbs_Context* cpA,
245 							 struct bbs_Int16Arr* ptrA,
246 							 const uint16* memPtrA,
247 							 struct bbs_MemSeg* mspA )
248 {
249 	uint32 memSizeL, sizeL;
250 	if( bbs_Context_error( cpA ) ) return 0;
251 	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
252 	memPtrA += bbs_memRead32( &sizeL, memPtrA );
253 	bbs_Int16Arr_create( cpA, ptrA, sizeL, mspA );
254 	memPtrA += bbs_memRead16Arr( cpA, ptrA->arrPtrE, ptrA->sizeE, memPtrA );
255 
256 	if( memSizeL != bbs_Int16Arr_memSize( cpA, ptrA ) )
257 	{
258 		bbs_ERR0( bbs_ERR_CORRUPT_DATA,
259 				  "uint32 bbs_Int16Arr_memRead( const struct bbs_Int16Arr*, const uint16* ):\n"
260                   "size mismatch" );
261 		return 0;
262 	}
263 	return memSizeL;
264 }
265 
266 /* ------------------------------------------------------------------------- */
267 
268 /* ========================================================================= */
269 /*                                                                           */
270 /* ---- \ghd{ exec functions } --------------------------------------------- */
271 /*                                                                           */
272 /* ========================================================================= */
273 
274 /* ------------------------------------------------------------------------- */
275 
bbs_Int16Arr_fill(struct bbs_Context * cpA,struct bbs_Int16Arr * ptrA,int16 valA)276 void bbs_Int16Arr_fill( struct bbs_Context* cpA,
277 					    struct bbs_Int16Arr* ptrA,
278 						int16 valA )
279 {
280 	uint32 iL;
281 	for( iL = 0; iL < ptrA->sizeE; iL++ )
282 	{
283 		ptrA->arrPtrE[ iL ] = valA;
284 	}
285 }
286 
287 /* ------------------------------------------------------------------------- */
288 
289 /* ========================================================================= */
290 
291 
292