Changeset fe18b46


Ignore:
Timestamp:
Oct 19, 2021, 2:55:36 PM (3 years ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
0ca15b7
Parents:
6f7aff3
Message:

String hybrid testing and fixing no-share version through the api-coverage case

Files:
2 added
2 edited

Legend:

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

    r6f7aff3 rfe18b46  
    258258}
    259259
     260// private ctor (not in header): use specified heap (ignore ambient) and copy chars in
     261void ?{}( string_res &s, VbyteHeap & heap, const char* rhs, size_t rhslnth ) with(s) {
     262    (Handle){ heap };
     263    Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
     264    Handle.lnth = rhslnth;
     265    (s.shareEditSet_owns_ulink){ false };
     266    for ( int i = 0; i < rhslnth; i += 1 ) {            // copy characters
     267        Handle.s[i] = rhs[i];
     268    } // for
     269    s.shareEditSet_prev = &s;
     270    s.shareEditSet_next = &s;
     271}
     272
    260273// General copy constructor
    261274void ?{}(string_res &s, const string_res & s2, StrResInitMode mode, size_t start, size_t end ) {
     
    263276    verify( start <= end && end <= s2.Handle.lnth );
    264277
    265     if (s2.Handle.ulink == ambient_string_sharectx->activeHeap) {
    266         // same heap: allow overlap
     278    if (s2.Handle.ulink != ambient_string_sharectx->activeHeap && mode == COPY_VALUE) {
     279        // crossing heaps (including private): copy eagerly
     280        eagerCopyCtorHelper(s, s2.Handle.s + start, end - start);
     281        verify(s.shareEditSet_prev == &s);
     282        verify(s.shareEditSet_next == &s);
     283    } else {
    267284        (s.Handle){};
    268285        s.Handle.s = s2.Handle.s + start;
    269286        s.Handle.lnth = end - start;
    270         s.Handle.ulink = ambient_string_sharectx->activeHeap;
    271         (s.shareEditSet_owns_ulink){ false };
     287        s.Handle.ulink = s2.Handle.ulink;
     288
    272289        AddThisAfter(s.Handle, s2.Handle );                     // insert this handle after rhs handle
    273290        // ^ bug?  skip others at early point in string
    274    
     291
    275292        if (mode == COPY_VALUE) {
     293            verify(s2.Handle.ulink == ambient_string_sharectx->activeHeap);
     294            // requested logical copy in same heap: defer copy until write
     295
     296            (s.shareEditSet_owns_ulink){ false };
     297
    276298            // make s alone in its shareEditSet
    277299            s.shareEditSet_prev = &s;
     
    279301        } else {
    280302            verify( mode == SHARE_EDITS );
     303            // sharing edits with source forces same heap as source (ignore context)
     304
     305            (s.shareEditSet_owns_ulink){ s2.shareEditSet_owns_ulink };
    281306
    282307            // s2 is logically const but not implementation const
     
    289314            s.shareEditSet_prev->shareEditSet_next = &s;
    290315        }
    291     } else {
    292         // crossing heaps: eager copy
    293         assert( mode == COPY_VALUE && "need to solidify context-crossing rules for requesting shared edits");
    294         eagerCopyCtorHelper(s, s2.Handle.s + start, end - start);
    295         verify(s.shareEditSet_prev == &s);
    296         verify(s.shareEditSet_next == &s);
    297316    }
    298317}
     
    400419            // growing: copy from old to new
    401420            char * dest = VbyteAlloc( *this.Handle.ulink, editSetLength );
    402             char *destCursor = dest;        memcpy(destCursor, prefixStartOrig, prefixLen);
    403             destCursor += prefixLen;        memcpy(destCursor, buffer         , bsize    );
    404             destCursor += this.Handle.lnth; memcpy(destCursor, suffixStartOrig, suffixLen);
     421            char *destCursor = dest;  memcpy(destCursor, prefixStartOrig, prefixLen);
     422            destCursor += prefixLen;  memcpy(destCursor, buffer         , bsize    );
     423            destCursor += bsize;      memcpy(destCursor, suffixStartOrig, suffixLen);
    405424            assignEditSet(this, shareEditSetStartPeer, shareEditSetEndPeer,
    406425                dest,
     
    437456        // `this` occurs in the middle of it, to be replaced
    438457        // build up the new text in `pasting`
    439 
    440         // super private string ctor: ignore ambient context
    441         void ?{}( string_res &s, VbyteHeap & heap, const char* rhs, size_t rhslnth ) with(s) {
    442             (Handle){ heap };
    443             Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
    444             Handle.lnth = rhslnth;
    445             (s.shareEditSet_owns_ulink){ false };
    446             for ( int i = 0; i < rhslnth; i += 1 ) {            // copy characters
    447                 Handle.s[i] = rhs[i];
    448             } // for
    449             s.shareEditSet_prev = &s;
    450             s.shareEditSet_next = &s;
    451         }
    452458
    453459        string_res pasting = {
     
    538544            VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area
    539545        } else {                                        // copy the two parts
    540             char * str1oldBuf = str1.Handle.s;
    541             str1.Handle.s = VbyteAlloc( *str1.Handle.ulink, clnth );
     546            char * str1newBuf = VbyteAlloc( *str1.Handle.ulink, clnth );
     547            char * str1oldBuf = str1.Handle.s;  // must read after VbyteAlloc call in case it gs's
     548            str1.Handle.s = str1newBuf;
    542549            ByteCopy( str1.Handle.s, 0, str1.Handle.lnth, str1oldBuf, 0, str1.Handle.lnth);
    543550        } // if
  • tests/collections/string-api-coverage.cfa

    r6f7aff3 rfe18b46  
    2828
    2929    #ifdef STRING_SHARING_OFF
    30     sout | "string sharing disabled";
    3130    string_sharectx c = { NO_SHARING };
    3231    #endif
Note: See TracChangeset for help on using the changeset viewer.