Changeset 804bf677


Ignore:
Timestamp:
Oct 13, 2021, 9:52:02 PM (3 years ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
7b0e8b7
Parents:
4b3b352
Message:

String hybrid: Basic cases of solo alloc now working

Files:
4 edited

Legend:

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

    r4b3b352 r804bf677  
    4848static inline void reduce( VbyteHeap &, int );                  // reduce the size of the byte area
    4949
    50 static inline void ?{}( VbyteHeap &, int = 1000 );
     50static inline void ?{}( VbyteHeap &, size_t = 1000 );
    5151static inline void ^?{}( VbyteHeap & );
    5252static inline void ByteCopy( char *, int, int, char *, int, int ); // copy a block of bytes from one location in the heap to another
     
    6262// Allocate the storage for the variable sized area and intialize the heap variables.
    6363
    64 static inline void ?{}( VbyteHeap & this, int Size ) with(this) {
     64static inline void ?{}( VbyteHeap & this, size_t Size ) with(this) {
    6565#ifdef VbyteDebug
    6666    serr | "enter:VbyteHeap::VbyteHeap, this:" | &this | " Size:" | Size;
     
    149149    (older){ ambient_string_sharectx };
    150150    if ( mode == NEW_SHARING ) {
    151         (activeHeap){ new( 1000 ) };
     151        (activeHeap){ new( (size_t) 1000 ) };
    152152    } else {
    153153        verify( mode == NO_SHARING );
     
    217217// Empty constructor
    218218void ?{}(string_res &s) with(s) {
    219     assert( ambient_string_sharectx->activeHeap && "Need to implement private contexts" );
    220     (Handle){ * ambient_string_sharectx->activeHeap };
     219    if( ambient_string_sharectx->activeHeap ) {
     220        (Handle){ * ambient_string_sharectx->activeHeap };
     221        (shareEditSet_owns_ulink){ false };
     222    } else {
     223        (Handle){ * new( (size_t) 10 ) };  // TODO: can I lazily avoid allocating for empty string
     224        (shareEditSet_owns_ulink){ true };
     225    }
    221226    s.shareEditSet_prev = &s;
    222227    s.shareEditSet_next = &s;
     
    224229
    225230static inline void eagerCopyCtorHelper(string_res &s, const char* rhs, size_t rhslnth) with(s) {
    226     assert( ambient_string_sharectx->activeHeap && "Need to implement private contexts" );
    227     (Handle){ * ambient_string_sharectx->activeHeap };
     231    if( ambient_string_sharectx->activeHeap ) {
     232        (Handle){ * ambient_string_sharectx->activeHeap };
     233        (shareEditSet_owns_ulink){ false };
     234    } else {
     235        (Handle){ * new( rhslnth ) };
     236        (shareEditSet_owns_ulink){ true };
     237    }
    228238    Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
    229239    Handle.lnth = rhslnth;
     
    248258void ?{}(string_res &s, const string_res & s2, StrResInitMode mode, size_t start, size_t end ) {
    249259
    250     assert( ambient_string_sharectx->activeHeap && "Need to implement private contexts" );
    251 
    252260    verify( start <= end && end <= s2.Handle.lnth );
    253261
     
    258266        s.Handle.lnth = end - start;
    259267        s.Handle.ulink = ambient_string_sharectx->activeHeap;
     268        (s.shareEditSet_owns_ulink){ false };
    260269        AddThisAfter(s.Handle, s2.Handle );                     // insert this handle after rhs handle
    261270        // ^ bug?  skip others at early point in string
     
    396405            Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
    397406            Handle.lnth = rhslnth;
     407            (s.shareEditSet_owns_ulink){ false };
    398408            for ( int i = 0; i < rhslnth; i += 1 ) {            // copy characters
    399409                Handle.s[i] = rhs[i];
     
    464474    // s.shareEditSet_next = &s;
    465475    // s.shareEditSet_prev = &s;
     476
     477    if (shareEditSet_owns_ulink && s.shareEditSet_next == &s) { // last one out
     478        delete( s.Handle.ulink );
     479    }
    466480}
    467481
  • libcfa/src/containers/string_res.hfa

    r4b3b352 r804bf677  
    4343struct string_res {
    4444    HandleNode Handle; // chars, start, end, global neighbours
     45    bool shareEditSet_owns_ulink;
    4546    string_res * shareEditSet_prev;
    4647    string_res * shareEditSet_next;
  • tests/collections/.expect/string-ctx-manage.txt

    r4b3b352 r804bf677  
    33hi
    44bye
     5hi
    56done
  • tests/collections/string-ctx-manage.cfa

    r4b3b352 r804bf677  
    2424    sout | y; // hi
    2525
    26     x = "bye";                                                     //.... Bookmark 9/30 shallow:  surprisingly this step failed
     26    x = "bye";
    2727    y = x; // y was already in different context => eager copy
    2828    assert( y.inner->Handle.s != x.inner->Handle.s);
     
    3030}
    3131
    32 void failSoloAlloc() {
     32void soloAlloc() {
    3333    string x = "hi";
    3434    string_sharectx c = { NO_SHARING };
     35   
     36    string y = x; // y allocates into private pad, implying eager copy
     37    assert( y.inner->Handle.s != x.inner->Handle.s);
     38    sout | y; // hi
    3539
    36     // want: allocation into private pad, with forced eager copy into it
    37     // got: assertion failure, "Need to implement private contexts"
    38     string y = x;
     40    // -- following hits "need to implement actual growth"
     41    //    and it passes if I modify string_res.cfa to oversize the owned heaps
     42
     43    // x = "bye";
     44    // y = x; // into private y => eager copy
     45    // assert( y.inner->Handle.s != x.inner->Handle.s);
     46    // sout | y; // bye
    3947}
    4048
    41 volatile bool showFail = false;
    4249
    4350int main() {
    4451    baseline();
    4552    eagerCopy();
    46     if (showFail) {
    47         failSoloAlloc();
    48     }
     53    soloAlloc();
    4954    printf("done\n");
    5055}
Note: See TracChangeset for help on using the changeset viewer.