Changeset 9ea58ca for src


Ignore:
Timestamp:
Apr 28, 2016, 3:29:10 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
string
Parents:
8243cf9
Message:

finished translating VbyteSM from C++ to cfa

Location:
src/examples/strings/src/internal
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/examples/strings/src/internal/VbyteSM.c

    r8243cf9 r9ea58ca  
    3838        }
    3939}
     40
     41void MoveThisBefore(HandleNode_t* const this, const HandleNode_t* rhs)
     42{
     43        assertf( this->string > rhs->string,
     44        "VbyteSM: Error - Cannot move byte string at: %lX before byte string at: %lX and keep handles in ascending order",
     45        (unsigned long int)this->string,
     46        (unsigned long int)rhs->string);
     47
     48        HandleNode_t* i;
     49        for( i = rhs->previous;
     50             (uintptr_t)this->string == (uintptr_t)i->string;
     51             i = i->previous);
     52
     53        if( this != i )
     54        {
     55                DeleteNode(this);
     56                AddThisAfter(this, i->previous);
     57        }
     58}
     59
     60int ByteCopy(char_t* Dst, int DstStart, int DstLnth, char_t* Src, int SrcStart, int SrcLnth)
     61{
     62        for ( int i = -1; i < DstLnth - 1; i += 1) {
     63                if( i == SrcLnth -1 ) {
     64                        for ( ; i < DstLnth - 1; i += 1 ) {
     65                                Dst[DstStart + i] = ' ';
     66                        }
     67                        break;
     68                }
     69                Dst[DstStart + i] = Src[SrcStart + i];
     70        }
     71}
     72
     73char_t* VbyteAloc(VbyteHeap* const this, int size ) {
     74        unsigned int NoBytes;
     75        char_t* r;
     76
     77        NoBytes = (uintptr_t)this->EndVbyte + size;
     78        if ( NoBytes > (uintptr_t)this->ExtVbyte ) {
     79                garbage(this);
     80                NoBytes = ( uintptr_t )this->EndVbyte + size;
     81                if ( NoBytes > ( uintptr_t )this->ExtVbyte ) {
     82                        extend( this, size );
     83                }
     84        }
     85        r = this->EndVbyte;
     86        this->EndVbyte += size;
     87        return r;
     88}
     89
     90void compaction(VbyteHeap* const this) {
     91        HandleNode_t* h;
     92        char_t *obase, *nbase, *limit;
     93
     94        this->NoOfCompactions += 1;
     95        this->EndVbyte = this->StartVbyte;
     96        h = this->Header.next;
     97        do {
     98                ByteCopy( this->EndVbyte, 1, h->length, h->string, 1, h->length );
     99                obase = h->string;
     100                h->string = this->EndVbyte;
     101                nbase = h->string;
     102                this->EndVbyte += h->length;
     103                limit = obase + h->length;
     104                h = h->next;
     105               
     106                for (;;) {
     107                        if ( h == &this->Header ) break;
     108                        if ( h->string >= limit ) break;
     109                       
     110                        h->string = nbase + (( uintptr_t )h->string - ( uintptr_t )obase );
     111                        h = h->next;
     112                }
     113        } while( h != &this->Header );
     114}
     115
     116void garbage(VbyteHeap* const this) {
     117        int AmountUsed = 0;
     118        for ( HandleNode_t* i = this->Header.next; i != &this->Header; i = i->next ) {
     119                AmountUsed += i->length;
     120        }
     121       
     122        int AmountFree = ( uintptr_t )this->ExtVbyte - ( uintptr_t )this->StartVbyte - AmountUsed;
     123
     124        if ( AmountFree < ( int )( this->CurrSize * 0.1 )) {
     125                extend( this, this->CurrSize );
     126        }
     127        compaction( this );
     128}
     129
     130void extend( VbyteHeap* const this, int size ) {
     131        this->NoOfExtensions;
     132        char_t* OldStartVbyte = this->StartVbyte;
     133
     134        this->CurrSize += max(size, this->InitSize);
     135        this->StartVbyte = this->EndVbyte = calloc(this->CurrSize);
     136        compaction( this );
     137        free( OldStartVbyte );
     138}
     139
     140void reduce( VbyteHeap* const this, int size ) {
     141        this->NoOfReductions += 1;
     142        char_t* OldStartVbyte = this->StartVbyte;
     143       
     144        this->CurrSize -= size;
     145        this->StartVbyte = this->EndVbyte = calloc(this->CurrSize);
     146        this->ExtVbyte = (void *)( this->StartVbyte + this->CurrSize );
     147        compaction(this);
     148        free( OldStartVbyte );
     149}
  • src/examples/strings/src/internal/VbyteSM.h

    r8243cf9 r9ea58ca  
    7474        this->CurrSize = size;
    7575
    76         #warning missing malloc
    77         this->StartVbyte = 0;
     76        this->StartVbyte = calloc(size);
    7877        this->EndVbyte = this->StartVbyte;
    7978        this->ExtVbyte = (void*)( this->StartVbyte + this->CurrSize );
Note: See TracChangeset for help on using the changeset viewer.