1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
/*
FALCON - The Falcon Programming Language.
FILE: membuf.cpp
Core memory buffer.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Mon, 17 Mar 2008 23:07:21 +0100
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
Memory buffer - Pure memory for Falcon.
*/
#include <falcon/membuf.h>
#include <falcon/memory.h>
#include <falcon/stream.h>
#include <falcon/common.h>
#include <falcon/vm.h>
#include <string.h>
namespace Falcon {
MemBuf::MemBuf( uint32 ws, uint32 length ):
Garbageable(),
m_length( length ),
m_mark( INVALID_MARK ),
m_limit( length ),
m_position( 0 ),
m_wordSize( ws ),
m_byteOrder(0xFEFF),
m_dependant(0)
{
m_memory = (byte *) memAlloc( length * ws );
m_deletor = memFree;
}
MemBuf::MemBuf( uint32 ws, byte *data, uint32 length ):
Garbageable(),
m_memory( data ),
m_length( length ),
m_mark( INVALID_MARK ),
m_limit( length ),
m_position( 0 ),
m_wordSize( ws ),
m_dependant(0),
m_deletor( 0 )
{
}
MemBuf::MemBuf( uint32 ws, byte *data, uint32 length, tf_deletor deletor ):
Garbageable(),
m_memory( data ),
m_length( length ),
m_mark( INVALID_MARK ),
m_limit( length ),
m_position( 0 ),
m_wordSize( ws ),
m_dependant(0),
m_deletor( deletor )
{
}
MemBuf::~MemBuf()
{
if ( m_deletor != 0 && m_memory != 0 )
m_deletor( m_memory );
}
void MemBuf::setData( byte *data, uint32 size, tf_deletor deletor )
{
if ( m_deletor != 0 && m_memory != 0 )
m_deletor( m_memory );
m_memory = data;
m_length = size/m_wordSize;
m_deletor = deletor;
}
void MemBuf::resize( uint32 newSize )
{
uint32 nsize = newSize * m_wordSize;
m_memory = (byte*) memRealloc( m_memory, nsize );
if ( m_limit > newSize )
{
m_limit = newSize;
if ( m_position > newSize )
{
m_position = newSize;
if ( m_mark > newSize )
{
m_mark = newSize;
}
}
}
m_length = newSize;
}
bool MemBuf::serialize( Stream *stream, bool bLive ) const
{
if ( bLive )
{
// write the live serializer
MemBuf *(*funcptr)( VMachine *vm, Stream *stream ) = MemBuf::deserialize;
stream->write( &funcptr, sizeof( funcptr ) );
}
uint32 wSize = endianInt32( wordSize() );
stream->write( &wSize, sizeof( wSize ) );
if ( ! stream->good() ) return false;
wSize = endianInt32( m_length );
stream->write( &wSize, sizeof( wSize ) );
int16 ws = endianInt16( m_wordSize );
stream->write( &ws, sizeof( ws ) );
if ( ! stream->good() ) return false;
if ( m_length > 0 )
{
stream->write( m_memory, m_length * m_wordSize );
if ( ! stream->good() ) return false;
}
return true;
}
MemBuf *MemBuf::deserialize( VMachine *vm, Stream *stream )
{
uint32 nWordSize;
if ( stream->read( &nWordSize, sizeof( nWordSize ) ) != sizeof( nWordSize ) )
return 0;
nWordSize = endianInt32( nWordSize );
if ( nWordSize < 1 || nWordSize > 4 )
return 0;
uint32 nWS;
if ( stream->read( &nWS, sizeof( nWS ) ) != sizeof( nWS ) )
return 0;
uint32 nSize;
if ( stream->read( &nSize, sizeof( nSize ) ) != sizeof( nSize ) )
return 0;
nSize = (uint32) endianInt32( nSize );
byte *mem = (byte *) memAlloc( nSize * nWS );
if ( mem == 0 )
return 0;
if ( stream->read( mem, nSize*nWS ) != (int32) (nSize*nWS) )
{
memFree( mem );
return 0;
}
switch( nWordSize )
{
case 1: return new MemBuf_1( mem, nSize, memFree );
case 2: return new MemBuf_2( mem, nSize, memFree );
case 3: return new MemBuf_3( mem, nSize, memFree );
case 4: return new MemBuf_4( mem, nSize, memFree );
}
return 0; // impossible
}
MemBuf *MemBuf::create( int bpp, uint32 nSize )
{
switch( bpp )
{
case 1: return new MemBuf_1( nSize );
case 2: return new MemBuf_2( nSize );
case 3: return new MemBuf_3( nSize );
case 4: return new MemBuf_4( nSize );
}
return 0;
}
MemBuf *MemBuf::clone() const
{
byte* data;
if( m_length != 0 )
{
data = (byte*) memAlloc( m_length * m_wordSize );
memcpy( data, m_memory, m_length * m_wordSize );
}
else
data = 0;
switch( m_wordSize )
{
case 1: return new MemBuf_1( data, m_length, memFree );
case 2: return new MemBuf_2( data, m_length, memFree );
case 3: return new MemBuf_3( data, m_length, memFree );
case 4: return new MemBuf_4( data, m_length, memFree );
}
return 0;
}
uint32 MemBuf_1::get( uint32 pos ) const
{
return m_memory[ pos ];
}
void MemBuf_1::set( uint32 pos, uint32 value )
{
m_memory[pos] = (byte) value;
}
uint32 MemBuf_2::get( uint32 pos ) const
{
return endianInt16(((uint16 *)m_memory)[pos]);
}
void MemBuf_2::set( uint32 pos, uint32 value )
{
((uint16 *)m_memory)[pos] = endianInt16((uint16) value);
}
uint32 MemBuf_3::get( uint32 pos ) const
{
byte *p = m_memory + (pos * 3);
// always act as little endian
return p[0] | p[1] << 8 | p[2] << 16;
}
void MemBuf_3::set( uint32 pos, uint32 value )
{
byte *p = m_memory + (pos * 3);
// always act as little endian
p[0] = value & 0xff;
p[1] = (value >> 8) & 0xff;
p[2] = (value >> 16) & 0xff;
}
uint32 MemBuf_4::get( uint32 pos ) const
{
return endianInt32(((uint32 *)m_memory)[pos]);
}
void MemBuf_4::set( uint32 pos, uint32 value )
{
((uint32 *)m_memory)[pos] = endianInt32( (uint32)value);
}
void MemBuf::readProperty( const String &prop, Item &item )
{
VMachine *vm = VMachine::getCurrent();
fassert( vm != 0 );
// try to find a generic method
CoreClass* cc = vm->getMetaClass( FLC_ITEM_MEMBUF );
if ( cc != 0 )
{
uint32 id;
if( cc->properties().findKey( prop, id ) )
{
item = *cc->properties().getValue( id );
item.methodize( this );
return;
}
}
String extra;
item.typeName( extra );
extra.A( '.' ).A( prop );
throw new AccessError( ErrorParam( e_prop_acc, __LINE__ ).extra( extra ) );
}
void MemBuf::writeProperty( const String &prop, const Item &item )
{
throw new AccessError( ErrorParam( e_prop_ro, __LINE__ ).extra( prop ) );
}
void MemBuf::readIndex( const Item &index, Item &target )
{
switch( index.type() )
{
case FLC_ITEM_INT:
{
int64 pos = (int64) index.asInteger();
uint32 uPos = (uint32) (pos >= 0 ? pos : length() + pos);
if ( uPos < length() )
{
target = (int64) get( uPos );
return;
}
}
break;
case FLC_ITEM_NUM:
{
int64 pos = (int64) index.asNumeric();
uint32 uPos = (uint32) (pos >= 0 ? pos : length() + pos);
if ( uPos < length() ) {
target = get( uPos );
return;
}
}
break;
case FLC_ITEM_RANGE:
{
int64 pos = (int64) index.asRangeStart();
int64 end;
int64 step;
if( index.asRangeIsOpen() )
{
if( pos == 0 ) {
target = clone();
return;
}
end = m_length;
}
else
end = index.asRangeEnd();
if ( pos < 0 ) pos += m_length;
if ( end < 0 ) end += m_length;
if( pos >= m_length || end > m_length ) break;
step = index.asRangeStep();
MemBuf* mb;
if ( pos <= end )
{
if ( step < 0 )
{
mb = create( m_wordSize, 0 );
}
else
{
if ( step == 0 ) step = 1;
uint32 size = (uint32) ((end - pos)/step);
mb = create( m_wordSize, size );
for (uint32 i = 0; pos < end; i++, pos += step ) {
mb->set(i, get((uint32)pos));
}
}
}
else
{
if ( step > 0 )
{
mb = create( m_wordSize, 0 );
}
else
{
if ( step == 0 ) step = -1;
uint32 size = (uint32) ((pos-end)/(-step))+1;
mb = create( m_wordSize, size );
for (uint32 i = 0; pos >= end; i++, pos += step ) {
mb->set(i, get((uint32)pos));
}
}
}
target = mb;
return;
}
break;
case FLC_ITEM_REFERENCE:
readIndex( index.asReference()->origin(), target );
return;
}
throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "LDV" ) );
}
void MemBuf::writeIndex( const Item &index, const Item &target )
{
uint32 data;
switch( target.type() )
{
case FLC_ITEM_INT: data = (uint32) target.asInteger(); break;
case FLC_ITEM_NUM: data = (uint32) target.asNumeric(); break;
case FLC_ITEM_REFERENCE:
writeIndex( index, target.asReference()->origin() );
return;
default:
throw new TypeError( ErrorParam( e_param_type, __LINE__ ).extra( "STV" ) );
}
switch( index.type() )
{
case FLC_ITEM_INT:
{
int64 pos = (int64) index.asInteger();
uint32 uPos = (uint32) (pos >= 0 ? pos : length() + pos);
if ( uPos < length() )
{
set( uPos, data );
return;
}
}
break;
case FLC_ITEM_NUM:
{
int64 pos = (int64) index.asNumeric();
uint32 uPos = (uint32) (pos >= 0 ? pos : length() + pos);
if ( uPos < length() ) {
set( uPos, data );
return;
}
}
break;
case FLC_ITEM_REFERENCE:
writeIndex( index.asReference()->origin(), target );
return;
}
throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "STV" ) );
}
void MemBuf::compact()
{
uint32 count = remaining();
if ( count > 0 )
{
memmove( m_memory, m_memory + (m_position * m_wordSize), count * m_wordSize );
}
m_mark = INVALID_MARK;
m_position = count;
m_limit = m_length;
}
void MemBuf::gcMark( uint32 gen )
{
if ( mark() != gen )
{
mark( gen );
// small optimization; resolve the problem here instead of looping again.
if( dependant() != 0 && dependant()->mark() != gen )
{
dependant()->gcMark( gen );
}
}
}
}
/* end of membuf.cpp */
|