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/UInt16Arr.h"
21
22/* ------------------------------------------------------------------------- */
23
24/* ========================================================================= */
25/*                                                                           */
26/* ---- \ghd{ auxiliary functions } ---------------------------------------- */
27/*                                                                           */
28/* ========================================================================= */
29
30/* ------------------------------------------------------------------------- */
31
32/* ========================================================================= */
33/*                                                                           */
34/* ---- \ghd{ constructor / destructor } ----------------------------------- */
35/*                                                                           */
36/* ========================================================================= */
37
38/* ------------------------------------------------------------------------- */
39
40void bbs_UInt16Arr_init( struct bbs_Context* cpA,
41						 struct bbs_UInt16Arr* ptrA )
42{
43	ptrA->arrPtrE = NULL;
44	ptrA->sizeE = 0;
45	ptrA->allocatedSizeE = 0;
46	ptrA->mspE = NULL;
47}
48
49/* ------------------------------------------------------------------------- */
50
51void bbs_UInt16Arr_exit( struct bbs_Context* cpA,
52						 struct bbs_UInt16Arr* ptrA )
53{
54	bbs_MemSeg_free( cpA, ptrA->mspE, ptrA->arrPtrE );
55	ptrA->arrPtrE = NULL;
56	ptrA->mspE = NULL;
57	ptrA->sizeE = 0;
58	ptrA->allocatedSizeE = 0;
59}
60
61/* ------------------------------------------------------------------------- */
62
63/* ========================================================================= */
64/*                                                                           */
65/* ---- \ghd{ operators } -------------------------------------------------- */
66/*                                                                           */
67/* ========================================================================= */
68
69/* ------------------------------------------------------------------------- */
70
71void bbs_UInt16Arr_copy( struct bbs_Context* cpA,
72						 struct bbs_UInt16Arr* ptrA,
73						 const struct bbs_UInt16Arr* srcPtrA )
74{
75#ifdef DEBUG1
76	if( ptrA->allocatedSizeE < srcPtrA->allocatedSizeE )
77	{
78		bbs_ERROR0( "void bbs_UInt16Arr_copy( ... ):\n"
79				   "Unsufficient allocated memory in destination array." );
80		return;
81	}
82#endif
83	bbs_UInt16Arr_size( cpA, ptrA, srcPtrA->sizeE );
84	bbs_memcpy16( ptrA->arrPtrE, srcPtrA->arrPtrE, srcPtrA->sizeE * bbs_SIZEOF16( uint16 ) );
85}
86
87/* ------------------------------------------------------------------------- */
88
89flag bbs_UInt16Arr_equal( struct bbs_Context* cpA,
90						  const struct bbs_UInt16Arr* ptrA,
91						  const struct bbs_UInt16Arr* srcPtrA )
92{
93	uint32 iL;
94	const uint16* ptr1L = ptrA->arrPtrE;
95	const uint16* ptr2L = srcPtrA->arrPtrE;
96	if( ptrA->sizeE != srcPtrA->sizeE ) return FALSE;
97	for( iL = ptrA->sizeE; iL > 0; iL-- )
98	{
99		if( *ptr1L++ != *ptr2L++ ) return FALSE;
100	}
101	return TRUE;
102}
103
104/* ------------------------------------------------------------------------- */
105
106/* ========================================================================= */
107/*                                                                           */
108/* ---- \ghd{ query functions } -------------------------------------------- */
109/*                                                                           */
110/* ========================================================================= */
111
112/* ------------------------------------------------------------------------- */
113
114uint32 bbs_UInt16Arr_checkSum( struct bbs_Context* cpA,
115							   const struct bbs_UInt16Arr* ptrA,
116							   uint32 startIndexA, uint32 sizeA )
117{
118	uint32 iL;
119	uint32 sumL = 0;
120	const uint16* ptrL = ptrA->arrPtrE + startIndexA;
121	for( iL = sizeA; iL > 0; iL-- )
122	{
123		sumL += *ptrL++;
124	}
125	return sumL;
126}
127
128/* ------------------------------------------------------------------------- */
129
130uint32 bbs_UInt16Arr_heapSize( struct bbs_Context* cpA,
131							   const struct bbs_UInt16Arr* ptrA,
132							   uint32 sizeA )
133{
134	return sizeA * bbs_SIZEOF16( uint16 ) + bbs_MEM_BLOCK_OVERHD;
135}
136
137/* ------------------------------------------------------------------------- */
138
139/* ========================================================================= */
140/*                                                                           */
141/* ---- \ghd{ modify functions } ------------------------------------------- */
142/*                                                                           */
143/* ========================================================================= */
144
145/* ------------------------------------------------------------------------- */
146
147void bbs_UInt16Arr_size( struct bbs_Context* cpA,
148						 struct bbs_UInt16Arr* ptrA,
149						 uint32 sizeA )
150{
151	if( ptrA->allocatedSizeE < sizeA )
152	{
153		bbs_ERROR1( "void bbs_UInt16Arr_size( struct bbs_UInt16Arr*, uint32 sizeA ):\n"
154				   "Unsufficient allocated memory (allocatedSizeE = '%i')",
155				   ptrA->allocatedSizeE );
156		return;
157	}
158	ptrA->sizeE = sizeA;
159}
160
161/* ------------------------------------------------------------------------- */
162
163void bbs_UInt16Arr_create( struct bbs_Context* cpA,
164						   struct bbs_UInt16Arr* ptrA,
165						   uint32 sizeA,
166						   struct bbs_MemSeg* mspA )
167{
168	if( bbs_Context_error( cpA ) ) return;
169	if( ptrA->sizeE == sizeA ) return;
170	if( ptrA->arrPtrE != 0 )
171	{
172		bbs_UInt16Arr_size( cpA, ptrA, sizeA );
173	}
174	else
175	{
176		ptrA->arrPtrE = bbs_MemSeg_alloc( cpA, mspA, sizeA * bbs_SIZEOF16( uint16 ) );
177		if( bbs_Context_error( cpA ) ) return;
178		ptrA->allocatedSizeE = sizeA;
179		ptrA->sizeE = sizeA;
180		if( !mspA->sharedE ) ptrA->mspE = mspA;
181	}
182}
183
184/* ------------------------------------------------------------------------- */
185
186/* ========================================================================= */
187/*                                                                           */
188/* ---- \ghd{ I/O } -------------------------------------------------------- */
189/*                                                                           */
190/* ========================================================================= */
191
192/* ------------------------------------------------------------------------- */
193
194uint32 bbs_UInt16Arr_memSize( struct bbs_Context* cpA,
195							  const struct bbs_UInt16Arr* ptrA )
196{
197	return bbs_SIZEOF16( uint32 ) + bbs_SIZEOF16( ptrA->sizeE ) +
198										ptrA->sizeE * bbs_SIZEOF16( uint16 );
199}
200
201/* ------------------------------------------------------------------------- */
202
203uint32 bbs_UInt16Arr_memWrite( struct bbs_Context* cpA,
204							   const struct bbs_UInt16Arr* ptrA,
205							   uint16* memPtrA )
206{
207	uint32 memSizeL = bbs_UInt16Arr_memSize( cpA, ptrA );
208	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
209	memPtrA += bbs_memWrite32( &ptrA->sizeE, memPtrA );
210	memPtrA += bbs_memWrite16Arr( cpA, ptrA->arrPtrE, ptrA->sizeE, memPtrA );
211	return memSizeL;
212}
213
214/* ------------------------------------------------------------------------- */
215
216uint32 bbs_UInt16Arr_memRead( struct bbs_Context* cpA,
217							  struct bbs_UInt16Arr* ptrA,
218							  const uint16* memPtrA,
219						      struct bbs_MemSeg* mspA )
220{
221	uint32 memSizeL, sizeL;
222	if( bbs_Context_error( cpA ) ) return 0;
223	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
224	memPtrA += bbs_memRead32( &sizeL, memPtrA );
225	bbs_UInt16Arr_create( cpA, ptrA, sizeL, mspA );
226	memPtrA += bbs_memRead16Arr( cpA, ptrA->arrPtrE, ptrA->sizeE, memPtrA );
227
228	if( memSizeL != bbs_UInt16Arr_memSize( cpA, ptrA ) )
229	{
230		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bbs_UInt16Arr_memRead( const struct bbs_UInt16Arr*, const uint16* ):\n"
231                   "size mismatch" );
232		return 0;
233	}
234	return memSizeL;
235}
236
237/* ------------------------------------------------------------------------- */
238
239/* ========================================================================= */
240/*                                                                           */
241/* ---- \ghd{ exec functions } --------------------------------------------- */
242/*                                                                           */
243/* ========================================================================= */
244
245/* ------------------------------------------------------------------------- */
246
247void bbs_UInt16Arr_fill( struct bbs_Context* cpA,
248						 struct bbs_UInt16Arr* ptrA,
249						 uint16 valA )
250{
251	uint32 iL;
252	for( iL = 0; iL < ptrA->sizeE; iL++ )
253	{
254		ptrA->arrPtrE[ iL ] = valA;
255	}
256}
257
258/* ------------------------------------------------------------------------- */
259
260/* ========================================================================= */
261
262
263