Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/collections/string_res.cfa

    rb7caceb r2f16569  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon May 25 16:22:58 2026
    13 // Update Count     : 316
     12// Last Modified On : Thu May  7 13:14:45 2026
     13// Update Count     : 135
    1414//
    1515
     
    2929//######################### VbyteHeap "header" #########################
    3030
    31 //#define VbyteDebug
    32 #ifdef VbyteDebug
    33 HandleNode * HeaderPtr;
    34 
    35 void printHandleList( HandleNode & s ) with(s) {
    36         printf( "HandleList: Header %p\n", HeaderPtr );
    37         for ( HandleNode * ni = HeaderPtr;; ) {                         // print header node
    38                 printf( "\tnode:%p lnth:%u s:%p, \"", ni, ni->lnth, ni->s );
    39                 for ( i; ni->lnth ) {
    40                         printf( "%c", ni->s[i] );
    41                 } // for
    42                 printf( "\" flink:%p blink:%p\n", ni->flink, ni->blink );
    43                 ni = ni->flink;
    44           if ( ni == HeaderPtr ) break;
    45         } // for
    46 }
     31#ifdef VbyteDebug
     32HandleNode *HeaderPtr;
    4733#endif // VbyteDebug
    4834
     
    5440        int InitSize;                                                                           // initial number of bytes in the byte-string area
    5541        int CurrSize;                                                                           // current number of bytes in the byte-string area
    56         char * StartVbyte;                                                                      // pointer to the `st byte of the start of the byte-string area
    57         char * EndVbyte;                                                                        // pointer to the next byte after the end of the currently used portion of byte-string area
    58         void * ExtVbyte;                                                                        // pointer to the next byte after the end of the byte-string area
     42        char *StartVbyte;                                                                       // pointer to the `st byte of the start of the byte-string area
     43        char *EndVbyte;                                                                         // pointer to the next byte after the end of the currently used portion of byte-string area
     44        void *ExtVbyte;                                                                         // pointer to the next byte after the end of the byte-string area
    5945
    6046        HandleNode Header;                                                                      // header node for handle list
     
    7662static void AddThisAfter( HandleNode &, HandleNode & );
    7763static void DeleteNode( HandleNode & );
    78 static void MoveThisAfter( HandleNode &, HandleNode & ); // move current handle after parameter handle
     64static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle
    7965
    8066
     
    8369static void ?{}( VbyteHeap & s, size_t Size ) with(s) {
    8470#ifdef VbyteDebug
    85         printf( "enter:VbyteHeap::VbyteHeap, s:%p Size:%zd\n", &s, Size );
     71        serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size;
    8672#endif // VbyteDebug
    8773        NoOfCompactions = NoOfExtensions = NoOfReductions = 0;
     
    9177        Header.flink = Header.blink = &Header;
    9278        Header.ulink = &s;
    93         // Should be unnecessary to initialize.
    94         // Header.s = 0p;
    95         // Header.lnth = 0;
    9679#ifdef VbyteDebug
    9780        HeaderPtr = &Header;
    98         printf( "exit:VbyteHeap::VbyteHeap, s:%p\n", &s );
     81        serr | "exit:VbyteHeap::VbyteHeap, s:" | &s;
    9982#endif // VbyteDebug
    10083} // VbyteHeap
     
    11699static void ?{}( HandleNode & s ) with(s) {
    117100#ifdef VbyteDebug
    118         printf( "enter:HandleNode::HandleNode, s:%p\n", &s );
    119 #endif // VbyteDebug
    120         // Should be unnecessary to initialize.
    121         // s = 0p;
    122         // lnth = 0;
    123 #ifdef VbyteDebug
    124         printf( "exit:HandleNode::HandleNode, s:%p\n", &s );
     101        serr | "enter:HandleNode::HandleNode, s:" | &s;
     102#endif // VbyteDebug
     103        s = 0;
     104        lnth = 0;
     105#ifdef VbyteDebug
     106        serr | "exit:HandleNode::HandleNode, s:" | &s;
    125107#endif // VbyteDebug
    126108} // HandleNode
     
    132114static void ?{}( HandleNode & s, VbyteHeap & vh ) with(s) {
    133115#ifdef VbyteDebug
    134         printf( "enter:HandleNode::HandleNode, s:%p\n", &s );
     116        serr | "enter:HandleNode::HandleNode, s:" | &s;
    135117#endif // VbyteDebug
    136118        s = 0;
     
    139121        AddThisAfter( s, *vh.Header.blink );
    140122#ifdef VbyteDebug
    141         printf( "exit:HandleNode::HandleNode, s:%p\n", &s );
     123        serr | "exit:HandleNode::HandleNode, s:" | &s;
    142124#endif // VbyteDebug
    143125} // HandleNode
     
    149131static void ^?{}( HandleNode & s ) with(s) {
    150132#ifdef VbyteDebug
    151         printf( "enter:HandleNode::~HandleNode, s:%p\n", &s );
     133        serr | "enter:HandleNode::~HandleNode, s:" | & s;
    152134        {
    153                 printf( " lnth:%u s:%p,\"", lnth, (void *)s );
     135                serr | nlOff;
     136                serr | " lnth:" | lnth | " s:" | (void *)s | ",\"";
    154137                for ( i; lnth ) {
    155                         printf( "%c", s[i] );
     138                        serr | s[i];
    156139                } // for
    157                 printf( "\" flink:%p blink:%p\n", flink, blink );
     140                serr | "\" flink:" | flink | " blink:" | blink | nl;
     141                serr | nlOn;
    158142        }
    159143#endif // VbyteDebug
     
    192176
    193177VbyteHeap * DEBUG_string_heap() {
    194 //      assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );
     178        assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );
    195179        return ambient_string_sharectx->activeHeap;
    196180}
     
    346330        s.shareSet_prev = &s;
    347331        s.shareSet_next = &s;
    348 }
     332                }
    349333
    350334static void eagerCopyCtorHelper(string_res & s, const char * rhs, size_t rhslnth) with(s) {
     
    356340                (shareSet_owns_ulink){ true };
    357341        }
    358         Handle.s = VbyteAlloc( *Handle.ulink, rhslnth );
     342        Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
    359343        Handle.lnth = rhslnth;
    360344        memmove( Handle.s, rhs, rhslnth );
     
    408392void ?{}( string_res & s, VbyteHeap & heap, const char * rhs, size_t rhslnth ) with(s) {
    409393        (Handle){ heap };
    410         Handle.s = VbyteAlloc( *Handle.ulink, rhslnth );
     394        Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
    411395        Handle.lnth = rhslnth;
    412396        (s.shareSet_owns_ulink){ false };
     
    414398        s.shareSet_prev = &s;
    415399        s.shareSet_next = &s;
    416 #ifdef VbyteDebug
    417         printHandleList( s.Handle );
    418         printf( "exit:constructor, s:%p\n", &s );
    419 #endif // VbyteDebug
    420400}
    421401
     
    555535
    556536static string_res & assign_(string_res & s, const char * buffer, size_t bsize, const string_res & valSrc) {
    557 #ifdef VbyteDebug
    558         printf( "enter:assign_\n" );
    559         printHandleList( s.Handle );
    560 #endif // VbyteDebug
    561 
    562537        string_res * shareSetStartPeer;
    563538        string_res * shareSetEndPeer;
     
    639614        }
    640615
    641 #ifdef VbyteDebug
    642         printHandleList( s.Handle );
    643         printf( "exit:assign_\n" );
    644 #endif // VbyteDebug
    645 
    646616        return s;
    647617}
     
    738708
    739709void append(string_res & str1, const char * buffer, size_t bsize) {
    740 #ifdef VbyteDebug
    741         printf( "enter append %p %p %zd\n", &str1, buffer, bsize );
    742         printHandleList( str1.Handle );
    743 #endif // VbyteDebug
    744710        size_t clnth = str1.Handle.lnth + bsize;
    745711        if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ?
    746712                // no-op
    747713        } else {                                                                                        // must copy some text
    748 //              if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?
    749                 if ( str1.Handle.s + str1.Handle.lnth == str1.Handle.ulink->EndVbyte ) { // str1 at end of string area ?
     714                if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?
    750715                        VbyteAlloc( *str1.Handle.ulink, bsize );        // create room for 2nd part at the end of string area
    751716                } else {                                                                                // copy the two parts
     
    758723        } // if
    759724        str1.Handle.lnth = clnth;
    760 #ifdef VbyteDebug
    761         printHandleList( str1.Handle );
    762         printf( "exit append\n" );
    763 #endif // VbyteDebug
    764725}
    765726
     
    977938static void AddThisAfter( HandleNode & s, HandleNode & n ) with(s) {
    978939#ifdef VbyteDebug
    979         printf( "enter:AddThisAfter, s:%p n:%p\n", &s, &n );
     940        serr | "enter:AddThisAfter, s:" | &s | " n:" | &n;
    980941#endif // VbyteDebug
    981942        // Performance note: we are on the critical path here. MB has ensured that the verifies don't contribute to runtime (are compiled away, like they're supposed to be).
     
    987948        n.flink = &s;
    988949#ifdef VbyteDebug
    989         printHandleList( s );
    990         printf( "exit:AddThisAfter\n" );
     950        {
     951                serr | "HandleList:";
     952                serr | nlOff;
     953                for ( HandleNode *ni = HeaderPtr->flink; ni != HeaderPtr; ni = ni->flink ) {
     954                        serr | "\tnode:" | ni | " lnth:" | ni->lnth | " s:" | (void *)ni->s | ",\"";
     955                        for ( i; ni->lnth ) {
     956                                serr | ni->s[i];
     957                        } // for
     958                        serr | "\" flink:" | ni->flink | " blink:" | ni->blink | nl;
     959                } // for
     960                serr | nlOn;
     961        }
     962        serr | "exit:AddThisAfter";
    991963#endif // VbyteDebug
    992964} // AddThisAfter
     
    997969static void DeleteNode( HandleNode & s ) with(s) {
    998970#ifdef VbyteDebug
    999         printf( "enter:DeleteNode, s:%p\n", &s );
    1000         printHandleList( s );
     971        serr | "enter:DeleteNode, s:" | &s;
    1001972#endif // VbyteDebug
    1002973        flink->blink = blink;
    1003974        blink->flink = flink;
    1004975#ifdef VbyteDebug
    1005         printHandleList( s );
    1006         printf( "exit:DeleteNode\n" );
     976        serr | "exit:DeleteNode";
    1007977#endif // VbyteDebug
    1008978} //  DeleteNode
     
    1014984static char * VbyteAlloc( VbyteHeap & s, int size ) with(s) {
    1015985#ifdef VbyteDebug
    1016         printf( "enter:VbyteAlloc, size:%d\n", size );
     986        serr | "enter:VbyteAlloc, size:" | size;
    1017987#endif // VbyteDebug
    1018988        uintptr_t NoBytes;
     
    1027997        EndVbyte += size;
    1028998#ifdef VbyteDebug
    1029         printf( "exit:VbyteAlloc, r:%p EndVbyte:%p ExtVbyte:%p\n", (void *)r, (void *)EndVbyte, ExtVbyte );
     999        serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;
    10301000#endif // VbyteDebug
    10311001        return r;
     
    10591029
    10601030
    1061 // Move HandleNode node s somewhere after the HandleNode node h so that it is in ascending order by the address in the
    1062 // byte string area.
    1063 
    1064 static void MoveThisAfter( HandleNode & s, HandleNode & h ) with(s) {
    1065 #ifdef VbyteDebug
    1066         printf( "enter:MoveThisAfter, s:%p h:%p\n", &s, &h );
     1031// Move an existing HandleNode node h somewhere after the current HandleNode node so that it is in ascending order by
     1032// the address in the byte string area.
     1033
     1034static void MoveThisAfter( HandleNode & s, const HandleNode & h ) with(s) {
     1035#ifdef VbyteDebug
     1036        serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h;
    10671037#endif // VbyteDebug
    10681038        verify( h.ulink != 0p );
    10691039        verify( s.ulink == h.ulink );
    1070         verify( s < h.s && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order" );
     1040        if ( s < h.s ) {                                        // check argument values
     1041                // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:"
     1042                //        | ( h->s ) | " and keep handles in ascending order";
     1043                // exit(-1 );
     1044                verify( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order");
     1045        } // if
    10711046
    10721047        HandleNode *i;
    1073         for ( i = h.flink; i != &s.ulink->Header && s > i->s; i = i->flink ); // find the position for this node after h
    1074         if ( &s != i->blink ) {                                                         // s just after h ? => nothing to move
     1048        for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h
     1049        if ( & s != i->blink ) {
    10751050                DeleteNode( s );
    10761051                AddThisAfter( s, *i->blink );
    10771052        } // if
    10781053#ifdef VbyteDebug
    1079         printHandleList( s );
    1080         printf( "exit:MoveThisAfter\n" );
     1054        {
     1055                serr | "HandleList:";
     1056                serr | nlOff;
     1057                for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) {
     1058                        serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
     1059                        for ( i; n->lnth ) {
     1060                                serr | n->s[i];
     1061                        } // for
     1062                        serr | "\" flink:" | n->flink | " blink:" | n->blink | nl;
     1063                } // for
     1064                serr | nlOn;
     1065        }
     1066        serr | "exit:MoveThisAfter";
    10811067#endif // VbyteDebug
    10821068} // MoveThisAfter
     
    10931079int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth )  {
    10941080#ifdef VbyteDebug
    1095         printf( "enter:ByteCmp, Src1Start:%d Src1Lnth:%d Src2Start:%d Src2Lnth:%d", Src1Start, Src1Lnth, Src2Start, Src2Lnth );
     1081        serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth;
    10961082#endif // VbyteDebug
    10971083        int cmp;
     
    11291115        } // for
    11301116#ifdef VbyteDebug
    1131         printf( "exit:ByteCmp, cmp:%d\n", cmp );
     1117        serr | "exit:ByteCmp, cmp:" | cmp;
    11321118#endif // VbyteDebug
    11331119        return cmp;
     
    11401126// the containing string has been moved. Hence, they only require that their string pointers be adjusted.
    11411127
    1142 void compaction( VbyteHeap & s ) with(s) {
    1143 #ifdef VbyteDebug
    1144         printf( "enter compaction\n" );
    1145         printHandleList( Header );
    1146 #endif // VbyteDebug
    1147         char * obase, * nbase, * limit;
     1128void compaction(VbyteHeap & s) with(s) {
     1129        HandleNode *h;
     1130        char *obase, *nbase, *limit;
    11481131       
    11491132        NoOfCompactions += 1;
    11501133        EndVbyte = StartVbyte;
    1151         for ( HandleNode * h = Header.flink; h != &Header; ) {
     1134        h = Header.flink;                                       // ignore header node
     1135        for () {
    11521136                memmove( EndVbyte, h->s, h->lnth );
    11531137                obase = h->s;
     
    11571141                limit = obase + h->lnth;
    11581142                h = h->flink;
     1143               
    11591144                // check if any substrings are allocated within a string
    1160                 for ( ; h != &Header; h = h->flink ) {
    1161                         if ( h->s >= limit )  break;                            // outside of current string ?
     1145               
     1146                for () {
     1147                        if ( h == &Header ) break;                      // end of header list ?
     1148                        if ( h->s >= limit ) break;                     // outside of current string ?
    11621149                        h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase );
     1150                        h = h->flink;
    11631151                } // for
     1152                if ( h == &Header ) break;                      // end of header list ?
    11641153        } // for
    1165 #ifdef VbyteDebug
    1166         printHandleList( Header );
    1167         printf( "exit compaction\n" );
    1168 #endif // VbyteDebug
    11691154} // compaction
    11701155
     
    11811166// the heap.  The heap is then compacted in the existing heap or into the newly allocated heap.
    11821167
    1183 void garbage( VbyteHeap & s, int minreq ) with(s) {
    1184 #ifdef VbyteDebug
    1185         printf( "enter:garbage\n" );
    1186         printHandleList( Header );
     1168void garbage(VbyteHeap & s, int minreq ) with(s) {
     1169#ifdef VbyteDebug
     1170        serr | "enter:garbage";
     1171        {
     1172                serr | "HandleList:";
     1173                for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
     1174                        serr | nlOff;
     1175                        serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
     1176                        for ( i; n->lnth ) {
     1177                                serr | n->s[i];
     1178                        } // for
     1179                        serr | nlOn;
     1180                        serr | "\" flink:" | n->flink | " blink:" | n->blink;
     1181                } // for
     1182        }
    11871183#endif // VbyteDebug
    11881184        int AmountUsed, AmountFree;
     
    12081204        }// if
    12091205#ifdef VbyteDebug
    1210         printHandleList( Header );
    1211         printf( "exit:garbage\n" );
     1206        {
     1207                serr | "HandleList:";
     1208                for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
     1209                        serr | nlOff;
     1210                        serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
     1211                        for ( i; n->lnth ) {
     1212                                serr | n->s[i];
     1213                        } // for
     1214                        serr | nlOn;
     1215                        serr | "\" flink:" | n->flink | " blink:" | n->blink;
     1216                } // for
     1217        }
     1218        serr | "exit:garbage";
    12121219#endif // VbyteDebug
    12131220} // garbage
     1221
     1222#undef VbyteDebug
     1223
    12141224
    12151225
     
    12191229void extend( VbyteHeap & s, int size ) with (s) {
    12201230#ifdef VbyteDebug
    1221         printf( "enter:extend, size:%d", size );
     1231        serr | "enter:extend, size:" | size;
    12221232#endif // VbyteDebug
    12231233        char *OldStartVbyte;
     
    12321242        free( OldStartVbyte );                          // release old heap
    12331243#ifdef VbyteDebug
    1234         printf( "exit:extend, CurrSize: %d\n", CurrSize );
     1244        serr | "exit:extend, CurrSize:" | CurrSize;
    12351245#endif // VbyteDebug
    12361246} // extend
     
    12381248//WIP
    12391249#if 0
     1250
    12401251// Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
    12411252// area is deleted.
     
    12431254void VbyteHeap::reduce( int size ) {
    12441255#ifdef VbyteDebug
    1245         printf( "enter:reduce, size: %d", size );
     1256        serr | "enter:reduce, size:" | size;
    12461257#endif // VbyteDebug
    12471258        char *OldStartVbyte;
     
    12561267        delete  OldStartVbyte;                          // release old heap
    12571268#ifdef VbyteDebug
    1258         printf( "exit:reduce, CurrSize: %d", CurrSize );
     1269        !serr | "exit:reduce, CurrSize:" | CurrSize;
    12591270#endif // VbyteDebug
    12601271} // reduce
    1261 #endif // 0
     1272
     1273
     1274#endif
Note: See TracChangeset for help on using the changeset viewer.