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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.