Changeset b7caceb for libcfa


Ignore:
Timestamp:
May 25, 2026, 4:40:18 PM (40 hours ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
893fb47
Parents:
bc547d3
Message:

fix loops incorrectly accessing header node, change from serr to printf for debug

Location:
libcfa/src/collections
Files:
2 edited

Legend:

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

    rbc547d3 rb7caceb  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu May  7 13:14:45 2026
    13 // Update Count     : 135
     12// Last Modified On : Mon May 25 16:22:58 2026
     13// Update Count     : 316
    1414//
    1515
     
    2929//######################### VbyteHeap "header" #########################
    3030
    31 #ifdef VbyteDebug
    32 HandleNode *HeaderPtr;
     31//#define VbyteDebug
     32#ifdef VbyteDebug
     33HandleNode * HeaderPtr;
     34
     35void 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}
    3347#endif // VbyteDebug
    3448
     
    4054        int InitSize;                                                                           // initial number of bytes in the byte-string area
    4155        int CurrSize;                                                                           // current number of bytes in 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
     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
    4559
    4660        HandleNode Header;                                                                      // header node for handle list
     
    6276static void AddThisAfter( HandleNode &, HandleNode & );
    6377static void DeleteNode( HandleNode & );
    64 static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle
     78static void MoveThisAfter( HandleNode &, HandleNode & ); // move current handle after parameter handle
    6579
    6680
     
    6983static void ?{}( VbyteHeap & s, size_t Size ) with(s) {
    7084#ifdef VbyteDebug
    71         serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size;
     85        printf( "enter:VbyteHeap::VbyteHeap, s:%p Size:%zd\n", &s, Size );
    7286#endif // VbyteDebug
    7387        NoOfCompactions = NoOfExtensions = NoOfReductions = 0;
     
    7791        Header.flink = Header.blink = &Header;
    7892        Header.ulink = &s;
     93        // Should be unnecessary to initialize.
     94        // Header.s = 0p;
     95        // Header.lnth = 0;
    7996#ifdef VbyteDebug
    8097        HeaderPtr = &Header;
    81         serr | "exit:VbyteHeap::VbyteHeap, s:" | &s;
     98        printf( "exit:VbyteHeap::VbyteHeap, s:%p\n", &s );
    8299#endif // VbyteDebug
    83100} // VbyteHeap
     
    99116static void ?{}( HandleNode & s ) with(s) {
    100117#ifdef VbyteDebug
    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;
     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 );
    107125#endif // VbyteDebug
    108126} // HandleNode
     
    114132static void ?{}( HandleNode & s, VbyteHeap & vh ) with(s) {
    115133#ifdef VbyteDebug
    116         serr | "enter:HandleNode::HandleNode, s:" | &s;
     134        printf( "enter:HandleNode::HandleNode, s:%p\n", &s );
    117135#endif // VbyteDebug
    118136        s = 0;
     
    121139        AddThisAfter( s, *vh.Header.blink );
    122140#ifdef VbyteDebug
    123         serr | "exit:HandleNode::HandleNode, s:" | &s;
     141        printf( "exit:HandleNode::HandleNode, s:%p\n", &s );
    124142#endif // VbyteDebug
    125143} // HandleNode
     
    131149static void ^?{}( HandleNode & s ) with(s) {
    132150#ifdef VbyteDebug
    133         serr | "enter:HandleNode::~HandleNode, s:" | & s;
     151        printf( "enter:HandleNode::~HandleNode, s:%p\n", &s );
    134152        {
    135                 serr | nlOff;
    136                 serr | " lnth:" | lnth | " s:" | (void *)s | ",\"";
     153                printf( " lnth:%u s:%p,\"", lnth, (void *)s );
    137154                for ( i; lnth ) {
    138                         serr | s[i];
     155                        printf( "%c", s[i] );
    139156                } // for
    140                 serr | "\" flink:" | flink | " blink:" | blink | nl;
    141                 serr | nlOn;
     157                printf( "\" flink:%p blink:%p\n", flink, blink );
    142158        }
    143159#endif // VbyteDebug
     
    176192
    177193VbyteHeap * DEBUG_string_heap() {
    178         assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );
     194//      assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );
    179195        return ambient_string_sharectx->activeHeap;
    180196}
     
    330346        s.shareSet_prev = &s;
    331347        s.shareSet_next = &s;
    332                 }
     348}
    333349
    334350static void eagerCopyCtorHelper(string_res & s, const char * rhs, size_t rhslnth) with(s) {
     
    340356                (shareSet_owns_ulink){ true };
    341357        }
    342         Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
     358        Handle.s = VbyteAlloc( *Handle.ulink, rhslnth );
    343359        Handle.lnth = rhslnth;
    344360        memmove( Handle.s, rhs, rhslnth );
     
    392408void ?{}( string_res & s, VbyteHeap & heap, const char * rhs, size_t rhslnth ) with(s) {
    393409        (Handle){ heap };
    394         Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
     410        Handle.s = VbyteAlloc( *Handle.ulink, rhslnth );
    395411        Handle.lnth = rhslnth;
    396412        (s.shareSet_owns_ulink){ false };
     
    398414        s.shareSet_prev = &s;
    399415        s.shareSet_next = &s;
     416#ifdef VbyteDebug
     417        printHandleList( s.Handle );
     418        printf( "exit:constructor, s:%p\n", &s );
     419#endif // VbyteDebug
    400420}
    401421
     
    535555
    536556static 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
    537562        string_res * shareSetStartPeer;
    538563        string_res * shareSetEndPeer;
     
    614639        }
    615640
     641#ifdef VbyteDebug
     642        printHandleList( s.Handle );
     643        printf( "exit:assign_\n" );
     644#endif // VbyteDebug
     645
    616646        return s;
    617647}
     
    708738
    709739void 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
    710744        size_t clnth = str1.Handle.lnth + bsize;
    711745        if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ?
    712746                // no-op
    713747        } else {                                                                                        // must copy some text
    714                 if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?
     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 ?
    715750                        VbyteAlloc( *str1.Handle.ulink, bsize );        // create room for 2nd part at the end of string area
    716751                } else {                                                                                // copy the two parts
     
    723758        } // if
    724759        str1.Handle.lnth = clnth;
     760#ifdef VbyteDebug
     761        printHandleList( str1.Handle );
     762        printf( "exit append\n" );
     763#endif // VbyteDebug
    725764}
    726765
     
    938977static void AddThisAfter( HandleNode & s, HandleNode & n ) with(s) {
    939978#ifdef VbyteDebug
    940         serr | "enter:AddThisAfter, s:" | &s | " n:" | &n;
     979        printf( "enter:AddThisAfter, s:%p n:%p\n", &s, &n );
    941980#endif // VbyteDebug
    942981        // 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).
     
    948987        n.flink = &s;
    949988#ifdef VbyteDebug
    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";
     989        printHandleList( s );
     990        printf( "exit:AddThisAfter\n" );
    963991#endif // VbyteDebug
    964992} // AddThisAfter
     
    969997static void DeleteNode( HandleNode & s ) with(s) {
    970998#ifdef VbyteDebug
    971         serr | "enter:DeleteNode, s:" | &s;
     999        printf( "enter:DeleteNode, s:%p\n", &s );
     1000        printHandleList( s );
    9721001#endif // VbyteDebug
    9731002        flink->blink = blink;
    9741003        blink->flink = flink;
    9751004#ifdef VbyteDebug
    976         serr | "exit:DeleteNode";
     1005        printHandleList( s );
     1006        printf( "exit:DeleteNode\n" );
    9771007#endif // VbyteDebug
    9781008} //  DeleteNode
     
    9841014static char * VbyteAlloc( VbyteHeap & s, int size ) with(s) {
    9851015#ifdef VbyteDebug
    986         serr | "enter:VbyteAlloc, size:" | size;
     1016        printf( "enter:VbyteAlloc, size:%d\n", size );
    9871017#endif // VbyteDebug
    9881018        uintptr_t NoBytes;
     
    9971027        EndVbyte += size;
    9981028#ifdef VbyteDebug
    999         serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;
     1029        printf( "exit:VbyteAlloc, r:%p EndVbyte:%p ExtVbyte:%p\n", (void *)r, (void *)EndVbyte, ExtVbyte );
    10001030#endif // VbyteDebug
    10011031        return r;
     
    10291059
    10301060
    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 
    1034 static void MoveThisAfter( HandleNode & s, const HandleNode & h ) with(s) {
    1035 #ifdef VbyteDebug
    1036         serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h;
     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
     1064static void MoveThisAfter( HandleNode & s, HandleNode & h ) with(s) {
     1065#ifdef VbyteDebug
     1066        printf( "enter:MoveThisAfter, s:%p h:%p\n", &s, &h );
    10371067#endif // VbyteDebug
    10381068        verify( h.ulink != 0p );
    10391069        verify( s.ulink == h.ulink );
    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
     1070        verify( s < h.s && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order" );
    10461071
    10471072        HandleNode *i;
    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 ) {
     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
    10501075                DeleteNode( s );
    10511076                AddThisAfter( s, *i->blink );
    10521077        } // if
    10531078#ifdef VbyteDebug
    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";
     1079        printHandleList( s );
     1080        printf( "exit:MoveThisAfter\n" );
    10671081#endif // VbyteDebug
    10681082} // MoveThisAfter
     
    10791093int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth )  {
    10801094#ifdef VbyteDebug
    1081         serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth;
     1095        printf( "enter:ByteCmp, Src1Start:%d Src1Lnth:%d Src2Start:%d Src2Lnth:%d", Src1Start, Src1Lnth, Src2Start, Src2Lnth );
    10821096#endif // VbyteDebug
    10831097        int cmp;
     
    11151129        } // for
    11161130#ifdef VbyteDebug
    1117         serr | "exit:ByteCmp, cmp:" | cmp;
     1131        printf( "exit:ByteCmp, cmp:%d\n", cmp );
    11181132#endif // VbyteDebug
    11191133        return cmp;
     
    11261140// the containing string has been moved. Hence, they only require that their string pointers be adjusted.
    11271141
    1128 void compaction(VbyteHeap & s) with(s) {
    1129         HandleNode *h;
    1130         char *obase, *nbase, *limit;
     1142void compaction( VbyteHeap & s ) with(s) {
     1143#ifdef VbyteDebug
     1144        printf( "enter compaction\n" );
     1145        printHandleList( Header );
     1146#endif // VbyteDebug
     1147        char * obase, * nbase, * limit;
    11311148       
    11321149        NoOfCompactions += 1;
    11331150        EndVbyte = StartVbyte;
    1134         h = Header.flink;                                       // ignore header node
    1135         for () {
     1151        for ( HandleNode * h = Header.flink; h != &Header; ) {
    11361152                memmove( EndVbyte, h->s, h->lnth );
    11371153                obase = h->s;
     
    11411157                limit = obase + h->lnth;
    11421158                h = h->flink;
    1143                
    11441159                // check if any substrings are allocated within a string
    1145                
    1146                 for () {
    1147                         if ( h == &Header ) break;                      // end of header list ?
    1148                         if ( h->s >= limit ) break;                     // outside of current string ?
     1160                for ( ; h != &Header; h = h->flink ) {
     1161                        if ( h->s >= limit )  break;                            // outside of current string ?
    11491162                        h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase );
    1150                         h = h->flink;
    11511163                } // for
    1152                 if ( h == &Header ) break;                      // end of header list ?
    11531164        } // for
     1165#ifdef VbyteDebug
     1166        printHandleList( Header );
     1167        printf( "exit compaction\n" );
     1168#endif // VbyteDebug
    11541169} // compaction
    11551170
     
    11661181// the heap.  The heap is then compacted in the existing heap or into the newly allocated heap.
    11671182
    1168 void 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         }
     1183void garbage( VbyteHeap & s, int minreq ) with(s) {
     1184#ifdef VbyteDebug
     1185        printf( "enter:garbage\n" );
     1186        printHandleList( Header );
    11831187#endif // VbyteDebug
    11841188        int AmountUsed, AmountFree;
     
    12041208        }// if
    12051209#ifdef VbyteDebug
    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";
     1210        printHandleList( Header );
     1211        printf( "exit:garbage\n" );
    12191212#endif // VbyteDebug
    12201213} // garbage
    1221 
    1222 #undef VbyteDebug
    1223 
    12241214
    12251215
     
    12291219void extend( VbyteHeap & s, int size ) with (s) {
    12301220#ifdef VbyteDebug
    1231         serr | "enter:extend, size:" | size;
     1221        printf( "enter:extend, size:%d", size );
    12321222#endif // VbyteDebug
    12331223        char *OldStartVbyte;
     
    12421232        free( OldStartVbyte );                          // release old heap
    12431233#ifdef VbyteDebug
    1244         serr | "exit:extend, CurrSize:" | CurrSize;
     1234        printf( "exit:extend, CurrSize: %d\n", CurrSize );
    12451235#endif // VbyteDebug
    12461236} // extend
     
    12481238//WIP
    12491239#if 0
    1250 
    12511240// Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
    12521241// area is deleted.
     
    12541243void VbyteHeap::reduce( int size ) {
    12551244#ifdef VbyteDebug
    1256         serr | "enter:reduce, size:" | size;
     1245        printf( "enter:reduce, size: %d", size );
    12571246#endif // VbyteDebug
    12581247        char *OldStartVbyte;
     
    12671256        delete  OldStartVbyte;                          // release old heap
    12681257#ifdef VbyteDebug
    1269         !serr | "exit:reduce, CurrSize:" | CurrSize;
     1258        printf( "exit:reduce, CurrSize: %d", CurrSize );
    12701259#endif // VbyteDebug
    12711260} // reduce
    1272 
    1273 
    1274 #endif
     1261#endif // 0
  • libcfa/src/collections/string_res.hfa

    rbc547d3 rb7caceb  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu May  7 13:14:04 2026
    13 // Update Count     : 83
     12// Last Modified On : Mon May 11 18:56:08 2026
     13// Update Count     : 84
    1414//
    1515
     
    3131
    3232        char * s;                                                                                       // pointer to byte string
    33         unsigned int lnth;                                                                      // length of byte string
     33        size_t lnth;                                                                            // length of byte string
    3434}; // HandleNode
    3535
Note: See TracChangeset for help on using the changeset viewer.