Home | History | Annotate | Download | only in b_BasicEm
      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/UInt8Arr.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 
     40 void bbs_UInt8Arr_init( struct bbs_Context* cpA,
     41 					    struct bbs_UInt8Arr* ptrA )
     42 {
     43 	ptrA->arrPtrE = NULL;
     44 	ptrA->sizeE = 0;
     45 	ptrA->allocatedSizeE = 0;
     46 	ptrA->mspE = NULL;
     47 }
     48 
     49 /* ------------------------------------------------------------------------- */
     50 
     51 void bbs_UInt8Arr_exit( struct bbs_Context* cpA,
     52 					    struct bbs_UInt8Arr* 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 
     71 void bbs_UInt8Arr_copy( struct bbs_Context* cpA,
     72 					    struct bbs_UInt8Arr* ptrA,
     73 						const struct bbs_UInt8Arr* srcPtrA )
     74 {
     75 #ifdef DEBUG1
     76 	if( ptrA->allocatedSizeE < srcPtrA->allocatedSizeE )
     77 	{
     78 		bbs_ERROR0( "void bbs_UInt8Arr_copy(...):\n"
     79 				   "Unsufficient allocated memory in destination array." );
     80 		return;
     81 	}
     82 #endif
     83 	bbs_UInt8Arr_size( cpA, ptrA, srcPtrA->sizeE );
     84 	bbs_memcpy16( ptrA->arrPtrE, srcPtrA->arrPtrE, srcPtrA->sizeE >> 1 );
     85 }
     86 
     87 /* ------------------------------------------------------------------------- */
     88 
     89 flag bbs_UInt8Arr_equal( struct bbs_Context* cpA,
     90 						 const struct bbs_UInt8Arr* ptrA,
     91 						 const struct bbs_UInt8Arr* srcPtrA )
     92 {
     93 	long iL;
     94 	const uint8* ptr1L = ptrA->arrPtrE;
     95 	const uint8* 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 
    114 uint32 bbs_UInt8Arr_heapSize( struct bbs_Context* cpA,
    115 							  const struct bbs_UInt8Arr* ptrA,
    116 							  uint32 sizeA )
    117 {
    118 	return ( sizeA >> 1 ) + bbs_MEM_BLOCK_OVERHD;
    119 }
    120 
    121 /* ------------------------------------------------------------------------- */
    122 
    123 /* ========================================================================= */
    124 /*                                                                           */
    125 /* ---- \ghd{ modify functions } ------------------------------------------- */
    126 /*                                                                           */
    127 /* ========================================================================= */
    128 
    129 /* ------------------------------------------------------------------------- */
    130 
    131 void bbs_UInt8Arr_create( struct bbs_Context* cpA,
    132 						  struct bbs_UInt8Arr* ptrA,
    133 						  uint32 sizeA,
    134 					      struct bbs_MemSeg* mspA )
    135 {
    136 	if( bbs_Context_error( cpA ) ) return;
    137 	if( ptrA->sizeE == sizeA ) return;
    138 	if( ptrA->arrPtrE != 0 )
    139 	{
    140 		bbs_UInt8Arr_size( cpA, ptrA, sizeA );
    141 	}
    142 	else
    143 	{
    144 		/* if size is odd increase by 1 byte */
    145 		uint32 sizeL = sizeA;
    146 		if( ( sizeL & 1 ) != 0 ) sizeL++;
    147 
    148 		ptrA->arrPtrE = bbs_MemSeg_alloc( cpA, mspA, sizeL >> 1 );
    149 		if( bbs_Context_error( cpA ) ) return;
    150 		ptrA->allocatedSizeE = sizeL;
    151 
    152 		ptrA->sizeE = sizeA;
    153 		if( !mspA->sharedE ) ptrA->mspE = mspA;
    154 	}
    155 }
    156 
    157 /* ------------------------------------------------------------------------- */
    158 
    159 void bbs_UInt8Arr_size( struct bbs_Context* cpA,
    160 					    struct bbs_UInt8Arr* ptrA,
    161 						uint32 sizeA )
    162 {
    163 	if( ptrA->allocatedSizeE < sizeA )
    164 	{
    165 		bbs_ERROR1( "void bbs_UInt8Arr_size( struct bbs_UInt8Arr*, uint32 ):\n"
    166 				   "Unsufficient allocated memory (allocatedSizeE = '%i')",
    167 				   ptrA->allocatedSizeE );
    168 		return;
    169 	}
    170 	ptrA->sizeE = sizeA;
    171 }
    172 
    173 /* ------------------------------------------------------------------------- */
    174 
    175 /* ========================================================================= */
    176 /*                                                                           */
    177 /* ---- \ghd{ I/O } -------------------------------------------------------- */
    178 /*                                                                           */
    179 /* ========================================================================= */
    180 
    181 /* ------------------------------------------------------------------------- */
    182 
    183 uint32 bbs_UInt8Arr_memSize( struct bbs_Context* cpA,
    184 							 const struct bbs_UInt8Arr* ptrA )
    185 {
    186 	return bbs_SIZEOF16( uint32 ) + bbs_SIZEOF16( ptrA->sizeE ) +
    187 										ptrA->sizeE / 2; /* int8 = 0.5 word size*/
    188 }
    189 
    190 /* ------------------------------------------------------------------------- */
    191 
    192 uint32 bbs_UInt8Arr_memWrite( struct bbs_Context* cpA,
    193 							  const struct bbs_UInt8Arr* ptrA,
    194 							  uint16* memPtrA )
    195 {
    196 	uint32 memSizeL = bbs_UInt8Arr_memSize( cpA, ptrA );
    197 	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
    198 	memPtrA += bbs_memWrite32( &ptrA->sizeE, memPtrA );
    199 	memPtrA += bbs_memWrite16Arr( cpA, ptrA->arrPtrE, ptrA->sizeE / 2, memPtrA );
    200 	return memSizeL;
    201 }
    202 
    203 /* ------------------------------------------------------------------------- */
    204 
    205 uint32 bbs_UInt8Arr_memRead( struct bbs_Context* cpA,
    206 							 struct bbs_UInt8Arr* ptrA,
    207 							 const uint16* memPtrA,
    208 					         struct bbs_MemSeg* mspA )
    209 {
    210 	uint32 memSizeL, sizeL;
    211 	if( bbs_Context_error( cpA ) ) return 0;
    212 	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
    213 	memPtrA += bbs_memRead32( &sizeL, memPtrA );
    214 	bbs_UInt8Arr_create( cpA, ptrA, sizeL, mspA );
    215 	memPtrA += bbs_memRead16Arr( cpA, ptrA->arrPtrE, ptrA->sizeE / 2, memPtrA );
    216 
    217 	if( memSizeL != bbs_UInt8Arr_memSize( cpA, ptrA ) )
    218 	{
    219 		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bbs_UInt8Arr_memRead( const struct bbs_UInt8Arr*, const uint16* ):\n"
    220                    "size mismatch" );
    221 		return 0;
    222 	}
    223 	return memSizeL;
    224 }
    225 
    226 /* ------------------------------------------------------------------------- */
    227 
    228 /* ========================================================================= */
    229 /*                                                                           */
    230 /* ---- \ghd{ exec functions } --------------------------------------------- */
    231 /*                                                                           */
    232 /* ========================================================================= */
    233 
    234 /* ------------------------------------------------------------------------- */
    235 
    236 void bbs_UInt8Arr_fill( struct bbs_Context* cpA,
    237 					    struct bbs_UInt8Arr* ptrA,
    238 						uint8 valA )
    239 {
    240 	uint32 iL;
    241 	for( iL = 0; iL < ptrA->sizeE; iL++ )
    242 	{
    243 		ptrA->arrPtrE[ iL ] = valA;
    244 	}
    245 }
    246 
    247 /* ------------------------------------------------------------------------- */
    248 
    249 /* ========================================================================= */
    250 
    251 
    252